diff options
author | Michael Brown <mcb30@ipxe.org> | 2024-01-30 13:26:36 +0000 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2024-01-30 13:26:36 +0000 |
commit | 17135c83fb056f93fe70bdb09dd05ecc08963eed (patch) | |
tree | 37d12a0223ff435890ce730b7467ff908963dcba /src/crypto/x25519.c | |
parent | 27398f136030efb8845c45fbe154e544feefd7e5 (diff) | |
download | ipxe-17135c83fb056f93fe70bdb09dd05ecc08963eed.tar.gz |
[crypto] Add an abstraction of an elliptic curve
Define an abstraction of an elliptic curve with a fixed generator and
one supported operation (scalar multiplication of a curve point).
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/crypto/x25519.c')
-rw-r--r-- | src/crypto/x25519.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/crypto/x25519.c b/src/crypto/x25519.c index d3a19bc8e..d58f7168c 100644 --- a/src/crypto/x25519.c +++ b/src/crypto/x25519.c @@ -61,6 +61,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include <assert.h> #include <errno.h> #include <ipxe/init.h> +#include <ipxe/crypto.h> #include <ipxe/x25519.h> /** X25519 reduction constant @@ -300,6 +301,11 @@ static const uint8_t x25519_121665_raw[] = { 0x01, 0xdb, 0x41 }; /** Constant 121665 (used in the Montgomery ladder) */ static union x25519_oct258 x25519_121665; +/** Constant g=9 (the group generator) */ +static struct x25519_value x25519_generator = { + .raw = { 9, } +}; + /** * Initialise constants * @@ -811,3 +817,28 @@ int x25519_key ( const struct x25519_value *base, /* Fail if result was all zeros (as required by RFC8422) */ return ( bigint_is_zero ( &point.value ) ? -EPERM : 0 ); } + +/** + * Multiply scalar by curve point + * + * @v base Base point (or NULL to use generator) + * @v scalar Scalar multiple + * @v result Result point to fill in + * @ret rc Return status code + */ +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 ); +} + +/** X25519 elliptic curve */ +struct elliptic_curve x25519_curve = { + .name = "x25519", + .keysize = sizeof ( struct x25519_value ), + .multiply = x25519_curve_multiply, +}; |