Skip to content

Commit

Permalink
Refactor ED25519_sign into hw and nohw backend
Browse files Browse the repository at this point in the history
  • Loading branch information
torben-hansen committed Oct 27, 2023
1 parent 04fa153 commit d3e395c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
23 changes: 18 additions & 5 deletions crypto/curve25519/curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ OPENSSL_INLINE int x25519_s2n_bignum_capable(void) {
#endif
}

// Return 0 until ED25519 lands in s2n-bignum
OPENSSL_INLINE int ed25519_s2n_bignum_capable(void) {
return 0;
}

// Stub functions if implementations are not compiled.
// These functions have to abort, otherwise we risk applications assuming they
// did work without actually doing anything.
Expand Down Expand Up @@ -234,21 +239,29 @@ static void x25519_s2n_bignum_public_from_private(
#endif
}

// Stub function until ED25519 lands in s2n-bignum
static void ed25519_keypair_from_seed_s2n_bignum(uint8_t out_public_key[32],
uint8_t az[SHA512_DIGEST_LENGTH]) {
abort();
}

void ED25519_keypair_from_seed(uint8_t out_public_key[32],
uint8_t out_private_key[64],
const uint8_t seed[ED25519_SEED_LEN]) {
uint8_t out_private_key[64], const uint8_t seed[ED25519_SEED_LEN]) {

uint8_t az[SHA512_DIGEST_LENGTH];
SHA512(seed, ED25519_SEED_LEN, az);

az[0] &= 248;
az[31] &= 127;
az[31] |= 64;

ge_p3 A;
x25519_ge_scalarmult_base(&A, az);
ge_p3_tobytes(out_public_key, &A);
if (ed25519_s2n_bignum_capable() == 1) {
ed25519_keypair_from_seed_s2n_bignum(out_public_key, az);
} else {
ed25519_keypair_from_seed_nohw(out_public_key, az);
}

OPENSSL_STATIC_ASSERT(64 == (ED25519_SEED_LEN + 32), ed25519_parameter_length_mismatch)
OPENSSL_memcpy(out_private_key, seed, ED25519_SEED_LEN);
OPENSSL_memcpy(out_private_key + ED25519_SEED_LEN, out_public_key, 32);
}
Expand Down
7 changes: 7 additions & 0 deletions crypto/curve25519/curve25519_nohw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1968,3 +1968,10 @@ void x25519_public_from_private_nohw(uint8_t out_public_value[32],
fe_tobytes(out_public_value, &zminusy_inv);
CONSTTIME_DECLASSIFY(out_public_value, 32);
}

void ed25519_keypair_from_seed_nohw(uint8_t out_public_key[32],
uint8_t az[SHA512_DIGEST_LENGTH]) {
ge_p3 A;
x25519_ge_scalarmult_base(&A, az);
ge_p3_tobytes(out_public_key, &A);
}
3 changes: 3 additions & 0 deletions crypto/curve25519/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
#endif

#include <openssl/base.h>
#include <openssl/curve25519.h>

#include "../internal.h"

Expand Down Expand Up @@ -114,6 +115,8 @@ void x25519_scalar_mult_generic_nohw(uint8_t out[32],
const uint8_t point[32]);
void x25519_public_from_private_nohw(uint8_t out_public_value[32],
const uint8_t private_key[32]);
void ed25519_keypair_from_seed_nohw(uint8_t out_public_key[32],
uint8_t az[SHA512_DIGEST_LENGTH]);

// Port to internal linkage in curve25519_nohw.c when adding implementation
// from s2n-bignum ed25519
Expand Down

0 comments on commit d3e395c

Please sign in to comment.