summaryrefslogtreecommitdiff
path: root/crypto/ecdh/ecdhtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/ecdh/ecdhtest.c')
-rw-r--r--crypto/ecdh/ecdhtest.c61
1 files changed, 33 insertions, 28 deletions
diff --git a/crypto/ecdh/ecdhtest.c b/crypto/ecdh/ecdhtest.c
index f9162b7e8b..212a87efa4 100644
--- a/crypto/ecdh/ecdhtest.c
+++ b/crypto/ecdh/ecdhtest.c
@@ -73,10 +73,10 @@
#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>
@@ -89,6 +89,7 @@ int main(int argc, char *argv[])
return(0);
}
#else
+#include <openssl/ec.h>
#include <openssl/ecdh.h>
#ifdef OPENSSL_SYS_WIN16
@@ -105,11 +106,13 @@ static const char rnd_seed[] = "string to make the random number generator think
static const int KDF1_SHA1_len = 20;
-static void *KDF1_SHA1(void *in, size_t inlen, void *out, size_t outlen)
+static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
{
#ifndef OPENSSL_NO_SHA
- if (outlen != SHA_DIGEST_LENGTH)
+ if (*outlen < SHA_DIGEST_LENGTH)
return NULL;
+ else
+ *outlen = SHA_DIGEST_LENGTH;
return SHA1(in, inlen, out);
#else
return NULL;
@@ -117,9 +120,7 @@ static void *KDF1_SHA1(void *in, size_t inlen, void *out, size_t outlen)
}
-int test_ecdh_curve(int , char *, BN_CTX *, BIO *);
-
-int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
+static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
{
EC_KEY *a=NULL;
EC_KEY *b=NULL;
@@ -128,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;
@@ -145,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=");
@@ -168,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
@@ -192,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=KDF1_SHA1_len;
abuf=(unsigned char *)OPENSSL_malloc(alen);
- aout=ECDH_compute_key(abuf,alen,b->pub_key,a,KDF1_SHA1);
+ aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
#ifdef NOISY
BIO_puts(out," key1 =");
@@ -209,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=KDF1_SHA1_len;
bbuf=(unsigned char *)OPENSSL_malloc(blen);
- bout=ECDH_compute_key(bbuf,blen,a->pub_key,b,KDF1_SHA1);
+ bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
#ifdef NOISY
BIO_puts(out," key2 =");
@@ -226,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))
@@ -235,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);
@@ -243,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);
@@ -284,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);
@@ -338,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);