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#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <bcmcrypto/dh.h>
63
64#define OPENSSL_malloc malloc
65#define OPENSSL_free free
66
67#define DEBUG 0
68#define FUNTEST 0
69#define BRCM
70#define MAX_PRIME_NUM 50
71#define dbg(fmt, arg...)       //printf(fmt, ##arg)
72
73unsigned long p1536[] = {
74 0xfe6adceb,
75 0xbd3934a4,
76 0x31139db1,
77 0x716fc9ec,
78 0x4f464d0e,
79 0x96783772,
80 0x6c22c9d7,
81 0x1b7e34d5,
82 0x40d9b1e7,
83 0x55df9cb1,
84 0xdfbec820,
85 0xb053c663,
86 0x8e4577a6,
87 0x88d6f5ca,
88 0x7143063f,
89 0x09cf3ed6,
90 0x2afc249a,
91 0x25a0fdf4,
92 0x0c939c04,
93 0x4451ad44,
94 0xe60a2efc,
95 0x67f19747,
96 0x840bb2f7,
97 0xe31f9830,
98 0x66d30ad7,
99 0x36298c7f,
100 0xb3802af6,
101 0x9c79b075,
102 0xc15eb521,
103 0xc72abad9,
104 0x9d0ec244,
105 0x19385d0e,
106 0x9386226a,
107 0x93580c99,
108 0x3983a696,
109 0xa735c837,
110 0x974f4e09,
111 0x91b101ed,
112 0x623ff671,
113 0x8c547d0c,
114 0x6d7cb0ac,
115 0x1377ff20,
116 0xf1c50a11,
117 0xf5071153,
118 0x849758ba,
119 0x5f92e05c,
120 0xe149fa4d,
121 0xab062592};
122
123void ots_rand(unsigned char *rnd, int rndlen)
124{
125	time_t tim;
126	int i, vrand;
127	unsigned long *bptr;
128
129	/* make a random number and set the top and bottom bits */
130	time(&tim);
131	srand((unsigned long)tim);
132
133	for(i=0;i<rndlen;i++)
134	{
135		vrand = rand();
136		rnd[i] = vrand;
137	}
138	return;
139}
140
141#ifdef FUNTEST
142#else
143int main(int argc, char *argv)
144{
145	DH *a;
146	DH *b=NULL;
147	unsigned char *abuf=NULL,*bbuf=NULL;
148	unsigned char *apubbuf=NULL,*bpubbuf=NULL;
149	int i,alen,blen,apublen,bpublen,aout,bout,ret=1, size = 0;
150
151	BN_register_RAND(ots_rand);
152
153	a = DH_init(p1536, 192, 5);
154
155	dbg("\np    =");
156	bn_print(a->p);
157	dbg("\ng    =");
158	bn_print(a->g);
159	dbg("\n");
160
161
162	b=DH_new();
163	if (b == NULL) goto err;
164
165	b->p=a->p;
166	b->g=a->g;
167	if ((b->p == NULL) || (b->g == NULL)) goto err;
168
169	apubbuf = (unsigned char *)OPENSSL_malloc(DH_size(a));
170	/* dbg("%s\n", file2str("/proc/uptime")); */
171	if (!(apublen=DH_generate_key(apubbuf,a))) goto err;
172	/* dbg("%s\n", file2str("/proc/uptime")); */
173	dbg("pri 1=");
174	bn_print(a->priv_key);
175	dbg("\npub 1:%x=", apublen);
176	for (i=0; i<apublen; i++) dbg("%02X",apubbuf[i]);
177	dbg("\n");
178
179	bpubbuf = (unsigned char *)OPENSSL_malloc(DH_size(b));
180	if (!(bpublen=DH_generate_key(bpubbuf,b))) goto err;
181	dbg("pri 2=");
182	bn_print(b->priv_key);
183	dbg("\npub 2:%x=", bpublen);
184	for (i=0; i<bpublen; i++) dbg("%02X",bpubbuf[i]);
185	dbg("\n");
186
187	alen=DH_size(a);
188	abuf=(unsigned char *)OPENSSL_malloc(alen);
189	/* dbg("%s\n", file2str("/proc/uptime")); */
190	aout=DH_compute_key(abuf,bpubbuf, bpublen,a);
191	/* dbg("%s\n", file2str("/proc/uptime")); */
192
193	dbg("key1:%x =", aout);
194	for (i=0; i<aout; i++) dbg("%02X",abuf[i]);
195	dbg("\n");
196
197	blen=DH_size(b);
198	bbuf=(unsigned char *)OPENSSL_malloc(blen);
199	bout=DH_compute_key(bbuf,apubbuf,apublen,b);
200
201	dbg("key2:%x =", bout);
202	for (i=0; i<bout; i++) dbg("%02X",bbuf[i]);
203	dbg("\n");
204
205	if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
206		{
207		fprintf(stderr,"Error in DH routines\n");
208		ret=1;
209		}
210	else
211		ret=0;
212err:
213	if (abuf != NULL) OPENSSL_free(abuf);
214	if (bbuf != NULL) OPENSSL_free(bbuf);
215	if(b != NULL) DH_free(b);
216	if(a != NULL) DH_free(a);
217	return(ret);
218}
219
220int DH_size(const DH *dh)
221	{
222	return(BN_num_bytes(dh->p));
223	}
224
225
226static const char *Hex="0123456789ABCDEF";
227
228static int bn_print(const BIGNUM *a)
229{
230        int i,j,v,z=0;
231        int ret=0;
232
233        if ((a->neg) && (dbg("-") != 1)) goto end;
234        if ((a->top == 0) && (dbg("0") != 1)) goto end;
235        for (i=a->top-1; i >=0; i--)
236                {
237                for (j=BN_BITS2-4; j >= 0; j-=4)
238                        {
239                        /* strip leading zeros */
240                        v=((int)(a->d[i]>>(long)j))&0x0f;
241                        if (z || (v != 0))
242                                {
243				dbg("%c", Hex[v]);
244                                z=1;
245                                }
246                        }
247                }
248        ret=1;
249end:
250        return(ret);
251}
252#endif
253