Merge pull request #7337 from gojimmypi/PR-test-certbuf-256

wolfcrypt tests: improve file system gating for USE_CERT_BUFFERS
This commit is contained in:
Daniel Pouzzner
2024-03-30 00:43:20 -04:00
committed by GitHub

View File

@ -1,6 +1,6 @@
/* test.c /* test.c
* *
* Copyright (C) 2006-2023 wolfSSL Inc. * Copyright (C) 2006-2024 wolfSSL Inc.
* *
* This file is part of wolfSSL. * This file is part of wolfSSL.
* *
@ -18776,7 +18776,9 @@ static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
word32 idx3 = 0; word32 idx3 = 0;
#if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \ #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
|| !defined(USE_CERT_BUFFERS_256) || !defined(USE_CERT_BUFFERS_256)
XFILE file3; #ifndef NO_FILESYSTEM
XFILE file3;
#endif
#endif #endif
wc_test_ret_t ret; wc_test_ret_t ret;
@ -18829,20 +18831,29 @@ static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa); ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
/* Get Cert Key */ /* Get Cert Key */
#ifdef USE_CERT_BUFFERS_256 #if defined(USE_CERT_BUFFERS_256)
XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256); {
bytes3 = sizeof_ecc_key_pub_der_256; XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
#else bytes3 = sizeof_ecc_key_pub_der_256;
file3 = XFOPEN(eccKeyPubFileDer, "rb");
if (!file3) {
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
} }
#elif !defined(NO_FILESYSTEM)
{
file3 = XFOPEN(eccKeyPubFileDer, "rb");
if (!file3) {
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
}
bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3); bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
XFCLOSE(file3); XFCLOSE(file3);
if (bytes3 == 0) if (bytes3 == 0)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
#endif }
#else
{
WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(1)");
ERROR_OUT(ASN_PARSE_E, exit_rsa);
}
#endif
ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId); ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
if (ret != 0) if (ret != 0)
@ -27422,10 +27433,12 @@ static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
wc_ecc_init_ex(key, HEAP_HINT, devId); wc_ecc_init_ex(key, HEAP_HINT, devId);
#ifndef NO_ECC256 #ifndef NO_ECC256
#ifdef USE_CERT_BUFFERS_256 #if defined(USE_CERT_BUFFERS_256)
XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256); {
tmpSz = (size_t)sizeof_ecc_key_der_256; XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
#else tmpSz = (size_t)sizeof_ecc_key_der_256;
}
#elif !defined(NO_FILESYSTEM)
{ {
XFILE file = XFOPEN(eccKeyDerFile, "rb"); XFILE file = XFOPEN(eccKeyDerFile, "rb");
if (!file) { if (!file) {
@ -27437,6 +27450,11 @@ static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
if (tmpSz == 0) if (tmpSz == 0)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
} }
#else
{
WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(2)");
ERROR_OUT(ASN_PARSE_E, done);
}
#endif /* USE_CERT_BUFFERS_256 */ #endif /* USE_CERT_BUFFERS_256 */
/* import private only then test with */ /* import private only then test with */
@ -28968,10 +28986,12 @@ static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \ ((defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
(defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))) (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)))
/* Use test ECC key - ensure real private "d" exists */ /* Use test ECC key - ensure real private "d" exists */
#ifdef USE_CERT_BUFFERS_256 #if defined(USE_CERT_BUFFERS_256)
ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key, {
sizeof_ecc_key_der_256); ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
#else sizeof_ecc_key_der_256);
}
#elif !defined(NO_FILESYSTEM)
{ {
XFILE file = XFOPEN(eccKeyDerFile, "rb"); XFILE file = XFOPEN(eccKeyDerFile, "rb");
byte der[128]; byte der[128];
@ -28985,6 +29005,12 @@ static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz); ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
} }
#else
{
(void)idx;
WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(3)");
ERROR_OUT(ASN_PARSE_E, done);
}
#endif #endif
if (ret != 0) { if (ret != 0) {
goto done; goto done;
@ -29913,9 +29939,9 @@ static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
ecc_key certPubKey[1]; ecc_key certPubKey[1];
#endif #endif
int certSz; int certSz;
size_t bytes; size_t bytes = 0;
word32 idx = 0; word32 idx = 0;
#ifndef USE_CERT_BUFFERS_256 #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_FILESYSTEM)
XFILE file; XFILE file;
#endif #endif
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@ -29946,38 +29972,46 @@ static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
/* Get cert private key */ /* Get cert private key */
#ifdef ENABLE_ECC384_CERT_GEN_TEST #ifdef ENABLE_ECC384_CERT_GEN_TEST
/* Get Cert Key 384 */ /* Get Cert Key 384 */
#ifdef USE_CERT_BUFFERS_256 #ifdef USE_CERT_BUFFERS_256
XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384); XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
bytes = sizeof_ca_ecc_key_der_384; bytes = sizeof_ca_ecc_key_der_384;
#else #elif !defined(NO_FILESYSTEM)
file = XFOPEN(eccCaKey384File, "rb"); file = XFOPEN(eccCaKey384File, "rb");
if (!file) { if (!file) {
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
} }
bytes = XFREAD(der, 1, FOURK_BUF, file); bytes = XFREAD(der, 1, FOURK_BUF, file);
XFCLOSE(file); XFCLOSE(file);
if (bytes == 0) if (bytes == 0)
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
(void)eccCaKeyFile; (void)eccCaKeyFile;
#endif /* USE_CERT_BUFFERS_256 */ #else
WOLFSSL_MSG("No file system and USE_CERT_BUFFERS_256 not defined.(4)");
ERROR_OUT(ASN_PARSE_E, exit);
#endif /* USE_CERT_BUFFERS_256 */
/* end if ENABLE_ECC384_CERT_GEN_TEST */
#else #else
#ifdef USE_CERT_BUFFERS_256 /* !ENABLE_ECC384_CERT_GEN_TEST */
XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
bytes = sizeof_ca_ecc_key_der_256; #ifdef USE_CERT_BUFFERS_256
#else XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
file = XFOPEN(eccCaKeyFile, "rb"); bytes = sizeof_ca_ecc_key_der_256;
if (!file) { #else
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit); file = XFOPEN(eccCaKeyFile, "rb");
} if (!file) {
bytes = XFREAD(der, 1, FOURK_BUF, file); ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
XFCLOSE(file); }
if (bytes == 0) bytes = XFREAD(der, 1, FOURK_BUF, file);
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit); XFCLOSE(file);
#ifdef ENABLE_ECC384_CERT_GEN_TEST if (bytes == 0)
(void)eccCaKey384File; ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
#endif
#endif /* USE_CERT_BUFFERS_256 */ #ifdef ENABLE_ECC384_CERT_GEN_TEST
(void)eccCaKey384File;
#endif
#endif /* USE_CERT_BUFFERS_256 */
#endif /* ENABLE_ECC384_CERT_GEN_TEST */ #endif /* ENABLE_ECC384_CERT_GEN_TEST */
/* Get CA Key */ /* Get CA Key */
@ -30038,23 +30072,28 @@ static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
#endif /* WOLFSSL_CERT_EXT */ #endif /* WOLFSSL_CERT_EXT */
#ifdef ENABLE_ECC384_CERT_GEN_TEST #ifdef ENABLE_ECC384_CERT_GEN_TEST
#if defined(USE_CERT_BUFFERS_256) #if defined(USE_CERT_BUFFERS_256)
ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384, ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
sizeof_ca_ecc_cert_der_384); sizeof_ca_ecc_cert_der_384);
#elif !defined(NO_FILESYSTEM)
ret = wc_SetIssuer(myCert, eccCaCert384File);
(void)eccCaCertFile;
#else
/* not testing with embedded, no file system target */
ERROR_OUT(ASN_PARSE_E, exit);
#endif /* USE_CERT_BUFFERS_256 */
#else #else
ret = wc_SetIssuer(myCert, eccCaCert384File); /* not ENABLE_ECC384_CERT_GEN_TEST */
(void)eccCaCertFile; #if defined(USE_CERT_BUFFERS_256)
#endif ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
#else sizeof_ca_ecc_cert_der_256);
#if defined(USE_CERT_BUFFERS_256) #else
ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256, ret = wc_SetIssuer(myCert, eccCaCertFile);
sizeof_ca_ecc_cert_der_256); #ifdef ENABLE_ECC384_CERT_GEN_TEST
#else (void)eccCaCert384File;
ret = wc_SetIssuer(myCert, eccCaCertFile); #endif
#ifdef ENABLE_ECC384_CERT_GEN_TEST
(void)eccCaCert384File;
#endif #endif
#endif
#endif /* ENABLE_ECC384_CERT_GEN_TEST */ #endif /* ENABLE_ECC384_CERT_GEN_TEST */
if (ret < 0) if (ret < 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit); ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);