fix spelling, refactor openssl extra struct, remove magic number, use static mutex

This commit is contained in:
JacobBarthelmeh
2023-02-22 14:31:16 -08:00
parent b801a96f8c
commit 121ee7a6df
5 changed files with 60 additions and 68 deletions

View File

@ -45,7 +45,7 @@ static caam_job_ring_interface_t jr2;
static caam_job_ring_interface_t jr3; static caam_job_ring_interface_t jr3;
#endif #endif
wolfSSL_Mutex caamMutex; static wolfSSL_Mutex caamMutex;
/* Initialize CAAM resources. /* Initialize CAAM resources.
* return 0 on success */ * return 0 on success */
@ -142,7 +142,7 @@ static int wc_CAAM_CommonHash(caam_handle_t* hndl, caam_hash_ctx_t *ctx,
byte *tmpIn = NULL; byte *tmpIn = NULL;
if ((wc_ptr_t)in % CAAM_BUFFER_ALIGN) { if ((wc_ptr_t)in % CAAM_BUFFER_ALIGN) {
/* input not alligned */ /* input not aligned */
tmpIn = (byte*)XMALLOC(inSz + CAAM_BUFFER_ALIGN, NULL, tmpIn = (byte*)XMALLOC(inSz + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedIn = tmpIn + (CAAM_BUFFER_ALIGN - alignedIn = tmpIn + (CAAM_BUFFER_ALIGN -
@ -169,7 +169,7 @@ static int wc_CAAM_CommonHash(caam_handle_t* hndl, caam_hash_ctx_t *ctx,
size_t sz = digestSz; size_t sz = digestSz;
if ((wc_ptr_t)digest % CAAM_BUFFER_ALIGN) { if ((wc_ptr_t)digest % CAAM_BUFFER_ALIGN) {
/* input not alligned */ /* input not aligned */
tmpOut = (byte*)XMALLOC(sz + CAAM_BUFFER_ALIGN, NULL, tmpOut = (byte*)XMALLOC(sz + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedOut = tmpOut + (CAAM_BUFFER_ALIGN - alignedOut = tmpOut + (CAAM_BUFFER_ALIGN -
@ -301,7 +301,7 @@ static int DoAesCTR(unsigned int args[4], CAAM_BUFFER *buf, int sz)
byte *alignedOut = NULL; byte *alignedOut = NULL;
if (buf[2].TheAddress % CAAM_BUFFER_ALIGN) { if (buf[2].TheAddress % CAAM_BUFFER_ALIGN) {
/* input not alligned */ /* input not aligned */
tmpIn = (byte*)XMALLOC(buf[2].Length + CAAM_BUFFER_ALIGN, NULL, tmpIn = (byte*)XMALLOC(buf[2].Length + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedIn = tmpIn + (CAAM_BUFFER_ALIGN - alignedIn = tmpIn + (CAAM_BUFFER_ALIGN -
@ -313,7 +313,7 @@ static int DoAesCTR(unsigned int args[4], CAAM_BUFFER *buf, int sz)
} }
if (buf[3].TheAddress % CAAM_BUFFER_ALIGN) { if (buf[3].TheAddress % CAAM_BUFFER_ALIGN) {
/* output not alligned */ /* output not aligned */
tmpOut = (byte*)XMALLOC(buf[3].Length + CAAM_BUFFER_ALIGN, NULL, tmpOut = (byte*)XMALLOC(buf[3].Length + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedOut = tmpOut + (CAAM_BUFFER_ALIGN - alignedOut = tmpOut + (CAAM_BUFFER_ALIGN -
@ -467,7 +467,7 @@ int wc_CAAM_EccSign(const byte* in, int inlen, byte* out, word32* outlen,
} }
if ((wc_ptr_t)in % CAAM_BUFFER_ALIGN) { if ((wc_ptr_t)in % CAAM_BUFFER_ALIGN) {
/* input not alligned */ /* input not aligned */
tmpIn = (byte*)XMALLOC(inlen + CAAM_BUFFER_ALIGN, NULL, tmpIn = (byte*)XMALLOC(inlen + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedIn = tmpIn + (CAAM_BUFFER_ALIGN - alignedIn = tmpIn + (CAAM_BUFFER_ALIGN -
@ -581,7 +581,7 @@ static int wc_CAAM_EccVerify_ex(mp_int* r, mp_int *s, const byte* hash,
} }
if ((wc_ptr_t)hash % CAAM_BUFFER_ALIGN) { if ((wc_ptr_t)hash % CAAM_BUFFER_ALIGN) {
/* input not alligned */ /* input not aligned */
tmpIn = (byte*)XMALLOC(hashlen + CAAM_BUFFER_ALIGN, NULL, tmpIn = (byte*)XMALLOC(hashlen + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
alignedIn = tmpIn + (CAAM_BUFFER_ALIGN - alignedIn = tmpIn + (CAAM_BUFFER_ALIGN -

View File

@ -71,21 +71,22 @@ static int _InitSha(byte* ctx, word32 ctxSz, void* heap, int devId,
{ {
CAAM_BUFFER buf[1]; CAAM_BUFFER buf[1];
word32 arg[4]; word32 arg[4];
int ret; int ret, idx = 0;
/* Set buffer for context */ /* Set buffer for context */
buf[0].BufferType = DataBuffer | LastBuffer; buf[idx].BufferType = DataBuffer | LastBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)ctx; buf[idx].TheAddress = (CAAM_ADDRESS)ctx;
buf[0].Length = ctxSz + WC_CAAM_CTXLEN; buf[idx].Length = ctxSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
arg[0] = CAAM_ALG_INIT; arg[0] = CAAM_ALG_INIT;
arg[1] = ctxSz + WC_CAAM_CTXLEN; arg[1] = ctxSz + WC_CAAM_CTXLEN;
arg[2] = (word32)devId; arg[2] = (word32)devId;
if ((ret = wc_caamAddAndWait(buf, 1, arg, type)) != 0) { if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA init"); WOLFSSL_MSG("Error with CAAM SHA init");
return ret; return ret;
} }
@ -99,7 +100,7 @@ static int _ShaUpdate(wc_Sha* sha, const byte* data, word32 len, word32 digestSz
{ {
CAAM_BUFFER buf[2]; CAAM_BUFFER buf[2];
word32 arg[4]; word32 arg[4];
int ret; int ret, idx = 0;
byte* local; byte* local;
if (sha == NULL ||(data == NULL && len > 0)) { if (sha == NULL ||(data == NULL && len > 0)) {
@ -120,25 +121,27 @@ static int _ShaUpdate(wc_Sha* sha, const byte* data, word32 len, word32 digestSz
if (sha->buffLen == WC_CAAM_HASH_BLOCK) { if (sha->buffLen == WC_CAAM_HASH_BLOCK) {
/* Set buffer for context */ /* Set buffer for context */
buf[0].BufferType = DataBuffer; buf[idx].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)sha->ctx; buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[0].Length = digestSz + WC_CAAM_CTXLEN; buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
/* data to update with */ /* data to update with */
buf[1].BufferType = DataBuffer | LastBuffer; buf[idx].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)sha->buffer; buf[idx].TheAddress = (CAAM_ADDRESS)sha->buffer;
buf[1].Length = sha->buffLen; buf[idx].Length = sha->buffLen;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
arg[0] = CAAM_ALG_UPDATE; arg[0] = CAAM_ALG_UPDATE;
arg[1] = digestSz + WC_CAAM_CTXLEN; arg[1] = digestSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, 2, arg, type)) != 0) { if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA update"); WOLFSSL_MSG("Error with CAAM SHA update");
return ret; return ret;
} }
@ -150,27 +153,30 @@ static int _ShaUpdate(wc_Sha* sha, const byte* data, word32 len, word32 digestSz
if (len >= WC_CAAM_HASH_BLOCK) { if (len >= WC_CAAM_HASH_BLOCK) {
word32 sz = len / WC_CAAM_HASH_BLOCK; word32 sz = len / WC_CAAM_HASH_BLOCK;
sz = sz * WC_CAAM_HASH_BLOCK; sz = sz * WC_CAAM_HASH_BLOCK;
idx = 0;
/* Set buffer for context */ /* Set buffer for context */
buf[0].BufferType = DataBuffer; buf[idx].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)sha->ctx; buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[0].Length = digestSz + WC_CAAM_CTXLEN; buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
/* data to update with */ /* data to update with */
buf[1].BufferType = DataBuffer | LastBuffer; buf[idx].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)data; buf[idx].TheAddress = (CAAM_ADDRESS)data;
buf[1].Length = sz; buf[idx].Length = sz;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
arg[0] = CAAM_ALG_UPDATE; arg[0] = CAAM_ALG_UPDATE;
arg[1] = digestSz + WC_CAAM_CTXLEN; arg[1] = digestSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, 2, arg, type)) != 0) { if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA update"); WOLFSSL_MSG("Error with CAAM SHA update");
return ret; return ret;
} }
@ -196,32 +202,34 @@ static int _ShaFinal(byte* ctx, word32 ctxSz, byte* in, word32 inSz, byte* out,
{ {
CAAM_BUFFER buf[2]; CAAM_BUFFER buf[2];
word32 arg[4]; word32 arg[4];
int ret; int ret, idx = 0;
if (ctx == NULL || out == NULL) { if (ctx == NULL || out == NULL) {
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
/* Set buffer for context */ /* Set buffer for context */
buf[0].BufferType = DataBuffer; buf[idx].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)ctx; buf[idx].TheAddress = (CAAM_ADDRESS)ctx;
buf[0].Length = ctxSz; buf[idx].Length = ctxSz;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
/* add any potential left overs */ /* add any potential left overs */
buf[1].BufferType = DataBuffer | LastBuffer; buf[idx].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)in; buf[idx].TheAddress = (CAAM_ADDRESS)in;
buf[1].Length = inSz; buf[idx].Length = inSz;
#if defined(__INTEGRITY) || defined(INTEGRITY) #if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0; buf[idx].Transferred = 0;
#endif #endif
idx++;
arg[0] = CAAM_ALG_FINAL; arg[0] = CAAM_ALG_FINAL;
arg[1] = ctxSz + WC_CAAM_CTXLEN; arg[1] = ctxSz + WC_CAAM_CTXLEN;
if ((ret = wc_caamAddAndWait(buf, 2, arg, type)) != 0) { if ((ret = wc_caamAddAndWait(buf, idx, arg, type)) != 0) {
WOLFSSL_MSG("Error with CAAM SHA Final"); WOLFSSL_MSG("Error with CAAM SHA Final");
return ret; return ret;
} }

View File

@ -36,7 +36,7 @@
/* for devctl use */ /* for devctl use */
int caamFd = -1; int caamFd = -1;
wolfSSL_Mutex caamMutex; static wolfSSL_Mutex caamMutex;
/* return 0 on success */ /* return 0 on success */
int wc_CAAMInitInterface() int wc_CAAMInitInterface()

View File

@ -39,7 +39,7 @@
#define MAX_SECO_TIMEOUT 1000 #define MAX_SECO_TIMEOUT 1000
wolfSSL_Mutex caamMutex; static wolfSSL_Mutex caamMutex;
static pthread_t tid; static pthread_t tid;
static uint32_t nvm_status = 0; static uint32_t nvm_status = 0;
static hsm_hdl_t hsm_session; static hsm_hdl_t hsm_session;

View File

@ -99,9 +99,13 @@ typedef WOLFSSL_SHA_CTX SHA_CTX;
/* adder for HW crypto */ /* adder for HW crypto */
#ifdef STM32_HASH #ifdef STM32_HASH
#define CTX_SHA2_HW_ADDER 34 #define CTX_SHA2_HW_ADDER 34
#elif defined(WOLFSSL_IMXRT1170_CAAM)
#define CTX_SHA2_HW_ADDER sizeof(caam_hash_ctx_t) + sizeof(caam_handle_t)
#elif defined(WOLFSSL_ESPWROOM32)
#define CTX_SHA2_HW_ADDER sizeof(WC_ESP32SHA)
#else #else
#define CTX_SHA2_HW_ADDER 0 #define CTX_SHA2_HW_ADDER 0
#endif #endif
#ifdef WOLFSSL_SHA224 #ifdef WOLFSSL_SHA224
@ -111,13 +115,8 @@ typedef WOLFSSL_SHA_CTX SHA_CTX;
* to Sha224, is expected to also be 16 byte aligned addresses. */ * to Sha224, is expected to also be 16 byte aligned addresses. */
typedef struct WOLFSSL_SHA224_CTX { typedef struct WOLFSSL_SHA224_CTX {
/* big enough to hold wolfcrypt Sha224, but check on init */ /* big enough to hold wolfcrypt Sha224, but check on init */
#if defined(WOLFSSL_IMXRT1170_CAAM)
ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE +
sizeof(caam_hash_ctx_t) + sizeof(caam_handle_t)) / sizeof(void*)];
#else
ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) / ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) /
sizeof(void*)]; sizeof(void*)];
#endif
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
ALIGN16 void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))]; ALIGN16 void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))];
#endif #endif
@ -157,13 +156,8 @@ typedef WOLFSSL_SHA224_CTX SHA224_CTX;
* to Sha256, is expected to also be 16 byte aligned addresses. */ * to Sha256, is expected to also be 16 byte aligned addresses. */
typedef struct WOLFSSL_SHA256_CTX { typedef struct WOLFSSL_SHA256_CTX {
/* big enough to hold wolfcrypt Sha256, but check on init */ /* big enough to hold wolfcrypt Sha256, but check on init */
#if defined(WOLFSSL_IMXRT1170_CAAM)
ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE +
sizeof(caam_hash_ctx_t) + sizeof(caam_handle_t)) / sizeof(void*)];
#else
ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) / ALIGN16 void* holder[(274 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) /
sizeof(void*)]; sizeof(void*)];
#endif
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
ALIGN16 void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))]; ALIGN16 void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))];
#endif #endif
@ -213,12 +207,7 @@ typedef WOLFSSL_SHA256_CTX SHA256_CTX;
#ifdef WOLFSSL_SHA384 #ifdef WOLFSSL_SHA384
typedef struct WOLFSSL_SHA384_CTX { typedef struct WOLFSSL_SHA384_CTX {
/* big enough to hold wolfCrypt Sha384, but check on init */ /* big enough to hold wolfCrypt Sha384, but check on init */
#if defined(WOLFSSL_IMXRT1170_CAAM) void* holder[(268 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
void* holder[(268 + WC_ASYNC_DEV_SIZE +
sizeof(caam_hash_ctx_t) + sizeof(caam_handle_t)) / sizeof(void*)];
#else
void* holder[(268 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
#endif
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))]; void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))];
#endif #endif
@ -253,12 +242,7 @@ typedef WOLFSSL_SHA384_CTX SHA384_CTX;
#ifdef WOLFSSL_SHA512 #ifdef WOLFSSL_SHA512
typedef struct WOLFSSL_SHA512_CTX { typedef struct WOLFSSL_SHA512_CTX {
/* big enough to hold wolfCrypt Sha384, but check on init */ /* big enough to hold wolfCrypt Sha384, but check on init */
#if defined(WOLFSSL_IMXRT1170_CAAM) void* holder[(288 + CTX_SHA2_HW_ADDER + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
void* holder[(288 + WC_ASYNC_DEV_SIZE +
sizeof(caam_hash_ctx_t) + sizeof(caam_handle_t)) / sizeof(void*)];
#else
void* holder[(288 + WC_ASYNC_DEV_SIZE) / sizeof(void*)];
#endif
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))]; void* keephash_holder[sizeof(void*) + (2 * sizeof(unsigned int))];
#endif #endif