diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index b70baf66d..62fd72609 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -288,7 +288,7 @@ /* Define AES implementation includes and functions */ #if defined(STM32_CRYPTO) - /* STM32F2/F4/F7/L4 hardware AES support for ECB, CBC, CTR and GCM modes */ + /* STM32F2/F4/F7/L4/L5/H7/WB55 hardware AES support for ECB, CBC, CTR and GCM modes */ #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM) || defined(HAVE_AESCCM) @@ -4028,18 +4028,6 @@ static WC_INLINE void IncrementGcmCounter(byte* inOutCtr) return; } } -#ifdef STM32_CRYPTO_AES_GCM -static WC_INLINE void DecrementGcmCounter(byte* inOutCtr) -{ - int i; - - /* in network byte order so start at end and work back */ - for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) { - if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */ - return; - } -} -#endif /* STM32_CRYPTO_AES_GCM */ #endif /* !FREESCALE_LTC_AES_GCM */ #if defined(GCM_SMALL) || defined(GCM_TABLE) @@ -5932,6 +5920,7 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz #endif word32 partial = sz % AES_BLOCK_SIZE; word32 tag[AES_BLOCK_SIZE/sizeof(word32)]; + word32 ctrInit[AES_BLOCK_SIZE/sizeof(word32)]; word32 ctr[AES_BLOCK_SIZE/sizeof(word32)]; word32 authhdr[AES_BLOCK_SIZE/sizeof(word32)]; byte* authInPadded = NULL; @@ -5961,6 +5950,7 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz else { GHASH(aes, NULL, 0, iv, ivSz, (byte*)ctr, AES_BLOCK_SIZE); } + XMEMCPY(ctrInit, ctr, sizeof(ctr)); /* save off initial counter for GMAC */ /* Authentication buffer - must be 4-byte multiple zero padded */ authPadSz = authInSz % sizeof(word32); @@ -6082,16 +6072,13 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz ret = AES_GCM_AUTH_E; #endif /* WOLFSSL_STM32_CUBEMX */ - /* hardware requires +1, so subtract it */ - DecrementGcmCounter((byte*)ctr); - if (ret == 0) { /* return authTag */ if (authTag) { /* For STM32 GCM fallback to software if partial AES block or IV != 12 */ if (sz == 0 || partial != 0 || ivSz != GCM_NONCE_MID_SZ) { GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz); - wc_AesEncrypt(aes, (byte*)ctr, (byte*)tag); + wc_AesEncrypt(aes, (byte*)ctrInit, (byte*)tag); xorbuf(authTag, tag, authTagSz); } else { @@ -6379,6 +6366,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, word32 tag[AES_BLOCK_SIZE/sizeof(word32)]; word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)]; word32 ctr[AES_BLOCK_SIZE/sizeof(word32)]; + word32 ctrInit[AES_BLOCK_SIZE/sizeof(word32)]; word32 authhdr[AES_BLOCK_SIZE/sizeof(word32)]; byte* authInPadded = NULL; int authPadSz, wasAlloc = 0; @@ -6407,6 +6395,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, else { GHASH(aes, NULL, 0, iv, ivSz, (byte*)ctr, AES_BLOCK_SIZE); } + XMEMCPY(ctrInit, ctr, sizeof(ctr)); /* save off initial counter for GMAC */ /* Authentication buffer - must be 4-byte multiple zero padded */ authPadSz = authInSz % sizeof(word32); @@ -6533,13 +6522,10 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, ret = AES_GCM_AUTH_E; #endif /* WOLFSSL_STM32_CUBEMX */ - /* hardware requires +1, so subtract it */ - DecrementGcmCounter((byte*)ctr); - /* For STM32 GCM fallback to software if partial AES block or IV != 12 */ if (sz == 0 || partial != 0 || ivSz != GCM_NONCE_MID_SZ) { GHASH(aes, authIn, authInSz, in, sz, (byte*)tag, sizeof(tag)); - wc_AesEncrypt(aes, (byte*)ctr, (byte*)partialBlock); + wc_AesEncrypt(aes, (byte*)ctrInit, (byte*)partialBlock); xorbuf(tag, partialBlock, sizeof(tag)); } diff --git a/wolfcrypt/src/port/st/stm32.c b/wolfcrypt/src/port/st/stm32.c index 2c3aac27d..552ef454c 100644 --- a/wolfcrypt/src/port/st/stm32.c +++ b/wolfcrypt/src/port/st/stm32.c @@ -440,32 +440,27 @@ static const uint8_t stm32_ecc192_prime[ECC192_KEYSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static const uint32_t stm32_ecc192_coef_sign = 1U; - static const uint8_t stm32_ecc192_coef[ECC192_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 }; - static const uint8_t stm32_ecc192_pointX[ECC192_KEYSIZE] = { 0x18, 0x8D, 0xA8, 0x0E, 0xB0, 0x30, 0x90, 0xF6, 0x7C, 0xBF, 0x20, 0xEB, 0x43, 0xA1, 0x88, 0x00, 0xF4, 0xFF, 0x0A, 0xFD, 0x82, 0xFF, 0x10, 0x12 }; - -const uint8_t stm32_ecc192_pointY[ECC192_KEYSIZE] = { +static const uint8_t stm32_ecc192_pointY[ECC192_KEYSIZE] = { 0x07, 0x19, 0x2B, 0x95, 0xFF, 0xC8, 0xDA, 0x78, 0x63, 0x10, 0x11, 0xED, 0x6B, 0x24, 0xCD, 0xD5, 0x73, 0xF9, 0x77, 0xA1, 0x1E, 0x79, 0x48, 0x11 }; - -const uint8_t stm32_ecc192_order[ECC192_KEYSIZE] = { +static const uint8_t stm32_ecc192_order[ECC192_KEYSIZE] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x99, 0xDE, 0xF8, 0x36, 0x14, 0x6B, 0xC9, 0xB1, 0xB4, 0xD2, 0x28, 0x31 }; -const uint32_t stm32_ecc192_cofactor = 1U; - +static const uint32_t stm32_ecc192_cofactor = 1U; #endif /* ECC192 */ /* P-224 */ @@ -478,38 +473,37 @@ static const uint8_t stm32_ecc224_prime[ECC224_KEYSIZE] = { 0x00, 0x00, 0x00, 0x01 }; static const uint32_t stm32_ecc224_coef_sign = 1U; - static const uint8_t stm32_ecc224_coef[ECC224_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 }; - static const uint8_t stm32_ecc224_pointX[ECC224_KEYSIZE] = { 0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, 0x90, 0xB9, 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xD6, 0x11, 0x5C, 0x1D, 0x21 }; - -const uint8_t stm32_ecc224_pointY[ECC224_KEYSIZE] = { +static const uint8_t stm32_ecc224_pointY[ECC224_KEYSIZE] = { 0xBD, 0x37, 0x63, 0x88, 0xB5, 0xF7, 0x23, 0xFB, 0x4C, 0x22, 0xDF, 0xE6, 0xCD, 0x43, 0x75, 0xA0, 0x5A, 0x07, 0x47, 0x64, 0x44, 0xD5, 0x81, 0x99, 0x85, 0x00, 0x7E, 0x34 }; - -const uint8_t stm32_ecc224_order[ECC224_KEYSIZE] = { +static const uint8_t stm32_ecc224_order[ECC224_KEYSIZE] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x16, 0xA2, + 0xE0, 0xB8, 0xF0, 0x3E, 0x13, 0xDD, 0x29, 0x45, + 0x5C, 0x5C, 0x2A, 0x3D }; -const uint32_t stm32_ecc224_cofactor = 1U; +static const uint32_t stm32_ecc224_cofactor = 1U; #endif /* ECC224 */ /* P-256 */ #ifdef ECC256 #define ECC256_KEYSIZE (32) - static const uint8_t stm32_ecc256_prime[ECC256_KEYSIZE] = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -517,42 +511,37 @@ static const uint8_t stm32_ecc256_prime[ECC256_KEYSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static const uint32_t stm32_ecc256_coef_sign = 1U; - static const uint8_t stm32_ecc256_coef[ECC256_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 }; - static const uint8_t stm32_ecc256_pointX[ECC256_KEYSIZE] = { 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96 }; - -const uint8_t stm32_ecc256_pointY[ECC256_KEYSIZE] = { +static const uint8_t stm32_ecc256_pointY[ECC256_KEYSIZE] = { 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 }; - -const uint8_t stm32_ecc256_order[ECC256_KEYSIZE] = { +static const uint8_t stm32_ecc256_order[ECC256_KEYSIZE] = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51 }; -const uint32_t stm32_ecc256_cofactor = 1U; +static const uint32_t stm32_ecc256_cofactor = 1U; #endif /* ECC256 */ /* P-384 */ #ifdef ECC384 #define ECC384_KEYSIZE (48) - static const uint8_t stm32_ecc384_prime[ECC384_KEYSIZE] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -562,7 +551,6 @@ static const uint8_t stm32_ecc384_prime[ECC384_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }; static const uint32_t stm32_ecc384_coef_sign = 1U; - static const uint8_t stm32_ecc384_coef[ECC384_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -571,7 +559,6 @@ static const uint8_t stm32_ecc384_coef[ECC384_KEYSIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 }; - static const uint8_t stm32_ecc384_pointX[ECC384_KEYSIZE] = { 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E, 0xF3, 0x20, 0xAD, 0x74, @@ -580,8 +567,7 @@ static const uint8_t stm32_ecc384_pointX[ECC384_KEYSIZE] = { 0x55, 0x02, 0xF2, 0x5D, 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7, }; - -const uint8_t stm32_ecc384_pointY[ECC384_KEYSIZE] = { +static const uint8_t stm32_ecc384_pointY[ECC384_KEYSIZE] = { 0x36, 0x17, 0xDE, 0x4A, 0x96, 0x26, 0x2C, 0x6F, 0x5D, 0x9E, 0x98, 0xBF, 0x92, 0x92, 0xDC, 0x29, 0xF8, 0xF4, 0x1D, 0xBD, 0x28, 0x9A, 0x14, 0x7C, @@ -589,12 +575,17 @@ const uint8_t stm32_ecc384_pointY[ECC384_KEYSIZE] = { 0x0A, 0x60, 0xB1, 0xCE, 0x1D, 0x7E, 0x81, 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E, 0x5F, }; - -const uint8_t stm32_ecc384_order[ECC384_KEYSIZE] = { +static const uint8_t stm32_ecc384_order[ECC384_KEYSIZE] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xC7, 0x63, 0x4D, 0x81, 0xF4, 0x37, 0x2D, 0xDF, + 0x58, 0x1A, 0x0D, 0xB2, 0x48, 0xB0, 0xA7, 0x7A, + 0xEC, 0xEC, 0x19, 0x6A, 0xCC, 0xC5, 0x29, 0x73 }; -const uint32_t stm32_ecc384_cofactor = 1U; - +static const uint32_t stm32_ecc384_cofactor = 1U; #endif /* ECC384 */ + static int stm32_get_ecc_specs(const uint8_t **prime, const uint8_t **coef, const uint32_t **coef_sign, const uint8_t **GenPointX, const uint8_t **GenPointY, const uint8_t **order, int size) @@ -615,6 +606,7 @@ static int stm32_get_ecc_specs(const uint8_t **prime, const uint8_t **coef, *GenPointX = stm32_ecc224_pointX; *GenPointY = stm32_ecc224_pointY; *coef_sign = &stm32_ecc224_coef; + *order = stm32_ecc224_order; break; #endif #ifdef ECC192 @@ -624,6 +616,7 @@ static int stm32_get_ecc_specs(const uint8_t **prime, const uint8_t **coef, *GenPointX = stm32_ecc192_pointX; *GenPointY = stm32_ecc192_pointY; *coef_sign = &stm32_ecc192_coef; + *order = stm32_ecc192_order; break; #endif #ifdef ECC384 @@ -633,6 +626,7 @@ static int stm32_get_ecc_specs(const uint8_t **prime, const uint8_t **coef, *GenPointX = stm32_ecc384_pointX; *GenPointY = stm32_ecc384_pointY; *coef_sign = &stm32_ecc384_coef; + *order = stm32_ecc384_order; break; #endif default: diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index b30359c95..7779dc0d9 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -823,33 +823,30 @@ char* wc_strsep(char **stringp, const char *delim) static wolfSSL_Mutex wcCryptHwMutex; static int wcCryptHwMutexInit = 0; -int wolfSSL_CryptHwMutexInit(void) { +int wolfSSL_CryptHwMutexInit(void) +{ int ret = 0; - if(wcCryptHwMutexInit == 0) { + if (wcCryptHwMutexInit == 0) { ret = wc_InitMutex(&wcCryptHwMutex); - if(ret == 0) { + if (ret == 0) { wcCryptHwMutexInit = 1; } } return ret; } - -int wolfSSL_CryptHwMutexLock(void) { +int wolfSSL_CryptHwMutexLock(void) +{ int ret = BAD_MUTEX_E; - - /* Make sure HW Mutex has been initialized */ - wolfSSL_CryptHwMutexInit(); - - if(wcCryptHwMutexInit) { + wolfSSL_CryptHwMutexInit(); /* Make sure HW Mutex has been initialized */ + if (wcCryptHwMutexInit) { ret = wc_LockMutex(&wcCryptHwMutex); } return ret; } - -int wolfSSL_CryptHwMutexUnLock(void) { +int wolfSSL_CryptHwMutexUnLock(void) +{ int ret = BAD_MUTEX_E; - - if(wcCryptHwMutexInit) { + if (wcCryptHwMutexInit) { ret = wc_UnLockMutex(&wcCryptHwMutex); } return ret;