mirror of https://github.com/ARMmbed/mbed-os.git
STM32 MBEDTLS_ALT use singleton
parent
59d2dd5e1f
commit
e3862d3430
|
@ -38,6 +38,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> aes_mutex;
|
||||
|
||||
#define MBEDTLS_DEBUG 0
|
||||
|
||||
/* Parameter validation macros based on platform_util.h */
|
||||
|
@ -138,16 +143,9 @@ void mbedtls_aes_init(mbedtls_aes_context *ctx)
|
|||
{
|
||||
AES_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!cryp_mutex_started) {
|
||||
mbedtls_mutex_init(&cryp_mutex);
|
||||
cryp_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
aes_mutex->lock();
|
||||
cryp_context_count++;
|
||||
__enable_irq();
|
||||
aes_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
||||
|
||||
|
@ -167,23 +165,16 @@ void mbedtls_aes_free(mbedtls_aes_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
aes_mutex->lock();
|
||||
if (cryp_context_count > 0) {
|
||||
cryp_context_count--;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (cryp_mutex_started) {
|
||||
mbedtls_mutex_free(&cryp_mutex);
|
||||
cryp_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_aes);
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_aes);
|
||||
}
|
||||
}
|
||||
aes_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
||||
}
|
|
@ -38,6 +38,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> ccm_mutex;
|
||||
|
||||
#define MBEDTLS_DEBUG 0
|
||||
|
||||
/* Parameter validation macros */
|
||||
|
@ -67,16 +72,9 @@ void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
|
|||
{
|
||||
CCM_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!cryp_mutex_started) {
|
||||
mbedtls_mutex_init(&cryp_mutex);
|
||||
cryp_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
ccm_mutex->lock();
|
||||
cryp_context_count++;
|
||||
__enable_irq();
|
||||
ccm_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
||||
|
||||
|
@ -186,23 +184,18 @@ void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
ccm_mutex->lock();
|
||||
|
||||
if (cryp_context_count > 0) {
|
||||
cryp_context_count--;
|
||||
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (cryp_mutex_started) {
|
||||
mbedtls_mutex_free(&cryp_mutex);
|
||||
cryp_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
|
||||
}
|
||||
ccm_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
||||
}
|
|
@ -29,6 +29,11 @@
|
|||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> gcm_mutex;
|
||||
|
||||
|
||||
/* Parameter validation macros */
|
||||
#define GCM_VALIDATE_RET( cond ) \
|
||||
|
@ -57,16 +62,9 @@ void mbedtls_gcm_init(mbedtls_gcm_context *ctx)
|
|||
{
|
||||
GCM_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!cryp_mutex_started) {
|
||||
mbedtls_mutex_init(&cryp_mutex);
|
||||
cryp_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
sha1_mutex->lock();
|
||||
cryp_context_count++;
|
||||
__enable_irq();
|
||||
sha1_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
||||
}
|
||||
|
@ -502,23 +500,16 @@ void mbedtls_gcm_free(mbedtls_gcm_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
gcm_mutex->lock();
|
||||
if (cryp_context_count > 0) {
|
||||
cryp_context_count--;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (cryp_mutex_started) {
|
||||
mbedtls_mutex_free(&cryp_mutex);
|
||||
cryp_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
|
||||
/* Shut down CRYP on last context */
|
||||
if (cryp_context_count == 0) {
|
||||
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
|
||||
}
|
||||
}
|
||||
gcm_mutex->unlock();
|
||||
|
||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
||||
}
|
|
@ -33,6 +33,10 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> md5_mutex;
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
|
@ -49,16 +53,9 @@ void mbedtls_md5_init(mbedtls_md5_context *ctx)
|
|||
{
|
||||
MD5_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!hash_mutex_started) {
|
||||
mbedtls_mutex_init(&hash_mutex);
|
||||
hash_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
md5_mutex->lock();
|
||||
hash_context_count++;
|
||||
__enable_irq();
|
||||
md5_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
||||
}
|
||||
|
@ -69,23 +66,16 @@ void mbedtls_md5_free(mbedtls_md5_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
md5_mutex->lock();
|
||||
if (hash_context_count > 0) {
|
||||
hash_context_count--;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (hash_mutex_started) {
|
||||
mbedtls_mutex_free(&hash_mutex);
|
||||
hash_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
}
|
||||
}
|
||||
md5_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
||||
}
|
|
@ -60,4 +60,4 @@ mbedtls_md5_context;
|
|||
|
||||
#endif /* MBEDTLS_MD5_ALT */
|
||||
|
||||
#endif /* MBEDTLS_MD5_ALT_H */
|
||||
#endif /* MBEDTLS_MD5_ALT_H */
|
||||
|
|
|
@ -33,6 +33,10 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> sha1_mutex;
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
|
@ -49,16 +53,9 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
|
|||
{
|
||||
SHA1_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!hash_mutex_started) {
|
||||
mbedtls_mutex_init(&hash_mutex);
|
||||
hash_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
sha1_mutex->lock();
|
||||
hash_context_count++;
|
||||
__enable_irq();
|
||||
sha1_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
||||
}
|
||||
|
@ -69,23 +66,16 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
sha1_mutex->lock();
|
||||
if (hash_context_count > 0) {
|
||||
hash_context_count--;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (hash_mutex_started) {
|
||||
mbedtls_mutex_free(&hash_mutex);
|
||||
hash_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
}
|
||||
}
|
||||
sha1_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
||||
}
|
|
@ -57,4 +57,4 @@ mbedtls_sha1_context;
|
|||
|
||||
#endif /* MBEDTLS_SHA1_ALT */
|
||||
|
||||
#endif /* MBEDTLS_SHA1_ALT_H */
|
||||
#endif /* MBEDTLS_SHA1_ALT_H */
|
||||
|
|
|
@ -33,6 +33,10 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
static SingletonPtr<PlatformMutex> sha256_mutex;
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
|
@ -50,16 +54,9 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
|||
{
|
||||
SHA256_VALIDATE(ctx != NULL);
|
||||
|
||||
__disable_irq();
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
/* mutex cannot be initialized twice */
|
||||
if (!hash_mutex_started) {
|
||||
mbedtls_mutex_init(&hash_mutex);
|
||||
hash_mutex_started = 1;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
sha256_mutex->lock();
|
||||
hash_context_count++;
|
||||
__enable_irq();
|
||||
sha256_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||
}
|
||||
|
@ -70,23 +67,16 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
|||
return;
|
||||
}
|
||||
|
||||
__disable_irq();
|
||||
sha256_mutex->lock();
|
||||
if (hash_context_count > 0) {
|
||||
hash_context_count--;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (hash_mutex_started) {
|
||||
mbedtls_mutex_free(&hash_mutex);
|
||||
hash_mutex_started = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
__enable_irq();
|
||||
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
/* Shut down HASH on last context */
|
||||
if (hash_context_count == 0) {
|
||||
HAL_HASH_DeInit(&ctx->hhash);
|
||||
}
|
||||
}
|
||||
sha256_mutex->unlock();
|
||||
|
||||
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||
}
|
|
@ -57,4 +57,4 @@ mbedtls_sha256_context;
|
|||
|
||||
|
||||
#endif /* MBEDTLS_SHA256_ALT */
|
||||
#endif /* MBEDTLS_SHA256_ALT_H */
|
||||
#endif /* MBEDTLS_SHA256_ALT_H */
|
||||
|
|
Loading…
Reference in New Issue