Skip to content

Commit 2e8cf39

Browse files
msi-debianZackLabPC
authored andcommitted
Initial PR for MAX32665 and MAX32666 TPU HW Support
1 parent d796d8c commit 2e8cf39

15 files changed

Lines changed: 1493 additions & 16 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14254,6 +14254,14 @@ void bench_sphincsKeySign(byte level, byte optim)
1425414254
return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
1425514255
}
1425614256

14257+
#elif (defined(WOLFSSL_MAX3266X_OLD) || defined(WOLFSSL_MAX3266X)) \
14258+
&& defined(MAX3266X_RTC)
14259+
14260+
double current_time(int reset)
14261+
{
14262+
return wc_MXC_RTC_Time();
14263+
}
14264+
1425714265
#elif defined(FREESCALE_KSDK_BM)
1425814266

1425914267
double current_time(int reset)

wolfcrypt/src/aes.c

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ 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+
#endif
88+
8589
#if defined(WOLFSSL_TI_CRYPT)
8690
#include <wolfcrypt/src/port/ti/ti-aes.c>
8791
#else
@@ -2789,6 +2793,9 @@ extern void AesEncryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz);
27892793
static WARN_UNUSED_RESULT int wc_AesEncrypt(
27902794
Aes* aes, const byte* inBlock, byte* outBlock)
27912795
{
2796+
#if defined(MAX3266X_AES)
2797+
word32 keySize;
2798+
#endif
27922799
word32 r;
27932800

27942801
if (aes == NULL) {
@@ -2892,6 +2899,14 @@ static WARN_UNUSED_RESULT int wc_AesEncrypt(
28922899
}
28932900
#endif
28942901

2902+
#if defined(MAX3266X_AES)
2903+
if (wc_AesGetKeySize(aes, &keySize) == 0) {
2904+
return wc_MXC_TPU_AesEncrypt(inBlock, (byte*)aes->reg, (byte*)aes->key,
2905+
MXC_TPU_MODE_ECB, AES_BLOCK_SIZE,
2906+
outBlock, (unsigned int)keySize);
2907+
}
2908+
#endif
2909+
28952910
AesEncrypt_C(aes, inBlock, outBlock, r);
28962911

28972912
return 0;
@@ -3539,6 +3554,9 @@ static void AesDecryptBlocks_C(Aes* aes, const byte* in, byte* out, word32 sz)
35393554
static WARN_UNUSED_RESULT int wc_AesDecrypt(
35403555
Aes* aes, const byte* inBlock, byte* outBlock)
35413556
{
3557+
#if defined(MAX3266X_AES)
3558+
word32 keySize;
3559+
#endif
35423560
word32 r;
35433561

35443562
if (aes == NULL) {
@@ -3615,6 +3633,14 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
36153633
} /* else !wc_esp32AesSupportedKeyLen for ESP32 */
36163634
#endif
36173635

3636+
#if defined(MAX3266X_AES)
3637+
if (wc_AesGetKeySize(aes, &keySize) == 0) {
3638+
return wc_MXC_TPU_AesDecrypt(inBlock, (byte*)aes->reg, (byte*)aes->key,
3639+
MXC_TPU_MODE_ECB, AES_BLOCK_SIZE,
3640+
outBlock, (unsigned int)keySize);
3641+
}
3642+
#endif
3643+
36183644
AesDecrypt_C(aes, inBlock, outBlock, r);
36193645

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

41044130
XMEMCPY(rk, key, keySz);
41054131
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
4106-
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES))
4132+
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES)) && \
4133+
!defined(MAX3266X_AES)
41074134
/* Always reverse words when using only SW */
41084135
{
41094136
ByteReverseWords(rk, rk, keySz);
@@ -4250,7 +4277,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
42504277
} /* switch */
42514278
ForceZero(&temp, sizeof(temp));
42524279

4253-
#if defined(HAVE_AES_DECRYPT)
4280+
#if defined(HAVE_AES_DECRYPT) && !defined(MAX3266X_AES)
42544281
if (dir == AES_DECRYPTION) {
42554282
unsigned int j;
42564283

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

45474574
#ifndef WC_AES_BITSLICED
45484575
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
4549-
(!defined(WOLFSSL_ESP32_CRYPT) || \
4550-
defined(NO_WOLFSSL_ESP32_CRYPT_AES))
4576+
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES)) \
4577+
&& !defined(MAX3266X_AES)
45514578

