diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index dcab06a81..260d70fc1 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -1807,7 +1807,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) aes->keylen = keylen; aes->rounds = keylen/4 + 6; XMEMCPY(rk, userKey, keylen); - #ifndef WOLFSSL_STM32_CUBEMX + #if !defined(WOLFSSL_STM32_CUBEMX) || defined(STM32_HAL_V2) ByteReverseWords(rk, rk, keylen); #endif #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) @@ -2467,6 +2467,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv) hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; #elif defined(STM32_HAL_V2) hcryp.Init.Algorithm = CRYP_AES_CBC; + ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE); #endif hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg; HAL_CRYP_Init(&hcryp); @@ -2519,6 +2520,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv) hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; #elif defined(STM32_HAL_V2) hcryp.Init.Algorithm = CRYP_AES_CBC; + ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE); #endif hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg; @@ -3205,6 +3207,9 @@ int wc_AesSetIV(Aes* aes, const byte* iv) int ret = 0; #ifdef WOLFSSL_STM32_CUBEMX CRYP_HandleTypeDef hcryp; + #ifdef STM32_HAL_V2 + word32 iv[AES_BLOCK_SIZE/sizeof(word32)]; + #endif #else word32 *iv; CRYP_InitTypeDef cryptInit; @@ -3221,10 +3226,12 @@ int wc_AesSetIV(Aes* aes, const byte* iv) hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CTR; hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; + hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg; #elif defined(STM32_HAL_V2) hcryp.Init.Algorithm = CRYP_AES_CTR; + ByteReverseWords(iv, aes->reg, AES_BLOCK_SIZE); + hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)iv; #endif - hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg; HAL_CRYP_Init(&hcryp); #ifdef STM32_CRYPTO_AES_ONLY @@ -5345,6 +5352,9 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz int ret; #ifdef WOLFSSL_STM32_CUBEMX CRYP_HandleTypeDef hcryp; + #ifdef STM32_HAL_V2 + word32 ivWord[AES_BLOCK_SIZE/sizeof(word32)]; + #endif #else word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)]; #endif @@ -5436,6 +5446,8 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz } #elif defined(STM32_HAL_V2) hcryp.Init.Algorithm = CRYP_AES_GCM; + ByteReverseWords(ivWord, (word32*)ctr, AES_BLOCK_SIZE); + hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ivWord; HAL_CRYP_Init(&hcryp); if (blocks) { /* GCM payload phase - blocks */ @@ -5764,6 +5776,9 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, int ret; #ifdef WOLFSSL_STM32_CUBEMX CRYP_HandleTypeDef hcryp; + #ifdef STM32_HAL_V2 + word32 ivWord[AES_BLOCK_SIZE/sizeof(word32)]; + #endif #else word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)]; #endif @@ -5855,6 +5870,8 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, } #elif defined(STM32_HAL_V2) hcryp.Init.Algorithm = CRYP_AES_GCM; + ByteReverseWords(ivWord, (word32*)ctr, AES_BLOCK_SIZE); + hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ivWord; HAL_CRYP_Init(&hcryp); if (blocks) { /* GCM payload phase - blocks */ diff --git a/wolfcrypt/src/port/st/stm32.c b/wolfcrypt/src/port/st/stm32.c index d0d54b026..0c24799a7 100644 --- a/wolfcrypt/src/port/st/stm32.c +++ b/wolfcrypt/src/port/st/stm32.c @@ -293,6 +293,9 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_HandleTypeDef* hcryp) hcryp->Instance = CRYP; hcryp->Init.DataType = CRYP_DATATYPE_8B; hcryp->Init.pKey = (STM_CRYPT_TYPE*)aes->key; +#ifdef STM32_HAL_V2 + hcryp->Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE; +#endif return 0; }