wolfcrypt/test/test.c: fix error codes in hpke_test_single(), hpke_test(), and ecc521_test_deterministic_k().

This commit is contained in:
Daniel Pouzzner 2023-10-03 22:44:11 -05:00
parent 71649388a0
commit 45b613ee55

View File

@ -24280,23 +24280,29 @@ static wc_test_ret_t hpke_test_single(Hpke* hpke)
rngRet = ret = wc_InitRng(rng);
if (ret != 0)
return ret;
return WC_TEST_RET_ENC_EC(ret);
#ifdef WOLFSSL_SMALL_STACK
if (ret == 0) {
pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER);
if (pubKey == NULL)
ret = MEMORY_E;
ret = WC_TEST_RET_ENC_EC(MEMORY_E);
}
#endif
/* generate the keys */
if (ret == 0)
if (ret == 0) {
ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
if (ret != 0)
ret = WC_TEST_RET_ENC_EC(ret);
}
if (ret == 0)
if (ret == 0) {
ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
if (ret != 0)
ret = WC_TEST_RET_ENC_EC(ret);
}
/* seal */
if (ret == 0) {
@ -24305,11 +24311,16 @@ static wc_test_ret_t hpke_test_single(Hpke* hpke)
(byte*)aad_text, (word32)XSTRLEN(aad_text),
(byte*)start_text, (word32)XSTRLEN(start_text),
ciphertext);
if (ret != 0)
ret = WC_TEST_RET_ENC_EC(ret);
}
/* export ephemeral key */
if (ret == 0)
if (ret == 0) {
ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
if (ret != 0)
ret = WC_TEST_RET_ENC_EC(ret);
}
/* open with exported ephemeral key */
if (ret == 0) {
@ -24318,10 +24329,15 @@ static wc_test_ret_t hpke_test_single(Hpke* hpke)
(byte*)aad_text, (word32)XSTRLEN(aad_text),
ciphertext, (word32)XSTRLEN(start_text),
plaintext);
if (ret != 0)
ret = WC_TEST_RET_ENC_EC(ret);
}
if (ret == 0)
if (ret == 0) {
ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
if (ret != 0)
ret = WC_TEST_RET_ENC_NC;
}
if (ephemeralKey != NULL)
wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
@ -24352,7 +24368,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
HPKE_AES_128_GCM, NULL);
if (ret != 0)
return ret;
return WC_TEST_RET_ENC_EC(ret);
ret = hpke_test_single(hpke);
@ -24367,7 +24383,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
HPKE_AES_128_GCM, NULL);
if (ret != 0)
return ret;
return WC_TEST_RET_ENC_EC(ret);
ret = hpke_test_single(hpke);
@ -24382,7 +24398,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
HPKE_AES_128_GCM, NULL);
if (ret != 0)
return ret;
return WC_TEST_RET_ENC_EC(ret);
ret = hpke_test_single(hpke);
@ -24397,7 +24413,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
HPKE_AES_256_GCM, NULL);
if (ret != 0)
return ret;
return WC_TEST_RET_ENC_EC(ret);
ret = hpke_test_single(hpke);
@ -25106,7 +25122,7 @@ static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
}
ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
if (ret != 0) {
return ret;
return WC_TEST_RET_ENC_EC(ret);
}
key_inited = 1;
@ -25750,41 +25766,40 @@ static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
if ((der == NULL) || (userA == NULL))
ERROR_OUT(MEMORY_E, done);
ERROR_OUT(WC_TEST_RET_ENC_EC(MEMORY_E), done);
#endif
ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
if (ret != 0)
goto done;
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
ret = wc_ecc_make_key(rng, keySize, userA);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
#endif
if (ret != 0)
goto done;
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
TEST_SLEEP();
ret = wc_ecc_check_key(userA);
if (ret != 0)
goto done;
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
TEST_SLEEP();
derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
if (derSz < 0) {
ERROR_OUT(derSz, done);
ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
}
ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
ECC_PRIVATEKEY_TYPE);
if (ret != 0) {
goto done;
}
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
/* test export of public key */
derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
if (derSz < 0) {
ERROR_OUT(derSz, done);
ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
}
if (derSz == 0) {
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
@ -25794,7 +25809,7 @@ static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
/* test export of compressed public key */
derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
if (derSz < 0) {
ERROR_OUT(derSz, done);
ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
}
if (derSz == 0) {
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
@ -25802,16 +25817,15 @@ static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
#endif
ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
if (ret != 0) {
goto done;
}
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
#ifdef HAVE_PKCS8
/* test export of PKCS#8 unencrypted private key */
pkcs8Sz = FOURK_BUF;
derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
if (derSz < 0) {
ERROR_OUT(derSz, done);
ERROR_OUT(WC_TEST_RET_ENC_I(derSz), done);
}
if (derSz == 0) {
@ -28723,7 +28737,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
if (ret < 0) {
printf("keySize=68, Default\n");
printf("keySize=66, Default\n");
goto done;
}
#endif /* HAVE_ECC521 */