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 night1rider committed Sep 19, 2024
1 parent be2079a commit 3a8578f
Show file tree
Hide file tree
Showing 16 changed files with 588 additions and 69 deletions.
8 changes: 1 addition & 7 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits

#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
#ifdef WOLF_CRYPTO_CB
#ifdef MAX3266X_CB
/* 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>
Expand Down Expand Up @@ -4168,9 +4168,6 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
unsigned int i = 0;

XMEMCPY(rk, key, keySz);
#ifdef MAX3266X_CB /* Copies needed values to use later if CB is used */
XMEMCPY(aes->cb_key, key, keySz);
#endif
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES)) && \
!defined(MAX3266X_AES)
Expand Down Expand Up @@ -4613,9 +4610,6 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
#endif

XMEMCPY(aes->key, userKey, keylen);
#ifdef MAX3266X_CB /* Copy Key for CB for use later if needed */
XMEMCMP(aes->cb_key, userKey, keylen);
#endif

#ifndef WC_AES_BITSLICED
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
Expand Down
5 changes: 0 additions & 5 deletions wolfcrypt/src/cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
#ifdef WOLFSSL_CAAM
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#endif

#if defined (WOLFSSL_MAX3266X) || defined (WOLFSSL_MAX3266X_OLD)
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
#endif

/* TODO: Consider linked list with mutex */
#ifndef MAX_CRYPTO_DEVID_CALLBACKS
#define MAX_CRYPTO_DEVID_CALLBACKS 8
Expand Down
146 changes: 145 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,14 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
return BAD_FUNC_ARG;
}
#endif

#ifdef WOLF_CRYPTO_CB
if (aes->devId != INVALID_DEVID) {
if (keylen > sizeof(aes->devKey)) {
return BAD_FUNC_ARG;
}
XMEMCPY(aes->devKey, userKey, keylen);
}
#endif
#ifdef WOLFSSL_AES_COUNTER
aes->left = 0;
#endif /* WOLFSSL_AES_COUNTER */
Expand Down Expand Up @@ -16584,6 +16630,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 +16658,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 +16725,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 +16766,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 +16800,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 +17189,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 +17357,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 +17459,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
60 changes: 57 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 @@ -1513,25 +1517,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 +1564,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,6 +1608,18 @@ 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;
Expand Down Expand Up @@ -1621,6 +1668,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 3a8578f

Please sign in to comment.