Skip to content

Commit e3862d3

Browse files
committed
STM32 MBEDTLS_ALT use singleton
1 parent 59d2dd5 commit e3862d3

File tree

9 files changed

+79
-134
lines changed

9 files changed

+79
-134
lines changed

features/mbedtls/targets/TARGET_STM/aes_alt.c renamed to features/mbedtls/targets/TARGET_STM/aes_alt.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
#include "mbedtls/platform.h"
3939
#include "mbedtls/platform_util.h"
4040

41+
#include "platform/PlatformMutex.h"
42+
#include "platform/SingletonPtr.h"
43+
44+
static SingletonPtr<PlatformMutex> aes_mutex;
45+
4146
#define MBEDTLS_DEBUG 0
4247

4348
/* Parameter validation macros based on platform_util.h */
@@ -138,16 +143,9 @@ void mbedtls_aes_init(mbedtls_aes_context *ctx)
138143
{
139144
AES_VALIDATE(ctx != NULL);
140145

141-
__disable_irq();
142-
#if defined(MBEDTLS_THREADING_C)
143-
/* mutex cannot be initialized twice */
144-
if (!cryp_mutex_started) {
145-
mbedtls_mutex_init(&cryp_mutex);
146-
cryp_mutex_started = 1;
147-
}
148-
#endif /* MBEDTLS_THREADING_C */
146+
aes_mutex->lock();
149147
cryp_context_count++;
150-
__enable_irq();
148+
aes_mutex->unlock();
151149

152150
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
153151

@@ -167,23 +165,16 @@ void mbedtls_aes_free(mbedtls_aes_context *ctx)
167165
return;
168166
}
169167

170-
__disable_irq();
168+
aes_mutex->lock();
171169
if (cryp_context_count > 0) {
172170
cryp_context_count--;
173-
}
174171

175-
#if defined(MBEDTLS_THREADING_C)
176-
if (cryp_mutex_started) {
177-
mbedtls_mutex_free(&cryp_mutex);
178-
cryp_mutex_started = 0;
179-
}
180-
#endif /* MBEDTLS_THREADING_C */
181-
__enable_irq();
182-
183-
/* Shut down CRYP on last context */
184-
if (cryp_context_count == 0) {
185-
HAL_CRYP_DeInit(&ctx->hcryp_aes);
172+
/* Shut down CRYP on last context */
173+
if (cryp_context_count == 0) {
174+
HAL_CRYP_DeInit(&ctx->hcryp_aes);
175+
}
186176
}
177+
aes_mutex->unlock();
187178

188179
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
189180
}

features/mbedtls/targets/TARGET_STM/ccm_alt.c renamed to features/mbedtls/targets/TARGET_STM/ccm_alt.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
#include "mbedtls/platform.h"
3939
#include "mbedtls/platform_util.h"
4040

41+
#include "platform/PlatformMutex.h"
42+
#include "platform/SingletonPtr.h"
43+
44+
static SingletonPtr<PlatformMutex> ccm_mutex;
45+
4146
#define MBEDTLS_DEBUG 0
4247

4348
/* Parameter validation macros */
@@ -67,16 +72,9 @@ void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
6772
{
6873
CCM_VALIDATE(ctx != NULL);
6974

70-
__disable_irq();
71-
#if defined(MBEDTLS_THREADING_C)
72-
/* mutex cannot be initialized twice */
73-
if (!cryp_mutex_started) {
74-
mbedtls_mutex_init(&cryp_mutex);
75-
cryp_mutex_started = 1;
76-
}
77-
#endif /* MBEDTLS_THREADING_C */
75+
ccm_mutex->lock();
7876
cryp_context_count++;
79-
__enable_irq();
77+
ccm_mutex->unlock();
8078

8179
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
8280

@@ -186,23 +184,18 @@ void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
186184
return;
187185
}
188186

189-
__disable_irq();
187+
ccm_mutex->lock();
188+
190189
if (cryp_context_count > 0) {
191190
cryp_context_count--;
192-
}
193191

194-
#if defined(MBEDTLS_THREADING_C)
195-
if (cryp_mutex_started) {
196-
mbedtls_mutex_free(&cryp_mutex);
197-
cryp_mutex_started = 0;
192+
/* Shut down CRYP on last context */
193+
if (cryp_context_count == 0) {
194+
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
195+
}
198196
}
199-
#endif /* MBEDTLS_THREADING_C */
200-
__enable_irq();
201197

202-
/* Shut down CRYP on last context */
203-
if (cryp_context_count == 0) {
204-
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
205-
}
198+
ccm_mutex->unlock();
206199

207200
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
208201
}

features/mbedtls/targets/TARGET_STM/gcm_alt.c renamed to features/mbedtls/targets/TARGET_STM/gcm_alt.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
#include "mbedtls/platform_util.h"
3030
#include "mbedtls/platform.h"
3131

