Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api.c and asn.c changes to allow 0 to be passed in and expanded coverage on test cases. #7791

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -23340,7 +23340,11 @@ static int test_wc_Ed25519PublicKeyToDer(void)
ExpectIntEQ(wc_ed25519_init(&key), 0);
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
/* length only */
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, NULL, 0, 0), 0);
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, NULL, 0, 1), 0);
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf,
(word32)sizeof(derBuf), 1), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
wc_ed25519_free(&key);
Expand Down Expand Up @@ -24233,8 +24237,11 @@ static int test_wc_Ed448PublicKeyToDer(void)
ExpectIntEQ(wc_ed448_init(&key), 0);
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);

ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
/* length only */
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, NULL, 0, 0), 0);
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, NULL, 0, 1), 0);
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf,
(word32)sizeof(derBuf), 1), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
wc_ed448_free(&key);
Expand Down Expand Up @@ -26860,9 +26867,10 @@ static int test_wc_Ed25519KeyToDer(void)
/* Bad Cases */
ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BUFFER_E);
/* Good Cases */
/* length only */
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, 0), 0);
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);

Expand Down Expand Up @@ -26898,10 +26906,10 @@ static int test_wc_Ed25519PrivateKeyToDer(void)
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
BAD_FUNC_ARG);
BUFFER_E);
/* Good Cases */
/* length only */
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, 0), 0);
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
Expand Down Expand Up @@ -26934,10 +26942,10 @@ static int test_wc_Ed448KeyToDer(void)
/* Bad Cases */
ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BUFFER_E);
/* Good Cases */
/* length only */
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, 0), 0);
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
Expand Down Expand Up @@ -26972,10 +26980,10 @@ static int test_wc_Ed448PrivateKeyToDer(void)
ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
BAD_FUNC_ARG);
BUFFER_E);
/* Good cases */
/* length only */
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, 0), 0);
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
Expand Down Expand Up @@ -27010,10 +27018,10 @@ static int test_wc_Curve448PrivateKeyToDer(void)
ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
BAD_FUNC_ARG);
BUFFER_E);
/* Good cases */
/* length only */
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, 0), 0);
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);

/* Bad Cases */
Expand All @@ -27025,8 +27033,8 @@ static int test_wc_Curve448PrivateKeyToDer(void)
BUFFER_E);
/* Good cases */
/* length only */
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, 0, 0), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, 0, 1), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);

Expand Down
15 changes: 11 additions & 4 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -11994,9 +11994,13 @@ int SetAsymKeyDerPublic(const byte* pubKey, word32 pubKeyLen,
DECL_ASNSETDATA(dataASN, edPubKeyASN_Length);
#endif

if (pubKey == NULL) {
/* validate parameters */
if (pubKey == NULL){
return BAD_FUNC_ARG;
}
if (output != NULL && outLen == 0) {
return BUFFER_E;
}

#ifndef WOLFSSL_ASN_TEMPLATE
/* calculate size */
Expand Down Expand Up @@ -35337,10 +35341,13 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
int sz;
#endif

/* Validate parameters. */
if (privKey == NULL || outLen == 0) {
/* validate parameters */
if (privKey == NULL) {
return BAD_FUNC_ARG;
aidangarske marked this conversation as resolved.
Show resolved Hide resolved
}
if (output != NULL && outLen == 0) {
return BUFFER_E;
}

#ifndef WOLFSSL_ASN_TEMPLATE
/* calculate size */
Expand Down Expand Up @@ -35498,7 +35505,7 @@ int wc_Curve25519PublicKeyToDer(curve25519_key* key, byte* output, word32 inLen,
byte pubKey[CURVE25519_PUB_KEY_SIZE];
word32 pubKeyLen = (word32)sizeof(pubKey);

if (key == NULL || output == NULL) {
if (key == NULL) {
return BAD_FUNC_ARG;
}

Expand Down