No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / test_dh.c
blob603142ad3b02523bee7fd8363335f6f3f52350ce
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("$Heimdal: test_rsa.c 20466 2007-04-20 08:29:05Z lha $"
46 "$NetBSD$");
47 #endif
49 #include <stdio.h>
50 #include <ctype.h>
52 #include <roken.h>
53 #include <getarg.h>
55 #include <dh.h>
61 static int version_flag;
62 static int help_flag;
63 static int verbose;
65 static struct getargs args[] = {
66 { "verbose", 0, arg_flag, &verbose,
67 "verbose output from tests", NULL },
68 { "version", 0, arg_flag, &version_flag,
69 "print version", NULL },
70 { "help", 0, arg_flag, &help_flag,
71 NULL, NULL }
78 #define OAKLEY_PRIME_MODP768 \
79 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
80 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
81 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
82 "E485B576 625E7EC6 F44C42E9 A63A3620 FFFFFFFF FFFFFFFF"
84 #define OAKLEY_PRIME_MODP1024 \
85 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
86 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
87 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
88 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
89 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381" \
90 "FFFFFFFF FFFFFFFF"
92 #define OAKLEY_PRIME_MODP1536 \
93 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
94 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
95 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
96 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
97 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
98 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
99 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
100 "670C354E 4ABC9804 F1746C08 CA237327 FFFFFFFF FFFFFFFF"
102 /* RFC 3526 */
103 #define OAKLEY_PRIME_MODP2048 \
104 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
105 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
106 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
107 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
108 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
109 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
110 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
111 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
112 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
113 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
114 "15728E5A 8AACAA68 FFFFFFFF FFFFFFFF"
116 #define OAKLEY_PRIME_MODP3072 \
117 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
118 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
119 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
120 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
121 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
122 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
123 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
124 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
125 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
126 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
127 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
128 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
129 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
130 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
131 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
132 "43DB5BFC E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF"
134 #define OAKLEY_PRIME_MODP4096 \
135 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
136 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
137 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
138 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
139 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
140 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
141 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
142 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
143 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
144 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
145 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
146 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
147 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
148 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
149 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
150 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
151 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
152 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
153 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
154 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
155 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199" \
156 "FFFFFFFF FFFFFFFF"
158 #define OAKLEY_PRIME_MODP6144 \
159 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
160 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
161 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
162 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
163 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
164 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
165 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
166 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
167 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
168 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
169 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
170 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
171 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
172 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
173 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
174 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
175 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
176 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
177 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
178 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
179 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492" \
180 "36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD" \
181 "F8FF9406 AD9E530E E5DB382F 413001AE B06A53ED 9027D831" \
182 "179727B0 865A8918 DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B" \
183 "DB7F1447 E6CC254B 33205151 2BD7AF42 6FB8F401 378CD2BF" \
184 "5983CA01 C64B92EC F032EA15 D1721D03 F482D7CE 6E74FEF6" \
185 "D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F BEC7E8F3" \
186 "23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA" \
187 "CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328" \
188 "06A1D58B B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C" \
189 "DA56C9EC 2EF29632 387FE8D7 6E3C0468 043E8F66 3F4860EE" \
190 "12BF2D5B 0B7474D6 E694F91E 6DCC4024 FFFFFFFF FFFFFFFF"
192 #define OAKLEY_PRIME_MODP8192 \
193 "FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" \
194 "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" \
195 "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" \
196 "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" \
197 "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D" \
198 "C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F" \
199 "83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D" \
200 "670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B" \
201 "E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9" \
202 "DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510" \
203 "15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64" \
204 "ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7" \
205 "ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B" \
206 "F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C" \
207 "BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31" \
208 "43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7" \
209 "88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA" \
210 "2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6" \
211 "287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED" \
212 "1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9" \
213 "93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34028492" \
214 "36C3FAB4 D27C7026 C1D4DCB2 602646DE C9751E76 3DBA37BD" \
215 "F8FF9406 AD9E530E E5DB382F 413001AE B06A53ED 9027D831" \
216 "179727B0 865A8918 DA3EDBEB CF9B14ED 44CE6CBA CED4BB1B" \
217 "DB7F1447 E6CC254B 33205151 2BD7AF42 6FB8F401 378CD2BF" \
218 "5983CA01 C64B92EC F032EA15 D1721D03 F482D7CE 6E74FEF6" \
219 "D55E702F 46980C82 B5A84031 900B1C9E 59E7C97F BEC7E8F3" \
220 "23A97A7E 36CC88BE 0F1D45B7 FF585AC5 4BD407B2 2B4154AA" \
221 "CC8F6D7E BF48E1D8 14CC5ED2 0F8037E0 A79715EE F29BE328" \
222 "06A1D58B B7C5DA76 F550AA3D 8A1FBFF0 EB19CCB1 A313D55C" \
223 "DA56C9EC 2EF29632 387FE8D7 6E3C0468 043E8F66 3F4860EE" \
224 "12BF2D5B 0B7474D6 E694F91E 6DBE1159 74A3926F 12FEE5E4" \
225 "38777CB6 A932DF8C D8BEC4D0 73B931BA 3BC832B6 8D9DD300" \
226 "741FA7BF 8AFC47ED 2576F693 6BA42466 3AAB639C 5AE4F568" \
227 "3423B474 2BF1C978 238F16CB E39D652D E3FDB8BE FC848AD9" \
228 "22222E04 A4037C07 13EB57A8 1A23F0C7 3473FC64 6CEA306B" \
229 "4BCBC886 2F8385DD FA9D4B7F A2C087E8 79683303 ED5BDD3A" \
230 "062B3CF5 B3A278A6 6D2A13F8 3F44F82D DF310EE0 74AB6A36" \
231 "4597E899 A0255DC1 64F31CC5 0846851D F9AB4819 5DED7EA1" \
232 "B1D510BD 7EE74D73 FAF36BC3 1ECFA268 359046F4 EB879F92" \
233 "4009438B 481C6CD7 889A002E D5EE382B C9190DA6 FC026E47" \
234 "9558E447 5677E9AA 9E3050E2 765694DF C81F56E8 80B96E71" \
235 "60C980DD 98EDD3DF FFFFFFFF FFFFFFFF"
237 struct prime {
238 char *name;
239 char *value;
240 } primes[] = {
241 { "modp768", OAKLEY_PRIME_MODP768 },
242 { "modp1024", OAKLEY_PRIME_MODP1024 },
243 { "modp1536", OAKLEY_PRIME_MODP1536 },
244 { "modp2048", OAKLEY_PRIME_MODP2048 },
245 { "modp3072", OAKLEY_PRIME_MODP3072 },
246 { "modp4096", OAKLEY_PRIME_MODP4096 },
247 { "modp6144", OAKLEY_PRIME_MODP6144 },
248 { "modp8192", OAKLEY_PRIME_MODP8192 },
249 { NULL, NULL }
253 * exchange a string based "base" to a value.
256 static char *
257 str2val(const char *str, int base, size_t *len)
259 int f;
260 size_t i;
261 char *dst;
262 char *rp;
263 const char *p;
264 char b[3];
266 i = 0;
267 for (p = str; *p != '\0'; p++) {
268 if (isxdigit((int)*p))
269 i++;
270 else if (isspace((int)*p))
272 else
273 return NULL;
275 if (i == 0 || (i % 2) != 0)
276 return NULL;
277 i /= 2;
279 if ((dst = malloc(i)) == NULL)
280 return NULL;
282 i = 0;
283 f = 0;
284 for (rp = dst, p = str; *p != '\0'; p++) {
285 if (isxdigit((int)*p)) {
286 if (!f) {
287 b[0] = *p;
288 f = 1;
289 } else {
290 b[1] = *p;
291 b[2] = '\0';
292 *rp++ = (char)strtol(b, NULL, base);
293 i++;
294 f = 0;
299 *len = i;
301 return(dst);
304 static void set_prime(BIGNUM *p, char *str)
306 size_t len = 0;
307 unsigned char *prime;
309 prime = (unsigned char *)str2val(str, 16, &len);
310 if (prime == NULL)
311 errx(1, "failed to parse %s", str);
312 BN_bin2bn(prime, len, p);
315 static void set_generator(BIGNUM *g)
317 BN_set_word(g, 2);
320 static void print_secret(unsigned char *sec, size_t len)
322 size_t i;
324 for (i = 0; i < len; ++i)
325 printf("%x", sec[i]);
327 printf("\n");
330 static int check_prime(struct prime *pr)
332 DH *dh1, *dh2;
333 BIGNUM *p, *g;
334 unsigned char *sec1, *sec2;
335 size_t size;
336 int ret;
338 if (verbose)
339 printf("Testing %s\n", pr->name);
341 p = BN_new();
342 g = BN_new();
343 dh1 = DH_new();
344 dh2 = DH_new();
346 /* 1. set shared parameter */
347 set_prime(p, pr->value);
348 set_generator(g);
349 dh1->p = BN_dup(p);
350 dh1->g = BN_dup(g);
351 dh2->p = BN_dup(p);
352 dh2->g = BN_dup(g);
354 /* 2. set keys */
355 ret = DH_generate_key(dh1);
356 if (ret == 0) {
357 fprintf(stderr, "DH_generate_key\n");
358 exit(EXIT_FAILURE);
360 ret = DH_generate_key(dh2);
361 if (ret == 0) {
362 fprintf(stderr, "DH_generate_key\n");
363 exit(EXIT_FAILURE);
366 /* 3. compute shared secret */
367 size = DH_size(dh1);
368 if (size != DH_size(dh2)) {
369 fprintf(stderr, "size does not match!\n");
370 exit(EXIT_FAILURE);
372 sec1 = malloc(size);
373 sec2 = malloc(size);
374 if (!sec1 || !sec2) {
375 perror("malloc");
376 exit(EXIT_FAILURE);
378 ret = DH_compute_key(sec1, dh2->pub_key, dh1);
379 if (ret == -1) {
380 fprintf(stderr, "DH_compute_key");
381 exit(EXIT_FAILURE);
383 ret = DH_compute_key(sec2, dh1->pub_key, dh2);
384 if (ret == -1) {
385 fprintf(stderr, "DH_compute_key");
386 exit(EXIT_FAILURE);
389 /* 4. compare shared secret */
390 if (verbose) {
391 printf("shared secret 1\n");
392 print_secret(sec1, size);
393 printf("shared secret 2\n");
394 print_secret(sec2, size);
397 if (memcmp(sec1, sec2, size) == 0)
398 ret = 1;
399 else
400 ret = 0;
402 free(sec2);
403 free(sec1);
404 DH_free(dh2);
405 DH_free(dh1);
406 BN_free(g);
407 BN_free(p);
409 return ret;
416 static void
417 usage (int ret)
419 arg_printusage (args,
420 sizeof(args)/sizeof(*args),
421 NULL,
422 "");
423 exit (ret);
427 main(int argc, char **argv)
429 int idx = 0;
431 setprogname(argv[0]);
433 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
434 usage(1);
436 if (help_flag)
437 usage(0);
439 if(version_flag){
440 print_version(NULL);
441 exit(0);
444 argc -= idx;
445 argv += idx;
448 struct prime *p = primes;
450 for (; p->name; ++p)
451 if (check_prime(p))
452 printf("%s: shared secret OK\n", p->name);
453 else
454 printf("%s: shared secret FAILURE\n", p->name);
456 return 0;
459 return 0;