Skip to content

Commit 3be076c

Browse files
author
Cruz Monrreal
authored
Merge pull request #7099 from RonEld/platform_context_reference_count
Add reference counter for platform context
2 parents e7c166e + a2531b5 commit 3be076c

File tree

7 files changed

+64
-56
lines changed

7 files changed

+64
-56
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ To port your CC 310 driver to Mbed OS on your specific target, do the following:
1616
1. In `objects.h`, include `objects_cryptocell.h`. You can use the `FEATURE_CRYPTOCELL310` precompilation check as defined above.
1717
1. In `features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_<target name>`, add your platform-specific libraries for all toolchains in `TOOLCHAIN_ARM`, `TOOLCHAIN_GCC_ARM` and `TOOLCHAIN_IAR` respectively.
1818
1. Add your CC setup code:
19-
* Implement `cc_platform_setup()` and `cc_platform_terminate()` to enable CC on your platform, in case you have board-specific setup functionality, required for CC setup. These functions can be empty.
20-
* Define `cc_platform_ctx` in `cc_platform.h` in a way that suits your implementation.
19+
* Implement `crypto_platform_setup()` and `crypto_platform_terminate()` to enable CC on your platform, in case you have board-specific setup functionality, required for CC setup. You MUST call 'SaSi_LibInit()` and 'SaSi_LibFini()' respectively in these functions.
20+
* Define `crypto_platform_ctx` in `crypto_platform.h` in a way that suits your implementation.
2121

features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/cc_platform_nrf52840.c renamed to features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/crypto_platform.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* cc_platform_nrf52840.c
2+
* crypto_platform.c
33
*
44
* Copyright (C) 2018, Arm Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
@@ -20,14 +20,25 @@
2020

2121
#include "platform_alt.h"
2222
#include "nrf52840.h"
23+
#include "sns_silib.h"
24+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
2325

24-
int cc_platform_setup( cc_platform_ctx *ctx )
26+
static CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
27+
28+
int crypto_platform_setup( crypto_platform_ctx *ctx )
2529
{
2630
NRF_CRYPTOCELL->ENABLE = 1;
31+
32+
if( SaSi_LibInit( &ctx->rndState, &rndWorkBuff ) != 0 )
33+
return ( MBEDTLS_ERR_PLATFORM_HW_FAILED );
34+
2735
return ( 0 );
2836
}
2937

30-
void cc_platform_terminate( cc_platform_ctx *ctx )
38+
void crypto_platform_terminate( crypto_platform_ctx *ctx )
3139
{
40+
SaSi_LibFini( &ctx->rndState );
3241
NRF_CRYPTOCELL->ENABLE = 0;
3342
}
43+
44+
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */

features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/cc_platform.h renamed to features/cryptocell/FEATURE_CRYPTOCELL310/TARGET_MCU_NRF52840/crypto_platform.h

Lines changed: 9 additions & 7 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,17 +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_
22+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23+
#include "crys_rnd.h"
2224
/**
2325
* \brief The CC platform context structure.
2426
*
2527
* \note This structure may be used to assist platform-specific
2628
* setup or teardown operations.
2729
*/
2830
typedef struct {
29-
char dummy; /**< Placeholder member, as empty structs are not portable. */
31+
CRYS_RND_State_t rndState;
3032
}
31-
cc_platform_ctx;
32-
33-
#endif /* __CC_PLATFORM_H_ */
33+
crypto_platform_ctx;
34+
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
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 = { { 0 } };
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/cryptocell/FEATURE_CRYPTOCELL310/platform_alt.h renamed to features/mbedtls/platform/inc/platform_alt.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,47 @@
2020

2121
#ifndef __PLATFORM_ALT__
2222
#define __PLATFORM_ALT__
23-
#include "cc_platform.h"
24-
#include "crys_rnd.h"
25-
23+
#include "platform_mbed.h"
24+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
25+
#include "crypto_platform.h"
2626
/**
2727
* \brief The platform context structure.
2828
*
2929
* \note This structure may be used to assist platform-specific
3030
* setup or teardown operations.
3131
*/
3232
typedef struct {
33-
cc_platform_ctx platform_impl_ctx; /** A context holding all the partner's platform specific context */
34-
/*
35-
* Add CRYS_RND_State_t rndState; when https://github.com/ARMmbed/mbedtls/issues/1200 is supported
36-
* */
33+
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;
3735
}
3836
mbedtls_platform_context;
3937