32+
#include "platform/PlatformMutex.h"
33+
#include "platform/SingletonPtr.h"
34+
35+
static SingletonPtr<PlatformMutex> gcm_mutex;
36+
3237

3338
/* Parameter validation macros */
3439
#define GCM_VALIDATE_RET( cond ) \
@@ -57,16 +62,9 @@ void mbedtls_gcm_init(mbedtls_gcm_context *ctx)
5762
{
5863
GCM_VALIDATE(ctx != NULL);
5964

60-
__disable_irq();
61-
#if defined(MBEDTLS_THREADING_C)
62-
/* mutex cannot be initialized twice */
63-
if (!cryp_mutex_started) {
64-
mbedtls_mutex_init(&cryp_mutex);
65-
cryp_mutex_started = 1;
66-
}
67-
#endif /* MBEDTLS_THREADING_C */
65+
sha1_mutex->lock();
6866
cryp_context_count++;
69-
__enable_irq();
67+
sha1_mutex->unlock();
7068

7169
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
7270
}
@@ -502,23 +500,16 @@ void mbedtls_gcm_free(mbedtls_gcm_context *ctx)
502500
return;
503501
}
504502

505-
__disable_irq();
503+
gcm_mutex->lock();
506504
if (cryp_context_count > 0) {
507505
cryp_context_count--;
508-
}
509506

510-
#if defined(MBEDTLS_THREADING_C)
511-
if (cryp_mutex_started) {
512-
mbedtls_mutex_free(&cryp_mutex);
513-
cryp_mutex_started = 0;
514-
}
515-
#endif /* MBEDTLS_THREADING_C */
516-
__enable_irq();
517-
518-
/* Shut down CRYP on last context */
519-
if (cryp_context_count == 0) {
520-
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
507+
/* Shut down CRYP on last context */
508+
if (cryp_context_count == 0) {
509+
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
510+
}
521511
}
512+
gcm_mutex->unlock();
522513

523514
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
524515
}

features/mbedtls/targets/TARGET_STM/md5_alt.c renamed to features/mbedtls/targets/TARGET_STM/md5_alt.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "mbedtls/platform.h"
3434
#include "mbedtls/platform_util.h"
3535

36+
#include "platform/PlatformMutex.h"
37+
#include "platform/SingletonPtr.h"
38+
39+
static SingletonPtr<PlatformMutex> md5_mutex;
3640

3741
/* Private typedef -----------------------------------------------------------*/
3842
/* Private define ------------------------------------------------------------*/
@@ -49,16 +53,9 @@ void mbedtls_md5_init(mbedtls_md5_context *ctx)
4953
{
5054
MD5_VALIDATE(ctx != NULL);
5155

52-
__disable_irq();
53-
#if defined(MBEDTLS_THREADING_C)
54-
/* mutex cannot be initialized twice */
55-
if (!hash_mutex_started) {
56-
mbedtls_mutex_init(&hash_mutex);
57-
hash_mutex_started = 1;
58-
}
59-
#endif /* MBEDTLS_THREADING_C */
56+
md5_mutex->lock();
6057
hash_context_count++;
61-
__enable_irq();
58+
md5_mutex->unlock();
6259

6360
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
6461
}
@@ -69,23 +66,16 @@ void mbedtls_md5_free(mbedtls_md5_context *ctx)
6966
return;
7067
}
7168

72-
__disable_irq();
69+
md5_mutex->lock();
7370
if (hash_context_count > 0) {
7471
hash_context_count--;
75-
}
7672

77-
#if defined(MBEDTLS_THREADING_C)
78-
if (hash_mutex_started) {
79-
mbedtls_mutex_free(&hash_mutex);
80-
hash_mutex_started = 0;
81-
}
82-
#endif /* MBEDTLS_THREADING_C */
83-
__enable_irq();
84-
85-
/* Shut down HASH on last context */
86-
if (hash_context_count == 0) {
87-
HAL_HASH_DeInit(&ctx->hhash);
73+
/* Shut down HASH on last context */
74+
if (hash_context_count == 0) {
75+
HAL_HASH_DeInit(&ctx->hhash);
76+
}
8877
}
78+
md5_mutex->unlock();
8979

9080
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
9181
}

features/mbedtls/targets/TARGET_STM/md5_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ mbedtls_md5_context;
6060

6161
#endif /* MBEDTLS_MD5_ALT */
6262

63-
#endif /* MBEDTLS_MD5_ALT_H */
63+
#endif /* MBEDTLS_MD5_ALT_H */

features/mbedtls/targets/TARGET_STM/sha1_alt.c renamed to features/mbedtls/targets/TARGET_STM/sha1_alt.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "mbedtls/platform.h"
3434
#include "mbedtls/platform_util.h"
3535

