OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / openswan / testing / crypto / pk-dh-03 / modp-case1.c
1 /* 
2  * unit tests for cryptographic helper function - calculate KE and nonce
3  *
4  * Copyright (C) 2006 Michael C. Richardson <mcr@xelerance.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * RCSID $Id: crypt_dh.c,v 1.11 2005/08/14 21:47:29 mcr Exp $
17  */
18
19 #include <fcntl.h>
20
21 #define VULCAN_PK 1
22 #define PK_DH_REGRESS 1
23
24 #include "../../../programs/pluto/hmac.c"
25 #include "../../../programs/pluto/crypto.c"
26 #include "../../../programs/pluto/ike_alg.c"
27 #include "../../../programs/pluto/crypt_utils.c"
28 #include "../../../programs/pluto/vulcan/vulcanpk_funcs.c"
29
30 #include "crypto.h"
31
32 char *progname;
33
34 void exit_log(const char *message, ...)
35 {
36         va_list args;
37         char m[LOG_WIDTH];      /* longer messages will be truncated */
38
39         va_start(args, message);
40         vsnprintf(m, sizeof(m), message, args);
41         va_end(args);
42
43         fprintf(stderr, "FATAL ERROR: %s\n", m);
44         exit(0);
45 }
46
47 void exit_tool(int code)
48 {
49         exit(code);
50 }
51
52 /*
53  * Input/output data for modp operation.
54  *
55  */
56 u_int32_t aModExpOperandA[] = {
57     0x80000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
58     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
59     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
60     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
61     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
62     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
63     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
64     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
65     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
66     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
67     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
68     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
69 };
70
71 /* Operand_B */
72 u_int32_t aModExpOperandB[] = {
73     0x00000000, 0x00000000, 0x00000000, 0x00000000,
74     0x00000000, 0x00000000, 0x00000000, 0x00000000,
75     0x00000000, 0x00000000, 0x00000000, 0x00000000,
76     0x00000000, 0x00000000, 0x00000000, 0x00000000,
77     0x00000000, 0x00000000, 0x00000000, 0x00000000,
78     0x00000000, 0x00000000, 0x00000000, 0x00000000,
79     0x00000000, 0x00000000, 0x00000000, 0x00000000,
80     0x00000000, 0x00000000, 0x00000000, 0x00000000,
81     0x00000000, 0x00000000, 0x00000000, 0x00000000,
82     0x00000000, 0x00000000, 0x00000000, 0x00000000,
83     0x00000000, 0x00000000, 0x00000000, 0x00000000,
84     0x00000000, 0x00000000, 0x00000000, 0x00000004
85 };
86
87 /* ExpectedResult */
88 u_int32_t aModExpExpectedRes[] = {
89     0x7FFFFFFC, 0x00000000, 0x00000000, 0x00000000,
90     0x00000000, 0x00000000, 0x00000000, 0x00000000,
91     0x00000000, 0x00000000, 0x00000000, 0x00000000,
92     0x00000000, 0x00000000, 0x00000000, 0x00000000,
93     0x00000000, 0x00000000, 0x00000000, 0x00000000,
94     0x00000000, 0x00000000, 0x00000000, 0x00000000,
95     0x00000000, 0x00000000, 0x00000000, 0x00000000,
96     0x00000000, 0x00000000, 0x00000000, 0x00000000,
97     0x00000000, 0x00000000, 0x00000000, 0x00000000,
98     0x00000000, 0x00000000, 0x00000000, 0x00000000,
99     0x00000000, 0x00000000, 0x00000000, 0x00000000,
100     0x00000000, 0x00000000, 0x00000000, 0x00000001
101 };
102
103 /******************* MODULUS data *******************/
104 u_int32_t aModulus[] = {
105     0x80000000, 0x00000000, 0x00000000, 0x00000000,
106     0x00000000, 0x00000000, 0x00000000, 0x00000000,
107     0x00000000, 0x00000000, 0x00000000, 0x00000000,
108     0x00000000, 0x00000000, 0x00000000, 0x00000000,
109     0x00000000, 0x00000000, 0x00000000, 0x00000000,
110     0x00000000, 0x00000000, 0x00000000, 0x00000000,
111     0x00000000, 0x00000000, 0x00000000, 0x00000000,
112     0x00000000, 0x00000000, 0x00000000, 0x00000000,
113     0x00000000, 0x00000000, 0x00000000, 0x00000000,
114     0x00000000, 0x00000000, 0x00000000, 0x00000000,
115     0x00000000, 0x00000000, 0x00000000, 0x00000000,
116     0x00000000, 0x00000000, 0x00000000, 0x00000000
117 };
118
119
120 /******************* RECIPROCAL of MODULUS data *******************/
121 u_int32_t aReciprocal[] = {
122     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
123     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
124     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
125     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
126     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
127     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
128     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
129     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
130     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
131     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
132     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
133     0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
134 };
135
136 void bigendianize(u_int32_t *data, int len)
137 {
138         while(len-->0) {
139                 *data = htonl(*data);
140                 data++;
141         }
142 }
143
144 int main(int argc, char *argv[])
145 {
146         u_int8_t *mapping;
147         u_int8_t aModExpOperandA_l[192];
148         u_int8_t aModExpOperandB_l[192];
149         u_int8_t aModExpExpectedRes_l[192];
150         u_int8_t aModulus_l[192];
151         u_int8_t aReciprocal_l[192];
152         u_int8_t gtothex[192];
153         struct pkprogram expModP;
154         
155         memset(&expModP, 0, sizeof(expModP));
156
157         progname = argv[0];
158
159         mapping = mapvulcanpk();
160
161         /* initialize chip */
162         vulcanpk_init(mapping);
163
164         memcpy(aModExpOperandA_l, aModExpOperandA, 192);
165         bigendianize((u_int32_t *)aModExpOperandA_l, 192/sizeof(u_int32_t));
166
167         memcpy(aModExpOperandB_l, aModExpOperandB, 192);
168         bigendianize((u_int32_t *)aModExpOperandB_l, 192/sizeof(u_int32_t));
169
170         memcpy(aModExpExpectedRes_l, aModExpExpectedRes, 192);
171         bigendianize((u_int32_t *)aModExpExpectedRes_l, 192/sizeof(u_int32_t));
172
173         memcpy(aModulus_l, aModulus, 192);
174         bigendianize((u_int32_t *)aModulus_l, 192/sizeof(u_int32_t));
175
176         memcpy(aReciprocal_l, aReciprocal, 192);
177         bigendianize((u_int32_t *)aReciprocal_l, 192/sizeof(u_int32_t));
178
179         expModP.valuesLittleEndian = FALSE;
180
181         /* g-value */
182         expModP.aValues[0]  = aModExpOperandA_l;
183         expModP.aValueLen[0]= 192;
184
185         /* ^x-value */
186         expModP.aValues[2]  = aModExpOperandB_l;
187         expModP.aValueLen[2]= 192;
188
189         /* register 2 is result. */
190         /* register 3 is scratch */
191         
192         /* M = modulus */
193         expModP.aValues[8]  = aModulus_l;
194         expModP.aValueLen[8]= 192;
195
196         /* reciprocal M(1) */
197         expModP.aValues[9]  = aReciprocal_l;
198         expModP.aValueLen[9]= 192;
199
200         /* registers 6,7,8 is M(2),M(3),M(4), scratch */
201
202         expModP.chunksize = 3;  /* *64 = 192 bytes/chunk */
203         expModP.oOffset   = 3;  /* B(1) is result */
204         expModP.oValue    = gtothex;
205         expModP.oValueLen = sizeof(gtothex);
206
207         /* ask to have the exponentiation done now! */
208         expModP.pk_program[0]=/* sizes are ModLen=48(*32=1536),
209                                  EXP_len=1535+1, RED_len=0 */
210                 (0<<24)|(1535<<8)|(48);
211         expModP.pk_program[1]=/* opcode 1100=0xC (mod-exp),
212                                  with A=0, B=2(6),M=8(24)*/
213                 (0xC<<24)|(24<<16)|(6<<8)|(0<<0);
214
215         expModP.pk_proglen=2;
216         execute_pkprogram(mapping, &expModP);
217
218         printf("got: \n");
219         hexdump(gtothex, 0, 192);
220
221         printf("expected: \n");
222         hexdump(aModExpExpectedRes_l, 0, 192);
223
224         if(memcmp(gtothex, aModExpExpectedRes_l, 192)==0) {
225                 printf("SUCCESS\n");
226         }
227
228         exit(0);
229 }
230