mirror of https://github.com/ARMmbed/mbed-os.git
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
parent
c94b5861e4
commit
77f9faf46c
|
|
@ -17,17 +17,19 @@
|
|||
#if DEVICE_TRNG
|
||||
|
||||
#include "hal/trng_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
SingletonPtr<PlatformMutex> mbedtls_mutex;
|
||||
|
||||
extern "C"
|
||||
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
|
||||
static PlatformMutex trng_mutex;
|
||||
trng_t trng_obj;
|
||||
trng_mutex.lock();
|
||||
mbedtls_mutex->lock();
|
||||
trng_init(&trng_obj);
|
||||
int ret = trng_get_bytes(&trng_obj, output, len, olen);
|
||||
trng_free(&trng_obj);
|
||||
trng_mutex.unlock();
|
||||
mbedtls_mutex->unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,33 +20,38 @@
|
|||
|
||||
#include "mbedtls/platform.h"
|
||||
#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 } };
|
||||
extern SingletonPtr<PlatformMutex> mbedtls_mutex;
|
||||
|
||||
int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
core_util_atomic_incr_u32( ( volatile uint32_t * )&plat_ctx.reference_count, 1 );
|
||||
mbedtls_mutex->lock();
|
||||
++plat_ctx.reference_count;
|
||||
|
||||
if( plat_ctx.reference_count == 1 )
|
||||
{
|
||||
/* call platform specific code to setup crypto driver */
|
||||
ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx );
|
||||
}
|
||||
mbedtls_mutex->unlock();
|
||||
return ( ret );
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
/* call platform specific code to terminate crypto driver */
|
||||
crypto_platform_terminate( &plat_ctx.platform_impl_ctx );
|
||||
plat_ctx.reference_count = 0;
|
||||
}
|
||||
mbedtls_mutex->unlock();
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/
|
||||
Loading…
Reference in New Issue