1 /* $NetBSD: test_dh.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2007, Novell, Inc.
5 * Author: Matthias Koenig <mkoenig@suse.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * * Neither the name of the Novell nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 /* openssl diffie-hellman test code
37 * works with openssl-0.9.8e
38 * primes with 3072 and 6144 bits as specified in RFC3526
39 * fail since openssl-0.9.8f
47 #include <krb5/roken.h>
48 #include <krb5/getarg.h>
57 static char *id_string
;
59 static int version_flag
;
62 static struct getargs args
[] = {
63 { "id", 0, arg_string
, &id_string
,
64 "type of ENGINE", NULL
},
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
,
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" \
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"
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" \
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"
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
},
252 * exchange a string based "base" to a value.
256 str2val(const char *str
, int base
, size_t *len
)
266 for (p
= str
; *p
!= '\0'; p
++) {
267 if (isxdigit((int)*p
))
269 else if (isspace((int)*p
))
274 if (i
== 0 || (i
% 2) != 0)
278 if ((dst
= malloc(i
)) == NULL
)
283 for (rp
= dst
, p
= str
; *p
!= '\0'; p
++) {
284 if (isxdigit((int)*p
)) {
291 *rp
++ = (char)strtol(b
, NULL
, base
);
303 static void set_prime(BIGNUM
*p
, char *str
)
306 unsigned char *prime
;
308 prime
= (unsigned char *)str2val(str
, 16, &len
);
310 errx(1, "failed to parse %s", str
);
311 BN_bin2bn(prime
, len
, p
);
314 static void set_generator(BIGNUM
*g
)
319 static void print_secret(unsigned char *sec
, size_t len
)
323 for (i
= 0; i
< len
; ++i
)
324 printf("%x", sec
[i
]);
329 static int check_prime(ENGINE
*engine
, struct prime
*pr
)
333 unsigned char *sec1
, *sec2
;
338 printf("Testing %s\n", pr
->name
);
342 dh1
= DH_new_method(engine
);
343 dh2
= DH_new_method(engine
);
345 /* 1. set shared parameter */
346 set_prime(p
, pr
->value
);
354 ret
= DH_generate_key(dh1
);
356 fprintf(stderr
, "DH_generate_key\n");
359 ret
= DH_generate_key(dh2
);
361 fprintf(stderr
, "DH_generate_key\n");
365 /* 3. compute shared secret */
367 if (size
!= DH_size(dh2
)) {
368 fprintf(stderr
, "size does not match!\n");
373 if (!sec1
|| !sec2
) {
377 ret
= DH_compute_key(sec1
, dh2
->pub_key
, dh1
);
379 fprintf(stderr
, "DH_compute_key");
382 ret
= DH_compute_key(sec2
, dh1
->pub_key
, dh2
);
384 fprintf(stderr
, "DH_compute_key");
388 /* 4. compare shared secret */
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)
418 arg_printusage (args
,
419 sizeof(args
)/sizeof(*args
),
426 main(int argc
, char **argv
)
428 ENGINE
*engine
= NULL
;
431 setprogname(argv
[0]);
433 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &idx
))
447 OpenSSL_add_all_algorithms();
449 ENGINE_load_openssl();
451 ENGINE_load_builtin_engines();
454 engine
= ENGINE_by_id(id_string
);
456 engine
= ENGINE_by_dso(id_string
, id_string
);
458 engine
= ENGINE_by_id("builtin");
461 errx(1, "ENGINE_by_dso failed");
463 printf("dh %s\n", ENGINE_get_DH(engine
)->name
);
466 struct prime
*p
= primes
;
469 if (check_prime(engine
, p
))
470 printf("%s: shared secret OK\n", p
->name
);
472 printf("%s: shared secret FAILURE\n", p
->name
);