Переглянути джерело

Add ED25519_keypair_from_seed.

This function allows callers to unpack an Ed25519 “seed” value, which is
a 32 byte value that contains sufficient information to build a public
and private key from.

Change-Id: Ie5d8212a73e5710306314b4f8a93b707665870fd
Reviewed-on: https://boringssl-review.googlesource.com/12040
Reviewed-by: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <alangley@gmail.com>
Commit-Queue: Adam Langley <alangley@gmail.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
kris/onging/CECPQ3_patch15
Ladar Levison 8 роки тому
committed by CQ bot account: commit-bot@chromium.org
джерело
коміт
c034e2d3ce
3 змінених файлів з 48 додано та 15 видалено
  1. +19
    -14
      crypto/curve25519/curve25519.c
  2. +20
    -1
      crypto/curve25519/ed25519_test.cc
  3. +9
    -0
      include/openssl/curve25519.h

+ 19
- 14
crypto/curve25519/curve25519.c Переглянути файл

@@ -4613,20 +4613,7 @@ static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
void ED25519_keypair(uint8_t out_public_key[32], uint8_t out_private_key[64]) {
uint8_t seed[32];
RAND_bytes(seed, 32);

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

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

ge_p3 A;
x25519_ge_scalarmult_base(&A, az);
ge_p3_tobytes(out_public_key, &A);

memcpy(out_private_key, seed, 32);
memmove(out_private_key + 32, out_public_key, 32);
ED25519_keypair_from_seed(out_public_key, out_private_key, seed);
}

int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
@@ -4700,6 +4687,24 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
return CRYPTO_memcmp(rcheck, rcopy, sizeof(rcheck)) == 0;
}

void ED25519_keypair_from_seed(uint8_t out_public_key[32],
uint8_t out_private_key[64],
const uint8_t seed[32]) {
uint8_t az[SHA512_DIGEST_LENGTH];
SHA512(seed, 32, az);

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

ge_p3 A;
x25519_ge_scalarmult_base(&A, az);
ge_p3_tobytes(out_public_key, &A);

memcpy(out_private_key, seed, 32);
memcpy(out_private_key + 32, out_public_key, 32);
}


#if defined(BORINGSSL_X25519_X86_64)



+ 20
- 1
crypto/curve25519/ed25519_test.cc Переглянути файл

@@ -53,11 +53,30 @@ static bool TestSignature(FileTest *t, void *arg) {
return true;
}

static bool TestKeypairFromSeed() {
uint8_t public_key1[32], private_key1[64];
ED25519_keypair(public_key1, private_key1);

uint8_t seed[32];
memcpy(seed, private_key1, sizeof(seed));

uint8_t public_key2[32], private_key2[64];
ED25519_keypair_from_seed(public_key2, private_key2, seed);

if (memcmp(public_key1, public_key2, sizeof(public_key1)) != 0 ||
memcmp(private_key1, private_key2, sizeof(private_key1)) != 0) {
fprintf(stderr, "TestKeypairFromSeed: resulting keypairs did not match.\n");
return false;
}

return true;
}

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "%s <test input.txt>\n", argv[0]);
return 1;
}

return FileTestMain(TestSignature, nullptr, argv[1]);
return TestKeypairFromSeed() && FileTestMain(TestSignature, nullptr, argv[1]);
}

+ 9
- 0
include/openssl/curve25519.h Переглянути файл

@@ -85,6 +85,15 @@ OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len,
const uint8_t signature[64],
const uint8_t public_key[32]);

/* ED25519_keypair_from_seed calculates a public and private key from an
* Ed25519 “seed”. Seed values are not exposed by this API (although they
* happen to be the first 32 bytes of a private key) so this function is for
* interoperating with systems that may store just a seed instead of a full
* private key. */
OPENSSL_EXPORT void ED25519_keypair_from_seed(uint8_t out_public_key[32],
uint8_t out_private_key[64],
const uint8_t seed[32]);


/* SPAKE2.
*


Завантаження…
Відмінити
Зберегти