2 * unit tests for cryptographic helper function - calculate KE and nonce
4 * Copyright (C) 2006 Michael C. Richardson <mcr@xelerance.com>
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>.
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
16 * RCSID $Id: crypt_dh.c,v 1.11 2005/08/14 21:47:29 mcr Exp $
22 #define PK_DH_REGRESS 1
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"
34 void exit_log(const char *message, ...)
37 char m[LOG_WIDTH]; /* longer messages will be truncated */
39 va_start(args, message);
40 vsnprintf(m, sizeof(m), message, args);
43 fprintf(stderr, "FATAL ERROR: %s\n", m);
47 void exit_tool(int code)
53 * Input/output data for modp operation.
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
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
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
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
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
136 void bigendianize(u_int32_t *data, int len)
139 *data = htonl(*data);
144 int main(int argc, char *argv[])
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;
155 memset(&expModP, 0, sizeof(expModP));
159 mapping = mapvulcanpk();
161 /* initialize chip */
162 vulcanpk_init(mapping);
164 memcpy(aModExpOperandA_l, aModExpOperandA, 192);
165 bigendianize((u_int32_t *)aModExpOperandA_l, 192/sizeof(u_int32_t));
167 memcpy(aModExpOperandB_l, aModExpOperandB, 192);
168 bigendianize((u_int32_t *)aModExpOperandB_l, 192/sizeof(u_int32_t));
170 memcpy(aModExpExpectedRes_l, aModExpExpectedRes, 192);
171 bigendianize((u_int32_t *)aModExpExpectedRes_l, 192/sizeof(u_int32_t));
173 memcpy(aModulus_l, aModulus, 192);
174 bigendianize((u_int32_t *)aModulus_l, 192/sizeof(u_int32_t));
176 memcpy(aReciprocal_l, aReciprocal, 192);
177 bigendianize((u_int32_t *)aReciprocal_l, 192/sizeof(u_int32_t));
179 expModP.valuesLittleEndian = FALSE;
182 expModP.aValues[0] = aModExpOperandA_l;
183 expModP.aValueLen[0]= 192;
186 expModP.aValues[2] = aModExpOperandB_l;
187 expModP.aValueLen[2]= 192;
189 /* register 2 is result. */
190 /* register 3 is scratch */
193 expModP.aValues[8] = aModulus_l;
194 expModP.aValueLen[8]= 192;
196 /* reciprocal M(1) */
197 expModP.aValues[9] = aReciprocal_l;
198 expModP.aValueLen[9]= 192;
200 /* registers 6,7,8 is M(2),M(3),M(4), scratch */
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);
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);
215 expModP.pk_proglen=2;
216 execute_pkprogram(mapping, &expModP);
219 hexdump(gtothex, 0, 192);
221 printf("expected: \n");
222 hexdump(aModExpExpectedRes_l, 0, 192);
224 if(memcmp(gtothex, aModExpExpectedRes_l, 192)==0) {