make
[openssl.git] / crypto / ecdh / ecdhtest.c
index 32b6a9ab8980483667cf3bad674f69f2399ee7b0..01baa5f4942f40b6e6bf16487b273a1f6313c9e2 100644 (file)
@@ -14,7 +14,7 @@
  *
  */
 /* ====================================================================
- * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifdef OPENSSL_SYS_WINDOWS
-#include "../bio/bss_file.c" 
-#endif
+
+#include "../e_os.h"
+
+#include <openssl/opensslconf.h>       /* for OPENSSL_NO_ECDH */
 #include <openssl/crypto.h>
 #include <openssl/bio.h>
 #include <openssl/bn.h>
-#include <openssl/ec.h>
 #include <openssl/objects.h>
 #include <openssl/rand.h>
+#include <openssl/sha.h>
 #include <openssl/err.h>
 
 #ifdef OPENSSL_NO_ECDH
@@ -88,6 +89,7 @@ int main(int argc, char *argv[])
     return(0);
 }
 #else
+#include <openssl/ec.h>
 #include <openssl/ecdh.h>
 
 #ifdef OPENSSL_SYS_WIN16
@@ -100,16 +102,25 @@ int main(int argc, char *argv[])
 static void MS_CALLBACK cb(int p, int n, void *arg);
 #endif
 
-#ifdef OPENSSL_NO_STDIO
-#define APPS_WIN16
-#include "bss_file.c"
-#endif
-
 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
-int test_ecdh_curve(int , char *, BN_CTX *, BIO *);
 
-int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
+static const int KDF1_SHA1_len = 20;
+static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
+       {
+#ifndef OPENSSL_NO_SHA
+       if (*outlen < SHA_DIGEST_LENGTH)
+               return NULL;
+       else
+               *outlen = SHA_DIGEST_LENGTH;
+       return SHA1(in, inlen, out);
+#else
+       return NULL;
+#endif
+       }
+
+
+static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
        {
        EC_KEY *a=NULL;
        EC_KEY *b=NULL;
@@ -118,12 +129,14 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        char buf[12];
        unsigned char *abuf=NULL,*bbuf=NULL;
        int i,alen,blen,aout,bout,ret=0;
+       const EC_GROUP *group;
 
-       if ((a=EC_KEY_new()) == NULL) goto err;
-       if ((a->group=EC_GROUP_new_by_nid(nid)) == NULL) goto err;
+       a = EC_KEY_new_by_curve_name(nid);
+       b = EC_KEY_new_by_curve_name(nid);
+       if (a == NULL || b == NULL)
+               goto err;
 
-       if ((b=EC_KEY_new()) == NULL) goto err;
-       b->group = a->group;
+       group = EC_KEY_get0_group(a);
 
        if ((x_a=BN_new()) == NULL) goto err;
        if ((y_a=BN_new()) == NULL) goto err;
@@ -140,13 +153,15 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
 
        if (!EC_KEY_generate_key(a)) goto err;
        
-       if (EC_METHOD_get_field_type(EC_GROUP_method_of(a->group)) == NID_X9_62_prime_field) 
+       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 
                {
-               if (!EC_POINT_get_affine_coordinates_GFp(a->group, a->pub_key, x_a, y_a, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GFp(group,
+                       EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
                }
        else
                {
-               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, a->pub_key, x_a, y_a, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GF2m(group,
+                       EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
                }
 #ifdef NOISY
        BIO_puts(out,"  pri 1=");
@@ -163,13 +178,15 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
 
        if (!EC_KEY_generate_key(b)) goto err;
 
-       if (EC_METHOD_get_field_type(EC_GROUP_method_of(b->group)) == NID_X9_62_prime_field) 
+       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 
                {
-               if (!EC_POINT_get_affine_coordinates_GFp(b->group, b->pub_key, x_b, y_b, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GFp(group, 
+                       EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
                }
        else
                {
-               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, b->pub_key, x_b, y_b, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GF2m(group, 
+                       EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
                }
 
 #ifdef NOISY
@@ -185,9 +202,9 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_flush(out);
 #endif
 
-       alen=ECDH_size(a);
+       alen=KDF1_SHA1_len;
        abuf=(unsigned char *)OPENSSL_malloc(alen);
-       aout=ECDH_compute_key(abuf,b->pub_key,a);
+       aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
 
 #ifdef NOISY
        BIO_puts(out,"  key1 =");
@@ -202,9 +219,9 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_flush(out);
 #endif
 
-       blen=ECDH_size(b);
+       blen=KDF1_SHA1_len;
        bbuf=(unsigned char *)OPENSSL_malloc(blen);
-       bout=ECDH_compute_key(bbuf,a->pub_key,b);
+       bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
 
 #ifdef NOISY
        BIO_puts(out,"  key2 =");
@@ -225,7 +242,7 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
                BIO_printf(out, " failed\n\n");
                BIO_printf(out, "key a:\n");
                BIO_printf(out, "private key: ");
-               BN_print(out, a->priv_key);
+               BN_print(out, EC_KEY_get0_private_key(a));
                BIO_printf(out, "\n");
                BIO_printf(out, "public key (x,y): ");
                BN_print(out, x_a);
@@ -233,7 +250,7 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
                BN_print(out, y_a);
                BIO_printf(out, "\nkey b:\n");
                BIO_printf(out, "private key: ");
-               BN_print(out, b->priv_key);
+               BN_print(out, EC_KEY_get0_private_key(b));
                BIO_printf(out, "\n");
                BIO_printf(out, "public key (x,y): ");
                BN_print(out, x_b);
@@ -274,8 +291,6 @@ err:
        if (y_a) BN_free(y_a);
        if (x_b) BN_free(x_b);
        if (y_b) BN_free(y_b);
-       if (a->group) EC_GROUP_free(a->group);
-       a->group = b->group = NULL;
        if (b) EC_KEY_free(b);
        if (a) EC_KEY_free(a);
        return(ret);
@@ -298,7 +313,7 @@ int main(int argc, char *argv[])
        RAND_seed(rnd_seed, sizeof rnd_seed);
 
        out=BIO_new(BIO_s_file());
-       if (out == NULL) exit(1);
+       if (out == NULL) EXIT(1);
        BIO_set_fp(out,stdout,BIO_NOCLOSE);
 
        if ((ctx=BN_CTX_new()) == NULL) goto err;
@@ -330,7 +345,7 @@ err:
        CRYPTO_cleanup_all_ex_data();
        ERR_remove_state(0);
        CRYPTO_mem_leaks_fp(stderr);
-       exit(ret);
+       EXIT(ret);
        return(ret);
        }