From 254ef80db19a6f6610ef07535efd699cef7aa855 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bodo=20M=C3=B6ller?= Date: Wed, 12 Jun 2002 14:01:17 +0000 Subject: [PATCH] simplify asn1_flag Submitted by: Nils Larsch Reviewed by: Bodo Moeller --- CHANGES | 24 ++++++++++++++++-------- apps/ecdsaparam.c | 3 +-- crypto/asn1/x_pubkey.c | 6 +++--- crypto/ec/ec.h | 9 ++++----- crypto/ec/ec_asn1.c | 28 ++++++---------------------- crypto/ec/ec_lcl.h | 3 ++- crypto/ec/ec_lib.c | 27 +++++++++++++++++++++------ 7 files changed, 53 insertions(+), 47 deletions(-) diff --git a/CHANGES b/CHANGES index 4a47eac400..512b802f71 100644 --- a/CHANGES +++ b/CHANGES @@ -4,12 +4,20 @@ Changes between 0.9.7 and 0.9.8 [xx XXX 2002] - *) Add 'nid' and 'asn1_flag' members to EC_GROUP, and functions - EC_GROUP_get_nid() - EC_GROUP_get_set_asn1_flag() - EC_GROUP_get_get_asn1_flag() - 'nid' is an optional NID for named curves. 'asn1_flag' - determines the encoding to be used for ASN1 purposes. + *) Add 'asn1_flag' and 'asn1_form' member to EC_GROUP with access + functions + EC_GROUP_set_asn1_flag() + EC_GROUP_get_asn1_flag() + EC_GROUP_set_point_conversion_form() + EC_GROUP_get_point_conversion_form() + These control ASN1 encoding details: + - Curve are encoded explicitly unless asn1_flag has been set to + OPENSSL_EC_NAMED_CURVE. + - Points are encoded in compressed form by default; options for + asn1_for are as for point2oct, namely + POINT_CONVERSION_COMPRESSED + POINT_CONVERSION_UNCOMPRESSED + POINT_CONVERSION_HYBRID [Nils Larsch ] *) Add 'field_type' member to EC_METHOD, which holds the NID @@ -69,8 +77,8 @@ functions EC_GROUP_new_by_nid() EC_GROUP_new_by_name() - Also add a 'nid' field to EC_GROUP objects, which can be accessed - via + Also add a 'curve_name' member to EC_GROUP objects, which can be + accessed via EC_GROUP_set_nid() EC_GROUP_get_nid() [Nils Larsch group = EC_GROUP_new_by_name(curve_type); if (named_curve) EC_GROUP_set_asn1_flag(ecdsa->group, - OPENSSL_EC_NAMED_CURVE | - (EC_GROUP_get_asn1_flag(ecdsa->group) & ~0x3)); + OPENSSL_EC_NAMED_CURVE); } else if (informat == FORMAT_ASN1) ecdsa = d2i_ECDSAParameters_bio(in,NULL); diff --git a/crypto/asn1/x_pubkey.c b/crypto/asn1/x_pubkey.c index 13c753aff3..68ddeb43f2 100644 --- a/crypto/asn1/x_pubkey.c +++ b/crypto/asn1/x_pubkey.c @@ -145,7 +145,7 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) goto err; } - if ((EC_GROUP_get_asn1_flag(ecdsa->group) & OPENSSL_EC_NAMED_CURVE) + if (EC_GROUP_get_asn1_flag(ecdsa->group) && (nid = EC_GROUP_get_nid(ecdsa->group))) { /* just set the OID */ @@ -312,8 +312,8 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) if ((ecdsa->group = EC_GROUP_new_by_name( OBJ_obj2nid(a->parameter->value.object))) == NULL) goto err; - EC_GROUP_set_asn1_flag(ecdsa->group, OPENSSL_EC_NAMED_CURVE | - (EC_GROUP_get_asn1_flag(ecdsa->group) & ~0x03)); + EC_GROUP_set_asn1_flag(ecdsa->group, + OPENSSL_EC_NAMED_CURVE); } /* the case implicitlyCA is currently not implemented */ ret->save_parameters = 1; diff --git a/crypto/ec/ec.h b/crypto/ec/ec.h index 1d79dd6a47..e3bae734f1 100644 --- a/crypto/ec/ec.h +++ b/crypto/ec/ec.h @@ -229,11 +229,7 @@ int EC_GROUP_precompute_mult(EC_GROUP *, BN_CTX *); /* ASN1 stuff */ -#define OPENSSL_EC_EXPLICIT 0x001 -#define OPENSSL_EC_NAMED_CURVE 0x002 -#define OPENSSL_EC_COMPRESSED 0x010 -#define OPENSSL_EC_UNCOMPRESSED 0x020 -#define OPENSSL_EC_HYBRID 0x040 +#define OPENSSL_EC_NAMED_CURVE 0x001 typedef struct ec_parameters_st ECPARAMETERS; typedef struct ecpk_parameters_st ECPKPARAMETERS; @@ -249,6 +245,9 @@ ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *, ECPKPARAMETERS *); void EC_GROUP_set_asn1_flag(EC_GROUP *, int flag); int EC_GROUP_get_asn1_flag(const EC_GROUP *); +void EC_GROUP_set_point_conversion_form(EC_GROUP *, point_conversion_form_t); +point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *); + EC_GROUP *d2i_ECParameters(EC_GROUP **, const unsigned char **in, long len); int i2d_ECParameters(const EC_GROUP *, unsigned char **out); diff --git a/crypto/ec/ec_asn1.c b/crypto/ec/ec_asn1.c index 9a8b7c9c7a..5a97e9f70a 100644 --- a/crypto/ec/ec_asn1.c +++ b/crypto/ec/ec_asn1.c @@ -394,7 +394,7 @@ err : if (!ok) static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param) { - int ok=0, i; + int ok=0; size_t len=0; ECPARAMETERS *ret=NULL; BIGNUM *tmp=NULL; @@ -455,15 +455,7 @@ static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, goto err; } - i = EC_GROUP_get_asn1_flag(group); - if (i | OPENSSL_EC_COMPRESSED) - form = POINT_CONVERSION_COMPRESSED; - else if (i | OPENSSL_EC_UNCOMPRESSED) - form = POINT_CONVERSION_UNCOMPRESSED; - else if (i | OPENSSL_EC_HYBRID) - form = POINT_CONVERSION_HYBRID; - else - goto err; + form = EC_GROUP_get_point_conversion_form(group); len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); if (len == 0) @@ -556,9 +548,7 @@ ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group, ECPARAMETERS_free(ret->value.parameters); } - tmp = EC_GROUP_get_asn1_flag(group); - - if (tmp & OPENSSL_EC_NAMED_CURVE) + if (EC_GROUP_get_asn1_flag(group)) { /* use the asn1 OID to describe the * the elliptic curve parameters @@ -581,7 +571,7 @@ ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group, ok = 0; } } - else if (tmp & OPENSSL_EC_EXPLICIT) + else { /* use the ECPARAMETERS structure */ ret->type = 1; @@ -589,8 +579,6 @@ ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *group, group, NULL)) == NULL) ok = 0; } - else - ok = 0; if (!ok) { @@ -759,9 +747,7 @@ EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params) EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); return NULL; } - tmp = EC_GROUP_get_asn1_flag(ret); - tmp = (tmp & ~0x03) | OPENSSL_EC_NAMED_CURVE; - EC_GROUP_set_asn1_flag(ret, tmp); + EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); } else if (params->type == 1) { /* the parameters are given by a ECPARAMETERS @@ -772,9 +758,7 @@ EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *params) ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB); return NULL; } - tmp = EC_GROUP_get_asn1_flag(ret); - tmp = (tmp & ~0x03) | OPENSSL_EC_EXPLICIT; - EC_GROUP_set_asn1_flag(ret, tmp); + EC_GROUP_set_asn1_flag(ret, 0x0); } else if (params->type == 2) { /* implicitlyCA */ diff --git a/crypto/ec/ec_lcl.h b/crypto/ec/ec_lcl.h index 037c7d95a6..1c2ae93bb6 100644 --- a/crypto/ec/ec_lcl.h +++ b/crypto/ec/ec_lcl.h @@ -144,8 +144,9 @@ struct ec_group_st { EC_POINT *generator; /* optional */ BIGNUM order, cofactor; - int nid; /* optional NID for named curve */ + int curve_name;/* optional NID for named curve */ int asn1_flag; /* flag to control the asn1 encoding */ + point_conversion_form_t asn1_form; void *extra_data; void *(*extra_data_dup_func)(void *); diff --git a/crypto/ec/ec_lib.c b/crypto/ec/ec_lib.c index 06d45e998f..965c229dea 100644 --- a/crypto/ec/ec_lib.c +++ b/crypto/ec/ec_lib.c @@ -98,8 +98,9 @@ EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) BN_init(&ret->order); BN_init(&ret->cofactor); - ret->nid = 0; - ret->asn1_flag = OPENSSL_EC_EXPLICIT | OPENSSL_EC_COMPRESSED; + ret->curve_name = 0; + ret->asn1_flag = 0; + ret->asn1_form = POINT_CONVERSION_COMPRESSED; ret->seed = NULL; ret->seed_len = 0; @@ -214,8 +215,9 @@ int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) if (!BN_copy(&dest->order, &src->order)) return 0; if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0; - dest->nid = src->nid; - dest->asn1_flag = src->asn1_flag; + dest->curve_name = src->curve_name; + dest->asn1_flag = src->asn1_flag; + dest->asn1_form = src->asn1_form; if (src->seed) { @@ -308,13 +310,13 @@ int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx) void EC_GROUP_set_nid(EC_GROUP *group, int nid) { - group->nid = nid; + group->curve_name = nid; } int EC_GROUP_get_nid(const EC_GROUP *group) { - return group->nid; + return group->curve_name; } @@ -330,6 +332,19 @@ int EC_GROUP_get_asn1_flag(const EC_GROUP *group) } +void EC_GROUP_set_point_conversion_form(EC_GROUP *group, + point_conversion_form_t form) + { + group->asn1_form = form; + } + + +point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *group) + { + return group->asn1_form; + } + + int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { if (group->meth->group_set_curve_GFp == 0) -- 2.25.1