Skip to content

Commit 9881edf

Browse files
ZackLabPCZackLabPC
authored andcommitted
Crypto Callback Support for ARM ASM: AES-ECB/CBC, SHA-1/256/384/512 + Fix SP SHA CB Bug
1 parent 1cb324a commit 9881edf

16 files changed

Lines changed: 588 additions & 69 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
8484

8585
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
8686
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
87-
#ifdef WOLF_CRYPTO_CB
87+
#ifdef MAX3266X_CB
8888
/* Revert back to SW so HW CB works */
8989
/* HW only works for AES: ECB, CBC, and partial via ECB for other modes */
9090
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
@@ -4168,9 +4168,6 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
41684168
unsigned int i = 0;
41694169

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

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

46204614
#ifndef WC_AES_BITSLICED
46214615
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \

wolfcrypt/src/cryptocb.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
#ifdef WOLFSSL_CAAM
5656
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
5757
#endif
58-
59-
#if defined (WOLFSSL_MAX3266X) || defined (WOLFSSL_MAX3266X_OLD)
60-
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
61-
#endif
62-
6358
/* TODO: Consider linked list with mutex */
6459
#ifndef MAX_CRYPTO_DEVID_CALLBACKS
6560
#define MAX_CRYPTO_DEVID_CALLBACKS 8

wolfcrypt/src/port/arm/armv8-aes.c

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
#endif
4545
#endif
4646

47+
#ifdef WOLF_CRYPTO_CB
48+
#include <wolfssl/wolfcrypt/cryptocb.h>
49+
50+
/* Enable Hardware Callback */
51+
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
52+
/* Revert back to SW so HW CB works */
53+
/* HW only works for AES: ECB, CBC, and partial via ECB for other modes */
54+
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
55+
#endif
56+
#endif
57+
4758
#include <wolfssl/wolfcrypt/aes.h>
4859
#include <wolfssl/wolfcrypt/logging.h>
4960

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

14942+
#ifdef WOLF_CRYPTO_CB
14943+
#ifndef WOLF_CRYPTO_CB_FIND
14944+
if (aes->devId != INVALID_DEVID)
14945+
#endif
14946+
{
14947+
int crypto_cb_ret =
14948+
wc_CryptoCb_AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz,
14949+
authTag, authTagSz, authIn, authInSz);
14950+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
14951+
return crypto_cb_ret;
14952+
/* fall-through when unavailable */
14953+
}
14954+
#endif
14955+
1493114956
XMEMCPY(B+1, nonce, nonceSz);
1493214957
lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
1493314958
B[0] = (authInSz > 0 ? 64 : 0)
@@ -15000,6 +15025,20 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
1500015025
return BAD_FUNC_ARG;
1500115026
}
1500215027

15028+
#ifdef WOLF_CRYPTO_CB
15029+
#ifndef WOLF_CRYPTO_CB_FIND
15030+
if (aes->devId != INVALID_DEVID)
15031+
#endif
15032+
{
15033+
int crypto_cb_ret =
15034+
wc_CryptoCb_AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz,
15035+
authTag, authTagSz, authIn, authInSz);
15036+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
15037+
return crypto_cb_ret;
15038+
/* fall-through when unavailable */
15039+
}
15040+
#endif
15041+
1500315042
o = out;
1500415043
oSz = inSz;
1500515044
XMEMCPY(B+1, nonce, nonceSz);
@@ -16534,7 +16573,14 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
1653416573
return BAD_FUNC_ARG;
1653516574
}
1653616575
#endif
16537-
16576+
#ifdef WOLF_CRYPTO_CB
16577+
if (aes->devId != INVALID_DEVID) {
16578+
if (keylen > sizeof(aes->devKey)) {
16579+
return BAD_FUNC_ARG;
16580+
}
16581+
XMEMCPY(aes->devKey, userKey, keylen);
16582+
}
16583+
#endif
1653816584
#ifdef WOLFSSL_AES_COUNTER
1653916585
aes->left = 0;
1654016586
#endif /* WOLFSSL_AES_COUNTER */
@@ -16584,6 +16630,20 @@ static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
1658416630
return KEYUSAGE_E;
1658516631
}
1658616632

