Skip to content

Commit ca94a49

Browse files
Ron EldorRon Eldor
authored andcommitted
Add reference counter for platform context
1. Move the `mbedtls_platform_context` to be platform code, in `features/mbedtls/platfrom/`. 2. Add static refernce counter, to setup and teardown the platform code only once. 3. Adjust Cryptocell porting accordingly.
1 parent 24cebba commit ca94a49

File tree

6 files changed

+92
-73
lines changed

6 files changed

+92
-73
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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* platform_alt.c
2+
* crypto_platform.c
33
*
44
* Copyright (C) 2018, Arm Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
@@ -18,40 +18,31 @@
1818
*
1919
*/
2020

21-
#include "mbedtls/platform.h"
22-
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
21+
#include "platform_alt.h"
22+
#include "nrf52840.h"
2323
#include "sns_silib.h"
24-
24+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
2525
/* once https://github.com/ARMmbed/mbedtls/issues/1200 will be supported,
2626
* rndState should be part of mbedtls_platform_context
2727
* Until then, we should keep it global and extern */
2828

2929
CRYS_RND_State_t rndState = { { 0 } } ;
3030
CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
3131

32-
33-
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
32+
int crypto_platform_setup( crypto_platform_ctx *ctx )
3433
{
35-
int ret = 0;
36-
if( ctx == NULL )
37-
return ( -1 );
38-
39-
/* call platform specific code to setup CC driver*/
40-
if( ( ret = cc_platform_setup( &ctx->platform_impl_ctx ) ) != 0 )
41-
return ( ret );
34+
NRF_CRYPTOCELL->ENABLE = 1;
4235

4336
if( SaSi_LibInit( &rndState, &rndWorkBuff ) != 0 )
44-
return ( -1 );
37+
return ( -1 );
38+
4539
return ( 0 );
4640
}
4741

48-
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
42+
void crypto_platform_terminate( crypto_platform_ctx *ctx )
4943
{
50-
if( ctx == NULL )
51-
return;
52-
5344
SaSi_LibFini( &rndState );
54-
cc_platform_terminate( &ctx->platform_impl_ctx );
45+
NRF_CRYPTOCELL->ENABLE = 0;
5546
}
5647

57-
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/
48+
#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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
#ifndef __CC_PLATFORM_H_
2121
#define __CC_PLATFORM_H_
22+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
2223
/**
2324
* \brief The CC platform context structure.
2425
*
@@ -27,7 +28,10 @@
2728
*/
2829
typedef struct {
2930
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+
*/
3034
}
31-
cc_platform_ctx;
32-
35+
crypto_platform_ctx;
36+
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
3337
#endif /* __CC_PLATFORM_H_ */

features/cryptocell/FEATURE_CRYPTOCELL310/platform_alt.h renamed to features/mbedtls/platform/inc/platform_alt.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
#ifndef __PLATFORM_ALT__
2222
#define __PLATFORM_ALT__
23-
#include "cc_platform.h"
24-
#include "crys_rnd.h"
23+
#include "platform_mbed.h"
24+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
25+
#include "crypto_platform.h"
2526

2627
/**
2728
* \brief The platform context structure.
@@ -30,40 +31,37 @@
3031
* setup or teardown operations.
3132
*/
3233
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-
* */
34+
crypto_platform_ctx platform_impl_ctx; /* A context holding all the platform specific context for cryptography. Should be defined in crypto_platform.h */
3735
}
3836
mbedtls_platform_context;
3937

4038

39+
void mbedtls_platform_init( mbedtls_platform_context* ctx);
4140
/**
42-
* \brief This function performs any partner platform initialization operations,
43-
* needed top enable CryptoCell.
41+
* \brief This function performs any platform initialization operations,
42+
* needed for setting up cryptographic modules.
4443
*
4544
* \param ctx The platform specific context.
4645
*
4746
* \return \c 0 on success.
4847
*
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
48+
* \note This function is intended to allow platform-specific initialization for Mbed TLS,
49+
* and is called before initializing the Mbed TLS functions. Its
5150
* implementation is platform-specific, and its implementation MUST be provided.
5251
*
5352
*/
54-
int cc_platform_setup( cc_platform_ctx *ctx );
53+
int crypto_platform_setup( crypto_platform_ctx *ctx );
5554

5655
/**
57-
* \brief This function performs any partner platform teardown operations, to disable CryptoCell.
56+
* \brief This function performs any platform teardown operations, to disable cryptographic operations.
5857
*
5958
* \param ctx The platform specific context.
6059
*
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.
60+
* \note This function is intended to free any resource used Mbed TLS by the platform.
6361
* Its implementation is platform-specific,and its implementation MUST be provided.
6462
*
6563
*/
66-
void cc_platform_terminate( cc_platform_ctx *ctx );
67-
64+
void crypto_platform_terminate( crypto_platform_ctx *ctx );
65+
#endif
6866
#endif /* __PLATFORM_ALT__ */
6967

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* platform_alt.c
3+
*
4+
* Copyright (C) 2018, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include "mbedtls/platform.h"
22+
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23+
24+
static int reference_count = 0;
25+
26+
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
27+
{
28+
int ret = 0;
29+
if( ctx == NULL )
30+
return ( -1 );
31+
32+
reference_count++;
33+
34+
if( reference_count == 1 )
35+
{
36+
/* call platform specific code to setup crypto driver*/
37+
ret = crypto_platform_setup( &ctx->platform_impl_ctx );
38+
}
39+
return ( ret );
40+
}
41+
42+
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
43+
{
44+
if( ctx == NULL )
45+
return;
46+
47+
if( reference_count == 0 )
48+
return;
49+
50+
reference_count--;
51+
52+
if( reference_count == 0 )
53+
{
54+
/* call platform specific code to terminate crypto driver*/
55+
crypto_platform_terminate( &ctx->platform_impl_ctx );
56+
}
57+
}
58+
59+
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/

0 commit comments

Comments
 (0)