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