45524579
/* software */
45534580
ByteReverseWords(aes->key, aes->key, keylen);
@@ -5378,6 +5405,82 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
53785405
}
53795406
#endif /* HAVE_AES_DECRYPT */
53805407

5408+
#elif defined(MAX3266X_AES)
5409+
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5410+
{
5411+
word32 keySize;
5412+
int status;
5413+
byte *iv;
5414+
5415+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5416+
if (sz % AES_BLOCK_SIZE) {
5417+
return BAD_LENGTH_E;
5418+
}
5419+
#endif
5420+
if (sz == 0)
5421+
return 0;
5422+
5423+
iv = (byte*)aes->reg;
5424+
5425+
status = wc_AesGetKeySize(aes, &keySize);
5426+
if (status != 0) {
5427+
return status;
5428+
}
5429+
5430+
status = wc_MXC_TPU_AesEncrypt(in, iv, (byte*)aes->key,
5431+
MXC_TPU_MODE_CBC, sz, out,
5432+
(unsigned int)keySize);
5433+
5434+
/* store iv for next call */
5435+
if (status == 0) {
5436+
XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
5437+
}
5438+
5439+
return (status == 0) ? 0 : -1;
5440+
}
5441+
5442+
#ifdef HAVE_AES_DECRYPT
5443+
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
5444+
{
5445+
word32 keySize;
5446+
int status;
5447+
byte *iv;
5448+
byte temp_block[AES_BLOCK_SIZE];
5449+
5450+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5451+
if (sz % AES_BLOCK_SIZE) {
5452+
return BAD_LENGTH_E;
5453+
}
5454+
#endif
5455+
if (sz == 0)
5456+
return 0;
5457+
5458+
iv = (byte*)aes->reg;
5459+
5460+
status = wc_AesGetKeySize(aes, &keySize);
5461+
if (status != 0) {
5462+
return status;
5463+
}
5464+
5465+
/* get IV for next call */
5466+
XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
5467+
5468+
status = wc_MXC_TPU_AesDecrypt(in, iv, (byte*)aes->key,
5469+
MXC_TPU_MODE_CBC, sz, out,
5470+
keySize);
5471+
5472+
5473+
/* store iv for next call */
5474+
if (status == 0) {
5475+
XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
5476+
}
5477+
5478+
return (status == 0) ? 0 : -1;
5479+
}
5480+
#endif /* HAVE_AES_DECRYPT */
5481+
5482+
5483+
53815484
#elif defined(WOLFSSL_PIC32MZ_CRYPT)
53825485

53835486
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)

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)/$@

wolfcrypt/src/port/maxim/README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,113 @@
1-
wolfSSL using Analog Devices MAXQ1065 or MAX1080
1+
wolfSSL using Analog Devices MAXQ1065, MAX1080, MAX32665 or MAX32666
22
================================================
33

44
## Overview
55

66
wolfSSL can be configured to use the MAXQ1065 or MAX1080 cryptographic
7-
controllers. Product datasheets, user guides and other resources can be found at
7+
controllers. wolfSSL can also be configure to utilize the TPU
8+
(crypto accelerator), MAA (math accelerator), and TRNG available on select
9+
MAX32665 and MAX32666 microcontrollers.
10+
11+
Product datasheets, user guides and other resources can be found at
812
Analog Devices website:
913

1014
https://www.analog.com
1115

