-
Notifications
You must be signed in to change notification settings - Fork 834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MAX32665 and MAX32666 TPU HW and ARM ASM Crypto Callback Support #7777
Conversation
Bug with MAA report here for visibility: analogdevicesinc/msdk#1089 |
I do plan on fixing, max3266x.c and max3266x.h to use proper wolfSSL types. Have had some compile issues when using |
wolfcrypt/src/port/maxim/max3266x.c
Outdated
@@ -0,0 +1,921 @@ | |||
/* max3266x.c | |||
* | |||
* Copyright (C) 2006-2023 wolfSSL Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
|
||
#include <wolfssl/wolfcrypt/settings.h> | ||
|
||
#warning "Inside max3266x.c" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, leftover from debugging
wolfcrypt/src/port/maxim/max3266x.c
Outdated
|
||
#warning "Inside max3266x.c" | ||
#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) | ||
#warning "Inside max3266x.c define" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
wolfcrypt/src/port/maxim/max3266x.c
Outdated
#endif | ||
|
||
#include <stdint.h> | ||
#include <wolfssl/internal.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really need internal.h?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I will need #include <stdarg.h>
for the int wc_MXC_MAA_Fallback(unsigned int count, ...)
function.
int wc_MXC_TPU_Init(void); | ||
int wc_MXC_TPU_Shutdown(void); | ||
int wc_MXC_error(int *ret); /* Convert Errors to wolfCrypt Codes */ | ||
//int wc_MXC_AesSetKey(Aes* aes, byte* userKey, unsigned int keylen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead, remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
extern "C" { | ||
#endif | ||
|
||
int wc_MXC_TPU_Init(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mark internal functions with WOLFSSL_LOCAL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
typedef wc_MXC_Sha wc_Sha256; | ||
#define WC_SHA256_TYPE_DEFINED | ||
|
||
// Define the SHA-256 digest for an empty string as a constant byte array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only C style comment /* */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
#define WC_SHA256_TYPE_DEFINED | ||
|
||
// Define the SHA-256 digest for an empty string as a constant byte array | ||
static const unsigned char MXC_EMPTY_DIGEST_SHA256[32] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use WC_SHA256_DIGEST_SIZE
. Please use byte array { 0xe3, 0xb0, ...};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
|
||
#if defined(MAX3266X_MATH) | ||
|
||
int hw_mulmod(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add arguments... They are not ().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right! This is part of the same reason I am using char instead of byte, I am having issues with including wolfssl types and structs to max3266x.h
. I am exploring a better solution and this is a temp solution to make sure the implementation works (HW works as intended).
18752ce
to
30a2feb
Compare
wolfcrypt/src/aes.c
Outdated
@@ -2888,6 +2892,15 @@ static WARN_UNUSED_RESULT int wc_AesEncrypt( | |||
} | |||
#endif | |||
|
|||
#if defined(MAX3266X_AES) | |||
word32 keySize; | |||
if(wc_AesGetKeySize(aes, &keySize) == 0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Please add space if (
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated if statements in aes.c and max3266x.c
wolfcrypt/src/aes.c
Outdated
return BAD_LENGTH_E; | ||
} | ||
#endif | ||
if (blocks == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sz == 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, this was a leftover copy paste, removed the blocks variable, and using sz instead.
wolfcrypt/src/aes.c
Outdated
if (blocks == 0) | ||
return 0; | ||
|
||
iv = (byte*)aes->reg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Reduce space after iv
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
#define MXC_WORD_SIZE SP_WORD_SIZE | ||
#endif | ||
|
||
#define MXC_MAA_MAX_SIZE (2048)/MXC_WORD_SIZE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define MXC_MAA_MAX_SIZE (2048 / MXC_WORD_SIZE)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
#if defined(WOLFSSL_MAX3266X_OLD) | ||
MXC_TPU_Shutdown(); /* Is a void return in older SDK */ | ||
#else | ||
if(MXC_TPU_Shutdown(MXC_SYS_PERIPH_CLOCK_TRNG) != EXIT_SUCCESS){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: if (
and ) {
. Please reduce indent of 69-76 4 spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated and fixed indenting
wolfcrypt/src/port/maxim/max3266x.c
Outdated
mp_int multiplicand; | ||
multiplicand.dp[0] = 0x01; | ||
multiplicand.used = 1; | ||
WOLFSSL_MSG("Preparing exptmod MAA HW Call"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again consider a new local file debugging macro for this level of verbosity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
|
||
int hw_sqrmod(mp_int* base, mp_int* mod, mp_int* result) | ||
{ | ||
if (base->used == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Fix indent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
else{ | ||
return wc_MXC_MAA_sqrmod(base, mod, result); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Remove extra line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
} | ||
|
||
/* Function to provide the current time as a double */ | ||
double wc_MXC_RTC_Time(void) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Functions should have starting/ending brace on their own line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was overlooked, fixed.
c00c33c
to
56b585f
Compare
Jenkins retest this please |
wolfcrypt/src/aes.c
Outdated
@@ -3611,6 +3624,15 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt( | |||
} /* else !wc_esp32AesSupportedKeyLen for ESP32 */ | |||
#endif | |||
|
|||
#if defined(MAX3266X_AES) | |||
word32 keySize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed declaration. Please move this top top of function and macro guard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated for wc_AesDecrypt
and wc_AesEncrypt
wolfcrypt/src/port/maxim/max3266x.c
Outdated
int wc_MXC_MAA_adjustUsed(unsigned int *array, unsigned int length) | ||
{ | ||
int lastNonZeroIndex = -1; /* Track the last non-zero index */ | ||
for (int i = 0; i < length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed declaration. Move the variable declarations to top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
wolfcrypt/src/port/maxim/max3266x.c
Outdated
va_start(args, count); | ||
unsigned int largest = va_arg(args, unsigned int); | ||
|
||
for (int i = 1; i < count; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed declaration. Move the variable declarations to top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
wolfcrypt/src/port/maxim/max3266x.c
Outdated
/* Create an array to compare values to to check edge for error edge case */ | ||
mp_digit *zero_tmp = XMALLOC(multiplier->size*(sizeof(mp_digit)), NULL, | ||
DYNAMIC_TYPE_TMP_BUFFER); | ||
XMEMSET(zero_tmp, 0x00, multiplier->size*(sizeof(mp_digit))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Please use (multiplier->size*sizeof(mp_digit))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/port/maxim/max3266x.c
Outdated
} | ||
|
||
/* Create an array to compare values to to check edge for error edge case */ | ||
mp_digit *zero_tmp = XMALLOC(multiplier->size*(sizeof(mp_digit)), NULL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need explicit cast to mp_digit*
and move variable declaration to top
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
#define WOLFSSL_MAX3266X_DEVID 9 | ||
#endif | ||
#ifndef MAX_CRYPTO_DEVID_CALLBACKS | ||
#define MAX_CRYPTO_DEVID_CALLBACKS WOLFSSL_MAX3266X_DEVID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This macro specifies the maximum number of concurrent callbacks to register. I don't think you need to change or set this. Also you don't need to include this header in cryptocb.c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed, and removed inclusion of the header in cryptocb.c
wolfcrypt/src/sha.c
Outdated
@@ -1003,7 +1014,11 @@ if (sha->ctx.mode == ESP32_SHA_HW) { | |||
/* we'll always reset state upon exit and return the error code from above, | |||
* which may cause fall back to SW if HW is busy. we do not return result | |||
* of initSha here */ | |||
(void)InitSha(sha); /* reset state */ | |||
#ifdef WOLF_CRYPTO_CB /* Use to reset state but keep devId info */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change should not be required. The InitSha should not be resetting heap hint or devId.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted
wolfcrypt/src/sha256.c
Outdated
@@ -1732,7 +1740,11 @@ static int InitSha256(wc_Sha256* sha256) | |||
#endif | |||
XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE); | |||
|
|||
#ifdef WOLF_CRYPTO_CB /* Use to reset state but keep devId info */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider fixing InitSha256
to not wipe out devId... and move the sha256->devId = wc_CryptoCb_DefaultDevID();
login into wc_InitSha256_ex
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the setting of the devId
in InitSha256()
, It doesn't seem like we need to add sha256->devId = wc_CryptoCb_DefaultDevID();
to wc_InitSha256_ex()
as this function expects a devId
argument passed to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be removed too. This was originally added to handle the cases where Sha256 didn't have a devId like CalcHashId. However those one-shot hash API's like wc_Sha256Hash
now have this logic built-in. devId = wc_CryptoCb_DefaultDevID();
f2ccfb3
to
e7ad571
Compare
wolfcrypt/src/aes.c
Outdated
@@ -82,6 +82,17 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits | |||
#include <wolfssl/wolfcrypt/port/psa/psa.h> | |||
#endif | |||
|
|||
#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) | |||
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h> | |||
#ifdef WOLF_CRYPTO_CB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be gated on MAX3266X_CB
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to MAX3266X_CB
for consistency
wolfssl/wolfcrypt/aes.h
Outdated
#if (defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)) && \ | ||
defined(WOLF_CRYPTO_CB) | ||
/* Need backup key for MXC CB */ | ||
word32 cb_key[60]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed... Already code in struct to support raw key. See devKey
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed and setup to use devKey
wolfcrypt/src/aes.c
Outdated
@@ -4102,8 +4168,12 @@ 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required. See devKey
. Use this for crypto callbacks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
wolfcrypt/src/aes.c
Outdated
@@ -4543,11 +4613,14 @@ 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required. See devKey
. Use this for crypto callbacks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
3a8578f
to
23ea92e
Compare
…MAA call update for MAX3266X Port.
… Fix SP SHA CB Bug
23ea92e
to
9881edf
Compare
Jenkins retest this please |
Description
MAX32665 and MAX32666 TPU HW
This adds support for the TPU HW acceleration on the MAX32665 and MAX32666 from Analog Devices
Code and Limitations where determined from the User Guide UG6971; Rev 3; 2/2022
Maxim SDK can be found here
Setup to run using these defines to run:
ARM ASM Crypto Callback support
Added in Ability to use Callbacks with ARM ASM with the following algos
AES ECB: 128, 192, 256
AES CBC: 128, 192, 256
SHA-1
SHA-256
SHA-384
SHA-512
Tested with MAX32666 dev board with these settings
ARM ASM Settings:
Testing
Checklist
HW Acceleration
Side Items