From a41b45909b4c3a3b84d31315f0c628d412950da6 Mon Sep 17 00:00:00 2001 From: msi-debian Date: Mon, 8 Jul 2024 08:51:24 -0600 Subject: [PATCH] Initial PR for MAX32665 and MAX32666 TPU HW Support --- wolfcrypt/benchmark/benchmark.c | 8 + wolfcrypt/src/aes.c | 86 ++- wolfcrypt/src/port/maxim/max3266x.c | 921 ++++++++++++++++++++++++ wolfcrypt/src/random.c | 9 + wolfcrypt/src/sha256.c | 7 +- wolfcrypt/src/wc_port.c | 19 + wolfssl/wolfcrypt/ecc.h | 9 + wolfssl/wolfcrypt/port/maxim/max3266x.h | 281 ++++++++ wolfssl/wolfcrypt/rsa.h | 10 + wolfssl/wolfcrypt/sha.h | 4 + wolfssl/wolfcrypt/sha256.h | 3 + wolfssl/wolfcrypt/wc_port.h | 4 + 12 files changed, 1357 insertions(+), 4 deletions(-) create mode 100644 wolfcrypt/src/port/maxim/max3266x.c create mode 100644 wolfssl/wolfcrypt/port/maxim/max3266x.h diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index cfd0b71486..1df8d487a8 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -14236,6 +14236,14 @@ void bench_sphincsKeySign(byte level, byte optim) return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000; } +#elif (defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)) \ + && defined(MAX3266X_RTC) + + double current_time(int reset) + { + return wc_MXC_RTC_Time(); + } + #elif defined(FREESCALE_KSDK_BM) double current_time(int reset) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 247cd3c777..f56d0ba203 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -82,6 +82,10 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits #include #endif +#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) + #include +#endif + #if defined(WOLFSSL_TI_CRYPT) #include #else @@ -4537,13 +4541,13 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir) return ret; } #endif - XMEMCPY(aes->key, userKey, keylen); #ifndef WC_AES_BITSLICED #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \ (!defined(WOLFSSL_ESP32_CRYPT) || \ - defined(NO_WOLFSSL_ESP32_CRYPT_AES)) + defined(NO_WOLFSSL_ESP32_CRYPT_AES)) && \ + (!defined(WOLFSSL_MAX32665) && !defined(WOLFSSL_MAX32666)) /* software */ ByteReverseWords(aes->key, aes->key, keylen); @@ -5374,6 +5378,84 @@ int wc_AesSetIV(Aes* aes, const byte* iv) } #endif /* HAVE_AES_DECRYPT */ +#elif defined(MAX3266X_AES) + int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) + { + word32 keySize; + int status; + byte *iv; + word32 blocks = (sz / AES_BLOCK_SIZE); + +#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS + if (sz % AES_BLOCK_SIZE) { + return BAD_LENGTH_E; + } +#endif + if (blocks == 0) + return 0; + + iv = (byte*)aes->reg; + + status = wc_AesGetKeySize(aes, &keySize); + if (status != 0) { + return status; + } + + status = wc_MXC_TPU_AesEncrypt(in, iv, aes->key, MXC_TPU_MODE_CBC, + MXC_AES_DATA_LEN, out, + (unsigned int)keySize); + + /* store iv for next call */ + if (status == E_SUCCESS) { + XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); + } + + return (status == E_SUCCESS) ? 0 : -1; + } + + #ifdef HAVE_AES_DECRYPT + int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) + { + word32 keySize; + int status; + byte *iv; + byte temp_block[AES_BLOCK_SIZE]; + word32 blocks = (sz / AES_BLOCK_SIZE); + +#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS + if (sz % AES_BLOCK_SIZE) { + return BAD_LENGTH_E; + } +#endif + if (blocks == 0) + return 0; + + iv = (byte*)aes->reg; + + status = wc_AesGetKeySize(aes, &keySize); + if (status != 0) { + return status; + } + + /* get IV for next call */ + XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); + + + status = wc_MXC_TPU_AesDecrypt(in, iv, aes->key, MXC_TPU_MODE_CBC, + MXC_AES_DATA_LEN, out, keySize); + + + /* store iv for next call */ + if (status == E_SUCCESS) { + XMEMCPY(iv, temp_block, AES_BLOCK_SIZE); + } + + return (status == E_SUCCESS) ? 0 : -1; + } + #endif /* HAVE_AES_DECRYPT */ + + + #elif defined(WOLFSSL_PIC32MZ_CRYPT) int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) diff --git a/wolfcrypt/src/port/maxim/max3266x.c b/wolfcrypt/src/port/maxim/max3266x.c new file mode 100644 index 0000000000..5370337cf9 --- /dev/null +++ b/wolfcrypt/src/port/maxim/max3266x.c @@ -0,0 +1,921 @@ +/* max3266x.c + * + * Copyright (C) 2006-2023 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#warning "Inside max3266x.c" +#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) +#warning "Inside max3266x.c define" + + +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#if defined(USE_FAST_MATH) && defined(WOLFSSL_SP_MATH_ALL) + #error Cannot Do Both FAST MATH and SP MATH ALL with MAX3266X +#elif defined(USE_FAST_MATH) + #error MXC Not Compatible with Fast Math + #warning Fast Math is being used + #include + #define MXC_WORD_SIZE DIGIT_BIT +#elif defined(WOLFSSL_SP_MATH_ALL) + #warning Assuming SP MATH ALL is being used + #include + #define MXC_WORD_SIZE SP_WORD_SIZE +#endif + +#if defined(MAX3266X_MATH) + #if defined(USE_FAST_MATH) + #define mxc_mod fp_mod + #define mxc_addmod fp_addmod + #define mxc_submod fp_submod + #define mxc_mulmod fp_mulmod + #define mxc_exptmod fp_exptmod + #define mxc_sqrmod fp_sqrmod + #elif defined(WOLFSSL_SP_MATH_ALL) + #define mxc_mod sp_mod + #define mxc_addmod sp_addmod + #define mxc_submod sp_submod + #define mxc_mulmod sp_mulmod + #define mxc_exptmod sp_exptmod + #define mxc_sqrmod sp_sqrmod + #else + #error Need USE_FAST_MATH OR WOLFSSL_SP_MATH_ALL + #endif +#endif + +int wc_MXC_TPU_Init(void) +{ + /* Initialize the TPU device */ + if(MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TRNG) != EXIT_SUCCESS){ + WOLFSSL_MSG("Device did not initialize"); + return RNG_FAILURE_E; + } + return EXIT_SUCCESS; +} + +int wc_MXC_TPU_Shutdown(void) +{ + /* Shutdown the TPU device */ + if(MXC_TPU_Shutdown(MXC_SYS_PERIPH_CLOCK_TRNG) != EXIT_SUCCESS){ + WOLFSSL_MSG("Device did not shutdown"); + return RNG_FAILURE_E; + } + return EXIT_SUCCESS; +} + + +/* Convert Error Codes Correctly */ +/* TODO: Convert to correct wolfCrypt Codes */ +/* TODO: Add wolfssl Message Statements to report HW issue on bad return */ +int wc_MXC_error(int *ret) +{ + switch(*ret){ + case(E_SUCCESS): + return E_SUCCESS; + + case(E_NULL_PTR): + return E_NULL_PTR; + + case(E_INVALID): /* Process Failed */ + return E_INVALID; + + case(E_BAD_PARAM): + return BAD_FUNC_ARG; + + case(E_BAD_STATE): + return E_BAD_STATE; + + default: + *ret = WC_HW_E; /* If something else return HW Error */ + return *ret; + } +} + + +#if defined(MAX3266X_RNG) + + /* Use this RNG_FAILURE_E for RNG Errors*/ + int wc_MXC_TRNG_Random(unsigned char* output, unsigned int sz) + { + /* Obtain Value from TRNG device */ + #if defined(WOLFSSL_MAX32665_OLD) || defined(WOLFSSL_MAX32666_OLD) + /* TODO: TRNG setup for OLD SDK */ + #warning "Nothing Done for OLD SDK" + #else + if(MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TRNG) != E_SUCCESS){ + /* TODO Add wolfSSL debugging */ + printf("Device did not initialize\n"); + return RNG_FAILURE_E; + } + MXC_TPU_TRNG_Read(MXC_TRNG, output, sz); + #endif + + return E_SUCCESS; + } +#endif /* MAX3266x_RNG */ + +#if defined(MAX3266X_AES) + int wc_MXC_TPU_AesEncrypt(const char *in, const char *iv, + const char *enc_key, + MXC_TPU_MODE_TYPE mode, + unsigned int data_size, + char *out, unsigned int keySize) + { + int status; + status = wolfSSL_CryptHwMutexLock(); + if (status != 0) + return status; + switch (keySize) { + case MXC_AES_KEY_128_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES128); + status = MXC_TPU_Cipher_AES_Encrypt(in, iv, enc_key, + MXC_TPU_CIPHER_AES128, mode, data_size, out); + break; + case MXC_AES_KEY_192_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES192); + status = MXC_TPU_Cipher_AES_Encrypt(in, iv, enc_key, + MXC_TPU_CIPHER_AES192, mode, data_size, out); + break; + + case MXC_AES_KEY_256_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES256); + status = MXC_TPU_Cipher_AES_Encrypt(in, iv, enc_key, + MXC_TPU_CIPHER_AES256, mode, data_size, out); + break; + default: + wolfSSL_CryptHwMutexUnLock(); + return WC_HW_E; + break; + } + wolfSSL_CryptHwMutexUnLock(); + if (status != E_SUCCESS){ + return WC_HW_E; + } + return E_SUCCESS; + } + + int wc_MXC_TPU_AesDecrypt(const char *in, const char *iv, + const char *dec_key, + MXC_TPU_MODE_TYPE mode, + unsigned int data_size, + char *out, unsigned int keySize) + { + int status; + status = wolfSSL_CryptHwMutexLock(); + if (status != 0) + return status; + switch (keySize) { + case MXC_AES_KEY_128_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES128); + status = MXC_TPU_Cipher_AES_Decrypt(in, iv, dec_key, + MXC_TPU_CIPHER_AES128, mode, data_size, out); + break; + case MXC_AES_KEY_192_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES192); + status = MXC_TPU_Cipher_AES_Decrypt(in, iv, dec_key, + MXC_TPU_CIPHER_AES192, mode, data_size, out); + break; + case MXC_AES_KEY_256_LEN: + MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES256); + status = MXC_TPU_Cipher_AES_Decrypt(in, iv, dec_key, + MXC_TPU_CIPHER_AES256, mode, data_size, out); + break; + default: + wolfSSL_CryptHwMutexUnLock(); + return WC_HW_E; + break; + } + + wolfSSL_CryptHwMutexUnLock(); + if (status != E_SUCCESS){ + return WC_HW_E; + } + return E_SUCCESS; + } + + +#endif + + +#if defined(MAX3266X_SHA) + + int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash) + { + if (hash == NULL) { + return BAD_FUNC_ARG; // Appropriate error handling for null argument + } + hash->msg = NULL; + hash->used = 0; + hash->size = 0; + return EXIT_SUCCESS; + } + + int wc_MXC_TPU_SHA_Update(wc_MXC_Sha *hash, const unsigned char* data, + unsigned int size) + { + void *p; + if(size != (0 || NULL)){ + if ((hash== NULL) || (data == NULL)) + return BAD_FUNC_ARG; + if (hash->size < hash->used+size) { + if (hash->msg == NULL) { + p = XMALLOC(hash->used+size, NULL, DYNAMIC_TYPE_TMP_BUFFER); + } else { + p = XREALLOC(hash->msg, hash->used+size, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + } + if (p == NULL) + return -1; + hash->msg = p; + hash->size = hash->used+size; + } + XMEMCPY(hash->msg+hash->used, data, size); + hash->used += size; + if(hash->msg == NULL){ + return BAD_FUNC_ARG; + } + } + return EXIT_SUCCESS; + } + + int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo) + { + int status; + status = wc_MXC_TPU_SHA_GetDigest(hash, digest, algo); + /* True Case that msg is an empty string */ + if(status == 0){ + return EXIT_SUCCESS; + } + /* False Case where msg needs to be processed*/ + else if (status == 1){ + status = wolfSSL_CryptHwMutexLock(); + if(wc_MXC_error(&status) != EXIT_SUCCESS){ + return status; + } + MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TPU); + MXC_TPU_Hash_Config(algo); + status = MXC_TPU_Hash_SHA((const char *)hash->msg, algo, hash->size, + (char *)digest); + wolfSSL_CryptHwMutexUnLock(); + if(wc_MXC_error(&status) != EXIT_SUCCESS){ + return status; + } + } + return status; + } + + int wc_MXC_TPU_SHA_Final(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo) + { + int status; + status = wc_MXC_TPU_SHA_GetHash(hash, digest, algo); + if (status != E_SUCCESS){ + return status; + } + XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); + status = wc_MXC_TPU_SHA_Init(hash); + if (status != E_SUCCESS){ + return status; + } + return status; + } + + int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst) + { + dst->msg = NULL; + dst->used = 0; + dst->size = 0; + XMEMCPY(dst->hash, src->hash, sizeof(dst->hash)); + return EXIT_SUCCESS; + } + + void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash) + { + XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); + wc_MXC_TPU_SHA_Init(hash); + return EXIT_SUCCESS; + } + + int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo) + { + if(hash->msg == 0 && hash->size == 0 && digest != NULL){ + switch(algo){ + #ifndef NO_SHA256 + case MXC_TPU_HASH_SHA256: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, + WC_SHA256_DIGEST_SIZE); + + break; + #endif + default: + return BAD_FUNC_ARG; + } + return 0; /* True */ + } + else{ + return 1; /* False */ + } + } + +#if !defined(NO_SHA256) + + + + WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) + { + if (sha256 == NULL){ + return 1; + } + (void)heap; + (void)devId; + return wc_MXC_TPU_SHA_Init((wc_MXC_Sha *)sha256); + } + + WOLFSSL_API int wc_InitSha256(wc_Sha256* sha256) + { + return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID); + } + + WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha256, const unsigned char* data, + unsigned int len) + { + return wc_MXC_TPU_SHA_Update(sha256, data, len); + } + + WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, unsigned char* hash) + { + return wc_MXC_TPU_SHA_Final((wc_MXC_Sha *)sha256, hash, + MXC_TPU_HASH_SHA256); + } + + WOLFSSL_API int wc_Sha256GetHash(wc_Sha256* sha256, unsigned char* hash) + { + return wc_MXC_TPU_SHA_GetHash((wc_MXC_Sha *)sha256, hash, + MXC_TPU_HASH_SHA256); + } + + WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) + { + return wc_MXC_TPU_SHA_Copy((wc_MXC_Sha *)src, (wc_MXC_Sha *)dst); + } + + WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256) + { + wc_MXC_TPU_SHA_Free((wc_MXC_Sha *)sha256); + return; + } + +#endif + + + +#endif /* MAX3266X_SHA */ + +#if defined(MAX3266X_MATH) + + int wc_MXC_MAA_init(unsigned int len) + { + int status; + WOLFSSL_MSG("Setting Hardware Mutex and Starting MAA"); + status = wolfSSL_CryptHwMutexLock(); + if (status != EXIT_SUCCESS){ + return status; + } + status = MXC_TPU_MAA_Init(len); + return wc_MXC_error(&status); /* Return Status of Init */ + } + + int wc_MXC_MAA_Shutdown(void) + { + int status; + WOLFSSL_MSG("Unlocking Hardware Mutex and Shutting Down MAA"); + status = MXC_TPU_MAA_Shutdown(); + if (status == E_BAD_PARAM){ /* Miss leading, Send WC_HW_ERROR */ + /* This is returned when MAA cannot stop */ + return WC_HW_E; + } + else if(wc_MXC_error(&status) != EXIT_SUCCESS) { + return status; + } + wolfSSL_CryptHwMutexUnLock(); + return status; + } + + /* Update used number for mp_int struct for results */ + int wc_MXC_MAA_adjustUsed(int *array, unsigned int length) + { + int lastNonZeroIndex = -1; /* Track the last non-zero index */ + for (int i = 0; i < length; i++) { + if (array[i] != 0) { + lastNonZeroIndex = i; + } + } + return (lastNonZeroIndex + 1); + } + + void printMAA(mp_int* var, unsigned int len) + { + if(var != NULL){ + printf("\n"); + for(int i = 0; i < len; i++){ + printf("%08X ", var->dp[i]); + } + printf("\n"); + } + } + + unsigned int wc_MXC_MAA_Largest(unsigned int count, ...) + { + va_list args; + va_start(args, count); + unsigned int largest = va_arg(args, unsigned int); + + for (int i = 1; i < count; i++) { + int num = va_arg(args, unsigned int); + if (num > largest) { + largest = num; + } + } + + va_end(args); + return largest; + } + + int wc_MXC_MAA_Fallback(unsigned int count, ...) + { + va_list args; + va_start(args, count); + int num; + for (int i = 0; i < count; i++) { + num = va_arg(args, unsigned int); + if (num > (2048)/MXC_WORD_SIZE) { + WOLFSSL_MSG("HW Falling Back to Software"); + return EXIT_FAILURE; + } + } + va_end(args); + WOLFSSL_MSG("HW Can Handle Input"); + return EXIT_SUCCESS; + } + + + + /* Have to zero pad the entire data array up to 256 bytes(2048 bits) */ + /* If length > 256 bytes then error */ + int wc_MXC_MAA_zeroPad(mp_int* multiplier, mp_int* multiplicand, + mp_int* exp, mp_int* mod, mp_int* result, + MXC_TPU_MAA_TYPE clc, unsigned int length) + { + WOLFSSL_MSG("Zero Padding Buffers for Hardware"); + if((length > (2048)/MXC_WORD_SIZE)){ + WOLFSSL_MSG("Hardware cannot exceed 2048 bit input"); + return BAD_FUNC_ARG; + } + if((result == NULL) || (multiplier == NULL) || (multiplicand == NULL) || + ((exp == NULL) && (clc == WC_MXC_TPU_MAA_EXP)) || (mod == NULL)) + { + return BAD_FUNC_ARG; + } + + /* Check for invalid arguments befor padding */ + switch(clc){ + case(WC_MXC_TPU_MAA_EXP): + /* Has to be set to 1 for a^e mod m operation */ + if(multiplicand->dp[0] != 1){ + return BAD_FUNC_ARG; + } + /* Checking for any values that are not 0 in array */ + for(int i = 1; i < multiplicand->used; i++){ + if(multiplicand->dp[i] != 0){ + return BAD_FUNC_ARG; + break; + } + } + /* Cannot be 0 for a^e mod m operation */ + for(int i = 0; i < exp->used; i++){ + if(exp->dp[i] != 0){ + break; + } + /* Return Errror if used amount is Reached */ + if(i == (exp->used - 1)){ + return BAD_FUNC_ARG; + break; + } + } + /* Padd out rest of data if used != length to ensure no */ + /* garbage is used in calculation */ + if ((exp != NULL) && (clc == WC_MXC_TPU_MAA_EXP)){ + if((exp->dp != NULL) && (exp->used < length)){ + WOLFSSL_MSG("Zero Padding Exp Buffer"); + XMEMSET(exp->dp + exp->used, 0x00, + sizeof(int) *(length - exp->used)); + } + } + /* Fall through to check mod is not 0 */ + case(WC_MXC_TPU_MAA_SQ): + case(WC_MXC_TPU_MAA_MUL): + case(WC_MXC_TPU_MAA_SQMUL): + case(WC_MXC_TPU_MAA_ADD): + case(WC_MXC_TPU_MAA_SUB): + /* Cannot be 0 for mod m value */ + for(int i = 0; i < mod->used; i++){ + if(mod->dp[i] != 0){ + break; + } + /* Return Error if used amount is Reached */ + if(i == (exp->used - 1)){ + WOLFSSL_MSG("Cannot use Value 0 for Mod"); + return BAD_FUNC_ARG; + break; + } + } + /* Padd out rest of data if used != length to ensure no */ + /* garbage is used in calculation */ + if((multiplier->dp != NULL) && (multiplier->used < length)){ + WOLFSSL_MSG("Zero Padding Multipler Buffer"); + XMEMSET(multiplier->dp + multiplier->used, 0x00, + sizeof(int) * (length - multiplier->used)); + } + if((multiplicand->dp != NULL) && (multiplicand->used < length)) + { + WOLFSSL_MSG("Zero Padding Multiplicand Buffer"); + XMEMSET(multiplicand->dp + multiplicand->used, 0x00, + sizeof(int) * (length - multiplicand->used)); + } + if((mod->dp != NULL) && (mod->used < length)){ + WOLFSSL_MSG("Zero Padding Mod Buffer"); + XMEMSET(mod->dp + mod->used, 0x00, + sizeof(int) *(length - mod->used)); + } + break; + default: + return BAD_FUNC_ARG; /* Invalid clc given */ + } + + /* Make sure result is 0 padded */ + if((result->dp != NULL)){ + XMEMSET(result->dp, 0x00, sizeof(int)*(length)); + result->used = length; + } + else if(result == NULL){ + return BAD_FUNC_ARG; /* Cannot be null */ + } + return EXIT_SUCCESS; + } + + + + /* General Control Over MAA Hardware to handle all needed Cases */ + int wc_MXC_MAA_math(mp_int* multipler, mp_int* multiplicand, mp_int* exp, + mp_int* mod, mp_int* result, + MXC_TPU_MAA_TYPE clc) + { + int ret; + int length; + /* Check if result shares struct pointer */ + mp_int* result_tmp_ptr; + if((multipler == result) || (multiplicand == result) || + (exp == result) || (mod == result)){ + WOLFSSL_MSG("Creating Temp Result Buffer for Hardware"); + mp_int result_tmp; + result_tmp_ptr = &result_tmp; /* Assign point to temp struct */ + } + else{ + result_tmp_ptr = result; /* No Shared Point to directly assign */ + } + if(result_tmp_ptr == NULL){ + WOLFSSL_MSG("tmp ptr is null"); + return MP_VAL; + } + + if(clc == WC_MXC_TPU_MAA_EXP){ + length = wc_MXC_MAA_Largest(5, multipler->used, multiplicand->used, + exp->used, mod->used, result->used); + } + else{ + length = wc_MXC_MAA_Largest(4, multipler->used, multiplicand->used, + mod->used, result->used); + } + + /* Zero Pad everything if needed */ + ret = wc_MXC_MAA_zeroPad(multipler, multiplicand, exp, mod, + result_tmp_ptr, clc, length); + if (ret != EXIT_SUCCESS){ + WOLFSSL_MSG("Zero Padding Failed"); + return ret; + } + + /* Init MAA HW */ + ret = wc_MXC_MAA_init(length*sizeof(mp_digit)*8); + if (ret != EXIT_SUCCESS){ + WOLFSSL_MSG("HW Init Failed"); + return ret; + } + + /* Start Math And Cast to expect types for SDK */ + WOLFSSL_MSG("Starting Computation in MAA"); + ret = MXC_TPU_MAA_Compute(clc, (char *)(multipler->dp), + (char *)(multiplicand->dp), + (char *)(exp->dp), (char *)(mod->dp), + (int *)(result_tmp_ptr->dp), + (length*sizeof(mp_digit))); + WOLFSSL_MSG("MAA Finished Computation"); + if(wc_MXC_error(&ret) != EXIT_SUCCESS){ + WOLFSSL_MSG("HW Computation Error"); + return ret; + } + + ret = wc_MXC_MAA_Shutdown(); + if(ret != EXIT_SUCCESS){ + WOLFSSL_MSG("HW Shutdown Failure"); + return ret; + } + + /* Copy tmp result if needed */ + if((multipler == result) || (multiplicand == result) || + (exp == result) || (mod == result)){ + mp_copy(result_tmp_ptr, result); + } + + result->used = wc_MXC_MAA_adjustUsed(result->dp, length); + return ret; + } + + + + int wc_MXC_MAA_expmod(mp_int* base, mp_int* exp, mp_int* mod, + mp_int* result) + { + mp_int multiplicand; + multiplicand.dp[0] = 0x01; + multiplicand.used = 1; + WOLFSSL_MSG("Preparing exptmod MAA HW Call"); + return wc_MXC_MAA_math(base, &multiplicand, exp, mod, result, + WC_MXC_TPU_MAA_EXP); + } + + int wc_MXC_MAA_sqrmod(mp_int* multipler, mp_int* mod, mp_int* result) + { + mp_int multiplicand; + multiplicand.dp[0] = 0x01; + multiplicand.used = 1; + WOLFSSL_MSG("Preparing sqrmod MAA HW Call"); + return wc_MXC_MAA_math(multipler, &multiplicand, NULL, mod, result, + WC_MXC_TPU_MAA_SQ); + } + + int wc_MXC_MAA_mulmod(mp_int* multipler, mp_int* multiplicand, mp_int* mod, + mp_int* result) + { + WOLFSSL_MSG("Preparing mulmod MAA HW Call"); + return wc_MXC_MAA_math(multipler, multiplicand, NULL, mod, result, + WC_MXC_TPU_MAA_MUL); + } + + int wc_MXC_MAA_sqrmulmod(mp_int* multipler, mp_int* multiplicand, + mp_int* exp, mp_int* mod, mp_int* result) + { + WOLFSSL_MSG("Preparing sqrmulmod MAA HW Call"); + return wc_MXC_MAA_math(multipler, multiplicand, NULL, mod, result, + WC_MXC_TPU_MAA_SQMUL); + } + + int wc_MXC_MAA_addmod(mp_int* multipler, mp_int* multiplicand, mp_int* mod, + mp_int* result) + { + WOLFSSL_MSG("Preparing addmod MAA HW Call"); + return wc_MXC_MAA_math(multipler, multiplicand, NULL, mod, result, + WC_MXC_TPU_MAA_ADD); + } + + int wc_MXC_MAA_submod(mp_int* multipler, mp_int* multiplicand, mp_int* mod, + mp_int* result) + { + WOLFSSL_MSG("Preparing submod MAA HW Call"); + if((mod->used < multipler->used) || (mod->used < multiplicand->used)){ + WOLFSSL_MSG("HW Limitation: Defaulting back to software"); + return mxc_submod(multipler, multiplicand, mod, result); + } + else{ + return wc_MXC_MAA_math(multipler, multiplicand, NULL, mod, result, + WC_MXC_TPU_MAA_SUB); + } + } + + /* General Function to call hardware control */ + int hw_mulmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod, + mp_int* result) + { + if ((multiplier->used == 0) || (multiplicand->used == 0)) { + mp_zero(result); + return EXIT_SUCCESS; + } + else{ + if(wc_MXC_MAA_Fallback(3, multiplier->used, mod->used, + multiplicand->used) + != EXIT_SUCCESS){ + return mxc_mulmod(multiplier, multiplicand, mod, result); + } + else{ + return wc_MXC_MAA_mulmod(multiplier, multiplicand, mod, result); + } + } + } + + int hw_addmod(mp_int* a, mp_int* b, mp_int* m, mp_int* r) + { + int err = MP_OKAY; + /* Validate parameters. */ + if ((a == NULL) || (b == NULL) || (m == NULL) || (r == NULL)) { + err = MP_VAL; + } + if (err == MP_OKAY) { + if(wc_MXC_MAA_Fallback(3, a->used, b->used, m->used) + != EXIT_SUCCESS){ + err = mxc_addmod(a, b, m, r); + } + else{ + err = wc_MXC_MAA_addmod(a, b, m, r); + } + } + } + + + int hw_submod(mp_int* a, mp_int* b, mp_int* m, mp_int* r) + { + int err = MP_OKAY; + /* Validate parameters. */ + if ((a == NULL) || (b == NULL) || (m == NULL) || (r == NULL)) { + err = MP_VAL; + } + if (err == MP_OKAY) { + if(wc_MXC_MAA_Fallback(3, a->used, b->used, m->used) + != EXIT_SUCCESS){ + err = mxc_submod(a, b, m, r); + } + else{ + err = wc_MXC_MAA_submod(a, b, m, r); + } + } + return err; + } + + int hw_exptmod(mp_int* base, mp_int* exp, mp_int* mod, mp_int* result) + { + int err = MP_OKAY; + /* Validate parameters. */ + if ((base == NULL) || (exp == NULL) || (mod == NULL) || + (result == NULL)){ + err = MP_VAL; + } + if (err == MP_OKAY) { + if((mod->used < exp->used) || (mod->used < base->used)){ + err = mxc_exptmod(base, exp, mod, result); + } + else if(wc_MXC_MAA_Fallback(3, base->used, exp->used, mod->used) + != EXIT_SUCCESS){ + return mxc_exptmod(base, exp, mod, result); + } + else{ + err = wc_MXC_MAA_expmod(base, exp, mod, result); + } + } + return err; + } + + + /* No mod function avaliable with hardware, however preform a submod */ + /* (a - 0) mod m will essentially preform the same operation as a mod m */ + int hw_mod(mp_int* multiplier, mp_int* mod, mp_int* result) + { + if(wc_MXC_MAA_Fallback(2, multiplier->used, mod->used) + != EXIT_SUCCESS){ + return mxc_mod(multiplier, mod, result); + } + else{ + mp_int multiplicand; + multiplicand.dp[0] = 0x00; + multiplicand.used = 0x01; + return hw_submod(multiplier, &multiplicand, mod, result); + } + } + + int hw_sqrmod(mp_int* multiplier, mp_int* mod, mp_int* result) + { + if (multiplier->used == 0) { + mp_zero(result); + return EXIT_SUCCESS; + } + else{ + return wc_MXC_MAA_sqrmod(multiplier, mod, result); + } + + } + +#endif + + +#if defined(MAX3266X_RTC) + /* Initialize the RTC */ + int wc_MXC_RTC_Init(void) + { + /* RTC Init for benchmark */ + if (MXC_RTC_Init(0, 0) != E_NO_ERROR) { + return WC_HW_E; + } + + /* Disable the Interrupt */ + if (MXC_RTC_DisableInt(MXC_RTC_INT_EN_LONG) == E_BUSY) { + return WC_HW_E; + } + + if (MXC_RTC_SquareWaveStart(MXC_RTC_F_512HZ) == E_BUSY) { + return E_BUSY; + } + + if (MXC_RTC_Start() != E_NO_ERROR){ + return WC_HW_E; + } + + return EXIT_SUCCESS; + } + + /* Reset the RTC */ + int wc_MXC_RTC_Reset(void) + { + if (MXC_RTC_Stop() != E_NO_ERROR){ + return WC_HW_E; + } + if (wc_MXC_RTC_Init() != E_NO_ERROR){ + return WC_HW_E; + } + return EXIT_SUCCESS; + } + + /* Function to handle RTC read retries */ + void wc_MXC_RTC_GetRTCValue(int32_t (*rtcGetFunction)(uint32_t*), + uint32_t* outValue, int32_t* err) { + *err = rtcGetFunction(outValue); /* Initial attempt to get the value */ + while (*err != E_NO_ERROR) { + *err = rtcGetFunction(outValue); /* Retry if the error persists */ + } + } + + /* Function to provide the current time as a double */ + double wc_MXC_RTC_Time(void) { + int32_t err; + uint32_t rtc_seconds, rtc_subseconds; + + /* Retrieve sub-seconds from RTC */ + wc_MXC_RTC_GetRTCValue((int32_t (*)(uint32_t*))MXC_RTC_GetSubSeconds, \ + &rtc_subseconds, &err); + if (err != E_NO_ERROR) return (double)err; + + /* Retrieve seconds from RTC */ + wc_MXC_RTC_GetRTCValue((int32_t (*)(uint32_t*))MXC_RTC_GetSeconds, \ + &rtc_seconds, &err); + if (err != E_NO_ERROR) return (double)err; + return ((double)rtc_seconds + ((double)rtc_subseconds / 4096)); + } + +#endif /* MAX3266X_RTC */ + + +#endif /* WOLFSSL_MAX32665 || WOLFSSL_MAX32666 */ \ No newline at end of file diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index 9338d1a552..deb3696da0 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -136,6 +136,8 @@ This library contains implementation for the random number generator. #elif defined(WOLFSSL_GETRANDOM) #include #include +#elif defined(MAX3266X_RNG) + #include "wolfssl/wolfcrypt/port/maxim/max3266x.h" #else /* include headers that may be needed to get good seed */ #include @@ -3834,6 +3836,13 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return maxq10xx_random(output, sz); } +#elif defined(MAX3266X_RNG) + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + (void)os; + return wc_MXC_TRNG_Random(output, sz); + } + #elif defined(WOLFSSL_GETRANDOM) /* getrandom() was added to the Linux kernel in version 3.17. diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index ee534ff66c..72a07e424b 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -89,7 +89,6 @@ on the specific device platform. #include #endif - /* determine if we are using Espressif SHA hardware acceleration */ #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW #if defined(WOLFSSL_ESP32_CRYPT) && \ @@ -122,6 +121,8 @@ on the specific device platform. #elif defined(WOLFSSL_PSOC6_CRYPTO) +#elif (defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)) \ + && defined(MAX3266X_SHA) #else @@ -288,7 +289,6 @@ static int InitSha256(wc_Sha256* sha256) #ifdef HAVE_ARIA sha256->hSession = NULL; #endif - return 0; } #endif @@ -2469,6 +2469,9 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz) /* implemented in wolfcrypt/src/port/psa/psa_hash.c */ #elif defined(WOLFSSL_RENESAS_RX64_HASH) /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */ +#elif (defined(WOLFSSL_MAX32665) || defined (WOLFSSL_MAX32666)) \ + && defined(MAX3266X_SHA) + /* Implemented in wolfcrypt/src/port/maxim/max3266x.c */ #else diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 7ff02abf41..7c9307e8f1 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -40,6 +40,10 @@ #include #endif +#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) + #include +#endif + #ifdef WOLFSSL_PSOC6_CRYPTO #include #endif @@ -247,6 +251,16 @@ int wolfCrypt_Init(void) } #endif + #if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) + #if defined(MAX3266X_RTC) + ret = wc_MXC_RTC_Init(); + if (ret != 0){ + WOLFSSL_MSG("MXC RTC Init Failed"); + return WC_HW_E; + } + #endif + #endif + #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ defined(WOLFSSL_ATECC608A) ret = atmel_init(); @@ -3128,6 +3142,11 @@ time_t mqx_time(time_t* timer) #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */ +#if (defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)) \ + && defined(MAX3266X_RTC) + #warning "Using custom RTC Function for time for benchmarking and testing" + #define XTIME wc_MXC_RTC_Time +#endif #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME) diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index e73c10b9bd..71da5bd88c 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -77,6 +77,15 @@ #include #endif +#if defined(MAX3266X_MATH) + #include + #define mp_mulmod(a, b, c, d) hw_mulmod(a, b, c, d) + #define mp_addmod(a, b, c, d) hw_addmod(a, b, c, d) + #define mp_submod(a, b, c, d) hw_submod(a, b, c, d) + #define mp_exptmod(a, b, c, d) hw_exptmod(a, b, c, d) + #define mp_mod(a, b, c) hw_mod(a, b, c) + #define mp_sqrmod(a, b, c) hw_sqrmod(a, b, c) +#endif #ifdef __cplusplus extern "C" { diff --git a/wolfssl/wolfcrypt/port/maxim/max3266x.h b/wolfssl/wolfcrypt/port/maxim/max3266x.h new file mode 100644 index 0000000000..9d64d9e3da --- /dev/null +++ b/wolfssl/wolfcrypt/port/maxim/max3266x.h @@ -0,0 +1,281 @@ +/* max3266x.h + * + * Copyright (C) 2006-2023 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifndef _WOLFPORT_MAX3266X_H_ +#define _WOLFPORT_MAX3266X_H_ + +#include + + +#ifndef WOLFSSL_MAX_HASH_SIZE + #define WOLFSSL_MAX_HASH_SIZE 64 +#endif + +#if defined(WOLFSSL_MAX32665_OLD) + #if !defined (WOLFSSL_MAX32665) + #error Need WOLFSSL_MAX32665 Defined + #endif +#elif defined(WOLFSSL_MAX32665_OLD) + #if !defined (WOLFSSL_MAX32665) + #error Need WOLFSSL_MAX32666 Defined + #endif +#endif + +#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) + + #if defined(WOLFSSL_MAX32665_OLD) || defined(WOLFSSL_MAX32666_OLD) + #if !defined(MAX3266X_RTC) + #error Cannot use RTC Code with older sdk + #endif + #endif + +/* Default to all HW on unless specified in user_settings */ +#if !defined(MAX3266X_RNG) && !defined(MAX3266X_AES) && \ + !defined(MAX3266X_AESGCM) && !defined(MAX3266X_SHA) && \ + !defined(MAX3266X_ECDSA) && !defined(MAX3266X_MATH) + #define MAX3266X_RNG + #define MAX3266X_AES + #define MAX3266X_SHA + #define MAX3266X_ECDSA + #define MAX3266X_MATH +#endif + + + + +#if defined(WOLFSSL_MAX32665_OLD) || (WOLFSSL_MAX32666_OLD) + /* Support for older SDK Maxim provides */ + #if defined(MAX3266X_RNG) + #include "trng.h" /* Provides TRNG Drivers */ + #endif + #if defined(MAX3266X_AES) + #include "cipher.h" /* Provides Drivers for AES */ + /* AES Defines */ + #define MXC_TPU_CIPHER_TYPE tpu_ciphersel_t + #define MXC_TPU_CIPHER_AES128 TPU_CIPHER_AES128 + #define MXC_TPU_CIPHER_AES192 TPU_CIPHER_AES192 + #define MXC_TPU_CIPHER_AES256 TPU_CIPHER_AES256 + + #define MXC_AES_KEY_128_LEN MXC_AES_KEY_128_LEN + #define MXC_AES_KEY_192_LEN MXC_AES_KEY_192_LEN + #define MXC_AES_KEY_256_LEN MXC_AES_KEY_256_LEN + + #define MXC_TPU_MODE_TYPE tpu_modesel_t + #define MXC_TPU_MODE_ECB TPU_MODE_ECB + #define MXC_TPU_MODE_CBC TPU_MODE_CBC + #define MXC_TPU_MODE_CFB TPU_MODE_CFB + #define MXC_TPU_MODE_CTR TPU_MODE_CTR + + /* AES Functions */ + #define MXC_TPU_Cipher_Config TPU_Cipher_Config + #define MXC_TPU_Cipher_AES_Encrypt TPU_Cipher_AES_Encrypt + #define MXC_TPU_Cipher_AES_Decrypt TPU_Cipher_AES_Decrypt + + #endif + #if defined(MAX3266X_SHA) + #include "hash.h" /* Proivdes Drivers for SHA */ + /* SHA Defines */ + #define MXC_TPU_HASH_TYPE tpu_hashfunsel_t + #define MXC_TPU_HASH_SHA1 TPU_HASH_SHA1 + #define MXC_TPU_HASH_SHA224 TPU_HASH_SHA224 + #define MXC_TPU_HASH_SHA256 TPU_HASH_SHA256 + #define MXC_TPU_HASH_SHA384 TPU_HASH_SHA384 + #define MXC_TPU_HASH_SHA512 TPU_HASH_SHA512 + + /* SHA Functions */ + #define MXC_TPU_Hash_Config TPU_Hash_Config + #define MXC_TPU_Hash_SHA TPU_SHA + + #endif + #if defined(MAX3266X_MATH) + #include "maa.h" /* Provides Drivers for math acceleration for */ + /* ECDSA and RSA Acceleration */ + /* MAA Defines */ + #define MXC_TPU_MAA_TYPE mxc_tpu_maa_clcsel_t + #define WC_MXC_TPU_MAA_EXP 0b0000 + #define WC_MXC_TPU_MAA_SQ 0b0010 + #define WC_MXC_TPU_MAA_MUL 0b0100 + #define WC_MXC_TPU_MAA_SQMUL 0b0110 + #define WC_MXC_TPU_MAA_ADD 0b1000 + #define WC_MXC_TPU_MAA_SUB 0b1010 + + /* MAA Functions */ + #define MXC_TPU_MAA_Compute MAA_Compute + #define MXC_TPU_MAA_Shutdown MAA_Shutdown + #define MXC_TPU_MAA_Init MAA_Init + #define MXC_TPU_MAA_Reset MAA_Reset + + #endif + + /* TPU Functions */ + #define MXC_TPU_Init SYS_TPU_Init + #define MXC_TPU_Shutdown SYS_TPU_Shutdown + #define MXC_SYS_PERIPH_CLOCK_TPU SYS_PERIPH_CLOCK_TPU + + #define MXC_SYS_PERIPH_CLOCK_TPU MXC_SYS_PERIPH_CLOCK_TPU + #define MXC_SYS_PERIPH_CLOCK_TRNG MXC_SYS_PERIPH_CLOCK_TRNG + + #define MXC_TRNG MXC_TRNG + +#else + /* Defaults to expect newer SDK */ + #if defined(MAX3266X_RNG) + #include "trng.h" /* Provides Drivers for TRNG */ + #endif + #if defined(MAX3266X_AES) || defined(MAX3266X_SHA) || \ + defined(MAX3266X_ECDSA) || defined(MAX3266X_RSA) || defined(MAX3266X_RNG) + #include "tpu.h" /* SDK Drivers for the TPU unit */ + /* Handles AES, SHA, and */ + /* MAA driver to accelerate RSA/ECDSA */ + + /* AES Defines */ + #define MXC_TPU_CIPHER_TYPE mxc_tpu_ciphersel_t + #define MXC_TPU_MODE_TYPE mxc_tpu_modesel_t + + + /* SHA Defines */ + #define MXC_TPU_HASH_TYPE mxc_tpu_hashfunsel_t + + + /* MAA Defines */ + /* Current SDK for TPU does not handle bit mask correctly */ + /* with expected enum values, so calue need to be set */ + /* manually to work with intended naming scheme */ + #define MXC_TPU_MAA_TYPE mxc_tpu_maa_clcsel_t + #define WC_MXC_TPU_MAA_EXP 0b0000 + #define WC_MXC_TPU_MAA_SQ 0b0010 + #define WC_MXC_TPU_MAA_MUL 0b0100 + #define WC_MXC_TPU_MAA_SQMUL 0b0110 + #define WC_MXC_TPU_MAA_ADD 0b1000 + #define WC_MXC_TPU_MAA_SUB 0b1010 + + #endif + +#endif + + +/* Provide Driver for RTC if specified, meant for wolfCrypt benchmark only */ +#if defined(MAX3266X_RTC) + #if defined(WOLFSSL_MAX32665_OLD) || (WOLFSSL_MAX32666_OLD) +i #error Not Implemented with old SDK + #endif + #include "time.h" + #include "rtc.h" + #define MXC_SECS_PER_MIN (60) + #define MXC_SECS_PER_HR (60 * MXC_SECS_PER_MIN) + #define MXC_SECS_PER_DAY (24 * MXC_SECS_PER_HR) +#endif + +#include "mxc_errors.h" /* ERROR Codes */ +#include "mxc_device.h" +#include "board.h" +#include "max32665.h" + + +/* Variable Definitions */ + +#ifdef __cplusplus + extern "C" { +#endif + + 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); + +#ifdef MAX3266X_RTC + int wc_MXC_RTC_Init(void); + int wc_MXC_RTC_Reset(void); + double wc_MXC_RTC_Time(void); +#endif + + +#ifdef MAX3266X_RNG + int wc_MXC_TRNG_Random(unsigned char* output, unsigned int sz); +#endif + +#ifdef MAX3266X_AES + int wc_MXC_TPU_AesEncrypt(const char *in, const char *iv, + const char *enc_key, + MXC_TPU_MODE_TYPE mode, + unsigned int data_size, + char *out, unsigned int keySize); + + int wc_MXC_TPU_AesDecrypt(const char *in, const char *iv, + const char *enc_key, + MXC_TPU_MODE_TYPE mode, + unsigned int data_size, + char *out, unsigned int keySize); +#endif + +#ifdef MAX3266X_SHA + + typedef struct { + unsigned char *msg; + unsigned int used; + unsigned int size; + unsigned char hash[WOLFSSL_MAX_HASH_SIZE]; + } wc_MXC_Sha; + + #if !defined(NO_SHA256) + 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 + static const unsigned char MXC_EMPTY_DIGEST_SHA256[32] = + "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9" + "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52" + "\xb8\x55"; + #endif + + + int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash); + int wc_MXC_TPU_SHA_Update(wc_MXC_Sha *hash, const unsigned char* data, + unsigned int size); + int wc_MXC_TPU_SHA_Final(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo); + int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo); + int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst); + void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash); + int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest, + MXC_TPU_HASH_TYPE algo); + + +#endif + +#if defined(MAX3266X_MATH) + + int hw_mulmod(); + int hw_addmod(); + int hw_submod(); + int hw_exptmod(); + int hw_mod(); + int hw_sqrmod(); + +#endif + +#ifdef __cplusplus + } +#endif + +#endif /* WOLFSSL_MAX32665 || WOLFSSL_MAX32666 */ +#endif /* _WOLFPORT_MAX3266X_H_ */ diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index c944a4cbfa..e24a64ca1d 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -93,6 +93,16 @@ RSA keys can be used to encrypt, decrypt, sign and verify data. #include #endif +#if defined(MAX3266X_MATH) + #include + #define mp_mulmod(a, b, c, d) hw_mulmod(a, b, c, d) + #define mp_addmod(a, b, c, d) hw_addmod(a, b, c, d) + #define mp_submod(a, b, c, d) hw_submod(a, b, c, d) + #define mp_exptmod(a, b, c, d) hw_exptmod(a, b, c, d) + #define mp_mod(a, b, c) hw_mod(a, b, c) + #define mp_sqrmod(a, b, c) hw_sqrmod(a, b, c) +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/wolfssl/wolfcrypt/sha.h b/wolfssl/wolfcrypt/sha.h index 6ed5950265..f71bb35089 100644 --- a/wolfssl/wolfcrypt/sha.h +++ b/wolfssl/wolfcrypt/sha.h @@ -76,6 +76,10 @@ #if defined(WOLFSSL_SILABS_SE_ACCEL) #include #endif +#if (defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)) && \ + defined(WOLFSSL_SHA) + #include +#endif #if !defined(NO_OLD_SHA_NAMES) #define SHA WC_SHA diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index aa4632cf3e..29c4cde3e4 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -144,6 +144,9 @@ enum { #include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h" #elif defined(WOLFSSL_RENESAS_RX64_HASH) #include "wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h" +#elif (defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666)) \ + && defined(MAX3266X_SHA) + #include "wolfssl/wolfcrypt/port/maxim/max3266x.h" #else #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 0cf0eea656..e9a69c3571 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -54,6 +54,10 @@ #endif #endif +#if defined(WOLFSSL_MAX32665) || defined(WOLFSSL_MAX32666) + #include +#endif + #ifdef WOLFSSL_LINUXKM #include "../../linuxkm/linuxkm_wc_port.h" #endif /* WOLFSSL_LINUXKM */