Skip to content

Commit

Permalink
Merge pull request #7519 from julek-wolfssl/gh/7516
Browse files Browse the repository at this point in the history
Return length in wc_Curve448PublicKeyToDer with NULL output param
  • Loading branch information
dgarske committed May 13, 2024
2 parents 81c2212 + 2397066 commit d39ab76
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -26801,6 +26801,58 @@ static int test_wc_Ed448PrivateKeyToDer(void)
return EXPECT_RESULT();
} /* End test_wc_Ed448PrivateKeyToDer*/

/*
* Testing wc_Curve448PrivateKeyToDer
*/
static int test_wc_Curve448PrivateKeyToDer(void)
{
EXPECT_DECLS;
#if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
(defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
byte output[ONEK_BUF];
curve448_key curve448PrivKey;
WC_RNG rng;
word32 inLen;

XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
XMEMSET(&rng, 0, sizeof(WC_RNG));

ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
0);
inLen = (word32)sizeof(output);

/* Bad Cases */
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);
/* Good cases */
/* length only */
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);

/* Bad Cases */
ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
BUFFER_E);
ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
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, output, inLen, 0), 0);
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);

DoExpectIntEQ(wc_FreeRng(&rng), 0);
wc_curve448_free(&curve448PrivKey);
#endif
return EXPECT_RESULT();
} /* End wc_Curve448PrivateKeyToDer*/

/*
* Testing wc_SetSubjectBuffer
*/
Expand Down Expand Up @@ -72082,6 +72134,7 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wc_Ed448PublicKeyToDer),
TEST_DECL(test_wc_Ed448KeyToDer),
TEST_DECL(test_wc_Ed448PrivateKeyToDer),
TEST_DECL(test_wc_Curve448PrivateKeyToDer),

/* Signature API */
TEST_DECL(test_wc_SignatureGetSize_ecc),
Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -35328,7 +35328,7 @@ int wc_Curve448PublicKeyToDer(curve448_key* key, byte* output, word32 inLen,
byte pubKey[CURVE448_PUB_KEY_SIZE];
word32 pubKeyLen = (word32)sizeof(pubKey);

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

Expand Down

0 comments on commit d39ab76

Please sign in to comment.