]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Optimization of ossl_ec_key_public_check()
authorTomas Mraz <tomas@openssl.org>
Mon, 8 Jul 2024 16:01:34 +0000 (18:01 +0200)
committerTomas Mraz <tomas@openssl.org>
Tue, 16 Jul 2024 13:36:46 +0000 (15:36 +0200)
We can do just the quick check if cofactor == 1 as the
fact that the point is on the curve already implies
that order * point = infinity.

Fixes #21833

Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/24816)

crypto/ec/ec_key.c

index 9bc4e032c557171925e732e01217b59af060b31b..b9a12bbf18050a9a16bde611a3f52154948e7c9f 100644 (file)
@@ -563,10 +563,16 @@ int ossl_ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx)
     int ret = 0;
     EC_POINT *point = NULL;
     const BIGNUM *order = NULL;
+    const BIGNUM *cofactor = EC_GROUP_get0_cofactor(eckey->group);
 
     if (!ossl_ec_key_public_check_quick(eckey, ctx))
         return 0;
 
+    if (cofactor != NULL && BN_is_one(cofactor)) {
+        /* Skip the unnecessary expensive computation for curves with cofactor of 1. */
+        return 1;
+    }
+
     point = EC_POINT_new(eckey->group);
     if (point == NULL)
         return 0;