From 6ea5254bb9d9f20e0a16aab843be6e7f54fa5194 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 12 May 2021 17:11:09 +0200 Subject: [PATCH] DCP refactor: do not override wc_Sha types --- wolfcrypt/src/port/nxp/dcp_port.c | 16 +++++++---- wolfcrypt/src/sha.c | 4 +-- wolfssl/wolfcrypt/port/nxp/dcp_port.h | 41 +++++++-------------------- wolfssl/wolfcrypt/random.h | 4 +-- wolfssl/wolfcrypt/sha.h | 10 +++++-- wolfssl/wolfcrypt/sha256.h | 8 ++++-- 6 files changed, 36 insertions(+), 47 deletions(-) diff --git a/wolfcrypt/src/port/nxp/dcp_port.c b/wolfcrypt/src/port/nxp/dcp_port.c index b03f0b135..532ed03d8 100644 --- a/wolfcrypt/src/port/nxp/dcp_port.c +++ b/wolfcrypt/src/port/nxp/dcp_port.c @@ -35,6 +35,8 @@ #include #include #include +#include "fsl_device_registers.h" +#include "fsl_debug_console.h" #ifdef WOLFSSL_IMXRT_DCP #if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) && defined(DCP_USE_DCACHE) && (DCP_USE_DCACHE == 1U) @@ -49,6 +51,15 @@ #include "fsl_debug_console.h" #include "fsl_dcp.h" +#ifdef USE_FAST_MATH + #include +#elif defined WOLFSSL_SP_MATH + #include +#else + #include +#endif + + #ifndef SINGLE_THREADED #define dcp_lock_init() wolfSSL_CryptHwMutexInit() #define dcp_lock() wolfSSL_CryptHwMutexLock() @@ -448,11 +459,6 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) return ret; } -int wc_InitSha(wc_Sha* sha) -{ - return wc_InitSha_ex(sha, NULL, INVALID_DEVID); -} - void DCPShaFree(wc_Sha* sha) { if (sha) diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index 034f3d72d..4b654217c 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -330,6 +330,7 @@ /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */ #elif defined(WOLFSSL_IMXRT_DCP) + #include /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */ #elif defined(WOLFSSL_SILABS_SE_ACCEL) @@ -793,13 +794,10 @@ int wc_ShaTransform(wc_Sha* sha, const unsigned char* data) #endif /* USE_SHA_SOFTWARE_IMPL */ - -#if !defined(WOLFSSL_IMXRT_DCP) int wc_InitSha(wc_Sha* sha) { return wc_InitSha_ex(sha, NULL, INVALID_DEVID); } -#endif /* !defined(WOLFSSL_IMXRT_DCP) */ void wc_ShaFree(wc_Sha* sha) { diff --git a/wolfssl/wolfcrypt/port/nxp/dcp_port.h b/wolfssl/wolfcrypt/port/nxp/dcp_port.h index f8f08d4fc..0813b37e7 100644 --- a/wolfssl/wolfcrypt/port/nxp/dcp_port.h +++ b/wolfssl/wolfcrypt/port/nxp/dcp_port.h @@ -22,23 +22,22 @@ #define _DCP_PORT_H_ #include -#ifdef USE_FAST_MATH - #include -#elif defined WOLFSSL_SP_MATH - #include -#else - #include +#include "fsl_dcp.h" + +#ifndef NO_SHA256 +#include +void DCPSha256Free(wc_Sha256 *sha256); #endif -#include -#include -#include "fsl_device_registers.h" -#include "fsl_debug_console.h" -#include "fsl_dcp.h" +#ifndef NO_SHA +#include +void DCPShaFree(wc_Sha *sha); +#endif int wc_dcp_init(void); #ifndef NO_AES +#include int DCPAesInit(Aes* aes); void DCPAesFree(Aes *aes); @@ -53,25 +52,5 @@ int DCPAesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); int DCPAesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz); #endif -#ifndef NO_SHA256 -typedef struct wc_Sha256_DCP { - dcp_handle_t handle; - dcp_hash_ctx_t ctx; -} wc_Sha256; -#define WC_SHA256_TYPE_DEFINED - -void DCPSha256Free(wc_Sha256 *sha256); - -#endif - -#ifndef NO_SHA -typedef struct wc_Sha_DCP { - dcp_handle_t handle; - dcp_hash_ctx_t ctx; -} wc_Sha; -#define WC_SHA_TYPE_DEFINED - -void DCPShaFree(wc_Sha *sha); -#endif #endif diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index b4e301df9..066eaff49 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -103,9 +103,7 @@ #ifdef NO_SHA256 #error "Hash DRBG requires SHA-256." #endif /* NO_SHA256 */ - #ifdef WOLFSSL_SMALL_STACK_CACHE - #include - #endif + #include #elif defined(HAVE_WNR) /* allow whitewood as direct RNG source using wc_GenerateSeed directly */ #elif defined(HAVE_INTEL_RDRAND) diff --git a/wolfssl/wolfcrypt/sha.h b/wolfssl/wolfcrypt/sha.h index 10c447115..96c032e28 100644 --- a/wolfssl/wolfcrypt/sha.h +++ b/wolfssl/wolfcrypt/sha.h @@ -52,6 +52,10 @@ #include "fsl_ltc.h" #endif +#ifdef WOLFSSL_IMXRT_DCP + #include "fsl_dcp.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -72,9 +76,6 @@ #ifdef WOLFSSL_ESP32WROOM32_CRYPT #include #endif -#ifdef WOLFSSL_IMXRT_DCP - #include -#endif #if defined(WOLFSSL_SILABS_SE_ACCEL) #include #endif @@ -119,6 +120,9 @@ struct wc_Sha { STM32_HASH_Context stmCtx; #elif defined(WOLFSSL_SILABS_SE_ACCEL) wc_silabs_sha_t silabsCtx; +#elif defined(WOLFSSL_IMXRT_DCP) + dcp_handle_t handle; + dcp_hash_ctx_t ctx; #else word32 buffLen; /* in bytes */ word32 loLen; /* length in bytes */ diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index 01228834d..42b113dae 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -61,6 +61,9 @@ #include "fsl_ltc.h" #endif +#ifdef WOLFSSL_IMXRT_DCP + #include "fsl_dcp.h" +#endif #ifdef __cplusplus extern "C" { @@ -131,8 +134,6 @@ enum { #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h" #elif defined(WOLFSSL_PSOC6_CRYPTO) #include "wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h" -#elif defined(WOLFSSL_IMXRT_DCP) - #include #else /* wc_Sha256 digest */ @@ -143,6 +144,9 @@ struct wc_Sha256 { STM32_HASH_Context stmCtx; #elif defined(WOLFSSL_SILABS_SE_ACCEL) wc_silabs_sha_t silabsCtx; +#elif defined(WOLFSSL_IMXRT_DCP) + dcp_handle_t handle; + dcp_hash_ctx_t ctx; #else /* alignment on digest and buffer speeds up ARMv8 crypto operations */ ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];