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