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.h"
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> aes_mutex;
|
||||||
|
|
||||||
#define MBEDTLS_DEBUG 0
|
#define MBEDTLS_DEBUG 0
|
||||||
|
|
||||||
/* Parameter validation macros based on platform_util.h */
|
/* Parameter validation macros based on platform_util.h */
|
||||||
|
@ -138,16 +143,9 @@ void mbedtls_aes_init(mbedtls_aes_context *ctx)
|
||||||
{
|
{
|
||||||
AES_VALIDATE(ctx != NULL);
|
AES_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
aes_mutex->lock();
|
||||||
#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 */
|
|
||||||
cryp_context_count++;
|
cryp_context_count++;
|
||||||
__enable_irq();
|
aes_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
||||||
|
|
||||||
|
@ -167,23 +165,16 @@ void mbedtls_aes_free(mbedtls_aes_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
aes_mutex->lock();
|
||||||
if (cryp_context_count > 0) {
|
if (cryp_context_count > 0) {
|
||||||
cryp_context_count--;
|
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 */
|
/* Shut down CRYP on last context */
|
||||||
if (cryp_context_count == 0) {
|
if (cryp_context_count == 0) {
|
||||||
HAL_CRYP_DeInit(&ctx->hcryp_aes);
|
HAL_CRYP_DeInit(&ctx->hcryp_aes);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
aes_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_aes_context));
|
||||||
}
|
}
|
|
@ -38,6 +38,11 @@
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> ccm_mutex;
|
||||||
|
|
||||||
#define MBEDTLS_DEBUG 0
|
#define MBEDTLS_DEBUG 0
|
||||||
|
|
||||||
/* Parameter validation macros */
|
/* Parameter validation macros */
|
||||||
|
@ -67,16 +72,9 @@ void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
|
||||||
{
|
{
|
||||||
CCM_VALIDATE(ctx != NULL);
|
CCM_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
ccm_mutex->lock();
|
||||||
#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 */
|
|
||||||
cryp_context_count++;
|
cryp_context_count++;
|
||||||
__enable_irq();
|
ccm_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
||||||
|
|
||||||
|
@ -186,23 +184,18 @@ void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
ccm_mutex->lock();
|
||||||
|
|
||||||
if (cryp_context_count > 0) {
|
if (cryp_context_count > 0) {
|
||||||
cryp_context_count--;
|
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 */
|
/* Shut down CRYP on last context */
|
||||||
if (cryp_context_count == 0) {
|
if (cryp_context_count == 0) {
|
||||||
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
|
HAL_CRYP_DeInit(&ctx->hcryp_ccm);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ccm_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_ccm_context));
|
||||||
}
|
}
|
|
@ -29,6 +29,11 @@
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> gcm_mutex;
|
||||||
|
|
||||||
|
|
||||||
/* Parameter validation macros */
|
/* Parameter validation macros */
|
||||||
#define GCM_VALIDATE_RET( cond ) \
|
#define GCM_VALIDATE_RET( cond ) \
|
||||||
|
@ -57,16 +62,9 @@ void mbedtls_gcm_init(mbedtls_gcm_context *ctx)
|
||||||
{
|
{
|
||||||
GCM_VALIDATE(ctx != NULL);
|
GCM_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
sha1_mutex->lock();
|
||||||
#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 */
|
|
||||||
cryp_context_count++;
|
cryp_context_count++;
|
||||||
__enable_irq();
|
sha1_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
||||||
}
|
}
|
||||||
|
@ -502,23 +500,16 @@ void mbedtls_gcm_free(mbedtls_gcm_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
gcm_mutex->lock();
|
||||||
if (cryp_context_count > 0) {
|
if (cryp_context_count > 0) {
|
||||||
cryp_context_count--;
|
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 */
|
/* Shut down CRYP on last context */
|
||||||
if (cryp_context_count == 0) {
|
if (cryp_context_count == 0) {
|
||||||
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
|
HAL_CRYP_DeInit(&ctx->hcryp_gcm);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
gcm_mutex->unlock();
|
||||||
|
|
||||||
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
cryp_zeroize((void *)ctx, sizeof(mbedtls_gcm_context));
|
||||||
}
|
}
|
|
@ -33,6 +33,10 @@
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> md5_mutex;
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
/* Private define ------------------------------------------------------------*/
|
/* Private define ------------------------------------------------------------*/
|
||||||
|
@ -49,16 +53,9 @@ void mbedtls_md5_init(mbedtls_md5_context *ctx)
|
||||||
{
|
{
|
||||||
MD5_VALIDATE(ctx != NULL);
|
MD5_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
md5_mutex->lock();
|
||||||
#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 */
|
|
||||||
hash_context_count++;
|
hash_context_count++;
|
||||||
__enable_irq();
|
md5_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
||||||
}
|
}
|
||||||
|
@ -69,23 +66,16 @@ void mbedtls_md5_free(mbedtls_md5_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
md5_mutex->lock();
|
||||||
if (hash_context_count > 0) {
|
if (hash_context_count > 0) {
|
||||||
hash_context_count--;
|
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 */
|
/* Shut down HASH on last context */
|
||||||
if (hash_context_count == 0) {
|
if (hash_context_count == 0) {
|
||||||
HAL_HASH_DeInit(&ctx->hhash);
|
HAL_HASH_DeInit(&ctx->hhash);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
md5_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
hash_zeroize(ctx, sizeof(mbedtls_md5_context));
|
||||||
}
|
}
|
|
@ -33,6 +33,10 @@
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> sha1_mutex;
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
/* Private define ------------------------------------------------------------*/
|
/* Private define ------------------------------------------------------------*/
|
||||||
|
@ -49,16 +53,9 @@ void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
|
||||||
{
|
{
|
||||||
SHA1_VALIDATE(ctx != NULL);
|
SHA1_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
sha1_mutex->lock();
|
||||||
#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 */
|
|
||||||
hash_context_count++;
|
hash_context_count++;
|
||||||
__enable_irq();
|
sha1_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
||||||
}
|
}
|
||||||
|
@ -69,23 +66,16 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
sha1_mutex->lock();
|
||||||
if (hash_context_count > 0) {
|
if (hash_context_count > 0) {
|
||||||
hash_context_count--;
|
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 */
|
/* Shut down HASH on last context */
|
||||||
if (hash_context_count == 0) {
|
if (hash_context_count == 0) {
|
||||||
HAL_HASH_DeInit(&ctx->hhash);
|
HAL_HASH_DeInit(&ctx->hhash);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
sha1_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
hash_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
||||||
}
|
}
|
|
@ -33,6 +33,10 @@
|
||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
|
|
||||||
|
#include "platform/PlatformMutex.h"
|
||||||
|
#include "platform/SingletonPtr.h"
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> sha256_mutex;
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
/* Private define ------------------------------------------------------------*/
|
/* Private define ------------------------------------------------------------*/
|
||||||
|
@ -50,16 +54,9 @@ void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
||||||
{
|
{
|
||||||
SHA256_VALIDATE(ctx != NULL);
|
SHA256_VALIDATE(ctx != NULL);
|
||||||
|
|
||||||
__disable_irq();
|
sha256_mutex->lock();
|
||||||
#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 */
|
|
||||||
hash_context_count++;
|
hash_context_count++;
|
||||||
__enable_irq();
|
sha256_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||||
}
|
}
|
||||||
|
@ -70,23 +67,16 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
__disable_irq();
|
sha256_mutex->lock();
|
||||||
if (hash_context_count > 0) {
|
if (hash_context_count > 0) {
|
||||||
hash_context_count--;
|
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 */
|
/* Shut down HASH on last context */
|
||||||
if (hash_context_count == 0) {
|
if (hash_context_count == 0) {
|
||||||
HAL_HASH_DeInit(&ctx->hhash);
|
HAL_HASH_DeInit(&ctx->hhash);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
sha256_mutex->unlock();
|
||||||
|
|
||||||
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
hash_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||||
}
|
}
|
Loading…
Reference in New Issue