4038

4139
/**
42-
* \brief This function performs any partner platform initialization operations,
43-
* needed top enable CryptoCell.
40+
* \brief This function performs any platform initialization operations,
41+
* needed for setting up cryptographic modules.
4442
*
4543
* \param ctx The platform specific context.
4644
*
4745
* \return \c 0 on success.
4846
*
49-
* \note This function is intended to allow platform-specific initialization for CryptoCell,
50-
* and is called before initializing the CC library(SaSi_LibInit). Its
47+
* \note This function is intended to allow platform-specific initialization for Mbed TLS,
48+
* and is called before initializing the Mbed TLS functions. Its
5149
* implementation is platform-specific, and its implementation MUST be provided.
5250
*
5351
*/
54-
int cc_platform_setup( cc_platform_ctx *ctx );
52+
int crypto_platform_setup( crypto_platform_ctx *unused_ctx );
5553

5654
/**
57-
* \brief This function performs any partner platform teardown operations, to disable CryptoCell.
55+
* \brief This function performs any platform teardown operations, to disable cryptographic operations.
5856
*
5957
* \param ctx The platform specific context.
6058
*
61-
* \note This function is called after terminating CC library(SaSi_LibFini)
62-
* and intended to free any resource used for CryptoCell by the platform.
59+
* \note This function is intended to free any resource used Mbed TLS by the platform.
6360
* Its implementation is platform-specific,and its implementation MUST be provided.
6461
*
6562
*/
66-
void cc_platform_terminate( cc_platform_ctx *ctx );
67-
63+
void crypto_platform_terminate( crypto_platform_ctx *unused_ctx );
64+
#endif
6865
#endif /* __PLATFORM_ALT__ */
6966

features/mbedtls/platform/inc/platform_mbed.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
2525
#include "mbedtls_device.h"
2626
#endif
27+
28+
#define MBEDTLS_ERR_PLATFORM_HW_FAILED -0x0080

features/cryptocell/FEATURE_CRYPTOCELL310/platform_alt.c renamed to features/mbedtls/platform/src/platform_alt.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,33 @@
2020

2121
#include "mbedtls/platform.h"
2222
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23-
#include "sns_silib.h"
23+
#include "mbed_critical.h"
2424

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 */
25+
mbedtls_platform_context ctx = { { 0 } };
2826

29-
CRYS_RND_State_t rndState = { { 0 } } ;
30-
CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
31-
32-
33-
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
27+
int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
3428
{
3529
int ret = 0;
36-
if( ctx == NULL )
37-
return ( -1 );
3830

39-
/* call platform specific code to setup CC driver*/
40-
if( ( ret = cc_platform_setup( &ctx->platform_impl_ctx ) ) != 0 )
41-
return ( ret );
31+
core_util_atomic_incr_u32( ( volatile uint32_t * )&ctx.reference_count, 1 );
4232

43-
if( SaSi_LibInit( &rndState, &rndWorkBuff ) != 0 )
44-
return ( -1 );
45-
return ( 0 );
33+
if( ctx.reference_count == 1 )
34+
{
35+
/* call platform specific code to setup crypto driver */
36+
ret = crypto_platform_setup( &ctx.platform_impl_ctx );
37+
}
38+
return ( ret );
4639
}
4740

48-
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
41+
void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx )
4942
{
50-
if( ctx == NULL )
51-
return;
52-
53-
SaSi_LibFini( &rndState );
54-
cc_platform_terminate( &ctx->platform_impl_ctx );
43+
core_util_atomic_decr_u32( ( volatile uint32_t * )&ctx.reference_count, 1 );
44+
if( ctx.reference_count < 1 )
45+
{
46+
/* call platform specific code to terminate crypto driver */
47+
crypto_platform_terminate( &ctx.platform_impl_ctx );
48+
ctx.reference_count = 0;
49+
}
5550
}
5651

5752
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/

0 commit comments

Comments
 (0)