16+
# MAX32665/MAX32666
17+
## Build and Usage
18+
19+
wolfSSL supports the [Maxim SDK](https://github.com/analogdevicesinc/msdk), to
20+
utilize the TPU and MAA located on the devices.
21+
22+
Building is supported by adding `#define WOLFSSL_MAX3266X` to `user_settings.h`.
23+
wolfSSL supports the usage of the older style API Maxim provides with the
24+
`#define WOLFSSL_MAX3266X_OLD` to `user_settings.h`.
25+
26+
When using `WOLFSSL_MAX3266X` or `WOLFSSL_MAX3266X_OLD` you will also need to
27+
add `#define WOLFSSL_SP_MATH_ALL` to `user_settings.h`.
28+
29+
If you want to be more specific on what hardware acceleration you want to use,
30+
this can be done by adding any combination of these defines:
31+
```
32+
#define MAX3266X_RNG - Allows usage of TRNG device
33+
#define MAX3266X_AES - Allows usage of TPU for AES Acceleration
34+
#define MAX3266X_SHA - Allows usage of TPU for Hash Acceleration
35+
#define MAX3266X_MATH - Allows usage of MAA for MOD based Math Acceleration
36+
```
37+
For this you will still need to use `#define WOLFSSL_MAX3266X` or `#define WOLFSSL_MAX3266X_OLD`. When you use a specific hardware define like
38+
`#define MAX3266X_RNG` this will mean only the TRNG device is being used, and
39+
all other operations will use the default software implementations.
40+
41+
The other prerequisite is that a change needs to be made to the Maxim SDK. This
42+
is to use the MAA Math Accelerator, this change only needs to be made if you are
43+
using `#define WOLFSSL_MAX3266X` or `define WOLFSSL_MAX3266X_OLD` by themselves
44+
or you are specifing `#define MAX3266X_MATH`.
45+
46+
In the SDK you will need to find the underlying function that
47+
`MXC_TPU_MAA_Compute()` from `tpu.h` compute calls in the newer SDK. In the
48+
older SDK this function is called `MAA_Compute()` in `maa.h`. In the underlying
49+
function you will need to change this error check:
50+
51+
```
52+
// Check that we're performing a valid operation
53+
if (clc >= 0x6) {
54+
return E_INVALID;
55+
}
56+
```
57+
to
58+
```
59+
// Check that we're performing a valid operation
60+
if (clc >= 0b1111) {
61+
return E_INVALID;
62+
}
63+
```
64+
65+
This bug has been reported to Analog Devices
66+
[here](https://github.com/analogdevicesinc/msdk/issues/1089)
67+
if you want to know more details on the issue.
68+
69+
70+
## Supported Algos
71+
Using these defines will replace software implentations with a call to the
72+
hardware.
73+
74+
`#define MAX3266X_RNG`
75+
- Uses entropy from TRNG to seed HASHDRBG
76+
77+
`#define MAX3266X_AES`:
78+
79+
- AES-CBC: 128, 192, 256
80+
- AES-ECB: 128, 192, 256
81+
82+
`#define MAX3266X_SHA`:
83+
84+
- SHA-256
85+
86+
`#define MAX3266X_MATH` (Replaces math operation calls for algos
87+
like RSA and ECC key generation):
88+
89+
- mod - `a mod m = r`
90+
- addmod - `(a+b)mod m = r`
91+
- submod - `(a-b)mod m = r`
92+
- mulmod - `(a*b)mod m = r`
93+
- sqrmod - `(b^2)mod m = r`
94+
- exptmod - `(b^e)mod m = r`
95+
96+
## Extra Information
97+
For more Verbose info you can use `#define DEBUG_WOLFSSL` in combination with
98+
`#define MAX3266X_VERBOSE` to see if errors are occuring during the hardware
99+
setup/
100+
101+
To reproduce benchmark numbers you can use `#define MAX3266X_RTC`.
102+
Do note that this will only work with `#define WOLFSSL_MAX3266X` and not
103+
`#define WOLFSSL_MAX3266X_OLD`. This is only meant for benchmark reproduction
104+
and not for any other application. Please implement your own rtc/time code for
105+
anything else.
106+
107+
For more infromation about the TPU, MAA, and TRNG please refer to the
108+
[MAX32665/MAX32666 User Guide: UG6971](https://www.analog.com/media/en/technical-documentation/user-guides/max32665max32666-user-guide.pdf)
109+
110+
# MAXQ1065/MAX1080
12111
## Build and Usage
13112

14113
Please use the appropriate SDK or Evkit to build wolfSSL.

0 commit comments

Comments
 (0)