Skip to content

Commit c72d008

Browse files
committed
add STM32_HW_CLOCK_AUTO which turns the stm32 hw
accleration clock on and off automatically
1 parent fbadcf6 commit c72d008

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,11 +2941,6 @@ int benchmark_init(void)
29412941

29422942
benchmark_static_init(0);
29432943

2944-
#ifdef WOLFSSL_STM32_CUBEMX
2945-
/* enable the peripheral clock */
2946-
__HAL_RCC_CRYP_CLK_ENABLE();
2947-
#endif
2948-
29492944
#ifdef WOLFSSL_STATIC_MEMORY
29502945
ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
29512946
sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
@@ -3054,11 +3049,6 @@ int benchmark_free(void)
30543049
printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
30553050
}
30563051

3057-
#ifdef WOLFSSL_STM32_CUBEMX
3058-
/* disable the peripheral clock */
3059-
__HAL_RCC_CRYP_CLK_DISABLE();
3060-
#endif
3061-
30623052
return ret;
30633053
}
30643054

wolfcrypt/src/aes.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
417417
CRYP_Cmd(DISABLE);
418418
#endif /* WOLFSSL_STM32_CUBEMX */
419419
wolfSSL_CryptHwMutexUnLock();
420+
wc_Stm32_Aes_Cleanup();
420421

421422
return ret;
422423
}
@@ -520,6 +521,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
520521
CRYP_Cmd(DISABLE);
521522
#endif /* WOLFSSL_STM32_CUBEMX */
522523
wolfSSL_CryptHwMutexUnLock();
524+
wc_Stm32_Aes_Cleanup();
523525

524526
return ret;
525527
}
@@ -3562,6 +3564,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
35623564
HAL_CRYP_DeInit(&hcryp);
35633565

35643566
wolfSSL_CryptHwMutexUnLock();
3567+
wc_Stm32_Aes_Cleanup();
35653568

35663569
return ret;
35673570
}
@@ -3624,6 +3627,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
36243627

36253628
HAL_CRYP_DeInit(&hcryp);
36263629
wolfSSL_CryptHwMutexUnLock();
3630+
wc_Stm32_Aes_Cleanup();
36273631

36283632
return ret;
36293633
}
@@ -3708,6 +3712,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
37083712
/* disable crypto processor */
37093713
CRYP_Cmd(DISABLE);
37103714
wolfSSL_CryptHwMutexUnLock();
3715+
wc_Stm32_Aes_Cleanup();
37113716

37123717
return ret;
37133718
}
@@ -3802,6 +3807,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
38023807
/* disable crypto processor */
38033808
CRYP_Cmd(DISABLE);
38043809
wolfSSL_CryptHwMutexUnLock();
3810+
wc_Stm32_Aes_Cleanup();
38053811

38063812
return ret;
38073813
}
@@ -4562,6 +4568,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
45624568
#endif /* WOLFSSL_STM32_CUBEMX */
45634569

45644570
wolfSSL_CryptHwMutexUnLock();
4571+
wc_Stm32_Aes_Cleanup();
45654572
return ret;
45664573
}
45674574

@@ -6708,6 +6715,7 @@ static WARN_UNUSED_RESULT int wc_AesGcmEncrypt_STM32(
67086715
ret = AES_GCM_AUTH_E;
67096716
#endif /* WOLFSSL_STM32_CUBEMX */
67106717
wolfSSL_CryptHwMutexUnLock();
6718+
wc_Stm32_Aes_Cleanup();
67116719

67126720
if (ret == 0) {
67136721
/* return authTag */
@@ -7242,6 +7250,7 @@ static WARN_UNUSED_RESULT int wc_AesGcmDecrypt_STM32(
72427250
XMEMCPY(tag, partialBlock, authTagSz);
72437251
#endif /* WOLFSSL_STM32_CUBEMX */
72447252
wolfSSL_CryptHwMutexUnLock();
7253+
wc_Stm32_Aes_Cleanup();
72457254

72467255
/* Check authentication tag */
72477256
if (ConstantCompare((const byte*)tagExpected, (byte*)tag, authTagSz) != 0) {

wolfcrypt/src/port/st/stm32.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_HandleTypeDef* hcryp)
394394
{
395395
int ret;
396396
word32 keySize;
397+
#ifdef STM32_HW_CLOCK_AUTO
398+
/* enable the peripheral clock */
399+
__HAL_RCC_CRYP_CLK_ENABLE();
400+
#endif
397401

398402
ret = wc_AesGetKeySize(aes, &keySize);
399403
if (ret != 0)
@@ -428,6 +432,13 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_HandleTypeDef* hcryp)
428432
return 0;
429433
}
430434

435+
void wc_Stm32_Aes_Cleanup(void)
436+
{
437+
#ifdef STM32_HW_CLOCK_AUTO
438+
/* disable the peripheral clock */
439+
__HAL_RCC_CRYP_CLK_DISABLE();
440+
#endif
441+
}
431442
#else /* Standard Peripheral Library */
432443

433444
int wc_Stm32_Aes_Init(Aes* aes, CRYP_InitTypeDef* cryptInit,
@@ -486,6 +497,10 @@ int wc_Stm32_Aes_Init(Aes* aes, CRYP_InitTypeDef* cryptInit,
486497

487498
return 0;
488499
}
500+
501+
void wc_Stm32_Aes_Cleanup(void)
502+
{
503+
}
489504
#endif /* WOLFSSL_STM32_CUBEMX */
490505
#endif /* !NO_AES */
491506
#endif /* STM32_CRYPTO */

wolfssl/wolfcrypt/port/st/stm32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
168168
struct Aes;
169169
#ifdef WOLFSSL_STM32_CUBEMX
170170
int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_HandleTypeDef* hcryp);
171+
void wc_Stm32_Aes_Cleanup(void);
171172
#else /* Standard Peripheral Library */
172173
int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_InitTypeDef* cryptInit,
173174
CRYP_KeyInitTypeDef* keyInit);
175+
void wc_Stm32_Aes_Cleanup(void);
174176
#endif /* WOLFSSL_STM32_CUBEMX */
175177
#endif /* !NO_AES */
176178

0 commit comments

Comments
 (0)