From aaf8962a38dde62945cde0170b03c5fd5321e885 Mon Sep 17 00:00:00 2001 From: tb Date: Tue, 24 May 2022 20:06:32 +0000 Subject: [PATCH] Simplify ec_asn1_group2curve() Don't try to reuse curve->seed to avoid an allocation. Free it unconditionally and copy over the group->seed if it's available. Use asn1_abs_set_unused_bits() instead of inlining it. ok jsing --- lib/libcrypto/ec/ec_asn1.c | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/libcrypto/ec/ec_asn1.c b/lib/libcrypto/ec/ec_asn1.c index 4cf0bf59726..6bf7e47d7db 100644 --- a/lib/libcrypto/ec/ec_asn1.c +++ b/lib/libcrypto/ec/ec_asn1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_asn1.c,v 1.36 2022/03/31 13:00:58 tb Exp $ */ +/* $OpenBSD: ec_asn1.c,v 1.37 2022/05/24 20:06:32 tb Exp $ */ /* * Written by Nils Larsch for the OpenSSL project. */ @@ -60,11 +60,13 @@ #include -#include "ec_lcl.h" #include #include #include +#include "asn1_locl.h" +#include "ec_lcl.h" + int EC_GROUP_get_basis_type(const EC_GROUP * group) { @@ -860,24 +862,24 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve) ECerror(ERR_R_ASN1_LIB); goto err; } + + ASN1_BIT_STRING_free(curve->seed); + curve->seed = NULL; + /* set the seed (optional) */ - if (group->seed) { - if (!curve->seed) - if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { - ECerror(ERR_R_MALLOC_FAILURE); - goto err; - } - curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); - curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; + if (group->seed != NULL) { + if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { + ECerror(ERR_R_MALLOC_FAILURE); + goto err; + } if (!ASN1_BIT_STRING_set(curve->seed, group->seed, (int) group->seed_len)) { ECerror(ERR_R_ASN1_LIB); goto err; } - } else { - if (curve->seed) { - ASN1_BIT_STRING_free(curve->seed); - curve->seed = NULL; + if (!asn1_abs_set_unused_bits(curve->seed, 0)) { + ECerror(ERR_R_ASN1_LIB); + goto err; } } @@ -1481,10 +1483,11 @@ i2d_ECPrivateKey(EC_KEY * a, unsigned char **out) ECerror(ERR_R_EC_LIB); goto err; } - priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); - priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; - if (!ASN1_STRING_set(priv_key->publicKey, buffer, - buf_len)) { + if (!ASN1_STRING_set(priv_key->publicKey, buffer, buf_len)) { + ECerror(ERR_R_ASN1_LIB); + goto err; + } + if (!asn1_abs_set_unused_bits(priv_key->publicKey, 0)) { ECerror(ERR_R_ASN1_LIB); goto err; } -- 2.20.1