diff options
Diffstat (limited to 'crypto/ecdh')
-rw-r--r-- | crypto/ecdh/ecdh.h | 7 | ||||
-rw-r--r-- | crypto/ecdh/ecdhtest.c | 40 | ||||
-rw-r--r-- | crypto/ecdh/ech_lib.c | 68 | ||||
-rw-r--r-- | crypto/ecdh/ech_locl.h | 5 | ||||
-rw-r--r-- | crypto/ecdh/ech_ossl.c | 18 |
5 files changed, 72 insertions, 66 deletions
diff --git a/crypto/ecdh/ecdh.h b/crypto/ecdh/ecdh.h index 98b8bb253c..b4b58ee65b 100644 --- a/crypto/ecdh/ecdh.h +++ b/crypto/ecdh/ecdh.h @@ -85,13 +85,6 @@ extern "C" { #endif -typedef struct ecdh_data_st ECDH_DATA; - -/* ECDH_DATA functions */ -ECDH_DATA *ECDH_DATA_new(void); -ECDH_DATA *ECDH_DATA_new_method(ENGINE *); -void ECDH_DATA_free(ECDH_DATA *); - const ECDH_METHOD *ECDH_OpenSSL(void); void ECDH_set_default_method(const ECDH_METHOD *); diff --git a/crypto/ecdh/ecdhtest.c b/crypto/ecdh/ecdhtest.c index f4e02966e2..c0414b92d3 100644 --- a/crypto/ecdh/ecdhtest.c +++ b/crypto/ecdh/ecdhtest.c @@ -119,9 +119,7 @@ static void *KDF1_SHA1(const 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; @@ -130,12 +128,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_curve_name(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; @@ -152,13 +152,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="); @@ -175,13 +177,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 @@ -199,7 +203,7 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out) 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 ="); @@ -216,7 +220,7 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out) 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 ="); @@ -237,7 +241,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); @@ -245,7 +249,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); @@ -286,8 +290,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); diff --git a/crypto/ecdh/ech_lib.c b/crypto/ecdh/ech_lib.c index a6392bcb3d..85fbfc5cac 100644 --- a/crypto/ecdh/ech_lib.c +++ b/crypto/ecdh/ech_lib.c @@ -76,10 +76,11 @@ const char *ECDH_version="ECDH" OPENSSL_VERSION_PTEXT; -static void ecdh_finish(EC_KEY *); - static const ECDH_METHOD *default_ECDH_method = NULL; +static void *ecdh_data_dup(void *); +static void ecdh_data_free(void *); + void ECDH_set_default_method(const ECDH_METHOD *meth) { default_ECDH_method = meth; @@ -122,12 +123,7 @@ int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) return 1; } -ECDH_DATA *ECDH_DATA_new(void) - { - return ECDH_DATA_new_method(NULL); - } - -ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) +static ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) { ECDH_DATA *ret; @@ -139,7 +135,6 @@ ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) } ret->init = NULL; - ret->finish = ecdh_finish; ret->meth = ECDH_get_default_method(); ret->engine = engine; @@ -172,12 +167,26 @@ ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) return(ret); } -void ECDH_DATA_free(ECDH_DATA *r) +void *ecdh_data_new(void) { -#if 0 - if (r->meth->finish) - r->meth->finish(r); -#endif + return (void *)ECDH_DATA_new_method(NULL); + } + +static void *ecdh_data_dup(void *data) +{ + ECDH_DATA *r = (ECDH_DATA *)data; + + /* XXX: dummy operation */ + if (r == NULL) + return NULL; + + return (void *)ecdh_data_new(); +} + +void ecdh_data_free(void *data) + { + ECDH_DATA *r = (ECDH_DATA *)data; + #ifndef OPENSSL_NO_ENGINE if (r->engine) ENGINE_finish(r->engine); @@ -192,25 +201,24 @@ void ECDH_DATA_free(ECDH_DATA *r) ECDH_DATA *ecdh_check(EC_KEY *key) { - if (key->meth_data) - { - if (key->meth_data->finish != ecdh_finish) - { - key->meth_data->finish(key); - key->meth_data = (EC_KEY_METH_DATA *)ECDH_DATA_new(); - } - } - else - key->meth_data = (EC_KEY_METH_DATA *)ECDH_DATA_new(); - return (ECDH_DATA *)key->meth_data; - } - -static void ecdh_finish(EC_KEY *key) + ECDH_DATA *ecdh_data; + + void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup, + ecdh_data_free, ecdh_data_free); + if (data == NULL) { - if (key->meth_data && key->meth_data->finish == ecdh_finish) - ECDH_DATA_free((ECDH_DATA *)key->meth_data); + ecdh_data = (ECDH_DATA *)ecdh_data_new(); + if (ecdh_data == NULL) + return NULL; + EC_KEY_insert_key_method_data(key, (void *)ecdh_data, + ecdh_data_dup, ecdh_data_free, ecdh_data_free); } + else + ecdh_data = (ECDH_DATA *)data; + + return ecdh_data; + } int ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) diff --git a/crypto/ecdh/ech_locl.h b/crypto/ecdh/ech_locl.h index 1a2db43b8a..f658526a7e 100644 --- a/crypto/ecdh/ech_locl.h +++ b/crypto/ecdh/ech_locl.h @@ -75,16 +75,15 @@ struct ecdh_method char *app_data; }; -struct ecdh_data_st { +typedef struct ecdh_data_st { /* EC_KEY_METH_DATA part */ int (*init)(EC_KEY *); - void (*finish)(EC_KEY *); /* method specific part */ ENGINE *engine; int flags; const ECDH_METHOD *meth; CRYPTO_EX_DATA ex_data; -}; +} ECDH_DATA; ECDH_DATA *ecdh_check(EC_KEY *); diff --git a/crypto/ecdh/ech_ossl.c b/crypto/ecdh/ech_ossl.c index dab7e8eab8..2a40ff12df 100644 --- a/crypto/ecdh/ech_ossl.c +++ b/crypto/ecdh/ech_ossl.c @@ -112,6 +112,8 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, BN_CTX *ctx; EC_POINT *tmp=NULL; BIGNUM *x=NULL, *y=NULL; + const BIGNUM *priv_key; + const EC_GROUP* group; int ret= -1; size_t buflen, len; unsigned char *buf=NULL; @@ -127,27 +129,29 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); - if (ecdh->priv_key == NULL) + priv_key = EC_KEY_get0_private_key(ecdh); + if (priv_key == NULL) { ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_NO_PRIVATE_VALUE); goto err; } - if ((tmp=EC_POINT_new(ecdh->group)) == NULL) + group = EC_KEY_get0_group(ecdh); + if ((tmp=EC_POINT_new(group)) == NULL) { ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); goto err; } - if (!EC_POINT_mul(ecdh->group, tmp, NULL, pub_key, ecdh->priv_key, ctx)) + if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) { ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE); goto err; } - if (EC_METHOD_get_field_type(EC_GROUP_method_of(ecdh->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(ecdh->group, tmp, x, y, ctx)) + if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) { ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE); goto err; @@ -155,14 +159,14 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, } else { - if (!EC_POINT_get_affine_coordinates_GF2m(ecdh->group, tmp, x, y, ctx)) + if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) { ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE); goto err; } } - buflen = (EC_GROUP_get_degree(ecdh->group) + 7)/8; + buflen = (EC_GROUP_get_degree(group) + 7)/8; len = BN_num_bytes(x); if (len > buflen) { |