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);
0 commit comments