Implement internally opaque bn access from dh
[openssl.git] / crypto / dh / dhtest.c
1 /* crypto/dh/dhtest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
60  * deprecated functions for openssl-internal code */
61 #ifdef OPENSSL_NO_DEPRECATED
62 #undef OPENSSL_NO_DEPRECATED
63 #endif
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 #include "../e_os.h"
70
71 #include <openssl/crypto.h>
72 #include <openssl/bio.h>
73 #include <openssl/bn.h>
74 #include <openssl/rand.h>
75 #include <openssl/err.h>
76
77 #ifdef OPENSSL_NO_DH
78 int main(int argc, char *argv[])
79 {
80     printf("No DH support\n");
81     return(0);
82 }
83 #else
84 #include <openssl/dh.h>
85
86 #ifdef OPENSSL_SYS_WIN16
87 #define MS_CALLBACK     _far _loadds
88 #else
89 #define MS_CALLBACK
90 #endif
91
92 static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
93
94 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
95
96 static int run_rfc5114_tests(void);
97
98 int main(int argc, char *argv[])
99         {
100         BN_GENCB *_cb;
101         DH *a=NULL;
102         DH *b=NULL;
103         char buf[12];
104         unsigned char *abuf=NULL,*bbuf=NULL;
105         int i,alen,blen,aout,bout,ret=1;
106         BIO *out;
107
108         CRYPTO_malloc_debug_init();
109         CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
110         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
111
112 #ifdef OPENSSL_SYS_WIN32
113         CRYPTO_malloc_init();
114 #endif
115
116         RAND_seed(rnd_seed, sizeof rnd_seed);
117
118         out=BIO_new(BIO_s_file());
119         if (out == NULL) EXIT(1);
120         BIO_set_fp(out,stdout,BIO_NOCLOSE);
121
122         _cb = BN_GENCB_new();
123         if(!_cb)
124                 goto err;
125         BN_GENCB_set(_cb, &cb, out);
126         if(((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 64,
127                                 DH_GENERATOR_5, _cb))
128                 goto err;
129
130         if (!DH_check(a, &i)) goto err;
131         if (i & DH_CHECK_P_NOT_PRIME)
132                 BIO_puts(out, "p value is not prime\n");
133         if (i & DH_CHECK_P_NOT_SAFE_PRIME)
134                 BIO_puts(out, "p value is not a safe prime\n");
135         if (i & DH_UNABLE_TO_CHECK_GENERATOR)
136                 BIO_puts(out, "unable to check the generator value\n");
137         if (i & DH_NOT_SUITABLE_GENERATOR)
138                 BIO_puts(out, "the g value is not a generator\n");
139
140         BIO_puts(out,"\np    =");
141         BN_print(out,a->p);
142         BIO_puts(out,"\ng    =");
143         BN_print(out,a->g);
144         BIO_puts(out,"\n");
145
146         b=DH_new();
147         if (b == NULL) goto err;
148
149         b->p=BN_dup(a->p);
150         b->g=BN_dup(a->g);
151         if ((b->p == NULL) || (b->g == NULL)) goto err;
152
153         /* Set a to run with normal modexp and b to use constant time */
154         a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
155         b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
156
157         if (!DH_generate_key(a)) goto err;
158         BIO_puts(out,"pri 1=");
159         BN_print(out,a->priv_key);
160         BIO_puts(out,"\npub 1=");
161         BN_print(out,a->pub_key);
162         BIO_puts(out,"\n");
163
164         if (!DH_generate_key(b)) goto err;
165         BIO_puts(out,"pri 2=");
166         BN_print(out,b->priv_key);
167         BIO_puts(out,"\npub 2=");
168         BN_print(out,b->pub_key);
169         BIO_puts(out,"\n");
170
171         alen=DH_size(a);
172         abuf=(unsigned char *)OPENSSL_malloc(alen);
173         aout=DH_compute_key(abuf,b->pub_key,a);
174
175         BIO_puts(out,"key1 =");
176         for (i=0; i<aout; i++)
177                 {
178                 sprintf(buf,"%02X",abuf[i]);
179                 BIO_puts(out,buf);
180                 }
181         BIO_puts(out,"\n");
182
183         blen=DH_size(b);
184         bbuf=(unsigned char *)OPENSSL_malloc(blen);
185         bout=DH_compute_key(bbuf,a->pub_key,b);
186
187         BIO_puts(out,"key2 =");
188         for (i=0; i<bout; i++)
189                 {
190                 sprintf(buf,"%02X",bbuf[i]);
191                 BIO_puts(out,buf);
192                 }
193         BIO_puts(out,"\n");
194         if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
195                 {
196                 fprintf(stderr,"Error in DH routines\n");
197                 ret=1;
198                 }
199         else
200                 ret=0;
201         if (!run_rfc5114_tests())
202                 ret = 1;
203 err:
204         ERR_print_errors_fp(stderr);
205
206         if (abuf != NULL) OPENSSL_free(abuf);
207         if (bbuf != NULL) OPENSSL_free(bbuf);
208         if(b != NULL) DH_free(b);
209         if(a != NULL) DH_free(a);
210         if(_cb) BN_GENCB_free(_cb);
211         BIO_free(out);
212 #ifdef OPENSSL_SYS_NETWARE
213     if (ret) printf("ERROR: %d\n", ret);
214 #endif
215         EXIT(ret);
216         return(ret);
217         }
218
219 static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
220         {
221         char c='*';
222
223         if (p == 0) c='.';
224         if (p == 1) c='+';
225         if (p == 2) c='*';
226         if (p == 3) c='\n';
227         BIO_write(BN_GENCB_get_arg(arg),&c,1);
228         (void)BIO_flush(BN_GENCB_get_arg(arg));
229 #ifdef LINT
230         p=n;
231 #endif
232         return 1;
233         }
234
235 /* Test data from RFC 5114 */
236
237 static const unsigned char dhtest_1024_160_xA[] = {
238         0xB9,0xA3,0xB3,0xAE,0x8F,0xEF,0xC1,0xA2,0x93,0x04,0x96,0x50,
239         0x70,0x86,0xF8,0x45,0x5D,0x48,0x94,0x3E
240 };
241 static const unsigned char dhtest_1024_160_yA[] = {
242         0x2A,0x85,0x3B,0x3D,0x92,0x19,0x75,0x01,0xB9,0x01,0x5B,0x2D,
243         0xEB,0x3E,0xD8,0x4F,0x5E,0x02,0x1D,0xCC,0x3E,0x52,0xF1,0x09,
244         0xD3,0x27,0x3D,0x2B,0x75,0x21,0x28,0x1C,0xBA,0xBE,0x0E,0x76,
245         0xFF,0x57,0x27,0xFA,0x8A,0xCC,0xE2,0x69,0x56,0xBA,0x9A,0x1F,
246         0xCA,0x26,0xF2,0x02,0x28,0xD8,0x69,0x3F,0xEB,0x10,0x84,0x1D,
247         0x84,0xA7,0x36,0x00,0x54,0xEC,0xE5,0xA7,0xF5,0xB7,0xA6,0x1A,
248         0xD3,0xDF,0xB3,0xC6,0x0D,0x2E,0x43,0x10,0x6D,0x87,0x27,0xDA,
249         0x37,0xDF,0x9C,0xCE,0x95,0xB4,0x78,0x75,0x5D,0x06,0xBC,0xEA,
250         0x8F,0x9D,0x45,0x96,0x5F,0x75,0xA5,0xF3,0xD1,0xDF,0x37,0x01,
251         0x16,0x5F,0xC9,0xE5,0x0C,0x42,0x79,0xCE,0xB0,0x7F,0x98,0x95,
252         0x40,0xAE,0x96,0xD5,0xD8,0x8E,0xD7,0x76
253 };
254 static const unsigned char dhtest_1024_160_xB[] = {
255         0x93,0x92,0xC9,0xF9,0xEB,0x6A,0x7A,0x6A,0x90,0x22,0xF7,0xD8,
256         0x3E,0x72,0x23,0xC6,0x83,0x5B,0xBD,0xDA
257 };
258 static const unsigned char dhtest_1024_160_yB[] = {
259         0x71,0x7A,0x6C,0xB0,0x53,0x37,0x1F,0xF4,0xA3,0xB9,0x32,0x94,
260         0x1C,0x1E,0x56,0x63,0xF8,0x61,0xA1,0xD6,0xAD,0x34,0xAE,0x66,
261         0x57,0x6D,0xFB,0x98,0xF6,0xC6,0xCB,0xF9,0xDD,0xD5,0xA5,0x6C,
262         0x78,0x33,0xF6,0xBC,0xFD,0xFF,0x09,0x55,0x82,0xAD,0x86,0x8E,
263         0x44,0x0E,0x8D,0x09,0xFD,0x76,0x9E,0x3C,0xEC,0xCD,0xC3,0xD3,
264         0xB1,0xE4,0xCF,0xA0,0x57,0x77,0x6C,0xAA,0xF9,0x73,0x9B,0x6A,
265         0x9F,0xEE,0x8E,0x74,0x11,0xF8,0xD6,0xDA,0xC0,0x9D,0x6A,0x4E,
266         0xDB,0x46,0xCC,0x2B,0x5D,0x52,0x03,0x09,0x0E,0xAE,0x61,0x26,
267         0x31,0x1E,0x53,0xFD,0x2C,0x14,0xB5,0x74,0xE6,0xA3,0x10,0x9A,
268         0x3D,0xA1,0xBE,0x41,0xBD,0xCE,0xAA,0x18,0x6F,0x5C,0xE0,0x67,
269         0x16,0xA2,0xB6,0xA0,0x7B,0x3C,0x33,0xFE
270 };
271 static const unsigned char dhtest_1024_160_Z[] = {
272         0x5C,0x80,0x4F,0x45,0x4D,0x30,0xD9,0xC4,0xDF,0x85,0x27,0x1F,
273         0x93,0x52,0x8C,0x91,0xDF,0x6B,0x48,0xAB,0x5F,0x80,0xB3,0xB5,
274         0x9C,0xAA,0xC1,0xB2,0x8F,0x8A,0xCB,0xA9,0xCD,0x3E,0x39,0xF3,
275         0xCB,0x61,0x45,0x25,0xD9,0x52,0x1D,0x2E,0x64,0x4C,0x53,0xB8,
276         0x07,0xB8,0x10,0xF3,0x40,0x06,0x2F,0x25,0x7D,0x7D,0x6F,0xBF,
277         0xE8,0xD5,0xE8,0xF0,0x72,0xE9,0xB6,0xE9,0xAF,0xDA,0x94,0x13,
278         0xEA,0xFB,0x2E,0x8B,0x06,0x99,0xB1,0xFB,0x5A,0x0C,0xAC,0xED,
279         0xDE,0xAE,0xAD,0x7E,0x9C,0xFB,0xB3,0x6A,0xE2,0xB4,0x20,0x83,
280         0x5B,0xD8,0x3A,0x19,0xFB,0x0B,0x5E,0x96,0xBF,0x8F,0xA4,0xD0,
281         0x9E,0x34,0x55,0x25,0x16,0x7E,0xCD,0x91,0x55,0x41,0x6F,0x46,
282         0xF4,0x08,0xED,0x31,0xB6,0x3C,0x6E,0x6D
283 };
284 static const unsigned char dhtest_2048_224_xA[] = {
285         0x22,0xE6,0x26,0x01,0xDB,0xFF,0xD0,0x67,0x08,0xA6,0x80,0xF7,
286         0x47,0xF3,0x61,0xF7,0x6D,0x8F,0x4F,0x72,0x1A,0x05,0x48,0xE4,
287         0x83,0x29,0x4B,0x0C
288 };
289 static const unsigned char dhtest_2048_224_yA[] = {
290         0x1B,0x3A,0x63,0x45,0x1B,0xD8,0x86,0xE6,0x99,0xE6,0x7B,0x49,
291         0x4E,0x28,0x8B,0xD7,0xF8,0xE0,0xD3,0x70,0xBA,0xDD,0xA7,0xA0,
292         0xEF,0xD2,0xFD,0xE7,0xD8,0xF6,0x61,0x45,0xCC,0x9F,0x28,0x04,
293         0x19,0x97,0x5E,0xB8,0x08,0x87,0x7C,0x8A,0x4C,0x0C,0x8E,0x0B,
294         0xD4,0x8D,0x4A,0x54,0x01,0xEB,0x1E,0x87,0x76,0xBF,0xEE,0xE1,
295         0x34,0xC0,0x38,0x31,0xAC,0x27,0x3C,0xD9,0xD6,0x35,0xAB,0x0C,
296         0xE0,0x06,0xA4,0x2A,0x88,0x7E,0x3F,0x52,0xFB,0x87,0x66,0xB6,
297         0x50,0xF3,0x80,0x78,0xBC,0x8E,0xE8,0x58,0x0C,0xEF,0xE2,0x43,
298         0x96,0x8C,0xFC,0x4F,0x8D,0xC3,0xDB,0x08,0x45,0x54,0x17,0x1D,
299         0x41,0xBF,0x2E,0x86,0x1B,0x7B,0xB4,0xD6,0x9D,0xD0,0xE0,0x1E,
300         0xA3,0x87,0xCB,0xAA,0x5C,0xA6,0x72,0xAF,0xCB,0xE8,0xBD,0xB9,
301         0xD6,0x2D,0x4C,0xE1,0x5F,0x17,0xDD,0x36,0xF9,0x1E,0xD1,0xEE,
302         0xDD,0x65,0xCA,0x4A,0x06,0x45,0x5C,0xB9,0x4C,0xD4,0x0A,0x52,
303         0xEC,0x36,0x0E,0x84,0xB3,0xC9,0x26,0xE2,0x2C,0x43,0x80,0xA3,
304         0xBF,0x30,0x9D,0x56,0x84,0x97,0x68,0xB7,0xF5,0x2C,0xFD,0xF6,
305         0x55,0xFD,0x05,0x3A,0x7E,0xF7,0x06,0x97,0x9E,0x7E,0x58,0x06,
306         0xB1,0x7D,0xFA,0xE5,0x3A,0xD2,0xA5,0xBC,0x56,0x8E,0xBB,0x52,
307         0x9A,0x7A,0x61,0xD6,0x8D,0x25,0x6F,0x8F,0xC9,0x7C,0x07,0x4A,
308         0x86,0x1D,0x82,0x7E,0x2E,0xBC,0x8C,0x61,0x34,0x55,0x31,0x15,
309         0xB7,0x0E,0x71,0x03,0x92,0x0A,0xA1,0x6D,0x85,0xE5,0x2B,0xCB,
310         0xAB,0x8D,0x78,0x6A,0x68,0x17,0x8F,0xA8,0xFF,0x7C,0x2F,0x5C,
311         0x71,0x64,0x8D,0x6F
312 };
313 static const unsigned char dhtest_2048_224_xB[] = {
314         0x4F,0xF3,0xBC,0x96,0xC7,0xFC,0x6A,0x6D,0x71,0xD3,0xB3,0x63,
315         0x80,0x0A,0x7C,0xDF,0xEF,0x6F,0xC4,0x1B,0x44,0x17,0xEA,0x15,
316         0x35,0x3B,0x75,0x90
317 };
318 static const unsigned char dhtest_2048_224_yB[] = {
319         0x4D,0xCE,0xE9,0x92,0xA9,0x76,0x2A,0x13,0xF2,0xF8,0x38,0x44,
320         0xAD,0x3D,0x77,0xEE,0x0E,0x31,0xC9,0x71,0x8B,0x3D,0xB6,0xC2,
321         0x03,0x5D,0x39,0x61,0x18,0x2C,0x3E,0x0B,0xA2,0x47,0xEC,0x41,
322         0x82,0xD7,0x60,0xCD,0x48,0xD9,0x95,0x99,0x97,0x06,0x22,0xA1,
323         0x88,0x1B,0xBA,0x2D,0xC8,0x22,0x93,0x9C,0x78,0xC3,0x91,0x2C,
324         0x66,0x61,0xFA,0x54,0x38,0xB2,0x07,0x66,0x22,0x2B,0x75,0xE2,
325         0x4C,0x2E,0x3A,0xD0,0xC7,0x28,0x72,0x36,0x12,0x95,0x25,0xEE,
326         0x15,0xB5,0xDD,0x79,0x98,0xAA,0x04,0xC4,0xA9,0x69,0x6C,0xAC,
327         0xD7,0x17,0x20,0x83,0xA9,0x7A,0x81,0x66,0x4E,0xAD,0x2C,0x47,
328         0x9E,0x44,0x4E,0x4C,0x06,0x54,0xCC,0x19,0xE2,0x8D,0x77,0x03,
329         0xCE,0xE8,0xDA,0xCD,0x61,0x26,0xF5,0xD6,0x65,0xEC,0x52,0xC6,
330         0x72,0x55,0xDB,0x92,0x01,0x4B,0x03,0x7E,0xB6,0x21,0xA2,0xAC,
331         0x8E,0x36,0x5D,0xE0,0x71,0xFF,0xC1,0x40,0x0A,0xCF,0x07,0x7A,
332         0x12,0x91,0x3D,0xD8,0xDE,0x89,0x47,0x34,0x37,0xAB,0x7B,0xA3,
333         0x46,0x74,0x3C,0x1B,0x21,0x5D,0xD9,0xC1,0x21,0x64,0xA7,0xE4,
334         0x05,0x31,0x18,0xD1,0x99,0xBE,0xC8,0xEF,0x6F,0xC5,0x61,0x17,
335         0x0C,0x84,0xC8,0x7D,0x10,0xEE,0x9A,0x67,0x4A,0x1F,0xA8,0xFF,
336         0xE1,0x3B,0xDF,0xBA,0x1D,0x44,0xDE,0x48,0x94,0x6D,0x68,0xDC,
337         0x0C,0xDD,0x77,0x76,0x35,0xA7,0xAB,0x5B,0xFB,0x1E,0x4B,0xB7,
338         0xB8,0x56,0xF9,0x68,0x27,0x73,0x4C,0x18,0x41,0x38,0xE9,0x15,
339         0xD9,0xC3,0x00,0x2E,0xBC,0xE5,0x31,0x20,0x54,0x6A,0x7E,0x20,
340         0x02,0x14,0x2B,0x6C
341 };
342 static const unsigned char dhtest_2048_224_Z[] = {
343         0x34,0xD9,0xBD,0xDC,0x1B,0x42,0x17,0x6C,0x31,0x3F,0xEA,0x03,
344         0x4C,0x21,0x03,0x4D,0x07,0x4A,0x63,0x13,0xBB,0x4E,0xCD,0xB3,
345         0x70,0x3F,0xFF,0x42,0x45,0x67,0xA4,0x6B,0xDF,0x75,0x53,0x0E,
346         0xDE,0x0A,0x9D,0xA5,0x22,0x9D,0xE7,0xD7,0x67,0x32,0x28,0x6C,
347         0xBC,0x0F,0x91,0xDA,0x4C,0x3C,0x85,0x2F,0xC0,0x99,0xC6,0x79,
348         0x53,0x1D,0x94,0xC7,0x8A,0xB0,0x3D,0x9D,0xEC,0xB0,0xA4,0xE4,
349         0xCA,0x8B,0x2B,0xB4,0x59,0x1C,0x40,0x21,0xCF,0x8C,0xE3,0xA2,
350         0x0A,0x54,0x1D,0x33,0x99,0x40,0x17,0xD0,0x20,0x0A,0xE2,0xC9,
351         0x51,0x6E,0x2F,0xF5,0x14,0x57,0x79,0x26,0x9E,0x86,0x2B,0x0F,
352         0xB4,0x74,0xA2,0xD5,0x6D,0xC3,0x1E,0xD5,0x69,0xA7,0x70,0x0B,
353         0x4C,0x4A,0xB1,0x6B,0x22,0xA4,0x55,0x13,0x53,0x1E,0xF5,0x23,
354         0xD7,0x12,0x12,0x07,0x7B,0x5A,0x16,0x9B,0xDE,0xFF,0xAD,0x7A,
355         0xD9,0x60,0x82,0x84,0xC7,0x79,0x5B,0x6D,0x5A,0x51,0x83,0xB8,
356         0x70,0x66,0xDE,0x17,0xD8,0xD6,0x71,0xC9,0xEB,0xD8,0xEC,0x89,
357         0x54,0x4D,0x45,0xEC,0x06,0x15,0x93,0xD4,0x42,0xC6,0x2A,0xB9,
358         0xCE,0x3B,0x1C,0xB9,0x94,0x3A,0x1D,0x23,0xA5,0xEA,0x3B,0xCF,
359         0x21,0xA0,0x14,0x71,0xE6,0x7E,0x00,0x3E,0x7F,0x8A,0x69,0xC7,
360         0x28,0xBE,0x49,0x0B,0x2F,0xC8,0x8C,0xFE,0xB9,0x2D,0xB6,0xA2,
361         0x15,0xE5,0xD0,0x3C,0x17,0xC4,0x64,0xC9,0xAC,0x1A,0x46,0xE2,
362         0x03,0xE1,0x3F,0x95,0x29,0x95,0xFB,0x03,0xC6,0x9D,0x3C,0xC4,
363         0x7F,0xCB,0x51,0x0B,0x69,0x98,0xFF,0xD3,0xAA,0x6D,0xE7,0x3C,
364         0xF9,0xF6,0x38,0x69
365 };
366 static const unsigned char dhtest_2048_256_xA[] = {
367         0x08,0x81,0x38,0x2C,0xDB,0x87,0x66,0x0C,0x6D,0xC1,0x3E,0x61,
368         0x49,0x38,0xD5,0xB9,0xC8,0xB2,0xF2,0x48,0x58,0x1C,0xC5,0xE3,
369         0x1B,0x35,0x45,0x43,0x97,0xFC,0xE5,0x0E
370 };
371 static const unsigned char dhtest_2048_256_yA[] = {
372         0x2E,0x93,0x80,0xC8,0x32,0x3A,0xF9,0x75,0x45,0xBC,0x49,0x41,
373         0xDE,0xB0,0xEC,0x37,0x42,0xC6,0x2F,0xE0,0xEC,0xE8,0x24,0xA6,
374         0xAB,0xDB,0xE6,0x6C,0x59,0xBE,0xE0,0x24,0x29,0x11,0xBF,0xB9,
375         0x67,0x23,0x5C,0xEB,0xA3,0x5A,0xE1,0x3E,0x4E,0xC7,0x52,0xBE,
376         0x63,0x0B,0x92,0xDC,0x4B,0xDE,0x28,0x47,0xA9,0xC6,0x2C,0xB8,
377         0x15,0x27,0x45,0x42,0x1F,0xB7,0xEB,0x60,0xA6,0x3C,0x0F,0xE9,
378         0x15,0x9F,0xCC,0xE7,0x26,0xCE,0x7C,0xD8,0x52,0x3D,0x74,0x50,
379         0x66,0x7E,0xF8,0x40,0xE4,0x91,0x91,0x21,0xEB,0x5F,0x01,0xC8,
380         0xC9,0xB0,0xD3,0xD6,0x48,0xA9,0x3B,0xFB,0x75,0x68,0x9E,0x82,
381         0x44,0xAC,0x13,0x4A,0xF5,0x44,0x71,0x1C,0xE7,0x9A,0x02,0xDC,
382         0xC3,0x42,0x26,0x68,0x47,0x80,0xDD,0xDC,0xB4,0x98,0x59,0x41,
383         0x06,0xC3,0x7F,0x5B,0xC7,0x98,0x56,0x48,0x7A,0xF5,0xAB,0x02,
384         0x2A,0x2E,0x5E,0x42,0xF0,0x98,0x97,0xC1,0xA8,0x5A,0x11,0xEA,
385         0x02,0x12,0xAF,0x04,0xD9,0xB4,0xCE,0xBC,0x93,0x7C,0x3C,0x1A,
386         0x3E,0x15,0xA8,0xA0,0x34,0x2E,0x33,0x76,0x15,0xC8,0x4E,0x7F,
387         0xE3,0xB8,0xB9,0xB8,0x7F,0xB1,0xE7,0x3A,0x15,0xAF,0x12,0xA3,
388         0x0D,0x74,0x6E,0x06,0xDF,0xC3,0x4F,0x29,0x0D,0x79,0x7C,0xE5,
389         0x1A,0xA1,0x3A,0xA7,0x85,0xBF,0x66,0x58,0xAF,0xF5,0xE4,0xB0,
390         0x93,0x00,0x3C,0xBE,0xAF,0x66,0x5B,0x3C,0x2E,0x11,0x3A,0x3A,
391         0x4E,0x90,0x52,0x69,0x34,0x1D,0xC0,0x71,0x14,0x26,0x68,0x5F,
392         0x4E,0xF3,0x7E,0x86,0x8A,0x81,0x26,0xFF,0x3F,0x22,0x79,0xB5,
393         0x7C,0xA6,0x7E,0x29
394 };
395 static const unsigned char dhtest_2048_256_xB[] = {
396         0x7D,0x62,0xA7,0xE3,0xEF,0x36,0xDE,0x61,0x7B,0x13,0xD1,0xAF,
397         0xB8,0x2C,0x78,0x0D,0x83,0xA2,0x3B,0xD4,0xEE,0x67,0x05,0x64,
398         0x51,0x21,0xF3,0x71,0xF5,0x46,0xA5,0x3D
399 };
400 static const unsigned char dhtest_2048_256_yB[] = {
401         0x57,0x5F,0x03,0x51,0xBD,0x2B,0x1B,0x81,0x74,0x48,0xBD,0xF8,
402         0x7A,0x6C,0x36,0x2C,0x1E,0x28,0x9D,0x39,0x03,0xA3,0x0B,0x98,
403         0x32,0xC5,0x74,0x1F,0xA2,0x50,0x36,0x3E,0x7A,0xCB,0xC7,0xF7,
404         0x7F,0x3D,0xAC,0xBC,0x1F,0x13,0x1A,0xDD,0x8E,0x03,0x36,0x7E,
405         0xFF,0x8F,0xBB,0xB3,0xE1,0xC5,0x78,0x44,0x24,0x80,0x9B,0x25,
406         0xAF,0xE4,0xD2,0x26,0x2A,0x1A,0x6F,0xD2,0xFA,0xB6,0x41,0x05,
407         0xCA,0x30,0xA6,0x74,0xE0,0x7F,0x78,0x09,0x85,0x20,0x88,0x63,
408         0x2F,0xC0,0x49,0x23,0x37,0x91,0xAD,0x4E,0xDD,0x08,0x3A,0x97,
409         0x8B,0x88,0x3E,0xE6,0x18,0xBC,0x5E,0x0D,0xD0,0x47,0x41,0x5F,
410         0x2D,0x95,0xE6,0x83,0xCF,0x14,0x82,0x6B,0x5F,0xBE,0x10,0xD3,
411         0xCE,0x41,0xC6,0xC1,0x20,0xC7,0x8A,0xB2,0x00,0x08,0xC6,0x98,
412         0xBF,0x7F,0x0B,0xCA,0xB9,0xD7,0xF4,0x07,0xBE,0xD0,0xF4,0x3A,
413         0xFB,0x29,0x70,0xF5,0x7F,0x8D,0x12,0x04,0x39,0x63,0xE6,0x6D,
414         0xDD,0x32,0x0D,0x59,0x9A,0xD9,0x93,0x6C,0x8F,0x44,0x13,0x7C,
415         0x08,0xB1,0x80,0xEC,0x5E,0x98,0x5C,0xEB,0xE1,0x86,0xF3,0xD5,
416         0x49,0x67,0x7E,0x80,0x60,0x73,0x31,0xEE,0x17,0xAF,0x33,0x80,
417         0xA7,0x25,0xB0,0x78,0x23,0x17,0xD7,0xDD,0x43,0xF5,0x9D,0x7A,
418         0xF9,0x56,0x8A,0x9B,0xB6,0x3A,0x84,0xD3,0x65,0xF9,0x22,0x44,
419         0xED,0x12,0x09,0x88,0x21,0x93,0x02,0xF4,0x29,0x24,0xC7,0xCA,
420         0x90,0xB8,0x9D,0x24,0xF7,0x1B,0x0A,0xB6,0x97,0x82,0x3D,0x7D,
421         0xEB,0x1A,0xFF,0x5B,0x0E,0x8E,0x4A,0x45,0xD4,0x9F,0x7F,0x53,
422         0x75,0x7E,0x19,0x13
423 };
424 static const unsigned char dhtest_2048_256_Z[] = {
425         0x86,0xC7,0x0B,0xF8,0xD0,0xBB,0x81,0xBB,0x01,0x07,0x8A,0x17,
426         0x21,0x9C,0xB7,0xD2,0x72,0x03,0xDB,0x2A,0x19,0xC8,0x77,0xF1,
427         0xD1,0xF1,0x9F,0xD7,0xD7,0x7E,0xF2,0x25,0x46,0xA6,0x8F,0x00,
428         0x5A,0xD5,0x2D,0xC8,0x45,0x53,0xB7,0x8F,0xC6,0x03,0x30,0xBE,
429         0x51,0xEA,0x7C,0x06,0x72,0xCA,0xC1,0x51,0x5E,0x4B,0x35,0xC0,
430         0x47,0xB9,0xA5,0x51,0xB8,0x8F,0x39,0xDC,0x26,0xDA,0x14,0xA0,
431         0x9E,0xF7,0x47,0x74,0xD4,0x7C,0x76,0x2D,0xD1,0x77,0xF9,0xED,
432         0x5B,0xC2,0xF1,0x1E,0x52,0xC8,0x79,0xBD,0x95,0x09,0x85,0x04,
433         0xCD,0x9E,0xEC,0xD8,0xA8,0xF9,0xB3,0xEF,0xBD,0x1F,0x00,0x8A,
434         0xC5,0x85,0x30,0x97,0xD9,0xD1,0x83,0x7F,0x2B,0x18,0xF7,0x7C,
435         0xD7,0xBE,0x01,0xAF,0x80,0xA7,0xC7,0xB5,0xEA,0x3C,0xA5,0x4C,
436         0xC0,0x2D,0x0C,0x11,0x6F,0xEE,0x3F,0x95,0xBB,0x87,0x39,0x93,
437         0x85,0x87,0x5D,0x7E,0x86,0x74,0x7E,0x67,0x6E,0x72,0x89,0x38,
438         0xAC,0xBF,0xF7,0x09,0x8E,0x05,0xBE,0x4D,0xCF,0xB2,0x40,0x52,
439         0xB8,0x3A,0xEF,0xFB,0x14,0x78,0x3F,0x02,0x9A,0xDB,0xDE,0x7F,
440         0x53,0xFA,0xE9,0x20,0x84,0x22,0x40,0x90,0xE0,0x07,0xCE,0xE9,
441         0x4D,0x4B,0xF2,0xBA,0xCE,0x9F,0xFD,0x4B,0x57,0xD2,0xAF,0x7C,
442         0x72,0x4D,0x0C,0xAA,0x19,0xBF,0x05,0x01,0xF6,0xF1,0x7B,0x4A,
443         0xA1,0x0F,0x42,0x5E,0x3E,0xA7,0x60,0x80,0xB4,0xB9,0xD6,0xB3,
444         0xCE,0xFE,0xA1,0x15,0xB2,0xCE,0xB8,0x78,0x9B,0xB8,0xA3,0xB0,
445         0xEA,0x87,0xFE,0xBE,0x63,0xB6,0xC8,0xF8,0x46,0xEC,0x6D,0xB0,
446         0xC2,0x6C,0x5D,0x7C
447 };
448
449 typedef struct
450         {
451         DH * (*get_param)(void);
452         const unsigned char *xA;
453         size_t xA_len;
454         const unsigned char *yA;
455         size_t yA_len;
456         const unsigned char *xB;
457         size_t xB_len;
458         const unsigned char *yB;
459         size_t yB_len;
460         const unsigned char *Z;
461         size_t Z_len;
462         } rfc5114_td;
463
464 #define make_rfc5114_td(pre) { \
465         DH_get_##pre, \
466         dhtest_##pre##_xA, sizeof(dhtest_##pre##_xA), \
467         dhtest_##pre##_yA, sizeof(dhtest_##pre##_yA), \
468         dhtest_##pre##_xB, sizeof(dhtest_##pre##_xB), \
469         dhtest_##pre##_yB, sizeof(dhtest_##pre##_yB), \
470         dhtest_##pre##_Z, sizeof(dhtest_##pre##_Z) \
471         }
472
473 static const rfc5114_td rfctd[] = {
474         make_rfc5114_td(1024_160),
475         make_rfc5114_td(2048_224),
476         make_rfc5114_td(2048_256)
477 };
478
479 static int run_rfc5114_tests(void)
480         {
481         int i;
482         for (i = 0; i < (int)(sizeof(rfctd)/sizeof(rfc5114_td)); i++)
483                 {
484                 DH *dhA, *dhB;
485                 unsigned char *Z1 = NULL, *Z2 = NULL;
486                 const rfc5114_td *td = rfctd + i;
487                 /* Set up DH structures setting key components */
488                 dhA = td->get_param();
489                 dhB = td->get_param();
490                 if (!dhA || !dhB)
491                         goto bad_err;
492                 
493                 dhA->priv_key = BN_bin2bn(td->xA, td->xA_len, NULL);
494                 dhA->pub_key = BN_bin2bn(td->yA, td->yA_len, NULL);
495
496                 dhB->priv_key = BN_bin2bn(td->xB, td->xB_len, NULL);
497                 dhB->pub_key = BN_bin2bn(td->yB, td->yB_len, NULL);
498
499                 if (!dhA->priv_key || !dhA->pub_key 
500                         || !dhB->priv_key || !dhB->pub_key)
501                         goto bad_err;
502
503                 if ((td->Z_len != (size_t)DH_size(dhA))
504                         || (td->Z_len != (size_t)DH_size(dhB)))
505                         goto err;
506
507                 Z1 = OPENSSL_malloc(DH_size(dhA));
508                 Z2 = OPENSSL_malloc(DH_size(dhB));
509                 /* Work out shared secrets using both sides and compare
510                  * with expected values.
511                  */
512                 if (!DH_compute_key(Z1, dhB->pub_key, dhA))
513                         goto bad_err;
514                 if (!DH_compute_key(Z2, dhA->pub_key, dhB))
515                         goto bad_err;
516
517                 if (memcmp(Z1, td->Z, td->Z_len))
518                         goto err;
519                 if (memcmp(Z2, td->Z, td->Z_len))
520                         goto err;
521
522                 printf("RFC5114 parameter test %d OK\n", i + 1);
523
524                 DH_free(dhA);
525                 DH_free(dhB);
526                 OPENSSL_free(Z1);
527                 OPENSSL_free(Z2);
528
529                 }
530         return 1;
531         bad_err:
532         fprintf(stderr, "Initalisation error RFC5114 set %d\n", i + 1);
533         ERR_print_errors_fp(stderr);
534         return 0;
535         err:
536         fprintf(stderr, "Test failed RFC5114 set %d\n", i + 1);
537         return 0;
538         }
539
540 #endif