1*2139Sjp161948 /* crypto/ecdh/ecdhtest.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4*2139Sjp161948 *
5*2139Sjp161948 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6*2139Sjp161948 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7*2139Sjp161948 * to the OpenSSL project.
8*2139Sjp161948 *
9*2139Sjp161948 * The ECC Code is licensed pursuant to the OpenSSL open source
10*2139Sjp161948 * license provided below.
11*2139Sjp161948 *
12*2139Sjp161948 * The ECDH software is originally written by Douglas Stebila of
13*2139Sjp161948 * Sun Microsystems Laboratories.
14*2139Sjp161948 *
15*2139Sjp161948 */
16*2139Sjp161948 /* ====================================================================
17*2139Sjp161948 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
18*2139Sjp161948 *
19*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
20*2139Sjp161948 * modification, are permitted provided that the following conditions
21*2139Sjp161948 * are met:
22*2139Sjp161948 *
23*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
24*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
25*2139Sjp161948 *
26*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
27*2139Sjp161948 * notice, this list of conditions and the following disclaimer in
28*2139Sjp161948 * the documentation and/or other materials provided with the
29*2139Sjp161948 * distribution.
30*2139Sjp161948 *
31*2139Sjp161948 * 3. All advertising materials mentioning features or use of this
32*2139Sjp161948 * software must display the following acknowledgment:
33*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
34*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35*2139Sjp161948 *
36*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37*2139Sjp161948 * endorse or promote products derived from this software without
38*2139Sjp161948 * prior written permission. For written permission, please contact
39*2139Sjp161948 * openssl-core@openssl.org.
40*2139Sjp161948 *
41*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
42*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
43*2139Sjp161948 * permission of the OpenSSL Project.
44*2139Sjp161948 *
45*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
46*2139Sjp161948 * acknowledgment:
47*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
48*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49*2139Sjp161948 *
50*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
62*2139Sjp161948 * ====================================================================
63*2139Sjp161948 *
64*2139Sjp161948 * This product includes cryptographic software written by Eric Young
65*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
66*2139Sjp161948 * Hudson (tjh@cryptsoft.com).
67*2139Sjp161948 *
68*2139Sjp161948 */
69*2139Sjp161948
70*2139Sjp161948 #include <stdio.h>
71*2139Sjp161948 #include <stdlib.h>
72*2139Sjp161948 #include <string.h>
73*2139Sjp161948
74*2139Sjp161948 #include "../e_os.h"
75*2139Sjp161948
76*2139Sjp161948 #include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */
77*2139Sjp161948 #include <openssl/crypto.h>
78*2139Sjp161948 #include <openssl/bio.h>
79*2139Sjp161948 #include <openssl/bn.h>
80*2139Sjp161948 #include <openssl/objects.h>
81*2139Sjp161948 #include <openssl/rand.h>
82*2139Sjp161948 #include <openssl/sha.h>
83*2139Sjp161948 #include <openssl/err.h>
84*2139Sjp161948
85*2139Sjp161948 #ifdef OPENSSL_NO_ECDH
main(int argc,char * argv[])86*2139Sjp161948 int main(int argc, char *argv[])
87*2139Sjp161948 {
88*2139Sjp161948 printf("No ECDH support\n");
89*2139Sjp161948 return(0);
90*2139Sjp161948 }
91*2139Sjp161948 #else
92*2139Sjp161948 #include <openssl/ec.h>
93*2139Sjp161948 #include <openssl/ecdh.h>
94*2139Sjp161948
95*2139Sjp161948 #ifdef OPENSSL_SYS_WIN16
96*2139Sjp161948 #define MS_CALLBACK _far _loadds
97*2139Sjp161948 #else
98*2139Sjp161948 #define MS_CALLBACK
99*2139Sjp161948 #endif
100*2139Sjp161948
101*2139Sjp161948 #if 0
102*2139Sjp161948 static void MS_CALLBACK cb(int p, int n, void *arg);
103*2139Sjp161948 #endif
104*2139Sjp161948
105*2139Sjp161948 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
106*2139Sjp161948
107*2139Sjp161948
108*2139Sjp161948 static const int KDF1_SHA1_len = 20;
KDF1_SHA1(const void * in,size_t inlen,void * out,size_t * outlen)109*2139Sjp161948 static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
110*2139Sjp161948 {
111*2139Sjp161948 #ifndef OPENSSL_NO_SHA
112*2139Sjp161948 if (*outlen < SHA_DIGEST_LENGTH)
113*2139Sjp161948 return NULL;
114*2139Sjp161948 else
115*2139Sjp161948 *outlen = SHA_DIGEST_LENGTH;
116*2139Sjp161948 return SHA1(in, inlen, out);
117*2139Sjp161948 #else
118*2139Sjp161948 return NULL;
119*2139Sjp161948 #endif
120*2139Sjp161948 }
121*2139Sjp161948
122*2139Sjp161948
test_ecdh_curve(int nid,const char * text,BN_CTX * ctx,BIO * out)123*2139Sjp161948 static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
124*2139Sjp161948 {
125*2139Sjp161948 EC_KEY *a=NULL;
126*2139Sjp161948 EC_KEY *b=NULL;
127*2139Sjp161948 BIGNUM *x_a=NULL, *y_a=NULL,
128*2139Sjp161948 *x_b=NULL, *y_b=NULL;
129*2139Sjp161948 char buf[12];
130*2139Sjp161948 unsigned char *abuf=NULL,*bbuf=NULL;
131*2139Sjp161948 int i,alen,blen,aout,bout,ret=0;
132*2139Sjp161948 const EC_GROUP *group;
133*2139Sjp161948
134*2139Sjp161948 a = EC_KEY_new_by_curve_name(nid);
135*2139Sjp161948 b = EC_KEY_new_by_curve_name(nid);
136*2139Sjp161948 if (a == NULL || b == NULL)
137*2139Sjp161948 goto err;
138*2139Sjp161948
139*2139Sjp161948 group = EC_KEY_get0_group(a);
140*2139Sjp161948
141*2139Sjp161948 if ((x_a=BN_new()) == NULL) goto err;
142*2139Sjp161948 if ((y_a=BN_new()) == NULL) goto err;
143*2139Sjp161948 if ((x_b=BN_new()) == NULL) goto err;
144*2139Sjp161948 if ((y_b=BN_new()) == NULL) goto err;
145*2139Sjp161948
146*2139Sjp161948 BIO_puts(out,"Testing key generation with ");
147*2139Sjp161948 BIO_puts(out,text);
148*2139Sjp161948 #ifdef NOISY
149*2139Sjp161948 BIO_puts(out,"\n");
150*2139Sjp161948 #else
151*2139Sjp161948 BIO_flush(out);
152*2139Sjp161948 #endif
153*2139Sjp161948
154*2139Sjp161948 if (!EC_KEY_generate_key(a)) goto err;
155*2139Sjp161948
156*2139Sjp161948 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
157*2139Sjp161948 {
158*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GFp(group,
159*2139Sjp161948 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
160*2139Sjp161948 }
161*2139Sjp161948 else
162*2139Sjp161948 {
163*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GF2m(group,
164*2139Sjp161948 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
165*2139Sjp161948 }
166*2139Sjp161948 #ifdef NOISY
167*2139Sjp161948 BIO_puts(out," pri 1=");
168*2139Sjp161948 BN_print(out,a->priv_key);
169*2139Sjp161948 BIO_puts(out,"\n pub 1=");
170*2139Sjp161948 BN_print(out,x_a);
171*2139Sjp161948 BIO_puts(out,",");
172*2139Sjp161948 BN_print(out,y_a);
173*2139Sjp161948 BIO_puts(out,"\n");
174*2139Sjp161948 #else
175*2139Sjp161948 BIO_printf(out," .");
176*2139Sjp161948 BIO_flush(out);
177*2139Sjp161948 #endif
178*2139Sjp161948
179*2139Sjp161948 if (!EC_KEY_generate_key(b)) goto err;
180*2139Sjp161948
181*2139Sjp161948 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
182*2139Sjp161948 {
183*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GFp(group,
184*2139Sjp161948 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
185*2139Sjp161948 }
186*2139Sjp161948 else
187*2139Sjp161948 {
188*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GF2m(group,
189*2139Sjp161948 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
190*2139Sjp161948 }
191*2139Sjp161948
192*2139Sjp161948 #ifdef NOISY
193*2139Sjp161948 BIO_puts(out," pri 2=");
194*2139Sjp161948 BN_print(out,b->priv_key);
195*2139Sjp161948 BIO_puts(out,"\n pub 2=");
196*2139Sjp161948 BN_print(out,x_b);
197*2139Sjp161948 BIO_puts(out,",");
198*2139Sjp161948 BN_print(out,y_b);
199*2139Sjp161948 BIO_puts(out,"\n");
200*2139Sjp161948 #else
201*2139Sjp161948 BIO_printf(out,".");
202*2139Sjp161948 BIO_flush(out);
203*2139Sjp161948 #endif
204*2139Sjp161948
205*2139Sjp161948 alen=KDF1_SHA1_len;
206*2139Sjp161948 abuf=(unsigned char *)OPENSSL_malloc(alen);
207*2139Sjp161948 aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
208*2139Sjp161948
209*2139Sjp161948 #ifdef NOISY
210*2139Sjp161948 BIO_puts(out," key1 =");
211*2139Sjp161948 for (i=0; i<aout; i++)
212*2139Sjp161948 {
213*2139Sjp161948 sprintf(buf,"%02X",abuf[i]);
214*2139Sjp161948 BIO_puts(out,buf);
215*2139Sjp161948 }
216*2139Sjp161948 BIO_puts(out,"\n");
217*2139Sjp161948 #else
218*2139Sjp161948 BIO_printf(out,".");
219*2139Sjp161948 BIO_flush(out);
220*2139Sjp161948 #endif
221*2139Sjp161948
222*2139Sjp161948 blen=KDF1_SHA1_len;
223*2139Sjp161948 bbuf=(unsigned char *)OPENSSL_malloc(blen);
224*2139Sjp161948 bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
225*2139Sjp161948
226*2139Sjp161948 #ifdef NOISY
227*2139Sjp161948 BIO_puts(out," key2 =");
228*2139Sjp161948 for (i=0; i<bout; i++)
229*2139Sjp161948 {
230*2139Sjp161948 sprintf(buf,"%02X",bbuf[i]);
231*2139Sjp161948 BIO_puts(out,buf);
232*2139Sjp161948 }
233*2139Sjp161948 BIO_puts(out,"\n");
234*2139Sjp161948 #else
235*2139Sjp161948 BIO_printf(out,".");
236*2139Sjp161948 BIO_flush(out);
237*2139Sjp161948 #endif
238*2139Sjp161948
239*2139Sjp161948 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
240*2139Sjp161948 {
241*2139Sjp161948 #ifndef NOISY
242*2139Sjp161948 BIO_printf(out, " failed\n\n");
243*2139Sjp161948 BIO_printf(out, "key a:\n");
244*2139Sjp161948 BIO_printf(out, "private key: ");
245*2139Sjp161948 BN_print(out, EC_KEY_get0_private_key(a));
246*2139Sjp161948 BIO_printf(out, "\n");
247*2139Sjp161948 BIO_printf(out, "public key (x,y): ");
248*2139Sjp161948 BN_print(out, x_a);
249*2139Sjp161948 BIO_printf(out, ",");
250*2139Sjp161948 BN_print(out, y_a);
251*2139Sjp161948 BIO_printf(out, "\nkey b:\n");
252*2139Sjp161948 BIO_printf(out, "private key: ");
253*2139Sjp161948 BN_print(out, EC_KEY_get0_private_key(b));
254*2139Sjp161948 BIO_printf(out, "\n");
255*2139Sjp161948 BIO_printf(out, "public key (x,y): ");
256*2139Sjp161948 BN_print(out, x_b);
257*2139Sjp161948 BIO_printf(out, ",");
258*2139Sjp161948 BN_print(out, y_b);
259*2139Sjp161948 BIO_printf(out, "\n");
260*2139Sjp161948 BIO_printf(out, "generated key a: ");
261*2139Sjp161948 for (i=0; i<bout; i++)
262*2139Sjp161948 {
263*2139Sjp161948 sprintf(buf, "%02X", bbuf[i]);
264*2139Sjp161948 BIO_puts(out, buf);
265*2139Sjp161948 }
266*2139Sjp161948 BIO_printf(out, "\n");
267*2139Sjp161948 BIO_printf(out, "generated key b: ");
268*2139Sjp161948 for (i=0; i<aout; i++)
269*2139Sjp161948 {
270*2139Sjp161948 sprintf(buf, "%02X", abuf[i]);
271*2139Sjp161948 BIO_puts(out,buf);
272*2139Sjp161948 }
273*2139Sjp161948 BIO_printf(out, "\n");
274*2139Sjp161948 #endif
275*2139Sjp161948 fprintf(stderr,"Error in ECDH routines\n");
276*2139Sjp161948 ret=0;
277*2139Sjp161948 }
278*2139Sjp161948 else
279*2139Sjp161948 {
280*2139Sjp161948 #ifndef NOISY
281*2139Sjp161948 BIO_printf(out, " ok\n");
282*2139Sjp161948 #endif
283*2139Sjp161948 ret=1;
284*2139Sjp161948 }
285*2139Sjp161948 err:
286*2139Sjp161948 ERR_print_errors_fp(stderr);
287*2139Sjp161948
288*2139Sjp161948 if (abuf != NULL) OPENSSL_free(abuf);
289*2139Sjp161948 if (bbuf != NULL) OPENSSL_free(bbuf);
290*2139Sjp161948 if (x_a) BN_free(x_a);
291*2139Sjp161948 if (y_a) BN_free(y_a);
292*2139Sjp161948 if (x_b) BN_free(x_b);
293*2139Sjp161948 if (y_b) BN_free(y_b);
294*2139Sjp161948 if (b) EC_KEY_free(b);
295*2139Sjp161948 if (a) EC_KEY_free(a);
296*2139Sjp161948 return(ret);
297*2139Sjp161948 }
298*2139Sjp161948
main(int argc,char * argv[])299*2139Sjp161948 int main(int argc, char *argv[])
300*2139Sjp161948 {
301*2139Sjp161948 BN_CTX *ctx=NULL;
302*2139Sjp161948 int ret=1;
303*2139Sjp161948 BIO *out;
304*2139Sjp161948
305*2139Sjp161948 CRYPTO_malloc_debug_init();
306*2139Sjp161948 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
307*2139Sjp161948 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
308*2139Sjp161948
309*2139Sjp161948 #ifdef OPENSSL_SYS_WIN32
310*2139Sjp161948 CRYPTO_malloc_init();
311*2139Sjp161948 #endif
312*2139Sjp161948
313*2139Sjp161948 RAND_seed(rnd_seed, sizeof rnd_seed);
314*2139Sjp161948
315*2139Sjp161948 out=BIO_new(BIO_s_file());
316*2139Sjp161948 if (out == NULL) EXIT(1);
317*2139Sjp161948 BIO_set_fp(out,stdout,BIO_NOCLOSE);
318*2139Sjp161948
319*2139Sjp161948 if ((ctx=BN_CTX_new()) == NULL) goto err;
320*2139Sjp161948
321*2139Sjp161948 /* NIST PRIME CURVES TESTS */
322*2139Sjp161948 if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
323*2139Sjp161948 if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
324*2139Sjp161948 if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
325*2139Sjp161948 if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
326*2139Sjp161948 if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
327*2139Sjp161948 /* NIST BINARY CURVES TESTS */
328*2139Sjp161948 if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
329*2139Sjp161948 if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
330*2139Sjp161948 if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
331*2139Sjp161948 if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
332*2139Sjp161948 if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
333*2139Sjp161948 if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
334*2139Sjp161948 if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
335*2139Sjp161948 if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
336*2139Sjp161948 if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
337*2139Sjp161948 if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
338*2139Sjp161948
339*2139Sjp161948 ret = 0;
340*2139Sjp161948
341*2139Sjp161948 err:
342*2139Sjp161948 ERR_print_errors_fp(stderr);
343*2139Sjp161948 if (ctx) BN_CTX_free(ctx);
344*2139Sjp161948 BIO_free(out);
345*2139Sjp161948 CRYPTO_cleanup_all_ex_data();
346*2139Sjp161948 ERR_remove_state(0);
347*2139Sjp161948 CRYPTO_mem_leaks_fp(stderr);
348*2139Sjp161948 EXIT(ret);
349*2139Sjp161948 return(ret);
350*2139Sjp161948 }
351*2139Sjp161948
352*2139Sjp161948 #if 0
353*2139Sjp161948 static void MS_CALLBACK cb(int p, int n, void *arg)
354*2139Sjp161948 {
355*2139Sjp161948 char c='*';
356*2139Sjp161948
357*2139Sjp161948 if (p == 0) c='.';
358*2139Sjp161948 if (p == 1) c='+';
359*2139Sjp161948 if (p == 2) c='*';
360*2139Sjp161948 if (p == 3) c='\n';
361*2139Sjp161948 BIO_write((BIO *)arg,&c,1);
362*2139Sjp161948 (void)BIO_flush((BIO *)arg);
363*2139Sjp161948 #ifdef LINT
364*2139Sjp161948 p=n;
365*2139Sjp161948 #endif
366*2139Sjp161948 }
367*2139Sjp161948 #endif
368*2139Sjp161948 #endif
369