ecdhtest.c revision 225736
131744Shelbig/* crypto/ecdh/ecdhtest.c */ 231744Shelbig/* ==================================================================== 331744Shelbig * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 431744Shelbig * 531744Shelbig * The Elliptic Curve Public-Key Crypto Library (ECC Code) included 631744Shelbig * herein is developed by SUN MICROSYSTEMS, INC., and is contributed 731744Shelbig * to the OpenSSL project. 831744Shelbig * 931744Shelbig * The ECC Code is licensed pursuant to the OpenSSL open source 1031744Shelbig * license provided below. 1131744Shelbig * 1231744Shelbig * The ECDH software is originally written by Douglas Stebila of 1331744Shelbig * Sun Microsystems Laboratories. 1431744Shelbig * 1531744Shelbig */ 1631744Shelbig/* ==================================================================== 1731744Shelbig * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. 1831744Shelbig * 1931744Shelbig * Redistribution and use in source and binary forms, with or without 2031744Shelbig * modification, are permitted provided that the following conditions 2131744Shelbig * are met: 2231744Shelbig * 2331744Shelbig * 1. Redistributions of source code must retain the above copyright 2431744Shelbig * notice, this list of conditions and the following disclaimer. 2531744Shelbig * 2632310Scharnier * 2. Redistributions in binary form must reproduce the above copyright 2732310Scharnier * notice, this list of conditions and the following disclaimer in 2832310Scharnier * the documentation and/or other materials provided with the 2945064Shelbig * distribution. 3032310Scharnier * 3132310Scharnier * 3. All advertising materials mentioning features or use of this 3231744Shelbig * software must display the following acknowledgment: 3331744Shelbig * "This product includes software developed by the OpenSSL Project 3431744Shelbig * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 3531744Shelbig * 3631744Shelbig * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 3731744Shelbig * endorse or promote products derived from this software without 3831744Shelbig * prior written permission. For written permission, please contact 3931744Shelbig * openssl-core@openssl.org. 4031744Shelbig * 4131744Shelbig * 5. Products derived from this software may not be called "OpenSSL" 4231744Shelbig * nor may "OpenSSL" appear in their names without prior written 4331744Shelbig * permission of the OpenSSL Project. 4431744Shelbig * 4531744Shelbig * 6. Redistributions of any form whatsoever must retain the following 4631744Shelbig * acknowledgment: 4731744Shelbig * "This product includes software developed by the OpenSSL Project 4831744Shelbig * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 4931744Shelbig * 5031744Shelbig * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 5131744Shelbig * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5231744Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5331744Shelbig * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 5431744Shelbig * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5531744Shelbig * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5631744Shelbig * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 5731744Shelbig * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5831744Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 5931744Shelbig * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 6031744Shelbig * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 6131744Shelbig * OF THE POSSIBILITY OF SUCH DAMAGE. 6231744Shelbig * ==================================================================== 6331744Shelbig * 6431744Shelbig * This product includes cryptographic software written by Eric Young 6531744Shelbig * (eay@cryptsoft.com). This product includes software written by Tim 6631744Shelbig * Hudson (tjh@cryptsoft.com). 6731744Shelbig * 6832215Shelbig */ 6931744Shelbig 7032215Shelbig#include <stdio.h> 7132282Shelbig#include <stdlib.h> 7232215Shelbig#include <string.h> 7332282Shelbig 7432215Shelbig#include "../e_os.h" 7532215Shelbig 7632215Shelbig#include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */ 7732215Shelbig#include <openssl/crypto.h> 7832215Shelbig#include <openssl/bio.h> 7932215Shelbig#include <openssl/bn.h> 8032215Shelbig#include <openssl/objects.h> 8132215Shelbig#include <openssl/rand.h> 8232282Shelbig#include <openssl/sha.h> 8332215Shelbig#include <openssl/err.h> 8432215Shelbig 8532215Shelbig#ifdef OPENSSL_NO_ECDH 8632215Shelbigint main(int argc, char *argv[]) 8732215Shelbig{ 8832215Shelbig printf("No ECDH support\n"); 8932215Shelbig return(0); 9032282Shelbig} 9145064Shelbig#else 9232282Shelbig#include <openssl/ec.h> 9332215Shelbig#include <openssl/ecdh.h> 9432282Shelbig 9532215Shelbig#ifdef OPENSSL_SYS_WIN16 9632215Shelbig#define MS_CALLBACK _far _loadds 9732215Shelbig#else 9832282Shelbig#define MS_CALLBACK 9932282Shelbig#endif 10032215Shelbig 10132215Shelbig#if 0 10232215Shelbigstatic void MS_CALLBACK cb(int p, int n, void *arg); 10332215Shelbig#endif 10432215Shelbig 10532215Shelbigstatic const char rnd_seed[] = "string to make the random number generator think it has entropy"; 10631744Shelbig 10731744Shelbig 10832215Shelbigstatic const int KDF1_SHA1_len = 20; 10932215Shelbigstatic void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen) 11032215Shelbig { 11132215Shelbig#ifndef OPENSSL_NO_SHA 11231744Shelbig if (*outlen < SHA_DIGEST_LENGTH) 11331744Shelbig return NULL; 11431744Shelbig else 11531744Shelbig *outlen = SHA_DIGEST_LENGTH; 11631744Shelbig return SHA1(in, inlen, out); 11731744Shelbig#else 11831744Shelbig return NULL; 11931744Shelbig#endif 12031744Shelbig } 12131744Shelbig 12231744Shelbig 12331744Shelbigstatic int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out) 12431744Shelbig { 12531744Shelbig EC_KEY *a=NULL; 12631744Shelbig EC_KEY *b=NULL; 12731744Shelbig BIGNUM *x_a=NULL, *y_a=NULL, 12831744Shelbig *x_b=NULL, *y_b=NULL; 12931744Shelbig char buf[12]; 13031744Shelbig unsigned char *abuf=NULL,*bbuf=NULL; 13131744Shelbig int i,alen,blen,aout,bout,ret=0; 13231744Shelbig const EC_GROUP *group; 13331744Shelbig 13431744Shelbig a = EC_KEY_new_by_curve_name(nid); 13531744Shelbig b = EC_KEY_new_by_curve_name(nid); 13631744Shelbig if (a == NULL || b == NULL) 13731744Shelbig goto err; 13831744Shelbig 13931744Shelbig group = EC_KEY_get0_group(a); 14031744Shelbig 14131744Shelbig if ((x_a=BN_new()) == NULL) goto err; 14231744Shelbig if ((y_a=BN_new()) == NULL) goto err; 14331744Shelbig if ((x_b=BN_new()) == NULL) goto err; 14431744Shelbig if ((y_b=BN_new()) == NULL) goto err; 14531744Shelbig 14631744Shelbig BIO_puts(out,"Testing key generation with "); 14731744Shelbig BIO_puts(out,text); 14831744Shelbig#ifdef NOISY 14931744Shelbig BIO_puts(out,"\n"); 15031744Shelbig#else 15131744Shelbig (void)BIO_flush(out); 15231744Shelbig#endif 15331744Shelbig 15431744Shelbig if (!EC_KEY_generate_key(a)) goto err; 15531744Shelbig 15631744Shelbig if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 15732139Shelbig { 15831744Shelbig if (!EC_POINT_get_affine_coordinates_GFp(group, 15931744Shelbig EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err; 16032215Shelbig } 16131744Shelbig else 16231744Shelbig { 16331744Shelbig if (!EC_POINT_get_affine_coordinates_GF2m(group, 16431744Shelbig EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err; 16531744Shelbig } 16631744Shelbig#ifdef NOISY 16731744Shelbig BIO_puts(out," pri 1="); 16831744Shelbig BN_print(out,a->priv_key); 16931744Shelbig BIO_puts(out,"\n pub 1="); 17031744Shelbig BN_print(out,x_a); 17131744Shelbig BIO_puts(out,","); 17232282Shelbig BN_print(out,y_a); 17331744Shelbig BIO_puts(out,"\n"); 17431744Shelbig#else 17531744Shelbig BIO_printf(out," ."); 17631744Shelbig (void)BIO_flush(out); 17732310Scharnier#endif 17831744Shelbig 17931744Shelbig if (!EC_KEY_generate_key(b)) goto err; 18031744Shelbig 18131744Shelbig if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 18231744Shelbig { 18331744Shelbig if (!EC_POINT_get_affine_coordinates_GFp(group, 18431744Shelbig EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err; 18531744Shelbig } 18631744Shelbig else 18731744Shelbig { 18831744Shelbig if (!EC_POINT_get_affine_coordinates_GF2m(group, 18931744Shelbig EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err; 19031744Shelbig } 19131744Shelbig 19231744Shelbig#ifdef NOISY 19331744Shelbig BIO_puts(out," pri 2="); 19431744Shelbig BN_print(out,b->priv_key); 19531744Shelbig BIO_puts(out,"\n pub 2="); 19631744Shelbig BN_print(out,x_b); 19732215Shelbig BIO_puts(out,","); 19831744Shelbig BN_print(out,y_b); 19932215Shelbig BIO_puts(out,"\n"); 20032215Shelbig#else 20132215Shelbig BIO_printf(out,"."); 20232215Shelbig (void)BIO_flush(out); 20332215Shelbig#endif 20432215Shelbig 20532215Shelbig alen=KDF1_SHA1_len; 20632215Shelbig abuf=(unsigned char *)OPENSSL_malloc(alen); 20732215Shelbig aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1); 20832215Shelbig 20932215Shelbig#ifdef NOISY 21032215Shelbig BIO_puts(out," key1 ="); 21132215Shelbig for (i=0; i<aout; i++) 21232215Shelbig { 21332215Shelbig sprintf(buf,"%02X",abuf[i]); 21432215Shelbig BIO_puts(out,buf); 21532215Shelbig } 21632215Shelbig BIO_puts(out,"\n"); 21732215Shelbig#else 21832215Shelbig BIO_printf(out,"."); 21932215Shelbig (void)BIO_flush(out); 22031744Shelbig#endif 22132215Shelbig 22231744Shelbig blen=KDF1_SHA1_len; 22331744Shelbig bbuf=(unsigned char *)OPENSSL_malloc(blen); 22431744Shelbig bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1); 22531744Shelbig 22631744Shelbig#ifdef NOISY 22731744Shelbig BIO_puts(out," key2 ="); 22831744Shelbig for (i=0; i<bout; i++) 22931744Shelbig { 23031744Shelbig sprintf(buf,"%02X",bbuf[i]); 23131744Shelbig BIO_puts(out,buf); 23231744Shelbig } 23331744Shelbig BIO_puts(out,"\n"); 23431744Shelbig#else 23531744Shelbig BIO_printf(out,"."); 23631744Shelbig (void)BIO_flush(out); 23731744Shelbig#endif 23831744Shelbig 23931744Shelbig if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0)) 24031744Shelbig { 24131744Shelbig#ifndef NOISY 24231744Shelbig BIO_printf(out, " failed\n\n"); 24331744Shelbig BIO_printf(out, "key a:\n"); 24431744Shelbig BIO_printf(out, "private key: "); 24531744Shelbig BN_print(out, EC_KEY_get0_private_key(a)); 24631744Shelbig BIO_printf(out, "\n"); 24731744Shelbig BIO_printf(out, "public key (x,y): "); 24831744Shelbig BN_print(out, x_a); 24931744Shelbig BIO_printf(out, ","); 25031744Shelbig BN_print(out, y_a); 25131744Shelbig BIO_printf(out, "\nkey b:\n"); 25231744Shelbig BIO_printf(out, "private key: "); 25331744Shelbig BN_print(out, EC_KEY_get0_private_key(b)); 25431744Shelbig BIO_printf(out, "\n"); 25531744Shelbig BIO_printf(out, "public key (x,y): "); 25631744Shelbig BN_print(out, x_b); 25731744Shelbig BIO_printf(out, ","); 25831744Shelbig BN_print(out, y_b); 25931744Shelbig BIO_printf(out, "\n"); 26031744Shelbig BIO_printf(out, "generated key a: "); 26131744Shelbig for (i=0; i<bout; i++) 26231744Shelbig { 26331744Shelbig sprintf(buf, "%02X", bbuf[i]); 26431744Shelbig BIO_puts(out, buf); 26531744Shelbig } 26631744Shelbig BIO_printf(out, "\n"); 26731744Shelbig BIO_printf(out, "generated key b: "); 26831744Shelbig for (i=0; i<aout; i++) 26931744Shelbig { 27031744Shelbig sprintf(buf, "%02X", abuf[i]); 27131744Shelbig BIO_puts(out,buf); 27231744Shelbig } 27331744Shelbig BIO_printf(out, "\n"); 27431744Shelbig#endif 27531744Shelbig fprintf(stderr,"Error in ECDH routines\n"); 27631744Shelbig ret=0; 27731744Shelbig } 27831744Shelbig else 27931744Shelbig { 28031744Shelbig#ifndef NOISY 28131744Shelbig BIO_printf(out, " ok\n"); 28231744Shelbig#endif 28331744Shelbig ret=1; 28431744Shelbig } 28531744Shelbigerr: 28631744Shelbig ERR_print_errors_fp(stderr); 28731744Shelbig 28831744Shelbig if (abuf != NULL) OPENSSL_free(abuf); 28931744Shelbig if (bbuf != NULL) OPENSSL_free(bbuf); 29031744Shelbig if (x_a) BN_free(x_a); 29131744Shelbig if (y_a) BN_free(y_a); 29231744Shelbig if (x_b) BN_free(x_b); 29331744Shelbig if (y_b) BN_free(y_b); 29431744Shelbig if (b) EC_KEY_free(b); 29531744Shelbig if (a) EC_KEY_free(a); 29631744Shelbig return(ret); 29731744Shelbig } 29831744Shelbig 29931744Shelbigint main(int argc, char *argv[]) 30031744Shelbig { 30131744Shelbig BN_CTX *ctx=NULL; 30231744Shelbig int ret=1; 30331744Shelbig BIO *out; 30431744Shelbig 30531744Shelbig CRYPTO_malloc_debug_init(); 30631744Shelbig CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); 30731744Shelbig CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 30831744Shelbig 30931744Shelbig#ifdef OPENSSL_SYS_WIN32 31031744Shelbig CRYPTO_malloc_init(); 31131744Shelbig#endif 31231744Shelbig 31331744Shelbig RAND_seed(rnd_seed, sizeof rnd_seed); 31431744Shelbig 31531744Shelbig out=BIO_new(BIO_s_file()); 31631744Shelbig if (out == NULL) EXIT(1); 31731744Shelbig BIO_set_fp(out,stdout,BIO_NOCLOSE); 31831744Shelbig 31931744Shelbig if ((ctx=BN_CTX_new()) == NULL) goto err; 32031744Shelbig 32131744Shelbig /* NIST PRIME CURVES TESTS */ 32231744Shelbig if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err; 32331744Shelbig if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err; 32431744Shelbig if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err; 32531744Shelbig if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err; 32631744Shelbig if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err; 32731744Shelbig /* NIST BINARY CURVES TESTS */ 32831744Shelbig if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err; 32931744Shelbig if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err; 33031744Shelbig if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err; 33131744Shelbig if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err; 33231744Shelbig if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err; 33331744Shelbig if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err; 33431744Shelbig if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err; 33531744Shelbig if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err; 33631744Shelbig if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err; 33731744Shelbig if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err; 33832310Scharnier 33931744Shelbig ret = 0; 34031744Shelbig 34131744Shelbigerr: 34232310Scharnier ERR_print_errors_fp(stderr); 34332525Shelbig if (ctx) BN_CTX_free(ctx); 34432310Scharnier BIO_free(out); 34532310Scharnier CRYPTO_cleanup_all_ex_data(); 34631744Shelbig ERR_remove_state(0); 34731744Shelbig CRYPTO_mem_leaks_fp(stderr); 34831744Shelbig EXIT(ret); 34931744Shelbig return(ret); 35031744Shelbig } 35131744Shelbig 35231744Shelbig#if 0 35331744Shelbigstatic void MS_CALLBACK cb(int p, int n, void *arg) 35431744Shelbig { 35531744Shelbig char c='*'; 35631744Shelbig 35732215Shelbig if (p == 0) c='.'; 35832215Shelbig if (p == 1) c='+'; 35932215Shelbig if (p == 2) c='*'; 36031744Shelbig if (p == 3) c='\n'; 36131744Shelbig BIO_write((BIO *)arg,&c,1); 36231744Shelbig (void)BIO_flush((BIO *)arg); 36331744Shelbig#ifdef LINT 36432215Shelbig p=n; 36532215Shelbig#endif 36631744Shelbig } 36732215Shelbig#endif 36831744Shelbig#endif 36931744Shelbig