Skip to content

Commit

Permalink
Adding in Callback for ARM ASM: AES-ECB/CBC, SHA-1/256/384/512 and Fi…
Browse files Browse the repository at this point in the history
…xing SP SHA CB Bug
  • Loading branch information
ZackLabPC authored and ZackLabPC committed Sep 13, 2024
1 parent be2079a commit 6fc1b16
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 34 deletions.
139 changes: 138 additions & 1 deletion wolfcrypt/src/port/arm/armv8-aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
#endif
#endif

#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>

/* Enable Hardware Callback */
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
/* Revert back to SW so HW CB works */
/* HW only works for AES: ECB, CBC, and partial via ECB for other modes */
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
#endif
#endif

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/logging.h>

Expand Down Expand Up @@ -14928,6 +14939,20 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret =
wc_CryptoCb_AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz,
authTag, authTagSz, authIn, authInSz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

XMEMCPY(B+1, nonce, nonceSz);
lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
B[0] = (authInSz > 0 ? 64 : 0)
Expand Down Expand Up @@ -15000,6 +15025,20 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret =
wc_CryptoCb_AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz,
authTag, authTagSz, authIn, authInSz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

o = out;
oSz = inSz;
XMEMCPY(B+1, nonce, nonceSz);
Expand Down Expand Up @@ -16534,7 +16573,7 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
return BAD_FUNC_ARG;
}
#endif

XMEMCPY(aes->cb_key, userKey, keylen);
#ifdef WOLFSSL_AES_COUNTER
aes->left = 0;
#endif /* WOLFSSL_AES_COUNTER */
Expand Down Expand Up @@ -16584,6 +16623,20 @@ static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
return KEYUSAGE_E;
}

#ifdef MAX3266X_CB /* Can do a basic ECB block */
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int ret_cb = wc_CryptoCb_AesEcbEncrypt(aes, outBlock, inBlock,
AES_BLOCK_SIZE);
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret_cb;
}
/* fall-through when unavailable */
}
#endif

AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE,
(const unsigned char*)aes->key, aes->rounds);
return 0;
Expand All @@ -16598,6 +16651,19 @@ static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
return KEYUSAGE_E;
}

#ifdef MAX3266X_CB /* Can do a basic ECB block */
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int ret_cb = wc_CryptoCb_AesEcbDecrypt(aes, outBlock, inBlock,
AES_BLOCK_SIZE);
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret_cb;
/* fall-through when unavailable */
}
#endif

AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE,
(const unsigned char*)aes->key, aes->rounds);
return 0;
Expand Down Expand Up @@ -16652,6 +16718,18 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
#endif
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

AES_CBC_encrypt(in, out, sz, (const unsigned char*)aes->key, aes->rounds,
(unsigned char*)aes->reg);

Expand Down Expand Up @@ -16681,6 +16759,18 @@ int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
#endif
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

AES_CBC_decrypt(in, out, sz, (const unsigned char*)aes->key, aes->rounds,
(unsigned char*)aes->reg);

Expand All @@ -16703,6 +16793,18 @@ int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
WOLFSSL_ERROR_VERBOSE(KEYUSAGE_E);
return KEYUSAGE_E;
}
#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret = wc_CryptoCb_AesCtrEncrypt(aes, out, in, sz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif


tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
/* consume any unused bytes left in aes->tmp */
Expand Down Expand Up @@ -17080,6 +17182,13 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
return BAD_FUNC_ARG;
}


#ifdef WOLF_CRYPTO_CB
if (aes->devId != INVALID_DEVID) {
XMEMCPY(aes->devKey, key, len);
}
#endif

XMEMSET(iv, 0, AES_BLOCK_SIZE);
ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);

Expand Down Expand Up @@ -17241,6 +17350,20 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
return KEYUSAGE_E;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret =
wc_CryptoCb_AesGcmEncrypt(aes, out, in, sz, iv, ivSz, authTag,
authTagSz, authIn, authInSz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
if (ivSz == GCM_NONCE_MID_SZ) {
XMEMCPY(initialCounter, iv, ivSz);
Expand Down Expand Up @@ -17329,6 +17452,20 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (aes->devId != INVALID_DEVID)
#endif
{
int crypto_cb_ret =
wc_CryptoCb_AesGcmDecrypt(aes, out, in, sz, iv, ivSz,
authTag, authTagSz, authIn, authInSz);
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return crypto_cb_ret;
/* fall-through when unavailable */
}
#endif

XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
if (ivSz == GCM_NONCE_MID_SZ) {
XMEMCPY(initialCounter, iv, ivSz);
Expand Down
68 changes: 65 additions & 3 deletions wolfcrypt/src/port/arm/armv8-sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include <wolfcrypt/src/misc.c>
#endif

#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif

#if defined(FREESCALE_MMCAU_SHA)
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
#include "cau_api.h"
Expand Down Expand Up @@ -121,6 +125,10 @@ static int InitSha256(wc_Sha256* sha256)
sha256->flags = 0;
#endif

#ifdef WOLF_CRYPTO_CB
sha256->devId = wc_CryptoCb_DefaultDevID();
#endif

return ret;
}

Expand Down Expand Up @@ -1513,25 +1521,44 @@ static WC_INLINE int Sha256Final(wc_Sha256* sha256, byte* hash)

int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
{
int ret = 0;
if (sha256 == NULL)
return BAD_FUNC_ARG;
ret = InitSha256(sha256);
if (ret != 0)
return ret;

sha256->heap = heap;
#ifdef WOLF_CRYPTO_CB
sha256->devId = devId;
sha256->devCtx = NULL;
#endif
(void)devId;

return InitSha256(sha256);
#ifdef MAX3266X_SHA_CB
ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx));
if (ret != 0) {
return ret;
}
#endif
(void)devId;
return ret;
}

int wc_InitSha256(wc_Sha256* sha256)
{
return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
int devId = INVALID_DEVID;

#ifdef WOLF_CRYPTO_CB
devId = wc_CryptoCb_DefaultDevID();
#endif
return wc_InitSha256_ex(sha256, NULL, devId);
}

void wc_Sha256Free(wc_Sha256* sha256)
{
#ifdef MAX3266X_SHA_CB
wc_MXC_TPU_SHA_Free(&(sha256->mxcCtx));
#endif
(void)sha256;
}

Expand All @@ -1541,6 +1568,18 @@ int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (sha256->devId != INVALID_DEVID)
#endif
{
int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret;
/* fall-through when unavailable */
}
#endif

return Sha256Update(sha256, data, len);
}

Expand Down Expand Up @@ -1573,11 +1612,27 @@ int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (sha256->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret;
/* fall-through when unavailable */
}
#endif

ret = Sha256Final(sha256, hash);
if (ret != 0)
return ret;

#ifdef WOLF_CRYPTO_CB /* Use to reset state but keep devId info */
return wc_InitSha256_ex(sha256, sha256->heap, sha256->devId);
#else
return InitSha256(sha256); /* reset state */
#endif
}

int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
Expand Down Expand Up @@ -1621,6 +1676,13 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)

XMEMCPY(dst, src, sizeof(wc_Sha256));

#ifdef MAX3266X_SHA_CB
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
if (ret != 0) {
return ret;
}
#endif

return ret;
}

Expand Down
Loading

0 comments on commit 6fc1b16

Please sign in to comment.