]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[crypto] Expose the base point as an explicit elliptic curve property
authorMichael Brown <mcb30@ipxe.org>
Fri, 5 Dec 2025 13:00:12 +0000 (13:00 +0000)
committerMichael Brown <mcb30@ipxe.org>
Fri, 5 Dec 2025 13:09:07 +0000 (13:09 +0000)
Add the generator base point as an explicit property of an elliptic
curve, and remove the ability to pass a NULL to elliptic_multiply() to
imply the use of the generator base point.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/crypto/ecdhe.c
src/crypto/weierstrass.c
src/crypto/x25519.c
src/include/ipxe/crypto.h
src/include/ipxe/weierstrass.h
src/tests/elliptic_test.c

index 5481b02eb3b8928f6dd00e18d9326e0127d7d199..592c8ca1aafcc1571b7e989ac8bc0ed54ed403aa 100644 (file)
@@ -55,7 +55,7 @@ int ecdhe_key ( struct elliptic_curve *curve, const void *partner,
        }
 
        /* Construct public key */
-       if ( ( rc = elliptic_multiply ( curve, NULL, private,
+       if ( ( rc = elliptic_multiply ( curve, curve->base, private,
                                        public ) ) != 0 ) {
                DBGC ( curve, "CURVE %s could not generate public key: %s\n",
                       curve->name, strerror ( rc ) );
index c149c7b210a7a2a7418e4a37cc32b24d6a1a40dd..4974e5252ab2f202a0b1ad42ea79d20a5fbfeb4c 100644 (file)
@@ -762,7 +762,7 @@ static int weierstrass_verify_raw ( const struct weierstrass_curve *curve,
  * Multiply curve point by scalar
  *
  * @v curve            Weierstrass curve
- * @v base             Base point (or NULL to use generator)
+ * @v base             Base point
  * @v scalar           Scalar multiple
  * @v result           Result point to fill in
  * @ret rc             Return status code
@@ -806,10 +806,6 @@ int weierstrass_multiply ( struct weierstrass_curve *curve, const void *base,
        if ( ! prime2->element[0] )
                weierstrass_init ( curve );
 
-       /* Use generator if applicable */
-       if ( ! base )
-               base = curve->base;
-
        /* Convert input to projective coordinates in Montgomery form */
        DBGC ( curve, "WEIERSTRASS %s base (", curve->name );
        for ( i = 0, offset = 0 ; i < WEIERSTRASS_AXES ; i++, offset += len ) {
index 41bc5fc5e387daec0053d18d43924d20a8e35df2..00a791877f65d61ae719b61546f8102b12f7390d 100644 (file)
@@ -822,7 +822,7 @@ int x25519_key ( const struct x25519_value *base,
 /**
  * Multiply scalar by curve point
  *
- * @v base             Base point (or NULL to use generator)
+ * @v base             Base point
  * @v scalar           Scalar multiple
  * @v result           Result point to fill in
  * @ret rc             Return status code
@@ -830,10 +830,6 @@ int x25519_key ( const struct x25519_value *base,
 static int x25519_curve_multiply ( const void *base, const void *scalar,
                                   void *result ) {
 
-       /* Use base point if applicable */
-       if ( ! base )
-               base = &x25519_generator;
-
        return x25519_key ( base, scalar, result );
 }
 
@@ -842,5 +838,6 @@ struct elliptic_curve x25519_curve = {
        .name = "x25519",
        .pointsize = sizeof ( struct x25519_value ),
        .keysize = sizeof ( struct x25519_value ),
+       .base = x25519_generator.raw,
        .multiply = x25519_curve_multiply,
 };
index ee63423c939bcff7763a24dad580b2886b9e7f35..d2adea5d6e02257674ccbdd2059d97b78371d6c1 100644 (file)
@@ -181,9 +181,11 @@ struct elliptic_curve {
        size_t pointsize;
        /** Scalar (and private key) size */
        size_t keysize;
+       /** Generator base point */
+       const void *base;
        /** Multiply scalar by curve point
         *
-        * @v base              Base point (or NULL to use generator)
+        * @v base              Base point
         * @v scalar            Scalar multiple
         * @v result            Result point to fill in
         * @ret rc              Return status code
index fb09fa6fa3bc1710486f2aecc66ec8772f6712e4..e5e411499cbb57ce2f83f26d7f947b374849598f 100644 (file)
@@ -160,6 +160,7 @@ extern int weierstrass_multiply ( struct weierstrass_curve *curve,
                .name = #_name,                                         \
                .pointsize = ( WEIERSTRASS_AXES * (_len) ),             \
                .keysize = (_len),                                      \
+               .base = (_base),                                        \
                .multiply = _name ## _multiply,                         \
        }
 
index 4c42e584892758d24b471b423c35ae4b38536cbc..d8cdf2c0f18d6e25d2059beffb28ccf1d4f0c1f4 100644 (file)
@@ -52,6 +52,7 @@ void elliptic_okx ( struct elliptic_test *test, const char *file,
        size_t pointsize = curve->pointsize;
        size_t keysize = curve->keysize;
        uint8_t actual[pointsize];
+       const void *base;
        int rc;
 
        /* Sanity checks */
@@ -62,8 +63,8 @@ void elliptic_okx ( struct elliptic_test *test, const char *file,
              file, line );
 
        /* Perform point multiplication */
-       rc = elliptic_multiply ( curve, ( test->base_len ? test->base : NULL ),
-                                test->scalar, actual );
+       base = ( test->base_len ? test->base : curve->base );
+       rc = elliptic_multiply ( curve, base, test->scalar, actual );
        if ( test->expected_len ) {
                okx ( rc == 0, file, line );
        } else {