Skip to content

Commit

Permalink
add STM32_HW_CLOCK_AUTO which turns the stm32 hw
Browse files Browse the repository at this point in the history
accleration clock on and off automatically
  • Loading branch information
jpbland1 committed Jun 27, 2023
1 parent fbadcf6 commit c72d008
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 0 additions & 10 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -2941,11 +2941,6 @@ int benchmark_init(void)

benchmark_static_init(0);

#ifdef WOLFSSL_STM32_CUBEMX
/* enable the peripheral clock */
__HAL_RCC_CRYP_CLK_ENABLE();
#endif

#ifdef WOLFSSL_STATIC_MEMORY
ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
Expand Down Expand Up @@ -3054,11 +3049,6 @@ int benchmark_free(void)
printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
}

#ifdef WOLFSSL_STM32_CUBEMX
/* disable the peripheral clock */
__HAL_RCC_CRYP_CLK_DISABLE();
#endif

return ret;
}

Expand Down
9 changes: 9 additions & 0 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
CRYP_Cmd(DISABLE);
#endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -520,6 +521,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
CRYP_Cmd(DISABLE);
#endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -3562,6 +3564,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
HAL_CRYP_DeInit(&hcryp);

wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -3624,6 +3627,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)

HAL_CRYP_DeInit(&hcryp);
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -3708,6 +3712,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
/* disable crypto processor */
CRYP_Cmd(DISABLE);
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -3802,6 +3807,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
/* disable crypto processor */
CRYP_Cmd(DISABLE);
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

return ret;
}
Expand Down Expand Up @@ -4562,6 +4568,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
#endif /* WOLFSSL_STM32_CUBEMX */

wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();
return ret;
}

Expand Down Expand Up @@ -6708,6 +6715,7 @@ static WARN_UNUSED_RESULT int wc_AesGcmEncrypt_STM32(
ret = AES_GCM_AUTH_E;
#endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

if (ret == 0) {
/* return authTag */
Expand Down Expand Up @@ -7242,6 +7250,7 @@ static WARN_UNUSED_RESULT int wc_AesGcmDecrypt_STM32(
XMEMCPY(tag, partialBlock, authTagSz);
#endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL_CryptHwMutexUnLock();
wc_Stm32_Aes_Cleanup();

/* Check authentication tag */
if (ConstantCompare((const byte*)tagExpected, (byte*)tag, authTagSz) != 0) {
Expand Down
15 changes: 15 additions & 0 deletions wolfcrypt/src/port/st/stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_HandleTypeDef* hcryp)
{
int ret;
word32 keySize;
#ifdef STM32_HW_CLOCK_AUTO
/* enable the peripheral clock */
__HAL_RCC_CRYP_CLK_ENABLE();
#endif

ret = wc_AesGetKeySize(aes, &keySize);
if (ret != 0)
Expand Down Expand Up @@ -428,6 +432,13 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_HandleTypeDef* hcryp)
return 0;
}

void wc_Stm32_Aes_Cleanup(void)
{
#ifdef STM32_HW_CLOCK_AUTO
/* disable the peripheral clock */
__HAL_RCC_CRYP_CLK_DISABLE();
#endif
}
#else /* Standard Peripheral Library */

int wc_Stm32_Aes_Init(Aes* aes, CRYP_InitTypeDef* cryptInit,
Expand Down Expand Up @@ -486,6 +497,10 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_InitTypeDef* cryptInit,

return 0;
}

void wc_Stm32_Aes_Cleanup(void)
{
}
#endif /* WOLFSSL_STM32_CUBEMX */
#endif /* !NO_AES */
#endif /* STM32_CRYPTO */
Expand Down
2 changes: 2 additions & 0 deletions wolfssl/wolfcrypt/port/st/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
struct Aes;
#ifdef WOLFSSL_STM32_CUBEMX
int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_HandleTypeDef* hcryp);
void wc_Stm32_Aes_Cleanup(void);
#else /* Standard Peripheral Library */
int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_InitTypeDef* cryptInit,
CRYP_KeyInitTypeDef* keyInit);
void wc_Stm32_Aes_Cleanup(void);
#endif /* WOLFSSL_STM32_CUBEMX */
#endif /* !NO_AES */

Expand Down

0 comments on commit c72d008

Please sign in to comment.