Skip to content

Commit 554d52b

Browse files
Merge pull request #7777 from night1rider/MAX32666-port
MAX32665 and MAX32666 TPU HW and ARM ASM Crypto Callback Support
2 parents 9781c1f + 1ffcf40 commit 554d52b

24 files changed

Lines changed: 3179 additions & 39 deletions

wolfcrypt/benchmark/benchmark.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@
220220
#ifdef HAVE_RENESAS_SYNC
221221
#include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
222222
#endif
223+
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
224+
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
225+
#endif
223226
#endif
224227

225228
#ifdef WOLFSSL_ASYNC_CRYPT
@@ -3167,8 +3170,9 @@ static void* benchmarks_do(void* args)
31673170
#endif
31683171
#if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
31693172
defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
3170-
defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) && \
3171-
!defined(NO_HW_BENCH)
3173+
defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) || \
3174+
((defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)) && \
3175+
defined(WOLF_CRYPTO_CB)) && !defined(NO_HW_BENCH)
31723176
bench_aes_aad_options_wrap(bench_aesgcm, 1);
31733177
#endif
31743178
#ifndef NO_SW_BENCH
@@ -14254,6 +14258,15 @@ void bench_sphincsKeySign(byte level, byte optim)
1425414258
return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
1425514259
}
1425614260

14261+
#elif (defined(WOLFSSL_MAX3266X_OLD) || defined(WOLFSSL_MAX3266X)) \
14262+
&& defined(MAX3266X_RTC)
14263+
14264+
double current_time(int reset)
14265+
{
14266+
(void)reset;
14267+
return wc_MXC_RTC_Time();
14268+
}
14269+
1425714270
#elif defined(FREESCALE_KSDK_BM)
1425814271

1425914272
double current_time(int reset)

wolfcrypt/src/aes.c

Lines changed: 200 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
8282
#include <wolfssl/wolfcrypt/port/psa/psa.h>
8383
#endif
8484

85+
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
86+
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
87+
#ifdef MAX3266X_CB
88+
/* Revert back to SW so HW CB works */
89+
/* HW only works for AES: ECB, CBC, and partial via ECB for other modes */
90+
#include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
91+
/* Turn off MAX3266X_AES in the context of this file when using CB */
92+
#undef MAX3266X_AES
93+
#endif
94+
#endif
95+
8596
#if defined(WOLFSSL_TI_CRYPT)
8697
#include <wolfcrypt/src/port/ti/ti-aes.c>
8798
#else
@@ -2205,7 +2216,8 @@ static void AesEncrypt_C(Aes* aes, const byte* inBlock, byte* outBlock,
22052216
}
22062217

22072218
#if defined(HAVE_AES_ECB) && !(defined(WOLFSSL_IMX6_CAAM) && \
2208-
!defined(NO_IMX6_CAAM_AES) && !defined(WOLFSSL_QNX_CAAM))
2219+
!defined(NO_IMX6_CAAM_AES) && !defined(WOLFSSL_QNX_CAAM)) && \
2220+
!defined(MAX3266X_AES)
22092221
/* Encrypt a number of blocks using AES.
22102222
*
22112223
* @param [in] aes AES object.
@@ -2789,6 +2801,12 @@ extern void AesEncryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz);
27892801
static WARN_UNUSED_RESULT int wc_AesEncrypt(
27902802
Aes* aes, const byte* inBlock, byte* outBlock)
27912803
{
2804+
#if defined(MAX3266X_AES)
2805+
word32 keySize;
2806+
#endif
2807+
#if defined(MAX3266X_CB)
2808+
int ret_cb;
2809+
#endif
27922810
word32 r;
27932811

27942812
if (aes == NULL) {
@@ -2892,6 +2910,26 @@ static WARN_UNUSED_RESULT int wc_AesEncrypt(
28922910
}
28932911
#endif
28942912

2913+
#if defined(MAX3266X_AES)
2914+
if (wc_AesGetKeySize(aes, &keySize) == 0) {
2915+
return wc_MXC_TPU_AesEncrypt(inBlock, (byte*)aes->reg, (byte*)aes->key,
2916+
MXC_TPU_MODE_ECB, AES_BLOCK_SIZE,
2917+
outBlock, (unsigned int)keySize);
2918+
}
2919+
#endif
2920+
#ifdef MAX3266X_CB /* Can do a basic ECB block */
2921+
#ifndef WOLF_CRYPTO_CB_FIND
2922+
if (aes->devId != INVALID_DEVID)
2923+
#endif
2924+
{
2925+
ret_cb = wc_CryptoCb_AesEcbEncrypt(aes, outBlock, inBlock,
2926+
AES_BLOCK_SIZE);
2927+
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2928+
return ret_cb;
2929+
/* fall-through when unavailable */
2930+
}
2931+
#endif
2932+
28952933
AesEncrypt_C(aes, inBlock, outBlock, r);
28962934

