mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +02:00
Merge pull request #5552 from SparkiDev/ed_make_key_fixes
Ed make public key wasn't checking whether private key set
This commit is contained in:
20
tests/api.c
20
tests/api.c
@@ -21426,11 +21426,21 @@ static int test_wc_ed25519_make_key(void)
|
|||||||
#if defined(HAVE_ED25519)
|
#if defined(HAVE_ED25519)
|
||||||
ed25519_key key;
|
ed25519_key key;
|
||||||
WC_RNG rng;
|
WC_RNG rng;
|
||||||
|
unsigned char pubkey[ED25519_PUB_KEY_SIZE];
|
||||||
|
|
||||||
ret = wc_InitRng(&rng);
|
ret = wc_InitRng(&rng);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_ed25519_init(&key);
|
ret = wc_ed25519_init(&key);
|
||||||
}
|
}
|
||||||
|
if (ret == 0) {
|
||||||
|
ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
|
||||||
|
if (ret == ECC_PRIV_KEY_E) {
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
else if (ret == 0) {
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
printf(testingFmt, "wc_ed25519_make_key()");
|
printf(testingFmt, "wc_ed25519_make_key()");
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
|
ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
|
||||||
@@ -23249,11 +23259,21 @@ static int test_wc_ed448_make_key(void)
|
|||||||
#if defined(HAVE_ED448)
|
#if defined(HAVE_ED448)
|
||||||
ed448_key key;
|
ed448_key key;
|
||||||
WC_RNG rng;
|
WC_RNG rng;
|
||||||
|
unsigned char pubkey[ED448_PUB_KEY_SIZE];
|
||||||
|
|
||||||
ret = wc_InitRng(&rng);
|
ret = wc_InitRng(&rng);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_ed448_init(&key);
|
ret = wc_ed448_init(&key);
|
||||||
}
|
}
|
||||||
|
if (ret == 0) {
|
||||||
|
ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
|
||||||
|
if (ret == ECC_PRIV_KEY_E) {
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
else if (ret == 0) {
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
printf(testingFmt, "wc_ed448_make_key()");
|
printf(testingFmt, "wc_ed448_make_key()");
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
|
ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
|
||||||
|
@@ -182,6 +182,10 @@ int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
|
|||||||
if (key == NULL || pubKey == NULL || pubKeySz != ED25519_PUB_KEY_SIZE)
|
if (key == NULL || pubKey == NULL || pubKeySz != ED25519_PUB_KEY_SIZE)
|
||||||
ret = BAD_FUNC_ARG;
|
ret = BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
if ((ret == 0) && (!key->privKeySet)) {
|
||||||
|
ret = ECC_PRIV_KEY_E;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az);
|
ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
@@ -201,6 +205,8 @@ int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
|
|||||||
ge_scalarmult_base(&A, az);
|
ge_scalarmult_base(&A, az);
|
||||||
ge_p3_tobytes(pubKey, &A);
|
ge_p3_tobytes(pubKey, &A);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
key->pubKeySet = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -220,6 +226,9 @@ int wc_ed25519_make_key(WC_RNG* rng, int keySz, ed25519_key* key)
|
|||||||
if (keySz != ED25519_KEY_SIZE)
|
if (keySz != ED25519_KEY_SIZE)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
key->privKeySet = 0;
|
||||||
|
key->pubKeySet = 0;
|
||||||
|
|
||||||
#ifdef WOLF_CRYPTO_CB
|
#ifdef WOLF_CRYPTO_CB
|
||||||
if (key->devId != INVALID_DEVID) {
|
if (key->devId != INVALID_DEVID) {
|
||||||
ret = wc_CryptoCb_Ed25519Gen(rng, keySz, key);
|
ret = wc_CryptoCb_Ed25519Gen(rng, keySz, key);
|
||||||
@@ -233,8 +242,10 @@ int wc_ed25519_make_key(WC_RNG* rng, int keySz, ed25519_key* key)
|
|||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
key->privKeySet = 1;
|
||||||
ret = wc_ed25519_make_public(key, key->p, ED25519_PUB_KEY_SIZE);
|
ret = wc_ed25519_make_public(key, key->p, ED25519_PUB_KEY_SIZE);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
|
key->privKeySet = 0;
|
||||||
ForceZero(key->k, ED25519_KEY_SIZE);
|
ForceZero(key->k, ED25519_KEY_SIZE);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -242,9 +253,6 @@ int wc_ed25519_make_key(WC_RNG* rng, int keySz, ed25519_key* key)
|
|||||||
/* put public key after private key, on the same buffer */
|
/* put public key after private key, on the same buffer */
|
||||||
XMEMMOVE(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
|
XMEMMOVE(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
|
||||||
|
|
||||||
key->privKeySet = 1;
|
|
||||||
key->pubKeySet = 1;
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -185,6 +185,10 @@ int wc_ed448_make_public(ed448_key* key, unsigned char* pubKey, word32 pubKeySz)
|
|||||||
ret = BAD_FUNC_ARG;
|
ret = BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((ret == 0) && (!key->privKeySet)) {
|
||||||
|
ret = ECC_PRIV_KEY_E;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
|
ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
|
||||||
|
|
||||||
@@ -196,6 +200,8 @@ int wc_ed448_make_public(ed448_key* key, unsigned char* pubKey, word32 pubKeySz)
|
|||||||
|
|
||||||
ge448_scalarmult_base(&A, az);
|
ge448_scalarmult_base(&A, az);
|
||||||
ge448_to_bytes(pubKey, &A);
|
ge448_to_bytes(pubKey, &A);
|
||||||
|
|
||||||
|
key->pubKeySet = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -225,20 +231,22 @@ int wc_ed448_make_key(WC_RNG* rng, int keySz, ed448_key* key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
|
key->pubKeySet = 0;
|
||||||
|
key->privKeySet = 0;
|
||||||
|
|
||||||
ret = wc_RNG_GenerateBlock(rng, key->k, ED448_KEY_SIZE);
|
ret = wc_RNG_GenerateBlock(rng, key->k, ED448_KEY_SIZE);
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
key->privKeySet = 1;
|
key->privKeySet = 1;
|
||||||
ret = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
|
ret = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
|
key->privKeySet = 0;
|
||||||
ForceZero(key->k, ED448_KEY_SIZE);
|
ForceZero(key->k, ED448_KEY_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
/* put public key after private key, on the same buffer */
|
/* put public key after private key, on the same buffer */
|
||||||
XMEMMOVE(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
|
XMEMMOVE(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
|
||||||
|
|
||||||
key->pubKeySet = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
Reference in New Issue
Block a user