mirror of https://github.com/ARMmbed/mbed-os.git
Make the platform context a global variable
Make the platform context a global variable, adding the refernce counter to it.pull/7099/head
parent
c3b31bc500
commit
127b68fbbc
|
|
@ -22,18 +22,14 @@
|
|||
#include "nrf52840.h"
|
||||
#include "sns_silib.h"
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
/* once https://github.com/ARMmbed/mbedtls/issues/1200 will be supported,
|
||||
* rndState should be part of mbedtls_platform_context
|
||||
* Until then, we should keep it global and extern */
|
||||
|
||||
CRYS_RND_State_t rndState = { { 0 } } ;
|
||||
CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
|
||||
static CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
|
||||
|
||||
int crypto_platform_setup( crypto_platform_ctx *ctx )
|
||||
{
|
||||
NRF_CRYPTOCELL->ENABLE = 1;
|
||||
|
||||
if( SaSi_LibInit( &rndState, &rndWorkBuff ) != 0 )
|
||||
if( SaSi_LibInit( &ctx->rndState, &rndWorkBuff ) != 0 )
|
||||
return ( MBEDTLS_PLATFORM_HW_FAILED );
|
||||
|
||||
return ( 0 );
|
||||
|
|
@ -41,7 +37,7 @@ int crypto_platform_setup( crypto_platform_ctx *ctx )
|
|||
|
||||
void crypto_platform_terminate( crypto_platform_ctx *ctx )
|
||||
{
|
||||
SaSi_LibFini( &rndState );
|
||||
SaSi_LibFini( &ctx->rndState );
|
||||
NRF_CRYPTOCELL->ENABLE = 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* cc_platform.h
|
||||
* crypto_platform.h
|
||||
*
|
||||
* Copyright (C) 2018, Arm Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
|
|
@ -17,9 +17,10 @@
|
|||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
#ifndef __CC_PLATFORM_H_
|
||||
#define __CC_PLATFORM_H_
|
||||
#ifndef __CRYPTO_PLATFORM_H_
|
||||
#define __CRYPTO_PLATFORM_H_
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
#include "crys_rnd.h"
|
||||
/**
|
||||
* \brief The CC platform context structure.
|
||||
*
|
||||
|
|
@ -27,11 +28,8 @@
|
|||
* setup or teardown operations.
|
||||
*/
|
||||
typedef struct {
|
||||
char dummy; /**< Placeholder member, as empty structs are not portable. */
|
||||
/*
|
||||
* Add CRYS_RND_State_t rndState; when https://github.com/ARMmbed/mbedtls/issues/1200 is supported
|
||||
*/
|
||||
CRYS_RND_State_t rndState;
|
||||
}
|
||||
crypto_platform_ctx;
|
||||
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
|
||||
#endif /* __CC_PLATFORM_H_ */
|
||||
#endif /* __CRYPTO_PLATFORM_H_ */
|
||||
|
|
|
|||
|
|
@ -22,9 +22,10 @@
|
|||
|
||||
#include <string.h>
|
||||
#include "trng_api.h"
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
extern CRYS_RND_State_t rndState;
|
||||
extern CRYS_RND_WorkBuff_t rndWorkBuff;
|
||||
extern mbedtls_platform_context ctx;
|
||||
static CRYS_RND_WorkBuff_t rndWorkBuff;
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
||||
|
|
@ -48,7 +49,7 @@ CRYSError_t LLF_RND_GetTrngSource(
|
|||
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
RNG_PLAT_SetUserRngParameters(&rndState, obj);
|
||||
RNG_PLAT_SetUserRngParameters(&ctx.platform_impl_ctx.rndState, obj);
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
|
|
@ -66,7 +67,7 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLe
|
|||
uint32_t actualLength;
|
||||
|
||||
ret = LLF_RND_GetTrngSource(
|
||||
&rndState , /*in/out*/
|
||||
&ctx.platform_impl_ctx.rndState , /*in/out*/
|
||||
obj, /*in/out*/
|
||||
0, /*in*/
|
||||
&entropySizeBits, /*in/out*/
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
#include "platform_mbed.h"
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
#include "crypto_platform.h"
|
||||
|
||||
/**
|
||||
* \brief The platform context structure.
|
||||
*
|
||||
|
|
@ -32,6 +31,7 @@
|
|||
*/
|
||||
typedef struct {
|
||||
crypto_platform_ctx platform_impl_ctx; /* A context holding all the platform specific context for cryptography. Should be defined in crypto_platform.h */
|
||||
int reference_count;
|
||||
}
|
||||
mbedtls_platform_context;
|
||||
|
||||
|
|
|
|||
|
|
@ -20,37 +20,32 @@
|
|||
|
||||
#include "mbedtls/platform.h"
|
||||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
||||
mbedtls_platform_context ctx = {0};
|
||||
|
||||
static int reference_count = 0;
|
||||
|
||||
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
|
||||
int mbedtls_platform_setup( mbedtls_platform_context *obsolete_ctx )
|
||||
{
|
||||
int ret = 0;
|
||||
if( ctx == NULL )
|
||||
return ( MBEDTLS_PLATFORM_INVALID_DATA );
|
||||
|
||||
reference_count++;
|
||||
ctx.reference_count++;
|
||||
|
||||
if( reference_count == 1 )
|
||||
if( ctx.reference_count == 1 )
|
||||
{
|
||||
/* call platform specific code to setup crypto driver*/
|
||||
ret = crypto_platform_setup( &ctx->platform_impl_ctx );
|
||||
ret = crypto_platform_setup( &ctx.platform_impl_ctx );
|
||||
}
|
||||
return ( ret );
|
||||
}
|
||||
|
||||
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
|
||||
void mbedtls_platform_teardown( mbedtls_platform_context *obsolete_ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
|
||||
reference_count--;
|
||||
ctx.reference_count--;
|
||||
|
||||
if( reference_count <= 0 )
|
||||
if( ctx.reference_count <= 0 )
|
||||
{
|
||||
/* call platform specific code to terminate crypto driver*/
|
||||
crypto_platform_terminate( &ctx->platform_impl_ctx );
|
||||
reference_count = 0;
|
||||
crypto_platform_terminate( &ctx.platform_impl_ctx );
|
||||
ctx.reference_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue