Skip to content

Commit a135fe1

Browse files
chlily1Boringssl LUCI CQ
authored andcommitted
Forbid setting EC public key to point at infinity
The point at infinity is not a valid EC public key. We already refuse to parse it. This CL makes it forbidden to set an EC public key to a manually-constructed point at infinity. This change will allow callers to assume that ECDH is infallible given a valid peer public key. Update-Note: EC_KEY_set_public_key will now return an error if configuring the point at infinity as a public key. This does not impact parsing, which already rejected such a point, and the resulting key would have failed all operations already. Change-Id: I202307a510543f4305b1cb46094d2c83174047af Fixed: 438493754 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/81267 Commit-Queue: Lily Chen <chlily@google.com> Reviewed-by: David Benjamin <davidben@google.com>
1 parent fa47b1d commit a135fe1

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

crypto/fipsmodule/ec/ec_key.cc.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
213213
return 0;
214214
}
215215

216+
if (pub_key != NULL && EC_POINT_is_at_infinity(pub_key->group, pub_key)) {
217+
OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
218+
return 0;
219+
}
220+
216221
if (pub_key != NULL && EC_GROUP_cmp(key->group, pub_key->group, NULL) != 0) {
217222
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
218223
return 0;

crypto/fipsmodule/ec/ec_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ TEST(ECTest, SetNULLKey) {
453453
EXPECT_FALSE(EC_KEY_get0_public_key(key.get()));
454454
}
455455

456+
TEST(ECTest, PointAtInfinity) {
457+
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
458+
ASSERT_TRUE(key);
459+
460+
bssl::UniquePtr<EC_POINT> inf(EC_POINT_new(key->group));
461+
ASSERT_TRUE(inf);
462+
ASSERT_TRUE(EC_POINT_set_to_infinity(key->group, inf.get()));
463+
// Configuring a public key with the point at infinity is invalid.
464+
EXPECT_FALSE(EC_KEY_set_public_key(key.get(), inf.get()));
465+
}
466+
456467
TEST(ECTest, GroupMismatch) {
457468
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_secp384r1));
458469
ASSERT_TRUE(key);

include/openssl/ec_key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
8787
// EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
8888
// It returns one on success and zero otherwise. |key| must already have had a
8989
// group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and
90-
// |pub| must also belong to that group.
90+
// |pub| must also belong to that group, and must not be the point at infinity.
9191
OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
9292

9393
#define EC_PKEY_NO_PARAMETERS 0x001

0 commit comments

Comments
 (0)