Use a mutex in platform setup \ teardown functions

Use a singleton Mutex in platforms_alt functions, to be shared
with the trng function, to save RAM. Rename `platform_alt.c`
to `platform_alt.cpp` as the mutex is in a `singletonPtr`
template class.
pull/9493/head
Ron Eldor 2019-02-14 14:25:55 +02:00
parent c94b5861e4
commit 77f9faf46c
2 changed files with 14 additions and 7 deletions

View File

@ -17,17 +17,19 @@
#if DEVICE_TRNG #if DEVICE_TRNG
#include "hal/trng_api.h" #include "hal/trng_api.h"
#include "platform/SingletonPtr.h"
#include "platform/PlatformMutex.h" #include "platform/PlatformMutex.h"
SingletonPtr<PlatformMutex> mbedtls_mutex;
extern "C" extern "C"
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) { int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
static PlatformMutex trng_mutex;
trng_t trng_obj; trng_t trng_obj;
trng_mutex.lock(); mbedtls_mutex->lock();
trng_init(&trng_obj); trng_init(&trng_obj);
int ret = trng_get_bytes(&trng_obj, output, len, olen); int ret = trng_get_bytes(&trng_obj, output, len, olen);
trng_free(&trng_obj); trng_free(&trng_obj);
trng_mutex.unlock(); mbedtls_mutex->unlock();
return ret; return ret;
} }

View File

@ -20,33 +20,38 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
#include "mbed_critical.h" #include "platform/SingletonPtr.h"
#include "platform/PlatformMutex.h"
mbedtls_platform_context plat_ctx = { { 0 } }; mbedtls_platform_context plat_ctx = { { 0 } };
extern SingletonPtr<PlatformMutex> mbedtls_mutex;
int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx ) int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
{ {
int ret = 0; int ret = 0;
mbedtls_mutex->lock();
core_util_atomic_incr_u32( ( volatile uint32_t * )&plat_ctx.reference_count, 1 ); ++plat_ctx.reference_count;
if( plat_ctx.reference_count == 1 ) if( plat_ctx.reference_count == 1 )
{ {
/* call platform specific code to setup crypto driver */ /* call platform specific code to setup crypto driver */
ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx ); ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx );
} }
mbedtls_mutex->unlock();
return ( ret ); return ( ret );
} }
void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx ) void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx )
{ {
core_util_atomic_decr_u32( ( volatile uint32_t * )&plat_ctx.reference_count, 1 ); mbedtls_mutex->lock();
--plat_ctx.reference_count;
if( plat_ctx.reference_count < 1 ) if( plat_ctx.reference_count < 1 )
{ {
/* call platform specific code to terminate crypto driver */ /* call platform specific code to terminate crypto driver */
crypto_platform_terminate( &plat_ctx.platform_impl_ctx ); crypto_platform_terminate( &plat_ctx.platform_impl_ctx );
plat_ctx.reference_count = 0; plat_ctx.reference_count = 0;
} }
mbedtls_mutex->unlock();
} }
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/ #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/