118334Speter/* crypto/ecdh/ecdhtest.c */
218334Speter/* ====================================================================
390075Sobrien * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4169689Skan *
518334Speter * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
618334Speter * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7132718Skan * to the OpenSSL project.
818334Speter *
9132718Skan * The ECC Code is licensed pursuant to the OpenSSL open source
1018334Speter * license provided below.
1118334Speter *
1218334Speter * The ECDH software is originally written by Douglas Stebila of
1318334Speter * Sun Microsystems Laboratories.
14132718Skan *
1518334Speter */
1618334Speter/* ====================================================================
1718334Speter * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
1818334Speter *
1918334Speter * Redistribution and use in source and binary forms, with or without
20132718Skan * modification, are permitted provided that the following conditions
21169689Skan * are met:
22169689Skan *
2318334Speter * 1. Redistributions of source code must retain the above copyright
2450397Sobrien *    notice, this list of conditions and the following disclaimer.
2518334Speter *
2618334Speter * 2. Redistributions in binary form must reproduce the above copyright
2750397Sobrien *    notice, this list of conditions and the following disclaimer in
28132718Skan *    the documentation and/or other materials provided with the
29132718Skan *    distribution.
3018334Speter *
3118334Speter * 3. All advertising materials mentioning features or use of this
3218334Speter *    software must display the following acknowledgment:
3318334Speter *    "This product includes software developed by the OpenSSL Project
3418334Speter *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
3518334Speter *
3650397Sobrien * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
3718334Speter *    endorse or promote products derived from this software without
38146895Skan *    prior written permission. For written permission, please contact
39169689Skan *    openssl-core@openssl.org.
40169689Skan *
41169689Skan * 5. Products derived from this software may not be called "OpenSSL"
42132718Skan *    nor may "OpenSSL" appear in their names without prior written
43169689Skan *    permission of the OpenSSL Project.
44169689Skan *
45169689Skan * 6. Redistributions of any form whatsoever must retain the following
46169689Skan *    acknowledgment:
47169689Skan *    "This product includes software developed by the OpenSSL Project
48169689Skan *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49169689Skan *
50132718Skan * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51132718Skan * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53169689Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54169689Skan * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55169689Skan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56169689Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57169689Skan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59169689Skan * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60169689Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61132718Skan * OF THE POSSIBILITY OF SUCH DAMAGE.
62132718Skan * ====================================================================
63132718Skan *
64132718Skan * This product includes cryptographic software written by Eric Young
65132718Skan * (eay@cryptsoft.com).  This product includes software written by Tim
66132718Skan * Hudson (tjh@cryptsoft.com).
6718334Speter *
6818334Speter */
6918334Speter
7050397Sobrien#include <stdio.h>
7118334Speter#include <stdlib.h>
7218334Speter#include <string.h>
7318334Speter
7418334Speter#include "../e_os.h"
7518334Speter
7618334Speter#include <openssl/opensslconf.h>	/* for OPENSSL_NO_ECDH */
7750397Sobrien#include <openssl/crypto.h>
7818334Speter#include <openssl/bio.h>
7918334Speter#include <openssl/bn.h>
80169689Skan#include <openssl/objects.h>
8150397Sobrien#include <openssl/rand.h>
82169689Skan#include <openssl/sha.h>
83169689Skan#include <openssl/err.h>
84169689Skan
85169689Skan#ifdef OPENSSL_NO_ECDH
86169689Skanint main(int argc, char *argv[])
87169689Skan{
88169689Skan    printf("No ECDH support\n");
89169689Skan    return(0);
90169689Skan}
91169689Skan#else
92169689Skan#include <openssl/ec.h>
9352284Sobrien#include <openssl/ecdh.h>
94169689Skan
95169689Skan#ifdef OPENSSL_SYS_WIN16
9690075Sobrien#define MS_CALLBACK	_far _loadds
97169689Skan#else
98169689Skan#define MS_CALLBACK
9918334Speter#endif
100169689Skan
101169689Skan#if 0
102169689Skanstatic void MS_CALLBACK cb(int p, int n, void *arg);
10318334Speter#endif
104169689Skan
10590075Sobrienstatic const char rnd_seed[] = "string to make the random number generator think it has entropy";
106169689Skan
107169689Skan
108169689Skanstatic const int KDF1_SHA1_len = 20;
10918334Speterstatic void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
110169689Skan	{
111169689Skan#ifndef OPENSSL_NO_SHA
112169689Skan	if (*outlen < SHA_DIGEST_LENGTH)
11318334Speter		return NULL;
114169689Skan	else
115169689Skan		*outlen = SHA_DIGEST_LENGTH;
116169689Skan	return SHA1(in, inlen, out);
117169689Skan#else
118169689Skan	return NULL;
119169689Skan#endif
120169689Skan	}
121169689Skan
12290075Sobrien
123169689Skanstatic int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
124117395Skan	{
125169689Skan	EC_KEY *a=NULL;
126169689Skan	EC_KEY *b=NULL;
127169689Skan	BIGNUM *x_a=NULL, *y_a=NULL,
128169689Skan	       *x_b=NULL, *y_b=NULL;
129169689Skan	char buf[12];
130169689Skan	unsigned char *abuf=NULL,*bbuf=NULL;
131169689Skan	int i,alen,blen,aout,bout,ret=0;
132169689Skan	const EC_GROUP *group;
133169689Skan
134169689Skan	a = EC_KEY_new_by_curve_name(nid);
135169689Skan	b = EC_KEY_new_by_curve_name(nid);
136169689Skan	if (a == NULL || b == NULL)
137169689Skan		goto err;
138169689Skan
139169689Skan	group = EC_KEY_get0_group(a);
140169689Skan
141169689Skan	if ((x_a=BN_new()) == NULL) goto err;
142169689Skan	if ((y_a=BN_new()) == NULL) goto err;
143169689Skan	if ((x_b=BN_new()) == NULL) goto err;
144169689Skan	if ((y_b=BN_new()) == NULL) goto err;
14590075Sobrien
14618334Speter	BIO_puts(out,"Testing key generation with ");
147169689Skan	BIO_puts(out,text);
148169689Skan#ifdef NOISY
14918334Speter	BIO_puts(out,"\n");
15018334Speter#else
151119256Skan	(void)BIO_flush(out);
152169689Skan#endif
153169689Skan
154169689Skan	if (!EC_KEY_generate_key(a)) goto err;
155119256Skan
156119256Skan	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
157169689Skan		{
158119256Skan		if (!EC_POINT_get_affine_coordinates_GFp(group,
159119256Skan			EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
160119256Skan		}
161119256Skan#ifndef OPENSSL_NO_EC2M
162119256Skan	else
163119256Skan		{
164169689Skan		if (!EC_POINT_get_affine_coordinates_GF2m(group,
165132718Skan			EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
166132718Skan		}
167132718Skan#endif
168132718Skan#ifdef NOISY
169132718Skan	BIO_puts(out,"  pri 1=");
170119256Skan	BN_print(out,a->priv_key);
171119256Skan	BIO_puts(out,"\n  pub 1=");
172119256Skan	BN_print(out,x_a);
173119256Skan	BIO_puts(out,",");
174119256Skan	BN_print(out,y_a);
175119256Skan	BIO_puts(out,"\n");
176119256Skan#else
177169689Skan	BIO_printf(out," .");
178119256Skan	(void)BIO_flush(out);
179119256Skan#endif
18090075Sobrien
181132718Skan	if (!EC_KEY_generate_key(b)) goto err;
182132718Skan
183132718Skan	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
18452284Sobrien		{
185117395Skan		if (!EC_POINT_get_affine_coordinates_GFp(group,
186117395Skan			EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
187117395Skan		}
188117395Skan#ifndef OPENSSL_NO_EC2M
18950397Sobrien	else
19018334Speter		{
191132718Skan		if (!EC_POINT_get_affine_coordinates_GF2m(group,
19218334Speter			EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
193169689Skan		}
194169689Skan#endif
19590075Sobrien
196169689Skan#ifdef NOISY
19790075Sobrien	BIO_puts(out,"  pri 2=");
19818334Speter	BN_print(out,b->priv_key);
19990075Sobrien	BIO_puts(out,"\n  pub 2=");
20090075Sobrien	BN_print(out,x_b);
20118334Speter	BIO_puts(out,",");
20218334Speter	BN_print(out,y_b);
203169689Skan	BIO_puts(out,"\n");
204169689Skan#else
205117395Skan	BIO_printf(out,".");
206117395Skan	(void)BIO_flush(out);
207117395Skan#endif
208117395Skan
209117395Skan	alen=KDF1_SHA1_len;
210169689Skan	abuf=(unsigned char *)OPENSSL_malloc(alen);
211169689Skan	aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
212169689Skan
213169689Skan#ifdef NOISY
214169689Skan	BIO_puts(out,"  key1 =");
215117395Skan	for (i=0; i<aout; i++)
21690075Sobrien		{
21790075Sobrien		sprintf(buf,"%02X",abuf[i]);
218169689Skan		BIO_puts(out,buf);
219169689Skan		}
220169689Skan	BIO_puts(out,"\n");
221169689Skan#else
222169689Skan	BIO_printf(out,".");
223169689Skan	(void)BIO_flush(out);
224169689Skan#endif
225169689Skan
226169689Skan	blen=KDF1_SHA1_len;
227169689Skan	bbuf=(unsigned char *)OPENSSL_malloc(blen);
228169689Skan	bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
229169689Skan
230169689Skan#ifdef NOISY
231169689Skan	BIO_puts(out,"  key2 =");
232169689Skan	for (i=0; i<bout; i++)
233169689Skan		{
234169689Skan		sprintf(buf,"%02X",bbuf[i]);
235169689Skan		BIO_puts(out,buf);
236169689Skan		}
237169689Skan	BIO_puts(out,"\n");
238169689Skan#else
239169689Skan	BIO_printf(out,".");
240169689Skan	(void)BIO_flush(out);
241169689Skan#endif
242169689Skan
243169689Skan	if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
244169689Skan		{
245169689Skan#ifndef NOISY
246169689Skan		BIO_printf(out, " failed\n\n");
247117395Skan		BIO_printf(out, "key a:\n");
248117395Skan		BIO_printf(out, "private key: ");
249117395Skan		BN_print(out, EC_KEY_get0_private_key(a));
250117395Skan		BIO_printf(out, "\n");
251117395Skan		BIO_printf(out, "public key (x,y): ");
252117395Skan		BN_print(out, x_a);
253117395Skan		BIO_printf(out, ",");
254117395Skan		BN_print(out, y_a);
255117395Skan		BIO_printf(out, "\nkey b:\n");
256117395Skan		BIO_printf(out, "private key: ");
257169689Skan		BN_print(out, EC_KEY_get0_private_key(b));
258117395Skan		BIO_printf(out, "\n");
259117395Skan		BIO_printf(out, "public key (x,y): ");
260117395Skan		BN_print(out, x_b);
261117395Skan		BIO_printf(out, ",");
262117395Skan		BN_print(out, y_b);
263169689Skan		BIO_printf(out, "\n");
264117395Skan		BIO_printf(out, "generated key a: ");
265117395Skan		for (i=0; i<bout; i++)
266117395Skan			{
267117395Skan			sprintf(buf, "%02X", bbuf[i]);
268117395Skan			BIO_puts(out, buf);
269117395Skan			}
270119256Skan		BIO_printf(out, "\n");
271169689Skan		BIO_printf(out, "generated key b: ");
272117395Skan		for (i=0; i<aout; i++)
273119256Skan			{
274117395Skan			sprintf(buf, "%02X", abuf[i]);
275169689Skan			BIO_puts(out,buf);
276119256Skan			}
277117395Skan		BIO_printf(out, "\n");
278119256Skan#endif
279119256Skan		fprintf(stderr,"Error in ECDH routines\n");
280119256Skan		ret=0;
281117395Skan		}
282117395Skan	else
283117395Skan		{
284117395Skan#ifndef NOISY
28590075Sobrien		BIO_printf(out, " ok\n");
28690075Sobrien#endif
287169689Skan		ret=1;
28890075Sobrien		}
28918334Spetererr:
29018334Speter	ERR_print_errors_fp(stderr);
291169689Skan
29250397Sobrien	if (abuf != NULL) OPENSSL_free(abuf);
293169689Skan	if (bbuf != NULL) OPENSSL_free(bbuf);
29418334Speter	if (x_a) BN_free(x_a);
295169689Skan	if (y_a) BN_free(y_a);
296169689Skan	if (x_b) BN_free(x_b);
297169689Skan	if (y_b) BN_free(y_b);
298169689Skan	if (b) EC_KEY_free(b);
299169689Skan	if (a) EC_KEY_free(a);
300169689Skan	return(ret);
301169689Skan	}
302169689Skan
303169689Skanint main(int argc, char *argv[])
304169689Skan	{
305169689Skan	BN_CTX *ctx=NULL;
306169689Skan	int ret=1;
307169689Skan	BIO *out;
308169689Skan
309169689Skan	CRYPTO_malloc_debug_init();
310169689Skan	CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
311169689Skan	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
312169689Skan
313169689Skan#ifdef OPENSSL_SYS_WIN32
314169689Skan	CRYPTO_malloc_init();
31518334Speter#endif
316169689Skan
317169689Skan	RAND_seed(rnd_seed, sizeof rnd_seed);
318169689Skan
319169689Skan	out=BIO_new(BIO_s_file());
320169689Skan	if (out == NULL) EXIT(1);
321169689Skan	BIO_set_fp(out,stdout,BIO_NOCLOSE);
322169689Skan
32352284Sobrien	if ((ctx=BN_CTX_new()) == NULL) goto err;
324169689Skan
325169689Skan	/* NIST PRIME CURVES TESTS */
326169689Skan	if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
32718334Speter	if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
328169689Skan	if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
329169689Skan	if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
33018334Speter	if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
33118334Speter#ifndef OPENSSL_NO_EC2M
332169689Skan	/* NIST BINARY CURVES TESTS */
333169689Skan	if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
334169689Skan	if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
335169689Skan	if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
336169689Skan	if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
337169689Skan	if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
338169689Skan	if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
339169689Skan	if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
340169689Skan	if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
341169689Skan	if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
342169689Skan	if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
343169689Skan#endif
344169689Skan
34590075Sobrien	ret = 0;
34690075Sobrien
34790075Sobrienerr:
34890075Sobrien	ERR_print_errors_fp(stderr);
34990075Sobrien	if (ctx) BN_CTX_free(ctx);
35090075Sobrien	BIO_free(out);
35190075Sobrien	CRYPTO_cleanup_all_ex_data();
35290075Sobrien	ERR_remove_thread_state(NULL);
35390075Sobrien	CRYPTO_mem_leaks_fp(stderr);
35490075Sobrien	EXIT(ret);
35518334Speter	return(ret);
35690075Sobrien	}
357169689Skan
35818334Speter#if 0
359169689Skanstatic void MS_CALLBACK cb(int p, int n, void *arg)
360169689Skan	{
361169689Skan	char c='*';
362169689Skan
363169689Skan	if (p == 0) c='.';
364169689Skan	if (p == 1) c='+';
365169689Skan	if (p == 2) c='*';
366169689Skan	if (p == 3) c='\n';
367169689Skan	BIO_write((BIO *)arg,&c,1);
368169689Skan	(void)BIO_flush((BIO *)arg);
36918334Speter#ifdef LINT
37018334Speter	p=n;
371132718Skan#endif
372132718Skan	}
37318334Speter#endif
374132718Skan#endif
375132718Skan