There was a need to support thread ID types that couldn't be reliably cast
[openssl.git] / crypto / ecdh / ecdhtest.c
index 6e0c14dc13712bc6eed3d345f499f02f2c277e1c..212a87efa4ef1d3da5db9a9b1b6d2542d6eb6c79 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 "../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
@@ -102,9 +104,23 @@ static void MS_CALLBACK cb(int p, int n, void *arg);
 
 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;
@@ -113,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;
@@ -130,18 +148,20 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
 #ifdef NOISY
        BIO_puts(out,"\n");
 #else
-       BIO_flush(out);
+       (void)BIO_flush(out);
 #endif
 
        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=");
@@ -153,18 +173,20 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_puts(out,"\n");
 #else
        BIO_printf(out," .");
-       BIO_flush(out);
+       (void)BIO_flush(out);
 #endif
 
        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
@@ -177,12 +199,12 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_puts(out,"\n");
 #else
        BIO_printf(out,".");
-       BIO_flush(out);
+       (void)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 =");
@@ -194,12 +216,12 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_puts(out,"\n");
 #else
        BIO_printf(out,".");
-       BIO_flush(out);
+       (void)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 =");
@@ -211,7 +233,7 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        BIO_puts(out,"\n");
 #else
        BIO_printf(out,".");
-       BIO_flush(out);
+       (void)BIO_flush(out);
 #endif
 
        if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
@@ -220,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);
@@ -228,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);
@@ -269,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);
@@ -323,7 +343,7 @@ err:
        if (ctx) BN_CTX_free(ctx);
        BIO_free(out);
        CRYPTO_cleanup_all_ex_data();
-       ERR_remove_state(0);
+       ERR_remove_thread_state(NULL);
        CRYPTO_mem_leaks_fp(stderr);
        EXIT(ret);
        return(ret);