36+
#include "platform/PlatformMutex.h"
37+
#include "platform/SingletonPtr.h"
38+
39+
static SingletonPtr<PlatformMutex> sha1_mutex;
3640

3741
/* Private typedef -----------------------------------------------------------*/
3842
/* Private define ------------------------------------------------------------*/
@@ -49,16 +53,9 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
4953
{
5054
SHA1_VALIDATE(ctx != NULL);
5155

52-
__disable_irq();
53-
#if defined(MBEDTLS_THREADING_C)
54-
/* mutex cannot be initialized twice */
55-
if (!hash_mutex_started) {
56-
mbedtls_mutex_init(&hash_mutex);
57-
hash_mutex_started = 1;
58-
}
59-
#endif /* MBEDTLS_THREADING_C */
56+
sha1_mutex->lock();
6057
hash_context_count++;
61-
__enable_irq();
58+
sha1_mutex->unlock();
6259

6360
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
6461
}
@@ -69,23 +66,16 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
6966
return;
7067
}
7168

72-
__disable_irq();
69+
sha1_mutex->lock();
7370
if (hash_context_count > 0) {
7471
hash_context_count--;
75-
}
7672

77-
#if defined(MBEDTLS_THREADING_C)
78-
if (hash_mutex_started) {
79-
mbedtls_mutex_free(&hash_mutex);
80-
hash_mutex_started = 0;
81-
}
82-
#endif /* MBEDTLS_THREADING_C */
83-
__enable_irq();
84-
85-
/* Shut down HASH on last context */
86-
if (hash_context_count == 0) {
87-
HAL_HASH_DeInit(&ctx->hhash);
73+
/* Shut down HASH on last context */
74+
if (hash_context_count == 0) {
75+
HAL_HASH_DeInit(&ctx->hhash);
76+
}
8877
}
78+
sha1_mutex->unlock();
8979

9080
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
9181
}

features/mbedtls/targets/TARGET_STM/sha1_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ mbedtls_sha1_context;
5757

5858
#endif /* MBEDTLS_SHA1_ALT */
5959

60-
#endif /* MBEDTLS_SHA1_ALT_H */
60+
#endif /* MBEDTLS_SHA1_ALT_H */

features/mbedtls/targets/TARGET_STM/sha256_alt.c renamed to features/mbedtls/targets/TARGET_STM/sha256_alt.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "mbedtls/platform.h"
3434
#include "mbedtls/platform_util.h"
3535

36+
#include "platform/PlatformMutex.h"
37+
#include "platform/SingletonPtr.h"
38+
39+
static SingletonPtr<PlatformMutex> sha256_mutex;
3640

3741
/* Private typedef -----------------------------------------------------------*/
3842
/* Private define ------------------------------------------------------------*/
@@ -50,16 +54,9 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
5054
{
5155
SHA256_VALIDATE(ctx != NULL);
5256

53-
__disable_irq();
54-
#if defined(MBEDTLS_THREADING_C)
55-
/* mutex cannot be initialized twice */
56-
if (!hash_mutex_started) {
57-
mbedtls_mutex_init(&hash_mutex);
58-
hash_mutex_started = 1;
59-
}
60-
#endif /* MBEDTLS_THREADING_C */
57+
sha256_mutex->lock();
6158
hash_context_count++;
62-
__enable_irq();
59+
sha256_mutex->unlock();
6360

6461
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
6562
}
@@ -70,23 +67,16 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
7067
return;
7168
}
7269

73-
__disable_irq();
70+
sha256_mutex->lock();
7471
if (hash_context_count > 0) {
7572
hash_context_count--;
76-
}
7773

78-
#if defined(MBEDTLS_THREADING_C)
79-
if (hash_mutex_started) {
80-
mbedtls_mutex_free(&hash_mutex);
81-
hash_mutex_started = 0;
82-
}
83-
#endif /* MBEDTLS_THREADING_C */
84-
__enable_irq();
85-
86-
/* Shut down HASH on last context */
87-
if (hash_context_count == 0) {
88-
HAL_HASH_DeInit(&ctx->hhash);
74+
/* Shut down HASH on last context */
75+
if (hash_context_count == 0) {
76+
HAL_HASH_DeInit(&ctx->hhash);
77+
}
8978
}
79+
sha256_mutex->unlock();
9080

9181
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
9282
}

features/mbedtls/targets/TARGET_STM/sha256_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ mbedtls_sha256_context;
5757

5858

5959
#endif /* MBEDTLS_SHA256_ALT */
60-
#endif /* MBEDTLS_SHA256_ALT_H */
60+
#endif /* MBEDTLS_SHA256_ALT_H */

0 commit comments

Comments
 (0)