Skip to content

Commit 127b68f

Browse files
Ron EldorRon Eldor
authored andcommitted
Make the platform context a global variable
Make the platform context a global variable, adding the refernce counter to it.
1 parent c3b31bc commit 127b68f

File tree

5 files changed

+25
-35
lines changed

5 files changed

+25
-35
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/crypto_platform.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,22 @@
2222
#include "nrf52840.h"
2323
#include "sns_silib.h"
2424
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
25-
/* once https://github.com/ARMmbed/mbedtls/issues/1200 will be supported,
26-
* rndState should be part of mbedtls_platform_context
27-
* Until then, we should keep it global and extern */
2825

29-
CRYS_RND_State_t rndState = { { 0 } } ;
30-
CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
26+
static CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
3127

3228
int crypto_platform_setup( crypto_platform_ctx *ctx )
3329
{
3430
NRF_CRYPTOCELL->ENABLE = 1;
3531

36-
if( SaSi_LibInit( &rndState, &rndWorkBuff ) != 0 )
32+
if( SaSi_LibInit( &ctx->rndState, &rndWorkBuff ) != 0 )
3733
return ( MBEDTLS_PLATFORM_HW_FAILED );
3834

3935
return ( 0 );
4036
}
4137

4238
void crypto_platform_terminate( crypto_platform_ctx *ctx )
4339
{
44-
SaSi_LibFini( &rndState );
40+
SaSi_LibFini( &ctx->rndState );
4541
NRF_CRYPTOCELL->ENABLE = 0;
4642
}
4743

features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/crypto_platform.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* cc_platform.h
2+
* crypto_platform.h
33
*
44
* Copyright (C) 2018, Arm Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
@@ -17,21 +17,19 @@
1717
* limitations under the License.
1818
*
1919
*/
20-
#ifndef __CC_PLATFORM_H_
21-
#define __CC_PLATFORM_H_
20+
#ifndef __CRYPTO_PLATFORM_H_
21+
#define __CRYPTO_PLATFORM_H_
2222
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23+
#include "crys_rnd.h"
2324
/**
2425
* \brief The CC platform context structure.
2526
*
2627
* \note This structure may be used to assist platform-specific
2728
* setup or teardown operations.
2829
*/
2930
typedef struct {
30-
char dummy; /**< Placeholder member, as empty structs are not portable. */
31-
/*
32-
* Add CRYS_RND_State_t rndState; when https://github.com/ARMmbed/mbedtls/issues/1200 is supported
33-
*/
31+
CRYS_RND_State_t rndState;
3432
}
3533
crypto_platform_ctx;
3634
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
37-
#endif /* __CC_PLATFORM_H_ */
35+
#endif /* __CRYPTO_PLATFORM_H_ */

features/cryptocell/FEATURE_CRYPTOCELL310/trng.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
#include <string.h>
2424
#include "trng_api.h"
25+
#include "mbedtls/platform.h"
2526

26-
extern CRYS_RND_State_t rndState;
27-
extern CRYS_RND_WorkBuff_t rndWorkBuff;
27+
extern mbedtls_platform_context ctx;
28+
static CRYS_RND_WorkBuff_t rndWorkBuff;
2829

2930
/* Implementation that should never be optimized out by the compiler */
3031
static void mbedtls_zeroize( void *v, size_t n ) {
@@ -48,7 +49,7 @@ CRYSError_t LLF_RND_GetTrngSource(
4849

4950
void trng_init(trng_t *obj)
5051
{
51-
RNG_PLAT_SetUserRngParameters(&rndState, obj);
52+
RNG_PLAT_SetUserRngParameters(&ctx.platform_impl_ctx.rndState, obj);
5253
}
5354

5455
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
6667
uint32_t actualLength;
6768

6869
ret = LLF_RND_GetTrngSource(
69-
&rndState , /*in/out*/
70+
&ctx.platform_impl_ctx.rndState , /*in/out*/
7071
obj, /*in/out*/
7172
0, /*in*/
7273
&entropySizeBits, /*in/out*/

features/mbedtls/platform/inc/platform_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "platform_mbed.h"
2424
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
2525
#include "crypto_platform.h"
26-
2726
/**
2827
* \brief The platform context structure.
2928
*
@@ -32,6 +31,7 @@
3231
*/
3332
typedef struct {
3433
crypto_platform_ctx platform_impl_ctx; /* A context holding all the platform specific context for cryptography. Should be defined in crypto_platform.h */
34+
int reference_count;
3535
}
3636
mbedtls_platform_context;
3737

features/mbedtls/platform/src/platform_alt.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,32 @@
2020

2121
#include "mbedtls/platform.h"
2222
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23+
mbedtls_platform_context ctx = {0};
2324

24-
static int reference_count = 0;
25-
26-
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
25+
int mbedtls_platform_setup( mbedtls_platform_context *obsolete_ctx )
2726
{
2827
int ret = 0;
29-
if( ctx == NULL )
30-
return ( MBEDTLS_PLATFORM_INVALID_DATA );
3128

32-
reference_count++;
29+
ctx.reference_count++;
3330

34-
if( reference_count == 1 )
31+
if( ctx.reference_count == 1 )
3532
{
3633
/* call platform specific code to setup crypto driver*/
37-
ret = crypto_platform_setup( &ctx->platform_impl_ctx );
34+
ret = crypto_platform_setup( &ctx.platform_impl_ctx );
3835
}
3936
return ( ret );
4037
}
4138

42-
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
39+
void mbedtls_platform_teardown( mbedtls_platform_context *obsolete_ctx )
4340
{
44-
if( ctx == NULL )
45-
return;
4641

47-
reference_count--;
42+
ctx.reference_count--;
4843

49-
if( reference_count <= 0 )
44+
if( ctx.reference_count <= 0 )
5045
{
5146
/* call platform specific code to terminate crypto driver*/
52-
crypto_platform_terminate( &ctx->platform_impl_ctx );
53-
reference_count = 0;
47+
crypto_platform_terminate( &ctx.platform_impl_ctx );
48+
ctx.reference_count = 0;
5449
}
5550
}
5651

0 commit comments

Comments
 (0)