Skip to content

Commit 70be012

Browse files
davidbenBoringssl LUCI CQ
authored and
Boringssl LUCI CQ
committed
Use constant curve-specific groups whenever possible
Also remove unnecessary EC_GROUP_free calls. EC_GROUP_free is only necessary in codepaths where arbitrary groups are possible. Bug: 20 Change-Id: I3dfb7f07b890ab002ba8a302724d8bc671590cfe Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60932 Reviewed-by: Bob Beck <[email protected]> Commit-Queue: David Benjamin <[email protected]>
1 parent ac6793a commit 70be012

19 files changed

+225
-378
lines changed

crypto/ec_extra/ec_asn1.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
9494
}
9595

9696
// Parse the optional parameters field.
97-
EC_GROUP *inner_group = NULL;
9897
EC_KEY *ret = NULL;
9998
BIGNUM *priv_key = NULL;
10099
if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) {
@@ -107,7 +106,7 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
107106
OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
108107
goto err;
109108
}
110-
inner_group = EC_KEY_parse_parameters(&child);
109+
const EC_GROUP *inner_group = EC_KEY_parse_parameters(&child);
111110
if (inner_group == NULL) {
112111
goto err;
113112
}
@@ -189,13 +188,11 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
189188
}
190189

191190
BN_free(priv_key);
192-
EC_GROUP_free(inner_group);
193191
return ret;
194192

195193
err:
196194
EC_KEY_free(ret);
197195
BN_free(priv_key);
198-
EC_GROUP_free(inner_group);
199196
return NULL;
200197
}
201198

@@ -353,8 +350,6 @@ EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
353350
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kAllGroups); i++) {
354351
const EC_GROUP *group = kAllGroups[i]();
355352
if (CBS_mem_equal(&named_curve, group->oid, group->oid_len)) {
356-
// TODO(davidben): Remove unnecessary calls to |EC_GROUP_free| within the
357-
// library.
358353
return (EC_GROUP *)group;
359354
}
360355
}
@@ -433,8 +428,6 @@ EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
433428
BN_free(b);
434429
BN_free(x);
435430
BN_free(y);
436-
// TODO(davidben): Remove unnecessary calls to |EC_GROUP_free| within the
437-
// library.
438431
return (EC_GROUP *)ret;
439432
}
440433

@@ -492,18 +485,16 @@ EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) {
492485

493486
CBS cbs;
494487
CBS_init(&cbs, *inp, (size_t)len);
495-
EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
488+
const EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
496489
if (group == NULL) {
497490
return NULL;
498491
}
499492

500493
EC_KEY *ret = EC_KEY_new();
501494
if (ret == NULL || !EC_KEY_set_group(ret, group)) {
502-
EC_GROUP_free(group);
503495
EC_KEY_free(ret);
504496
return NULL;
505497
}
506-
EC_GROUP_free(group);
507498

508499
if (out_key != NULL) {
509500
EC_KEY_free(*out_key);

crypto/ecdh_extra/ecdh_test.cc

+14-15
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,23 @@
3535
#include "../test/wycheproof_util.h"
3636

3737

38-
static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
38+
static const EC_GROUP *GetCurve(FileTest *t, const char *key) {
3939
std::string curve_name;
4040
if (!t->GetAttribute(&curve_name, key)) {
4141
return nullptr;
4242
}
4343

4444
if (curve_name == "P-224") {
45-
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
45+
return EC_group_p224();
4646
}
4747
if (curve_name == "P-256") {
48-
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(
49-
NID_X9_62_prime256v1));
48+
return EC_group_p256();
5049
}
5150
if (curve_name == "P-384") {
52-
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
51+
return EC_group_p384();
5352
}
5453
if (curve_name == "P-521") {
55-
return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
54+
return EC_group_p521();
5655
}
5756

5857
t->PrintLine("Unknown curve '%s'", curve_name.c_str());
@@ -70,7 +69,7 @@ static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
7069

7170
TEST(ECDHTest, TestVectors) {
7271
FileTestGTest("crypto/ecdh_extra/ecdh_tests.txt", [](FileTest *t) {
73-
bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
72+
const EC_GROUP *group = GetCurve(t, "Curve");
7473
ASSERT_TRUE(group);
7574
bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
7675
ASSERT_TRUE(priv_key);
@@ -87,16 +86,16 @@ TEST(ECDHTest, TestVectors) {
8786

8887
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
8988
ASSERT_TRUE(key);
90-
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
89+
bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group));
9190
ASSERT_TRUE(pub_key);
92-
bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group.get()));
91+
bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group));
9392
ASSERT_TRUE(peer_pub_key);
94-
ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
93+
ASSERT_TRUE(EC_KEY_set_group(key.get(), group));
9594
ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get()));
96-
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(),
95+
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(group, pub_key.get(),
9796
x.get(), y.get(), nullptr));
9897
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp(
99-
group.get(), peer_pub_key.get(), peer_x.get(), peer_y.get(), nullptr));
98+
group, peer_pub_key.get(), peer_x.get(), peer_y.get(), nullptr));
10099
ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get()));
101100
ASSERT_TRUE(EC_KEY_check_key(key.get()));
102101