16633+
#ifdef MAX3266X_CB /* Can do a basic ECB block */
16634+
#ifndef WOLF_CRYPTO_CB_FIND
16635+
if (aes->devId != INVALID_DEVID)
16636+
#endif
16637+
{
16638+
int ret_cb = wc_CryptoCb_AesEcbEncrypt(aes, outBlock, inBlock,
16639+
AES_BLOCK_SIZE);
16640+
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
16641+
return ret_cb;
16642+
}
16643+
/* fall-through when unavailable */
16644+
}
16645+
#endif
16646+
1658716647
AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE,
1658816648
(const unsigned char*)aes->key, aes->rounds);
1658916649
return 0;
@@ -16598,6 +16658,19 @@ static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
1659816658
return KEYUSAGE_E;
1659916659
}
1660016660

16661+
#ifdef MAX3266X_CB /* Can do a basic ECB block */
16662+
#ifndef WOLF_CRYPTO_CB_FIND
16663+
if (aes->devId != INVALID_DEVID)
16664+
#endif
16665+
{
16666+
int ret_cb = wc_CryptoCb_AesEcbDecrypt(aes, outBlock, inBlock,
16667+
AES_BLOCK_SIZE);
16668+
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
16669+
return ret_cb;
16670+
/* fall-through when unavailable */
16671+
}
16672+
#endif
16673+
1660116674
AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE,
1660216675
(const unsigned char*)aes->key, aes->rounds);
1660316676
return 0;
@@ -16652,6 +16725,18 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1665216725
#endif
1665316726
}
1665416727

16728+
#ifdef WOLF_CRYPTO_CB
16729+
#ifndef WOLF_CRYPTO_CB_FIND
16730+
if (aes->devId != INVALID_DEVID)
16731+
#endif
16732+
{
16733+
int crypto_cb_ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz);
16734+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
16735+
return crypto_cb_ret;
16736+
/* fall-through when unavailable */
16737+
}
16738+
#endif
16739+
1665516740
AES_CBC_encrypt(in, out, sz, (const unsigned char*)aes->key, aes->rounds,
1665616741
(unsigned char*)aes->reg);
1665716742

@@ -16681,6 +16766,18 @@ int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1668116766
#endif
1668216767
}
1668316768

16769+
#ifdef WOLF_CRYPTO_CB
16770+
#ifndef WOLF_CRYPTO_CB_FIND
16771+
if (aes->devId != INVALID_DEVID)
16772+
#endif
16773+
{
16774+
int crypto_cb_ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz);
16775+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
16776+
return crypto_cb_ret;
16777+
/* fall-through when unavailable */
16778+
}
16779+
#endif
16780+
1668416781
AES_CBC_decrypt(in, out, sz, (const unsigned char*)aes->key, aes->rounds,
1668516782
(unsigned char*)aes->reg);
1668616783

@@ -16703,6 +16800,18 @@ int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
1670316800
WOLFSSL_ERROR_VERBOSE(KEYUSAGE_E);
1670416801
return KEYUSAGE_E;
1670516802
}
16803+
#ifdef WOLF_CRYPTO_CB
16804+
#ifndef WOLF_CRYPTO_CB_FIND
16805+
if (aes->devId != INVALID_DEVID)
16806+
#endif
16807+
{
16808+
int crypto_cb_ret = wc_CryptoCb_AesCtrEncrypt(aes, out, in, sz);
16809+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
16810+
return crypto_cb_ret;
16811+
/* fall-through when unavailable */
16812+
}
16813+
#endif
16814+
1670616815

1670716816
tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
1670816817
/* consume any unused bytes left in aes->tmp */
@@ -17080,6 +17189,13 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
1708017189
return BAD_FUNC_ARG;
1708117190
}
1708217191

17192+
17193+
#ifdef WOLF_CRYPTO_CB
17194+
if (aes->devId != INVALID_DEVID) {
17195+
XMEMCPY(aes->devKey, key, len);
17196+
}
17197+
#endif
17198+
1708317199
XMEMSET(iv, 0, AES_BLOCK_SIZE);
1708417200
ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);
1708517201

@@ -17241,6 +17357,20 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1724117357
return KEYUSAGE_E;
1724217358
}
1724317359

17360+
#ifdef WOLF_CRYPTO_CB
17361+
#ifndef WOLF_CRYPTO_CB_FIND
17362+
if (aes->devId != INVALID_DEVID)
17363+
#endif
17364+
{
17365+
int crypto_cb_ret =
17366+
wc_CryptoCb_AesGcmEncrypt(aes, out, in, sz, iv, ivSz, authTag,
17367+
authTagSz, authIn, authInSz);
17368+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
17369+
return crypto_cb_ret;
17370+
/* fall-through when unavailable */
17371+
}
17372+
#endif
17373+
1724417374
XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
1724517375
if (ivSz == GCM_NONCE_MID_SZ) {
1724617376
XMEMCPY(initialCounter, iv, ivSz);
@@ -17329,6 +17459,20 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1732917459
return BAD_FUNC_ARG;
1733017460
}
1733117461

