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;
#endif
wolfSSL_Mutex caamMutex;
static wolfSSL_Mutex caamMutex;
/* Initialize CAAM resources.
* 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;
if ((wc_ptr_t)in % CAAM_BUFFER_ALIGN) {
/* input not alligned */
/* input not aligned */
tmpIn = (byte*)XMALLOC(inSz + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
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;
if ((wc_ptr_t)digest % CAAM_BUFFER_ALIGN) {
/* input not alligned */
/* input not aligned */
tmpOut = (byte*)XMALLOC(sz + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
alignedOut = tmpOut + (CAAM_BUFFER_ALIGN -
@ -301,7 +301,7 @@ static int DoAesCTR(unsigned int args[4], CAAM_BUFFER *buf, int sz)
byte *alignedOut = NULL;
if (buf[2].TheAddress % CAAM_BUFFER_ALIGN) {
/* input not alligned */
/* input not aligned */
tmpIn = (byte*)XMALLOC(buf[2].Length + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
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) {
/* output not alligned */
/* output not aligned */
tmpOut = (byte*)XMALLOC(buf[3].Length + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
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) {
/* input not alligned */
/* input not aligned */
tmpIn = (byte*)XMALLOC(inlen + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
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) {
/* input not alligned */
/* input not aligned */
tmpIn = (byte*)XMALLOC(hashlen + CAAM_BUFFER_ALIGN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
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];
word32 arg[4];
int ret;
int ret, idx = 0;
/* Set buffer for context */
buf[0].BufferType = DataBuffer | LastBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)ctx;
buf[0].Length = ctxSz + WC_CAAM_CTXLEN;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)ctx;
buf[idx].Length = ctxSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_INIT;
arg[1] = ctxSz + WC_CAAM_CTXLEN;
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");
return ret;
}
@ -99,7 +100,7 @@ static int _ShaUpdate(wc_Sha* sha, const byte* data, word32 len, word32 digestSz
{
CAAM_BUFFER buf[2];
word32 arg[4];
int ret;
int ret, idx = 0;
byte* local;
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) {
/* Set buffer for context */
buf[0].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[0].Length = digestSz + WC_CAAM_CTXLEN;
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
/* data to update with */
buf[1].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)sha->buffer;
buf[1].Length = sha->buffLen;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->buffer;
buf[idx].Length = sha->buffLen;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_UPDATE;
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");
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) {
word32 sz = len / WC_CAAM_HASH_BLOCK;
sz = sz * WC_CAAM_HASH_BLOCK;
idx = 0;
/* Set buffer for context */
buf[0].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[0].Length = digestSz + WC_CAAM_CTXLEN;
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)sha->ctx;
buf[idx].Length = digestSz + WC_CAAM_CTXLEN;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
/* data to update with */
buf[1].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)data;
buf[1].Length = sz;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)data;
buf[idx].Length = sz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_UPDATE;
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");
return ret;
}
@ -196,32 +202,34 @@ static int _ShaFinal(byte* ctx, word32 ctxSz, byte* in, word32 inSz, byte* out,
{
CAAM_BUFFER buf[2];
word32 arg[4];
int ret;
int ret, idx = 0;
if (ctx == NULL || out == NULL) {
return BAD_FUNC_ARG;
}
/* Set buffer for context */
buf[0].BufferType = DataBuffer;
buf[0].TheAddress = (CAAM_ADDRESS)ctx;
buf[0].Length = ctxSz;
buf[idx].BufferType = DataBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)ctx;
buf[idx].Length = ctxSz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[0].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
/* add any potential left overs */
buf[1].BufferType = DataBuffer | LastBuffer;
buf[1].TheAddress = (CAAM_ADDRESS)in;
buf[1].Length = inSz;
buf[idx].BufferType = DataBuffer | LastBuffer;
buf[idx].TheAddress = (CAAM_ADDRESS)in;
buf[idx].Length = inSz;
#if defined(__INTEGRITY) || defined(INTEGRITY)
buf[1].Transferred = 0;
buf[idx].Transferred = 0;
#endif
idx++;
arg[0] = CAAM_ALG_FINAL;
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");
return ret;
}

View File

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

View File

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

View File

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