ecdhtest.c revision 267654
1212420Sken/* crypto/ecdh/ecdhtest.c */
2212420Sken/* ====================================================================
3212420Sken * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4212420Sken *
5212420Sken * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6212420Sken * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7212420Sken * to the OpenSSL project.
8212420Sken *
9212420Sken * The ECC Code is licensed pursuant to the OpenSSL open source
10212420Sken * license provided below.
11212420Sken *
12212420Sken * The ECDH software is originally written by Douglas Stebila of
13212420Sken * Sun Microsystems Laboratories.
14212420Sken *
15212420Sken */
16212420Sken/* ====================================================================
17212420Sken * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
18212420Sken *
19212420Sken * Redistribution and use in source and binary forms, with or without
20212420Sken * modification, are permitted provided that the following conditions
21212420Sken * are met:
22212420Sken *
23212420Sken * 1. Redistributions of source code must retain the above copyright
24212420Sken *    notice, this list of conditions and the following disclaimer.
25212420Sken *
26212420Sken * 2. Redistributions in binary form must reproduce the above copyright
27212420Sken *    notice, this list of conditions and the following disclaimer in
28212420Sken *    the documentation and/or other materials provided with the
29212420Sken *    distribution.
30279253Sslm *
31212420Sken * 3. All advertising materials mentioning features or use of this
32230592Sken *    software must display the following acknowledgment:
33279253Sslm *    "This product includes software developed by the OpenSSL Project
34279253Sslm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35230592Sken *
36230592Sken * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37230592Sken *    endorse or promote products derived from this software without
38230592Sken *    prior written permission. For written permission, please contact
39230592Sken *    openssl-core@openssl.org.
40230592Sken *
41230592Sken * 5. Products derived from this software may not be called "OpenSSL"
42230592Sken *    nor may "OpenSSL" appear in their names without prior written
43230592Sken *    permission of the OpenSSL Project.
44230592Sken *
45230592Sken * 6. Redistributions of any form whatsoever must retain the following
46230592Sken *    acknowledgment:
47230592Sken *    "This product includes software developed by the OpenSSL Project
48230592Sken *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49230592Sken *
50230592Sken * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51230592Sken * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52230592Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53230592Sken * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54230592Sken * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55230592Sken * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56230592Sken * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57230592Sken * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58279253Sslm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59230592Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60230592Sken * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61230592Sken * OF THE POSSIBILITY OF SUCH DAMAGE.
62212420Sken * ====================================================================
63212420Sken *
64212420Sken * This product includes cryptographic software written by Eric Young
65212420Sken * (eay@cryptsoft.com).  This product includes software written by Tim
66213702Smdf * Hudson (tjh@cryptsoft.com).
67213702Smdf *
68230592Sken */
69212420Sken
70212420Sken#include <stdio.h>
71212420Sken#include <stdlib.h>
72212420Sken#include <string.h>
73212420Sken
74212420Sken#include "../e_os.h"
75212420Sken
76212420Sken#include <openssl/opensslconf.h>	/* for OPENSSL_NO_ECDH */
77212420Sken#include <openssl/crypto.h>
78212420Sken#include <openssl/bio.h>
79212420Sken#include <openssl/bn.h>
80212420Sken#include <openssl/objects.h>
81212420Sken#include <openssl/rand.h>
82212420Sken#include <openssl/sha.h>
83230592Sken#include <openssl/err.h>
84230592Sken
85230592Sken#ifdef OPENSSL_NO_ECDH
86213702Smdfint main(int argc, char *argv[])
87213702Smdf{
88212420Sken    printf("No ECDH support\n");
89212420Sken    return(0);
90212420Sken}
91212420Sken#else
92212420Sken#include <openssl/ec.h>
93230592Sken#include <openssl/ecdh.h>
94279253Sslm
95212420Sken#ifdef OPENSSL_SYS_WIN16
96212420Sken#define MS_CALLBACK	_far _loadds
97212420Sken#else
98212420Sken#define MS_CALLBACK
99212420Sken#endif
100212420Sken
101230592Sken#if 0
102230592Skenstatic void MS_CALLBACK cb(int p, int n, void *arg);
103230592Sken#endif
104212420Sken
105212420Skenstatic const char rnd_seed[] = "string to make the random number generator think it has entropy";
106279253Sslm
107230592Sken
108230592Skenstatic const int KDF1_SHA1_len = 20;
109212420Skenstatic void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
110212420Sken	{
111212420Sken#ifndef OPENSSL_NO_SHA
112213702Smdf	if (*outlen < SHA_DIGEST_LENGTH)
113212420Sken		return NULL;
114212420Sken	else
115212420Sken		*outlen = SHA_DIGEST_LENGTH;
116212420Sken	return SHA1(in, inlen, out);
117212420Sken#else
118212420Sken	return NULL;
119213702Smdf#endif
120212420Sken	}
121212420Sken
122212420Sken
123213708Smdfstatic int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
124213708Smdf	{
125213708Smdf	EC_KEY *a=NULL;
126213708Smdf	EC_KEY *b=NULL;
127213708Smdf	BIGNUM *x_a=NULL, *y_a=NULL,
128213708Smdf	       *x_b=NULL, *y_b=NULL;
129213708Smdf	char buf[12];
130213708Smdf	unsigned char *abuf=NULL,*bbuf=NULL;
131213708Smdf	int i,alen,blen,aout,bout,ret=0;
132213708Smdf	const EC_GROUP *group;
133213707Smdf
134213707Smdf	a = EC_KEY_new_by_curve_name(nid);
135213707Smdf	b = EC_KEY_new_by_curve_name(nid);
136213707Smdf	if (a == NULL || b == NULL)
137213707Smdf		goto err;
138213707Smdf
139213707Smdf	group = EC_KEY_get0_group(a);
140213707Smdf
141213707Smdf	if ((x_a=BN_new()) == NULL) goto err;
142213707Smdf	if ((y_a=BN_new()) == NULL) goto err;
143213708Smdf	if ((x_b=BN_new()) == NULL) goto err;
144213708Smdf	if ((y_b=BN_new()) == NULL) goto err;
145213707Smdf
146213707Smdf	BIO_puts(out,"Testing key generation with ");
147230592Sken	BIO_puts(out,text);
148230592Sken#ifdef NOISY
149230592Sken	BIO_puts(out,"\n");
150230592Sken#else
151230592Sken	(void)BIO_flush(out);
152230592Sken#endif
153230592Sken
154230592Sken	if (!EC_KEY_generate_key(a)) goto err;
155230592Sken
156230592Sken	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
157230592Sken		{
158230592Sken		if (!EC_POINT_get_affine_coordinates_GFp(group,
159230592Sken			EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
160230592Sken		}
161230592Sken	else
162230592Sken		{
163230592Sken		if (!EC_POINT_get_affine_coordinates_GF2m(group,
164230592Sken			EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
165230592Sken		}
166230592Sken#ifdef NOISY
167230592Sken	BIO_puts(out,"  pri 1=");
168230592Sken	BN_print(out,a->priv_key);
169230592Sken	BIO_puts(out,"\n  pub 1=");
170230592Sken	BN_print(out,x_a);
171230592Sken	BIO_puts(out,",");
172230592Sken	BN_print(out,y_a);
173230592Sken	BIO_puts(out,"\n");
174230592Sken#else
175230592Sken	BIO_printf(out," .");
176230592Sken	(void)BIO_flush(out);
177230592Sken#endif
178230592Sken
179230592Sken	if (!EC_KEY_generate_key(b)) goto err;
180230592Sken
181212420Sken	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
182212420Sken		{
183230592Sken		if (!EC_POINT_get_affine_coordinates_GFp(group,
184230592Sken			EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
185230592Sken		}
186230592Sken	else
187230592Sken		{
188230592Sken		if (!EC_POINT_get_affine_coordinates_GF2m(group,
189230592Sken			EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
190230592Sken		}
191230592Sken
192230592Sken#ifdef NOISY
193212420Sken	BIO_puts(out,"  pri 2=");
194212420Sken	BN_print(out,b->priv_key);
195212420Sken	BIO_puts(out,"\n  pub 2=");
196212420Sken	BN_print(out,x_b);
197212420Sken	BIO_puts(out,",");
198212420Sken	BN_print(out,y_b);
199212420Sken	BIO_puts(out,"\n");
200212420Sken#else
201212420Sken	BIO_printf(out,".");
202212420Sken	(void)BIO_flush(out);
203212420Sken#endif
204212420Sken
205212420Sken	alen=KDF1_SHA1_len;
206212420Sken	abuf=(unsigned char *)OPENSSL_malloc(alen);
207212420Sken	aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
208212420Sken
209212420Sken#ifdef NOISY
210212420Sken	BIO_puts(out,"  key1 =");
211212420Sken	for (i=0; i<aout; i++)
212212420Sken		{
213250900Smav		sprintf(buf,"%02X",abuf[i]);
214250900Smav		BIO_puts(out,buf);
215212420Sken		}
216212420Sken	BIO_puts(out,"\n");
217212420Sken#else
218212420Sken	BIO_printf(out,".");
219212420Sken	(void)BIO_flush(out);
220212420Sken#endif
221212420Sken
222212420Sken	blen=KDF1_SHA1_len;
223212420Sken	bbuf=(unsigned char *)OPENSSL_malloc(blen);
224212420Sken	bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
225212420Sken
226212420Sken#ifdef NOISY
227212420Sken	BIO_puts(out,"  key2 =");
228212420Sken	for (i=0; i<bout; i++)
229212420Sken		{
230212420Sken		sprintf(buf,"%02X",bbuf[i]);
231212420Sken		BIO_puts(out,buf);
232212420Sken		}
233212420Sken	BIO_puts(out,"\n");
234212420Sken#else
235212420Sken	BIO_printf(out,".");
236212420Sken	(void)BIO_flush(out);
237212420Sken#endif
238212420Sken
239212420Sken	if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
240212420Sken		{
241212420Sken#ifndef NOISY
242212420Sken		BIO_printf(out, " failed\n\n");
243212420Sken		BIO_printf(out, "key a:\n");
244212420Sken		BIO_printf(out, "private key: ");
245212420Sken		BN_print(out, EC_KEY_get0_private_key(a));
246212420Sken		BIO_printf(out, "\n");
247212420Sken		BIO_printf(out, "public key (x,y): ");
248212420Sken		BN_print(out, x_a);
249212420Sken		BIO_printf(out, ",");
250212420Sken		BN_print(out, y_a);
251212420Sken		BIO_printf(out, "\nkey b:\n");
252212420Sken		BIO_printf(out, "private key: ");
253212420Sken		BN_print(out, EC_KEY_get0_private_key(b));
254212420Sken		BIO_printf(out, "\n");
255212420Sken		BIO_printf(out, "public key (x,y): ");
256212420Sken		BN_print(out, x_b);
257212420Sken		BIO_printf(out, ",");
258212420Sken		BN_print(out, y_b);
259212420Sken		BIO_printf(out, "\n");
260212420Sken		BIO_printf(out, "generated key a: ");
261212420Sken		for (i=0; i<bout; i++)
262212420Sken			{
263212420Sken			sprintf(buf, "%02X", bbuf[i]);
264212420Sken			BIO_puts(out, buf);
265212420Sken			}
266212420Sken		BIO_printf(out, "\n");
267212420Sken		BIO_printf(out, "generated key b: ");
268212420Sken		for (i=0; i<aout; i++)
269212420Sken			{
270212420Sken			sprintf(buf, "%02X", abuf[i]);
271212420Sken			BIO_puts(out,buf);
272212420Sken			}
273212420Sken		BIO_printf(out, "\n");
274212420Sken#endif
275212420Sken		fprintf(stderr,"Error in ECDH routines\n");
276212420Sken		ret=0;
277212420Sken		}
278212420Sken	else
279212420Sken		{
280212420Sken#ifndef NOISY
281212420Sken		BIO_printf(out, " ok\n");
282212420Sken#endif
283212420Sken		ret=1;
284212420Sken		}
285212420Skenerr:
286212420Sken	ERR_print_errors_fp(stderr);
287212420Sken
288212420Sken	if (abuf != NULL) OPENSSL_free(abuf);
289212420Sken	if (bbuf != NULL) OPENSSL_free(bbuf);
290212420Sken	if (x_a) BN_free(x_a);
291212420Sken	if (y_a) BN_free(y_a);
292212420Sken	if (x_b) BN_free(x_b);
293212420Sken	if (y_b) BN_free(y_b);
294212420Sken	if (b) EC_KEY_free(b);
295212420Sken	if (a) EC_KEY_free(a);
296212420Sken	return(ret);
297212420Sken	}
298212420Sken
299212420Skenint main(int argc, char *argv[])
300212420Sken	{
301212420Sken	BN_CTX *ctx=NULL;
302212420Sken	int ret=1;
303212420Sken	BIO *out;
304212420Sken
305212420Sken	CRYPTO_malloc_debug_init();
306212420Sken	CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
307212420Sken	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
308212420Sken
309212420Sken#ifdef OPENSSL_SYS_WIN32
310251396Sasomers	CRYPTO_malloc_init();
311212420Sken#endif
312212420Sken
313212420Sken	RAND_seed(rnd_seed, sizeof rnd_seed);
314212420Sken
315277717Sscottl	out=BIO_new(BIO_s_file());
316277717Sscottl	if (out == NULL) EXIT(1);
317277717Sscottl	BIO_set_fp(out,stdout,BIO_NOCLOSE);
318277717Sscottl
319212420Sken	if ((ctx=BN_CTX_new()) == NULL) goto err;
320212420Sken
321212420Sken	/* NIST PRIME CURVES TESTS */
322212420Sken	if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
323212420Sken	if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
324212420Sken	if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
325212420Sken	if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
326212420Sken	if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
327212420Sken	/* NIST BINARY CURVES TESTS */
328212420Sken	if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
329212420Sken	if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
330212420Sken	if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
331212420Sken	if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
332212420Sken	if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
333212420Sken	if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
334212420Sken	if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
335212420Sken	if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
336212420Sken	if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
337212420Sken	if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
338212420Sken
339212420Sken	ret = 0;
340212420Sken
341212420Skenerr:
342212420Sken	ERR_print_errors_fp(stderr);
343212420Sken	if (ctx) BN_CTX_free(ctx);
344212420Sken	BIO_free(out);
345212420Sken	CRYPTO_cleanup_all_ex_data();
346212420Sken	ERR_remove_state(0);
347212420Sken	CRYPTO_mem_leaks_fp(stderr);
348212420Sken	EXIT(ret);
349212420Sken	return(ret);
350212420Sken	}
351212420Sken
352212420Sken#if 0
353212420Skenstatic void MS_CALLBACK cb(int p, int n, void *arg)
354212420Sken	{
355212420Sken	char c='*';
356251396Sasomers
357212420Sken	if (p == 0) c='.';
358212420Sken	if (p == 1) c='+';
359212420Sken	if (p == 2) c='*';
360212420Sken	if (p == 3) c='\n';
361212420Sken	BIO_write((BIO *)arg,&c,1);
362212420Sken	(void)BIO_flush((BIO *)arg);
363212420Sken#ifdef LINT
364212420Sken	p=n;
365212420Sken#endif
366212420Sken	}
367212420Sken#endif
368212420Sken#endif
369212420Sken