ed25519: validate presence of keys in export functions

This commit is contained in:
Juliusz Sosinowicz
2025-12-18 13:44:11 +01:00
parent bbc3a72ea8
commit dd35f10b57
2 changed files with 7 additions and 2 deletions

View File

@@ -490,7 +490,7 @@ int test_wc_Ed25519PublicKeyToDer(void)
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_ed25519_init(&key), 0);
ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0),
WC_NO_ERR_TRACE(BUFFER_E));
WC_NO_ERR_TRACE(PUBLIC_KEY_E));
wc_ed25519_free(&key);
/* Test good args */

View File

@@ -1127,6 +1127,9 @@ int wc_ed25519_export_public(const ed25519_key* key, byte* out, word32* outLen)
return BUFFER_E;
}
if (!key->pubKeySet)
return PUBLIC_KEY_E;
*outLen = ED25519_PUB_KEY_SIZE;
XMEMCPY(out, key->p, ED25519_PUB_KEY_SIZE);
@@ -1368,7 +1371,7 @@ int wc_ed25519_export_private_only(const ed25519_key* key, byte* out, word32* ou
int wc_ed25519_export_private(const ed25519_key* key, byte* out, word32* outLen)
{
/* sanity checks on arguments */
if (key == NULL || out == NULL || outLen == NULL)
if (key == NULL || !key->privKeySet || out == NULL || outLen == NULL)
return BAD_FUNC_ARG;
if (*outLen < ED25519_PRV_KEY_SIZE) {
@@ -1398,6 +1401,8 @@ int wc_ed25519_export_key(const ed25519_key* key,
/* export public part */
ret = wc_ed25519_export_public(key, pub, pubSz);
if (ret == WC_NO_ERR_TRACE(PUBLIC_KEY_E))
ret = 0; /* ignore no public key */
return ret;
}