28972935
return 0;
@@ -3172,7 +3210,8 @@ static void AesDecrypt_C(Aes* aes, const byte* inBlock, byte* outBlock,
31723210
}
31733211

31743212
#if defined(HAVE_AES_ECB) && !(defined(WOLFSSL_IMX6_CAAM) && \
3175-
!defined(NO_IMX6_CAAM_AES) && !defined(WOLFSSL_QNX_CAAM))
3213+
!defined(NO_IMX6_CAAM_AES) && !defined(WOLFSSL_QNX_CAAM)) && \
3214+
!defined(MAX3266X_AES)
31763215
/* Decrypt a number of blocks using AES.
31773216
*
31783217
* @param [in] aes AES object.
@@ -3539,6 +3578,12 @@ static void AesDecryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz)
35393578
static WARN_UNUSED_RESULT int wc_AesDecrypt(
35403579
Aes* aes, const byte* inBlock, byte* outBlock)
35413580
{
3581+
#if defined(MAX3266X_AES)
3582+
word32 keySize;
3583+
#endif
3584+
#if defined(MAX3266X_CB)
3585+
int ret_cb;
3586+
#endif
35423587
word32 r;
35433588

35443589
if (aes == NULL) {
@@ -3615,6 +3660,27 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
36153660
} /* else !wc_esp32AesSupportedKeyLen for ESP32 */
36163661
#endif
36173662

3663+
#if defined(MAX3266X_AES)
3664+
if (wc_AesGetKeySize(aes, &keySize) == 0) {
3665+
return wc_MXC_TPU_AesDecrypt(inBlock, (byte*)aes->reg, (byte*)aes->key,
3666+
MXC_TPU_MODE_ECB, AES_BLOCK_SIZE,
3667+
outBlock, (unsigned int)keySize);
3668+
}
3669+
#endif
3670+
3671+
#ifdef MAX3266X_CB /* Can do a basic ECB block */
3672+
#ifndef WOLF_CRYPTO_CB_FIND
3673+
if (aes->devId != INVALID_DEVID)
3674+
#endif
3675+
{
3676+
ret_cb = wc_CryptoCb_AesEcbDecrypt(aes, outBlock, inBlock,
3677+
AES_BLOCK_SIZE);
3678+
if (ret_cb != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3679+
return ret_cb;
3680+
/* fall-through when unavailable */
3681+
}
3682+
#endif
3683+
36183684
AesDecrypt_C(aes, inBlock, outBlock, r);
36193685

36203686
return 0;
@@ -4103,7 +4169,8 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
41034169

41044170
XMEMCPY(rk, key, keySz);
41054171
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
4106-
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES))
4172+
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES)) && \
4173+
!defined(MAX3266X_AES)
41074174
/* Always reverse words when using only SW */
41084175
{
41094176
ByteReverseWords(rk, rk, keySz);
@@ -4250,7 +4317,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
42504317
} /* switch */
42514318
ForceZero(&temp, sizeof(temp));
42524319

4253-
#if defined(HAVE_AES_DECRYPT)
4320+
#if defined(HAVE_AES_DECRYPT) && !defined(MAX3266X_AES)
42544321
if (dir == AES_DECRYPTION) {
42554322
unsigned int j;
42564323

@@ -4546,8 +4613,8 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
45464613

45474614
#ifndef WC_AES_BITSLICED
45484615
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
4549-
(!defined(WOLFSSL_ESP32_CRYPT) || \
4550-
defined(NO_WOLFSSL_ESP32_CRYPT_AES))
4616+
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES)) \
4617+
&& !defined(MAX3266X_AES)
45514618

45524619
/* software */
45534620
ByteReverseWords(aes->key, aes->key, keylen);
@@ -5378,6 +5445,91 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
53785445
}
53795446
#endif /* HAVE_AES_DECRYPT */
53805447

