diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index a50007a9d..f095b9360 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -1002,7 +1002,7 @@ static const char* bench_result_words2[][5] = { #ifdef WOLFSSL_ASYNC_CRYPT static WOLF_EVENT_QUEUE eventQueue; - #define BENCH_ASYNC_CAAM_DEVID(obj) (&(obj)->asyncDev) + #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev) #define BENCH_ASYNC_GET_NAME(useDeviceID) (useDeviceID) ? "HW" : "SW" #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING) diff --git a/wolfcrypt/src/hash.c b/wolfcrypt/src/hash.c index 30b382e60..28b1edc9d 100644 --- a/wolfcrypt/src/hash.c +++ b/wolfcrypt/src/hash.c @@ -1706,3 +1706,32 @@ int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags) #endif /* !NO_HASH_WRAPPER */ +#ifdef WOLFSSL_HASH_KEEP +int _wc_Sha_Grow(byte** msg, word32* used, word32* len, const byte* in, + int inSz, void* heap) +{ + if (*len < *used + inSz) { + if (*msg == NULL) { + *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); + } + else { + byte* pt = (byte*)XMALLOC(*used + inSz, heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (pt == NULL) { + return MEMORY_E; + } + XMEMCPY(pt, *msg, *used); + XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER); + *msg = pt; + } + if (*msg == NULL) { + return MEMORY_E; + } + *len = *used + inSz; + } + XMEMCPY(*msg + *used, in, inSz); + *used += inSz; + return 0; +} +#endif /* WOLFSSL_HASH_KEEP */ + diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 022c7a02f..eddd3820f 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -1509,6 +1509,12 @@ static int InitSha256(wc_Sha256* sha256) #ifdef WOLFSSL_HASH_FLAGS sha224->flags = 0; #endif + #ifdef WOLFSSL_HASH_KEEP + sha224->msg = NULL; + sha224->len = 0; + sha224->used = 0; + #endif + return ret; } @@ -1700,34 +1706,6 @@ void wc_Sha256Free(wc_Sha256* sha256) #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */ #ifdef WOLFSSL_HASH_KEEP -int _wc_Sha_Grow(byte** msg, word32* used, word32* len, const byte* in, - int inSz, void* heap) -{ - if (*len < *used + inSz) { - if (*msg == NULL) { - *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); - } - else { - byte* pt = (byte*)XMALLOC(*used + inSz, heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (pt == NULL) { - return MEMORY_E; - } - XMEMCPY(pt, *msg, *used); - XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER); - *msg = pt; - } - if (*msg == NULL) { - return MEMORY_E; - } - *len = *used + inSz; - } - XMEMCPY(*msg + *used, in, inSz); - *used += inSz; - return 0; -} - - /* Some hardware have issues with update, this function stores the data to be * hashed into an array. Once ready, the Final operation is called on all of the * data to be hashed at once. @@ -1766,6 +1744,7 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz) int ret; wc_Sha224 tmpSha224; + wc_InitSha224(&tmpSha224); if (sha224 == NULL || hash == NULL) return BAD_FUNC_ARG; diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index 51fab19b7..d9e4b3d32 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -1822,35 +1822,6 @@ int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags) #endif /* WOLFSSL_SHA384 */ #ifdef WOLFSSL_HASH_KEEP -#ifdef NO_SHA256 -static int _wc_Sha_Grow(byte** msg, word32* used, word32* len, const byte* in, - int inSz, void* heap) -{ - if (*len < *used + inSz) { - if (*msg == NULL) { - *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); - } - else { - byte* pt = (byte*)XMALLOC(*used + inSz, heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (pt == NULL) { - return MEMORY_E; - } - XMEMCPY(pt, *msg, *used); - XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER); - *msg = pt; - } - if (*msg == NULL) { - return MEMORY_E; - } - *len = *used + inSz; - } - XMEMCPY(*msg + *used, in, inSz); - *used += inSz; - return 0; -} -#endif /* NO_SHA256 */ - /* Some hardware have issues with update, this function stores the data to be * hashed into an array. Once ready, the Final operation is called on all of the * data to be hashed at once. @@ -1867,6 +1838,6 @@ int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz) return _wc_Sha_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in, inSz, sha384->heap); } -#endif /* WOLFSSL_SHA224 */ +#endif /* WOLFSSL_SHA384 */ #endif /* WOLFSSL_HASH_KEEP */ #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 865eb5e1c..b2ae77b54 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -8462,6 +8462,88 @@ static int aes_cbc_test(void) } #endif +#ifdef HAVE_AES_ECB +static int aesecb_test(void) +{ +#ifdef WOLFSSL_SMALL_STACK + Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES); +#else + Aes enc[1]; +#endif + byte cipher[AES_BLOCK_SIZE * 4]; +#ifdef HAVE_AES_DECRYPT +#ifdef WOLFSSL_SMALL_STACK + Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES); +#else + Aes dec[1]; +#endif + byte plain [AES_BLOCK_SIZE * 4]; +#endif /* HAVE_AES_DECRYPT */ + int ret = 0; + +#if defined(WOLFSSL_AES_256) + { + WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] = + { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96, + 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a + }; + + WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] = + { + 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c, + 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8 + }; + + WOLFSSL_SMALL_STACK_STATIC const byte niKey[] = + { + 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe, + 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81, + 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7, + 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 + }; + + XMEMSET(cipher, 0, AES_BLOCK_SIZE); + ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION); + if (ret != 0) + ERROR_OUT(-5943, out); + if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0) + ERROR_OUT(-5950, out); + if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0) + ERROR_OUT(-5944, out); + + XMEMSET(plain, 0, AES_BLOCK_SIZE); + ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION); + if (ret != 0) + ERROR_OUT(-5945, out); + if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0) + ERROR_OUT(-5951, out); + wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE); + if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0) + ERROR_OUT(-5946, out); + } + + wc_AesFree(enc); +#ifdef HAVE_AES_DECRYPT + wc_AesFree(dec); +#endif + + out: +#ifdef WOLFSSL_SMALL_STACK + if (enc) + XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES); +#ifdef HAVE_AES_DECRYPT + if (dec) + XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES); +#endif +#endif +#endif /* WOLFSSL_AES_256 */ + + return ret; +} +#endif /* HAVE_AES_ECB */ + + WOLFSSL_TEST_SUBROUTINE int aes_test(void) { #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT) @@ -9092,6 +9174,12 @@ WOLFSSL_TEST_SUBROUTINE int aes_test(void) #endif #endif +#if defined(HAVE_AES_ECB) + ret = aesecb_test(); + if (ret != 0) + goto out; +#endif + out: #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT) @@ -38638,6 +38726,36 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx) } } #endif /* HAVE_AES_CBC */ + #ifdef HAVE_AES_ECB + if (info->cipher.type == WC_CIPHER_AES_ECB) { + if (info->cipher.enc) { + /* set devId to invalid, so software is used */ + info->cipher.aesecb.aes->devId = INVALID_DEVID; + + ret = wc_AesEcbEncrypt( + info->cipher.aesecb.aes, + info->cipher.aesecb.out, + info->cipher.aesecb.in, + info->cipher.aesecb.sz); + + /* reset devId */ + info->cipher.aesecb.aes->devId = devIdArg; + } + else { + /* set devId to invalid, so software is used */ + info->cipher.aesecb.aes->devId = INVALID_DEVID; + + ret = wc_AesEcbDecrypt( + info->cipher.aesecb.aes, + info->cipher.aesecb.out, + info->cipher.aesecb.in, + info->cipher.aesecb.sz); + + /* reset devId */ + info->cipher.aesecb.aes->devId = devIdArg; + } + } + #endif /* HAVE_AES_CBC */ #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128) if (info->cipher.type == WC_CIPHER_AES_CCM) { if (info->cipher.enc) { diff --git a/wolfssl/openssl/sha.h b/wolfssl/openssl/sha.h index 6f787bff9..17af1ed2b 100644 --- a/wolfssl/openssl/sha.h +++ b/wolfssl/openssl/sha.h @@ -44,6 +44,9 @@ typedef struct WOLFSSL_SHA_CTX { #else void* holder[(112 + WC_ASYNC_DEV_SIZE) / sizeof(void*)]; #endif + #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) + void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))]; + #endif #ifdef WOLF_CRYPTO_CB void* cryptocb_holder[(sizeof(int) + sizeof(void*) + 4) / sizeof(void*)]; #endif diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 0c7967429..6e5f32a84 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -222,6 +222,11 @@ WOLFSSL_API int wc_Shake256Hash(const byte* data, word32 len, byte* hash, word32 #endif /* !NO_HASH_WRAPPER */ +#if defined(WOLFSSL_HASH_KEEP) +WOLFSSL_LOCAL int _wc_Sha_Grow(byte** msg, word32* used, word32* len, + const byte* in, int inSz, void* heap); +#endif + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index 5308e48ac..38abbf4a1 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -241,8 +241,6 @@ WOLFSSL_API int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data); #endif #if defined(WOLFSSL_HASH_KEEP) WOLFSSL_API int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz); -WOLFSSL_LOCAL int _wc_Sha_Grow(byte** msg, word32* used, word32* len, - const byte* in, int inSz, void* heap); #endif WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*); WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);