Skip to content
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

Merged
merged 8 commits into from
Sep 20, 2024

Conversation

night1rider
Copy link
Contributor

@night1rider night1rider commented Jul 23, 2024

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:

#define WOLFSSL_MAX3266X
#define WOLFSSL_MAX3266X_OLD - only needed if with old SDK
#define MAX3266X_RTC - only setup for new sdk and not old for baremetal benchmarking
#define WOLFSSL_SP_MATH_ALL

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:

#define WOLFSSL_ARMASM
#define WOLFSSL_ARMASM_INLINE
#define WOLFSSL_ARMASM_NO_HW_CRYPTO
#define WOLFSSL_ARMASM_NO_NEON
#define WOLFSSL_ARM_ARCH 7
#define GCM_TABLE

Testing

  • wolfCrypt Test and wolfCrypt Benchmark on Baremetal with the MAX32666FTHR with new SDK
  • wolfCrypt Test and wolfCrypt Benchmark on FreeRTOS with custom hardware with MAX32666 with old SDK

Checklist

HW Acceleration

  • TRNG
  • AES CBC 128, 192, 256
  • AES GCM 128, 192, 256 (hardware does not support this mode, using ECB)
  • SHA-256
  • RSA Math Acceleration
  • ECDSA Math Acceleration

Side Items

  • Jenkins Test Nightly using the MAX32666FTHR
  • Update wolfSSL Manual
  • Update Readme in wolfssl/wolfcrypt/port/maxim/

@night1rider
Copy link
Contributor Author

Bug with MAA report here for visibility: analogdevicesinc/msdk#1089
I will be looking to file a ticket on Analogs website as well.

@night1rider
Copy link
Contributor Author

I do plan on fixing, max3266x.c and max3266x.h to use proper wolfSSL types. Have had some compile issues when using byte instead of char type

wolfcrypt/src/port/maxim/max3266x.c Show resolved Hide resolved
@@ -0,0 +1,921 @@
/* max3266x.c
*
* Copyright (C) 2006-2023 wolfSSL Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024

Copy link
Contributor Author

@night1rider night1rider Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed


#include <wolfssl/wolfcrypt/settings.h>

#warning "Inside max3266x.c"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these.

Copy link
Contributor Author

@night1rider night1rider Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, leftover from debugging


#warning "Inside max3266x.c"
#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)
#warning "Inside max3266x.c define"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#endif

#include <stdint.h>
#include <wolfssl/internal.h>
Copy link
Contributor

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?

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead, remove?

Copy link
Contributor Author

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);
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only C style comment /* */

Copy link
Contributor Author

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] =
Copy link
Contributor

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, ...};

Copy link
Contributor Author

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();
Copy link
Contributor

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 ().

Copy link
Contributor Author

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).

@night1rider night1rider force-pushed the MAX32666-port branch 11 times, most recently from 18752ce to 30a2feb Compare July 25, 2024 04:54
@@ -2888,6 +2892,15 @@ static WARN_UNUSED_RESULT int wc_AesEncrypt(
}
#endif

#if defined(MAX3266X_AES)
word32 keySize;
if(wc_AesGetKeySize(aes, &keySize) == 0){
Copy link
Contributor

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 (

Copy link
Contributor Author

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

return BAD_LENGTH_E;
}
#endif
if (blocks == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sz == 0?

Copy link
Contributor Author

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.

if (blocks == 0)
return 0;

iv = (byte*)aes->reg;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

#define MXC_WORD_SIZE SP_WORD_SIZE
#endif

#define MXC_MAA_MAX_SIZE (2048)/MXC_WORD_SIZE
Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

#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){
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated and fixed indenting

mp_int multiplicand;
multiplicand.dp[0] = 0x01;
multiplicand.used = 1;
WOLFSSL_MSG("Preparing exptmod MAA HW Call");
Copy link
Contributor

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.

Copy link
Contributor Author

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 Show resolved Hide resolved

int hw_sqrmod(mp_int* base, mp_int* mod, mp_int* result)
{
if (base->used == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Fix indent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

else{
return wc_MXC_MAA_sqrmod(base, mod, result);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Remove extra line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

}

/* Function to provide the current time as a double */
double wc_MXC_RTC_Time(void) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was overlooked, fixed.

@night1rider night1rider force-pushed the MAX32666-port branch 11 times, most recently from c00c33c to 56b585f Compare July 25, 2024 22:55
@night1rider
Copy link
Contributor Author

Jenkins retest this please

@@ -3611,6 +3624,15 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
} /* else !wc_esp32AesSupportedKeyLen for ESP32 */
#endif

#if defined(MAX3266X_AES)
word32 keySize;
Copy link
Contributor

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.

Copy link
Contributor Author

@night1rider night1rider Jul 29, 2024

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

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++) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

va_start(args, count);
unsigned int largest = va_arg(args, unsigned int);

for (int i = 1; i < count; i++) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

/* 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)));
Copy link
Contributor

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))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

}

/* 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,
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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

@@ -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 */
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

@@ -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 */
Copy link
Contributor

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.

Copy link
Contributor Author

@night1rider night1rider Sep 19, 2024

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.

Copy link
Contributor

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();

@@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

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

#if (defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)) && \
defined(WOLF_CRYPTO_CB)
/* Need backup key for MXC CB */
word32 cb_key[60];
Copy link
Contributor

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

Copy link
Contributor Author

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

@@ -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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

@@ -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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

wolfcrypt/src/port/arm/armv8-aes.c Show resolved Hide resolved
@night1rider night1rider changed the title Initial PR for MAX32665 and MAX32666 TPU HW Support MAX32665 and MAX32666 TPU HW and ARM ASM Crypto Callback Support Sep 19, 2024
@night1rider night1rider force-pushed the MAX32666-port branch 2 times, most recently from 3a8578f to 23ea92e Compare September 19, 2024 22:54
dgarske
dgarske previously approved these changes Sep 20, 2024
@dgarske dgarske removed their assignment Sep 20, 2024
wolfssl/wolfcrypt/wc_port.h Outdated Show resolved Hide resolved
@night1rider
Copy link
Contributor Author

Jenkins retest this please

@JacobBarthelmeh JacobBarthelmeh merged commit 554d52b into wolfSSL:master Sep 20, 2024
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants