1.2.2rc2
[heimdal.git] / lib / hcrypto / test_dh.c
blob207f8fcb6b09708daabf58d5d0fcd48c283bc007
1 /*
2 * Copyright (c) 2007, Novell, Inc.
3 * Author: Matthias Koenig <mkoenig@suse.de>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
17 * * Neither the name of the Novell nor the names of its contributors may be used
18 * to endorse or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 /* openssl diffie-hellman test code
35 * works with openssl-0.9.8e
36 * primes with 3072 and 6144 bits as specified in RFC3526
37 * fail since openssl-0.9.8f
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
44 #ifdef RCSID
45 RCSID("$Id: test_rsa.c 20466 2007-04-20 08:29:05Z lha $");
46 #endif
48 #include <stdio.h>
49 #include <ctype.h>
51 #include <roken.h>
52 #include <getarg.h>
54 #include <dh.h>
60 static int version_flag;
61 static int help_flag;
62 static int verbose;
64 static struct getargs args[] = {
65 { "verbose", 0, arg_flag, &verbose,
66 "verbose output from tests", NULL },
67 { "version", 0, arg_flag, &version_flag,
68 "print version", NULL },
69 { "help", 0, arg_flag, &help_flag,
70 NULL, NULL }
77 #define OAKLEY_PRIME_MODP768 \
78 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
79 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
80 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
81 "E485B576 625E7EC6 F44C42E9 A63A3620 FFFFFFFF FFFFFFFF"
83 #define OAKLEY_PRIME_MODP1024 \
84 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
85 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
86 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
87 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
88 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381" \
89 "FFFFFFFF FFFFFFFF"
91 #define OAKLEY_PRIME_MODP1536 \
92 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
93 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
94 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
95 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
96 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
97 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
98 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
99 "670C354E 4ABC9804 F1746C08 CA237327 FFFFFFFF FFFFFFFF"
101 /* RFC 3526 */
102 #define OAKLEY_PRIME_MODP2048 \
103 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
104 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
105 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
106 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
107 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
108 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
109 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
110 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
111 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
112 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
113 "15728E5A 8AACAA68 FFFFFFFF FFFFFFFF"
115 #define OAKLEY_PRIME_MODP3072 \
116 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
117 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
118 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
119 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
120 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
121 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
122 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
123 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
124 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
125 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
126 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
127 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
128 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
129 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
130 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
131 "43DB5BFC E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF"
133 #define OAKLEY_PRIME_MODP4096 \
134 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
135 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
136 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
137 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
138 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
139 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
140 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
141 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
142 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
143 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
144 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
145 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
146 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
147 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
148 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
149 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
150 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
151 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
152 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
153 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
154 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199" \
155 "FFFFFFFF FFFFFFFF"
157 #define OAKLEY_PRIME_MODP6144 \
158 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
159 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
160 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
161 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
162 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
163 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
164 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
165 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
166 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
167 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
168 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
169 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
170 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
171 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
172 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
173 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
174 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
175 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
176 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
177 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
178 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492" \
179 "36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD" \
180 "F8FF9406 AD9E530E E5DB382F 413001AE B06A53ED 9027D831" \
181 "179727B0 865A8918 DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B" \
182 "DB7F1447 E6CC254B 33205151 2BD7AF42 6FB8F401 378CD2BF" \
183 "5983CA01 C64B92EC F032EA15 D1721D03 F482D7CE 6E74FEF6" \
184 "D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F BEC7E8F3" \
185 "23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA" \
186 "CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328" \
187 "06A1D58B B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C" \
188 "DA56C9EC 2EF29632 387FE8D7 6E3C0468 043E8F66 3F4860EE" \
189 "12BF2D5B 0B7474D6 E694F91E 6DCC4024 FFFFFFFF FFFFFFFF"
191 #define OAKLEY_PRIME_MODP8192 \
192 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
193 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
194 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
195 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
196 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
197 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
198 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
199 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
200 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
201 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
202 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
203 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
204 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
205 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
206 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
207 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
208 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
209 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
210 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
211 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
212 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492" \
213 "36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD" \
214 "F8FF9406 AD9E530E E5DB382F 413001AE B06A53ED 9027D831" \
215 "179727B0 865A8918 DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B" \
216 "DB7F1447 E6CC254B 33205151 2BD7AF42 6FB8F401 378CD2BF" \
217 "5983CA01 C64B92EC F032EA15 D1721D03 F482D7CE 6E74FEF6" \
218 "D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F BEC7E8F3" \
219 "23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA" \
220 "CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328" \
221 "06A1D58B B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C" \
222 "DA56C9EC 2EF29632 387FE8D7 6E3C0468 043E8F66 3F4860EE" \
223 "12BF2D5B 0B7474D6 E694F91E 6DBE1159 74A3926F 12FEE5E4" \
224 "38777CB6 A932DF8C D8BEC4D0 73B931BA 3BC832B6 8D9DD300" \
225 "741FA7BF 8AFC47ED 2576F693 6BA42466 3AAB639C 5AE4F568" \
226 "3423B474 2BF1C978 238F16CB E39D652D E3FDB8BE FC848AD9" \
227 "22222E04 A4037C07 13EB57A8 1A23F0C7 3473FC64 6CEA306B" \
228 "4BCBC886 2F8385DD FA9D4B7F A2C087E8 79683303 ED5BDD3A" \
229 "062B3CF5 B3A278A6 6D2A13F8 3F44F82D DF310EE0 74AB6A36" \
230 "4597E899 A0255DC1 64F31CC5 0846851D F9AB4819 5DED7EA1" \
231 "B1D510BD 7EE74D73 FAF36BC3 1ECFA268 359046F4 EB879F92" \
232 "4009438B 481C6CD7 889A002E D5EE382B C9190DA6 FC026E47" \
233 "9558E447 5677E9AA 9E3050E2 765694DF C81F56E8 80B96E71" \
234 "60C980DD 98EDD3DF FFFFFFFF FFFFFFFF"
236 struct prime {
237 char *name;
238 char *value;
239 } primes[] = {
240 { "modp768", OAKLEY_PRIME_MODP768 },
241 { "modp1024", OAKLEY_PRIME_MODP1024 },
242 { "modp1536", OAKLEY_PRIME_MODP1536 },
243 { "modp2048", OAKLEY_PRIME_MODP2048 },
244 { "modp3072", OAKLEY_PRIME_MODP3072 },
245 { "modp4096", OAKLEY_PRIME_MODP4096 },
246 { "modp6144", OAKLEY_PRIME_MODP6144 },
247 { "modp8192", OAKLEY_PRIME_MODP8192 },
248 { NULL, NULL }
252 * exchange a string based "base" to a value.
255 static char *
256 str2val(const char *str, int base, size_t *len)
258 int f;
259 size_t i;
260 char *dst;
261 char *rp;
262 const char *p;
263 char b[3];
265 i = 0;
266 for (p = str; *p != '\0'; p++) {
267 if (isxdigit((int)*p))
268 i++;
269 else if (isspace((int)*p))
271 else
272 return NULL;
274 if (i == 0 || (i % 2) != 0)
275 return NULL;
276 i /= 2;
278 if ((dst = malloc(i)) == NULL)
279 return NULL;
281 i = 0;
282 f = 0;
283 for (rp = dst, p = str; *p != '\0'; p++) {
284 if (isxdigit((int)*p)) {
285 if (!f) {
286 b[0] = *p;
287 f = 1;
288 } else {
289 b[1] = *p;
290 b[2] = '\0';
291 *rp++ = (char)strtol(b, NULL, base);
292 i++;
293 f = 0;
298 *len = i;
300 return(dst);
303 static void set_prime(BIGNUM *p, char *str)
305 size_t len = 0;
306 unsigned char *prime;
308 prime = (unsigned char *)str2val(str, 16, &len);
309 if (prime == NULL)
310 errx(1, "failed to parse %s", str);
311 BN_bin2bn(prime, len, p);
314 static void set_generator(BIGNUM *g)
316 BN_set_word(g, 2);
319 static void print_secret(unsigned char *sec, size_t len)
321 size_t i;
323 for (i = 0; i < len; ++i)
324 printf("%x", sec[i]);
326 printf("\n");
329 static int check_prime(struct prime *pr)
331 DH *dh1, *dh2;
332 BIGNUM *p, *g;
333 unsigned char *sec1, *sec2;
334 size_t size;
335 int ret;
337 if (verbose)
338 printf("Testing %s\n", pr->name);
340 p = BN_new();
341 g = BN_new();
342 dh1 = DH_new();
343 dh2 = DH_new();
345 /* 1. set shared parameter */
346 set_prime(p, pr->value);
347 set_generator(g);
348 dh1->p = BN_dup(p);
349 dh1->g = BN_dup(g);
350 dh2->p = BN_dup(p);
351 dh2->g = BN_dup(g);
353 /* 2. set keys */
354 ret = DH_generate_key(dh1);
355 if (ret == 0) {
356 fprintf(stderr, "DH_generate_key\n");
357 exit(EXIT_FAILURE);
359 ret = DH_generate_key(dh2);
360 if (ret == 0) {
361 fprintf(stderr, "DH_generate_key\n");
362 exit(EXIT_FAILURE);
365 /* 3. compute shared secret */
366 size = DH_size(dh1);
367 if (size != DH_size(dh2)) {
368 fprintf(stderr, "size does not match!\n");
369 exit(EXIT_FAILURE);
371 sec1 = malloc(size);
372 sec2 = malloc(size);
373 if (!sec1 || !sec2) {
374 perror("malloc");
375 exit(EXIT_FAILURE);
377 ret = DH_compute_key(sec1, dh2->pub_key, dh1);
378 if (ret == -1) {
379 fprintf(stderr, "DH_compute_key");
380 exit(EXIT_FAILURE);
382 ret = DH_compute_key(sec2, dh1->pub_key, dh2);
383 if (ret == -1) {
384 fprintf(stderr, "DH_compute_key");
385 exit(EXIT_FAILURE);
388 /* 4. compare shared secret */
389 if (verbose) {
390 printf("shared secret 1\n");
391 print_secret(sec1, size);
392 printf("shared secret 2\n");
393 print_secret(sec2, size);
396 if (memcmp(sec1, sec2, size) == 0)
397 ret = 1;
398 else
399 ret = 0;
401 free(sec2);
402 free(sec1);
403 DH_free(dh2);
404 DH_free(dh1);
405 BN_free(g);
406 BN_free(p);
408 return ret;
415 static void
416 usage (int ret)
418 arg_printusage (args,
419 sizeof(args)/sizeof(*args),
420 NULL,
421 "");
422 exit (ret);
426 main(int argc, char **argv)
428 int idx = 0;
430 setprogname(argv[0]);
432 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
433 usage(1);
435 if (help_flag)
436 usage(0);
438 if(version_flag){
439 print_version(NULL);
440 exit(0);
443 argc -= idx;
444 argv += idx;
447 struct prime *p = primes;
449 for (; p->name; ++p)
450 if (check_prime(p))
451 printf("%s: shared secret OK\n", p->name);
452 else
453 printf("%s: shared secret FAILURE\n", p->name);
455 return 0;
458 return 0;