Merge pull request #2983 from dgarske/stm_stdperilib

Fixes for building with STM32 StdPeriLib and CubeMX
This commit is contained in:
toddouska
2020-05-21 10:39:36 -07:00
committed by GitHub
3 changed files with 30 additions and 22 deletions

View File

@@ -392,7 +392,7 @@
return ret; return ret;
#ifdef STM32_CRYPTO_AES_ONLY #ifdef STM32_CRYPTO_AES_ONLY
hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; hcryp.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB; hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
#elif defined(STM32_HAL_V2) #elif defined(STM32_HAL_V2)
@@ -5927,11 +5927,15 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)]; word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
#endif #endif
word32 keySize; word32 keySize;
#ifdef WOLFSSL_STM32_CUBEMX
int status = HAL_OK; int status = HAL_OK;
word32 blocks = sz / AES_BLOCK_SIZE; word32 blocks = sz / AES_BLOCK_SIZE;
word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)];
#else
int status = SUCCESS;
#endif
word32 partial = sz % AES_BLOCK_SIZE; word32 partial = sz % AES_BLOCK_SIZE;
word32 tag[AES_BLOCK_SIZE/sizeof(word32)]; word32 tag[AES_BLOCK_SIZE/sizeof(word32)];
word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)];
word32 ctr[AES_BLOCK_SIZE/sizeof(word32)]; word32 ctr[AES_BLOCK_SIZE/sizeof(word32)];
byte* authInPadded = NULL; byte* authInPadded = NULL;
int authPadSz; int authPadSz;
@@ -5983,10 +5987,10 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
#ifdef WOLFSSL_STM32_CUBEMX #ifdef WOLFSSL_STM32_CUBEMX
hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr; hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded; hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
#ifdef STM32_CRYPTO_AES_ONLY #ifdef STM32_CRYPTO_AES_ONLY
/* Set the CRYP parameters */ /* Set the CRYP parameters */
hcryp.Init.HeaderSize = authPadSz;
hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC; hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT; hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE; hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
@@ -6007,21 +6011,22 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
(blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT); (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
} }
} }
if (status == HAL_OK && (partial != 0 || blocks == 0)) { if (status == HAL_OK && (partial != 0 || (sz > 0 && blocks == 0))) {
/* GCM payload phase - partial remainder */ /* GCM payload phase - partial remainder */
XMEMSET(partialBlock, 0, sizeof(partialBlock)); XMEMSET(partialBlock, 0, sizeof(partialBlock));
XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial); XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial, status = HAL_CRYPEx_AES_Auth(&hcryp, (uint8_t*)partialBlock, partial,
partialBlock, STM32_HAL_TIMEOUT); (uint8_t*)partialBlock, STM32_HAL_TIMEOUT);
XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial); XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
} }
if (status == HAL_OK) { if (status == HAL_OK) {
/* GCM final phase */ /* GCM final phase */
hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE; hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT); status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, (uint8_t*)tag, STM32_HAL_TIMEOUT);
} }
#elif defined(STM32_HAL_V2) #elif defined(STM32_HAL_V2)
hcryp.Init.Algorithm = CRYP_AES_GCM; hcryp.Init.Algorithm = CRYP_AES_GCM;
hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE); ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE);
hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock; hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
HAL_CRYP_Init(&hcryp); HAL_CRYP_Init(&hcryp);
@@ -6035,6 +6040,7 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
STM32_HAL_TIMEOUT); STM32_HAL_TIMEOUT);
} }
#else #else
hcryp.Init.HeaderSize = authPadSz;
HAL_CRYP_Init(&hcryp); HAL_CRYP_Init(&hcryp);
if (blocks) { if (blocks) {
/* GCM payload phase - blocks */ /* GCM payload phase - blocks */
@@ -6045,13 +6051,13 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
/* GCM payload phase - partial remainder */ /* GCM payload phase - partial remainder */
XMEMSET(partialBlock, 0, sizeof(partialBlock)); XMEMSET(partialBlock, 0, sizeof(partialBlock));
XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial); XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, partialBlock, partial, status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, (uint8_t*)partialBlock, partial,
partialBlock, STM32_HAL_TIMEOUT); (uint8_t*)partialBlock, STM32_HAL_TIMEOUT);
XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial); XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
} }
if (status == HAL_OK) { if (status == HAL_OK) {
/* Compute the authTag */ /* Compute the authTag */
status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT); status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, (uint8_t*)tag, STM32_HAL_TIMEOUT);
} }
#endif #endif
@@ -6065,7 +6071,7 @@ static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz
(uint8_t*)keyCopy, keySize * 8, (uint8_t*)keyCopy, keySize * 8,
(uint8_t*)in, sz, (uint8_t*)in, sz,
(uint8_t*)authInPadded, authInSz, (uint8_t*)authInPadded, authInSz,
(uint8_t*)out, tag); (uint8_t*)out, (uint8_t*)tag);
if (status != SUCCESS) if (status != SUCCESS)
ret = AES_GCM_AUTH_E; ret = AES_GCM_AUTH_E;
#endif /* WOLFSSL_STM32_CUBEMX */ #endif /* WOLFSSL_STM32_CUBEMX */
@@ -6360,13 +6366,14 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
{ {
int ret; int ret;
#ifdef WOLFSSL_STM32_CUBEMX #ifdef WOLFSSL_STM32_CUBEMX
int status = HAL_OK;
CRYP_HandleTypeDef hcryp; CRYP_HandleTypeDef hcryp;
word32 blocks = sz / AES_BLOCK_SIZE;
#else #else
int status = SUCCESS;
word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)]; word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
#endif #endif
word32 keySize; word32 keySize;
int status = HAL_OK;
word32 blocks = sz / AES_BLOCK_SIZE;
word32 partial = sz % AES_BLOCK_SIZE; word32 partial = sz % AES_BLOCK_SIZE;
word32 tag[AES_BLOCK_SIZE/sizeof(word32)]; word32 tag[AES_BLOCK_SIZE/sizeof(word32)];
word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)]; word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)];
@@ -6421,10 +6428,10 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
#ifdef WOLFSSL_STM32_CUBEMX #ifdef WOLFSSL_STM32_CUBEMX
hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr; hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded; hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
#ifdef STM32_CRYPTO_AES_ONLY #ifdef STM32_CRYPTO_AES_ONLY
/* Set the CRYP parameters */ /* Set the CRYP parameters */
hcryp.Init.HeaderSize = authPadSz;
hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC; hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT; hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE; hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
@@ -6445,7 +6452,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
(blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT); (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
} }
} }
if (status == HAL_OK && (partial != 0 || blocks == 0)) { if (status == HAL_OK && (partial != 0 || (sz > 0 && blocks == 0))) {
/* GCM payload phase - partial remainder */ /* GCM payload phase - partial remainder */
XMEMSET(partialBlock, 0, sizeof(partialBlock)); XMEMSET(partialBlock, 0, sizeof(partialBlock));
XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial); XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
@@ -6459,6 +6466,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, (byte*)tag, STM32_HAL_TIMEOUT); status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, (byte*)tag, STM32_HAL_TIMEOUT);
} }
#elif defined(STM32_HAL_V2) #elif defined(STM32_HAL_V2)
hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
hcryp.Init.Algorithm = CRYP_AES_GCM; hcryp.Init.Algorithm = CRYP_AES_GCM;
ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE); ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE);
hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock; hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
@@ -6473,6 +6481,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
STM32_HAL_TIMEOUT); STM32_HAL_TIMEOUT);
} }
#else #else
hcryp.Init.HeaderSize = authPadSz;
HAL_CRYP_Init(&hcryp); HAL_CRYP_Init(&hcryp);
if (blocks) { if (blocks) {
/* GCM payload phase - blocks */ /* GCM payload phase - blocks */
@@ -6508,7 +6517,7 @@ static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
(uint8_t*)keyCopy, keySize * 8, (uint8_t*)keyCopy, keySize * 8,
(uint8_t*)in, sz, (uint8_t*)in, sz,
(uint8_t*)authInPadded, authInSz, (uint8_t*)authInPadded, authInSz,
(uint8_t*)out, tag); (uint8_t*)out, (uint8_t*)tag);
if (status != SUCCESS) if (status != SUCCESS)
ret = AES_GCM_AUTH_E; ret = AES_GCM_AUTH_E;
#endif /* WOLFSSL_STM32_CUBEMX */ #endif /* WOLFSSL_STM32_CUBEMX */

View File

@@ -345,7 +345,7 @@
return 0; return 0;
} }
static void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz, static int Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz,
int dir) int dir)
{ {
if (des == NULL || out == NULL || in == NULL) if (des == NULL || out == NULL || in == NULL)
@@ -460,18 +460,17 @@
CRYP_Cmd(DISABLE); CRYP_Cmd(DISABLE);
} }
#endif /* WOLFSSL_STM32_CUBEMX */ #endif /* WOLFSSL_STM32_CUBEMX */
return 0;
} }
int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
{ {
Des3Crypt(des, out, in, sz, DES_ENCRYPTION); return Des3Crypt(des, out, in, sz, DES_ENCRYPTION);
return 0;
} }
int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
{ {
Des3Crypt(des, out, in, sz, DES_DECRYPTION); return Des3Crypt(des, out, in, sz, DES_DECRYPTION);
return 0;
} }
#elif defined(HAVE_COLDFIRE_SEC) #elif defined(HAVE_COLDFIRE_SEC)