17462+
#ifdef WOLF_CRYPTO_CB
17463+
#ifndef WOLF_CRYPTO_CB_FIND
17464+
if (aes->devId != INVALID_DEVID)
17465+
#endif
17466+
{
17467+
int crypto_cb_ret =
17468+
wc_CryptoCb_AesGcmDecrypt(aes, out, in, sz, iv, ivSz,
17469+
authTag, authTagSz, authIn, authInSz);
17470+
if (crypto_cb_ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
17471+
return crypto_cb_ret;
17472+
/* fall-through when unavailable */
17473+
}
17474+
#endif
17475+
1733217476
XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
1733317477
if (ivSz == GCM_NONCE_MID_SZ) {
1733417478
XMEMCPY(initialCounter, iv, ivSz);

wolfcrypt/src/port/arm/armv8-sha256.c

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#include <wolfcrypt/src/misc.c>
5858
#endif
5959

60+
#ifdef WOLF_CRYPTO_CB
61+
#include <wolfssl/wolfcrypt/cryptocb.h>
62+
#endif
63+
6064
#if defined(FREESCALE_MMCAU_SHA)
6165
#ifdef FREESCALE_MMCAU_CLASSIC_SHA
6266
#include "cau_api.h"
@@ -1513,25 +1517,44 @@ static WC_INLINE int Sha256Final(wc_Sha256* sha256, byte* hash)
15131517

15141518
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
15151519
{
1520+
int ret = 0;
15161521
if (sha256 == NULL)
15171522
return BAD_FUNC_ARG;
1523+
ret = InitSha256(sha256);
1524+
if (ret != 0)
1525+
return ret;
15181526

15191527
sha256->heap = heap;
15201528
#ifdef WOLF_CRYPTO_CB
15211529
sha256->devId = devId;
1530+
sha256->devCtx = NULL;
15221531
#endif
1523-
(void)devId;
15241532

1525-
return InitSha256(sha256);
1533+
#ifdef MAX3266X_SHA_CB
1534+
ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx));
1535+
if (ret != 0) {
1536+
return ret;
1537+
}
1538+
#endif
1539+
(void)devId;
1540+
return ret;
15261541
}
15271542

15281543
int wc_InitSha256(wc_Sha256* sha256)
15291544
{
1530-
return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
1545+
int devId = INVALID_DEVID;
1546+
1547+
#ifdef WOLF_CRYPTO_CB
1548+
devId = wc_CryptoCb_DefaultDevID();
1549+
#endif
1550+
return wc_InitSha256_ex(sha256, NULL, devId);
15311551
}
15321552

15331553
void wc_Sha256Free(wc_Sha256* sha256)
15341554
{
1555+
#ifdef MAX3266X_SHA_CB
1556+
wc_MXC_TPU_SHA_Free(&(sha256->mxcCtx));
1557+
#endif
15351558
(void)sha256;
15361559
}
15371560

@@ -1541,6 +1564,18 @@ int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
15411564
return BAD_FUNC_ARG;
15421565
}
15431566

1567+
#ifdef WOLF_CRYPTO_CB
1568+
#ifndef WOLF_CRYPTO_CB_FIND
1569+
if (sha256->devId != INVALID_DEVID)
1570+
#endif
1571+
{
1572+
int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
1573+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1574+
return ret;
1575+
/* fall-through when unavailable */
1576+
}
1577+
#endif
1578+
15441579
return Sha256Update(sha256, data, len);
15451580
}
15461581

@@ -1573,6 +1608,18 @@ int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
15731608
return BAD_FUNC_ARG;
15741609
}
15751610

1611+
#ifdef WOLF_CRYPTO_CB
1612+
#ifndef WOLF_CRYPTO_CB_FIND
1613+
if (sha256->devId != INVALID_DEVID)
1614+
#endif
1615+
{
1616+
ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
1617+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1618+
return ret;
1619+
/* fall-through when unavailable */
1620+
}
1621+
#endif
1622+
15761623
ret = Sha256Final(sha256, hash);
15771624
if (ret != 0)
15781625
return ret;
@@ -1621,6 +1668,13 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
16211668

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

1671+
#ifdef MAX3266X_SHA_CB
1672+
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
1673+
if (ret != 0) {
1674+
return ret;
1675+
}
1676+
#endif
1677+
16241678
return ret;
16251679
}
16261680

0 commit comments

Comments
 (0)