@@ -130,7 +129,7 @@ TEST(ECDHTest, TestVectors) {
130129
static void RunWycheproofTest(FileTest *t) {
131130
t->IgnoreInstruction("encoding");
132131

133-
bssl::UniquePtr<EC_GROUP> group = GetWycheproofCurve(t, "curve", true);
132+
const EC_GROUP *group = GetWycheproofCurve(t, "curve", true);
134133
ASSERT_TRUE(group);
135134
bssl::UniquePtr<BIGNUM> priv_key = GetWycheproofBIGNUM(t, "private", false);
136135
ASSERT_TRUE(priv_key);
@@ -157,10 +156,10 @@ static void RunWycheproofTest(FileTest *t) {
157156

158157
bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
159158
ASSERT_TRUE(key);
160-
ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get()));
159+
ASSERT_TRUE(EC_KEY_set_group(key.get(), group));
161160
ASSERT_TRUE(EC_KEY_set_private_key(key.get(), priv_key.get()));
162161

163-
std::vector<uint8_t> actual((EC_GROUP_get_degree(group.get()) + 7) / 8);
162+
std::vector<uint8_t> actual((EC_GROUP_get_degree(group) + 7) / 8);
164163
int ret =
165164
ECDH_compute_key(actual.data(), actual.size(),
166165
EC_KEY_get0_public_key(peer_ec), key.get(), nullptr);

crypto/err/ssl.errordata

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ SSL,253,NO_COMMON_SIGNATURE_ALGORITHMS
121121
SSL,178,NO_COMPRESSION_SPECIFIED
122122
SSL,265,NO_GROUPS_SPECIFIED
123123
SSL,179,NO_METHOD_SPECIFIED
124-
SSL,180,NO_P256_SUPPORT
125124
SSL,181,NO_PRIVATE_KEY_ASSIGNED
126125
SSL,182,NO_RENEGOTIATION
127126
SSL,183,NO_REQUIRED_DIGEST

crypto/evp/p_ec.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
typedef struct {
7676
// message digest
7777
const EVP_MD *md;
78-
EC_GROUP *gen_group;
78+
const EC_GROUP *gen_group;
7979
} EC_PKEY_CTX;
8080

8181

@@ -111,7 +111,6 @@ static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) {
111111
return;
112112
}
113113

114-
EC_GROUP_free(dctx->gen_group);
115114
OPENSSL_free(dctx);
116115
}
117116

@@ -195,11 +194,10 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
195194
return 1;
196195

197196
case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: {
198-
EC_GROUP *group = EC_GROUP_new_by_curve_name(p1);
197+
const EC_GROUP *group = EC_GROUP_new_by_curve_name(p1);
199198
if (group == NULL) {
200199
return 0;
201200
}
202-
EC_GROUP_free(dctx->gen_group);
203201
dctx->gen_group = group;
204202
return 1;
205203
}

crypto/evp/p_ec_asn1.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int eckey_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
9494

9595
// The parameters are a named curve.
9696
EC_KEY *eckey = NULL;
97-
EC_GROUP *group = EC_KEY_parse_curve_name(params);
97+
const EC_GROUP *group = EC_KEY_parse_curve_name(params);
9898
if (group == NULL || CBS_len(params) != 0) {
9999
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
100100
goto err;
@@ -107,12 +107,10 @@ static int eckey_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
107107
goto err;
108108
}
109109

110-
EC_GROUP_free(group);
111110
EVP_PKEY_assign_EC_KEY(out, eckey);
112111
return 1;
113112

114113
err:
115-
EC_GROUP_free(group);
116114
EC_KEY_free(eckey);
117115
return 0;
118116
}
@@ -135,15 +133,13 @@ static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
135133

136134
static int eckey_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
137135
// See RFC 5915.
138-
EC_GROUP *group = EC_KEY_parse_parameters(params);
136+
const EC_GROUP *group = EC_KEY_parse_parameters(params);
139137
if (group == NULL || CBS_len(params) != 0) {
140138
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
141-
EC_GROUP_free(group);
142139
return 0;
143140
}
144141

145142
EC_KEY *ec_key = EC_KEY_parse_private_key(key, group);
146-
EC_GROUP_free(group);
147143
if (ec_key == NULL || CBS_len(key) != 0) {
148144
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
149145
EC_KEY_free(ec_key);

0 commit comments

Comments
 (0)