5448+
#elif defined(MAX3266X_AES)
5449+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5450+
{
5451+
word32 keySize;
5452+
int status;
5453+
byte *iv;
5454+
5455+
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
5456+
return BAD_FUNC_ARG;
5457+
}
5458+
5459+
/* Always enforce a length check */
5460+
if (sz % AES_BLOCK_SIZE) {
5461+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5462+
return BAD_LENGTH_E;
5463+
#else
5464+
return BAD_FUNC_ARG;
5465+
#endif
5466+
}
5467+
if (sz == 0) {
5468+
return 0;
5469+
}
5470+
5471+
iv = (byte*)aes->reg;
5472+
status = wc_AesGetKeySize(aes, &keySize);
5473+
if (status != 0) {
5474+
return status;
5475+
}
5476+
5477+
status = wc_MXC_TPU_AesEncrypt(in, iv, (byte*)aes->key,
5478+
MXC_TPU_MODE_CBC, sz, out,
5479+
(unsigned int)keySize);
5480+
/* store iv for next call */
5481+
if (status == 0) {
5482+
XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
5483+
}
5484+
return (status == 0) ? 0 : -1;
5485+
}
5486+
5487+
#ifdef HAVE_AES_DECRYPT
5488+
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5489+
{
5490+
word32 keySize;
5491+
int status;
5492+
byte *iv;
5493+
byte temp_block[AES_BLOCK_SIZE];
5494+
5495+
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
5496+
return BAD_FUNC_ARG;
5497+
}
5498+
5499+
/* Always enforce a length check */
5500+
if (sz % AES_BLOCK_SIZE) {
5501+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5502+
return BAD_LENGTH_E;
5503+
#else
5504+
return BAD_FUNC_ARG;
5505+
#endif
5506+
}
5507+
if (sz == 0) {
5508+
return 0;
5509+
}
5510+
5511+
iv = (byte*)aes->reg;
5512+
status = wc_AesGetKeySize(aes, &keySize);
5513+
if (status != 0) {
5514+
return status;
5515+
}
5516+
5517+
/* get IV for next call */
5518+
XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
5519+
status = wc_MXC_TPU_AesDecrypt(in, iv, (byte*)aes->key,
5520+
MXC_TPU_MODE_CBC, sz, out,
5521+
keySize);
5522+
5523+
/* store iv for next call */
5524+
if (status == 0) {
5525+
XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
5526+
}
5527+
return (status == 0) ? 0 : -1;
5528+
}
5529+
#endif /* HAVE_AES_DECRYPT */
5530+
5531+
5532+
53815533
#elif defined(WOLFSSL_PIC32MZ_CRYPT)
53825534

53835535
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
@@ -11405,6 +11557,48 @@ int wc_AesGetKeySize(Aes* aes, word32* keySize)
1140511557
#elif defined(WOLFSSL_RISCV_ASM)
1140611558
/* implemented in wolfcrypt/src/port/riscv/riscv-64-aes.c */
1140711559

11560+
#elif defined(MAX3266X_AES)
11561+
11562+
int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
11563+
{
11564+
int status;
11565+
word32 keySize;
11566+
11567+
if ((in == NULL) || (out == NULL) || (aes == NULL))
11568+
return BAD_FUNC_ARG;
11569+
11570+
status = wc_AesGetKeySize(aes, &keySize);
11571+
if (status != 0) {
11572+
return status;
11573+
}
11574+
11575+
status = wc_MXC_TPU_AesEncrypt(in, (byte*)aes->reg, (byte*)aes->key,
11576+
MXC_TPU_MODE_ECB, sz, out, keySize);
11577+
11578+
return status;
11579+
}
11580+
11581+
#ifdef HAVE_AES_DECRYPT
11582+
int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
11583+
{
11584+
int status;
11585+
word32 keySize;
11586+
11587+
if ((in == NULL) || (out == NULL) || (aes == NULL))
11588+
return BAD_FUNC_ARG;
11589+
11590+
status = wc_AesGetKeySize(aes, &keySize);
11591+
if (status != 0) {
11592+
return status;
11593+
}
11594+
11595+
status = wc_MXC_TPU_AesDecrypt(in, (byte*)aes->reg, (byte*)aes->key,
11596+
MXC_TPU_MODE_ECB, sz, out, keySize);
11597+
11598+
return status;
11599+
}
11600+
#endif /* HAVE_AES_DECRYPT */
11601+
1140811602
#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
1140911603

1141011604
/* Software AES - ECB */

wolfcrypt/src/cryptocb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
#ifdef WOLFSSL_CAAM
5656
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
5757
#endif
58-
5958
/* TODO: Consider linked list with mutex */
6059
#ifndef MAX_CRYPTO_DEVID_CALLBACKS
6160
#define MAX_CRYPTO_DEVID_CALLBACKS 8

wolfcrypt/src/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \
139139
wolfcrypt/src/port/Renesas/renesas_rx64_hw_util.c \
140140
wolfcrypt/src/port/Renesas/README.md \
141141
wolfcrypt/src/port/cypress/psoc6_crypto.c \
142-
wolfcrypt/src/port/liboqs/liboqs.c
142+
wolfcrypt/src/port/liboqs/liboqs.c \
143+
wolfcrypt/src/port/maxim/max3266x.c
143144

144145
$(ASYNC_FILES):
145146
$(AM_V_at)touch $(srcdir)/$@

0 commit comments

Comments
 (0)