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.
pull/7099/head
Ron Eldor 2018-06-04 10:29:39 +03:00
parent 24cebbaec3
commit ca94a49eff
6 changed files with 92 additions and 73 deletions

View File

@ -16,6 +16,6 @@ To port your CC 310 driver to Mbed OS on your specific target, do the following:
1. In `objects.h`, include `objects_cryptocell.h`. You can use the `FEATURE_CRYPTOCELL310` precompilation check as defined above.
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.
1. Add your CC setup code:
* 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.
* Define `cc_platform_ctx` in `cc_platform.h` in a way that suits your implementation.
* 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.
* Define `crypto_platform_ctx` in `crypto_platform.h` in a way that suits your implementation.

View File

@ -1,33 +0,0 @@
/*
* cc_platform_nrf52840.c
*
* Copyright (C) 2018, Arm Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "platform_alt.h"
#include "nrf52840.h"
int cc_platform_setup( cc_platform_ctx *ctx )
{
NRF_CRYPTOCELL->ENABLE = 1;
return ( 0 );
}
void cc_platform_terminate( cc_platform_ctx *ctx )
{
NRF_CRYPTOCELL->ENABLE = 0;
}

View File

@ -1,5 +1,5 @@
/*
* platform_alt.c
* crypto_platform.c
*
* Copyright (C) 2018, Arm Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
@ -18,10 +18,10 @@
*
*/
#include "mbedtls/platform.h"
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
#include "platform_alt.h"
#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 */
@ -29,29 +29,20 @@
CRYS_RND_State_t rndState = { { 0 } } ;
CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } } ;
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
int crypto_platform_setup( crypto_platform_ctx *ctx )
{
int ret = 0;
if( ctx == NULL )
return ( -1 );
/* call platform specific code to setup CC driver*/
if( ( ret = cc_platform_setup( &ctx->platform_impl_ctx ) ) != 0 )
return ( ret );
NRF_CRYPTOCELL->ENABLE = 1;
if( SaSi_LibInit( &rndState, &rndWorkBuff ) != 0 )
return ( -1 );
return ( -1 );
return ( 0 );
}
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
void crypto_platform_terminate( crypto_platform_ctx *ctx )
{
if( ctx == NULL )
return;
SaSi_LibFini( &rndState );
cc_platform_terminate( &ctx->platform_impl_ctx );
NRF_CRYPTOCELL->ENABLE = 0;
}
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */

View File

@ -19,6 +19,7 @@
*/
#ifndef __CC_PLATFORM_H_
#define __CC_PLATFORM_H_
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
/**
* \brief The CC platform context structure.
*
@ -27,7 +28,10 @@
*/
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
*/
}
cc_platform_ctx;
crypto_platform_ctx;
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
#endif /* __CC_PLATFORM_H_ */

View File

@ -20,8 +20,9 @@
#ifndef __PLATFORM_ALT__
#define __PLATFORM_ALT__
#include "cc_platform.h"
#include "crys_rnd.h"
#include "platform_mbed.h"
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
#include "crypto_platform.h"
/**
* \brief The platform context structure.
@ -30,40 +31,37 @@
* setup or teardown operations.
*/
typedef struct {
cc_platform_ctx platform_impl_ctx; /** A context holding all the partner's platform specific context */
/*
* Add CRYS_RND_State_t rndState; when https://github.com/ARMmbed/mbedtls/issues/1200 is supported
* */
crypto_platform_ctx platform_impl_ctx; /* A context holding all the platform specific context for cryptography. Should be defined in crypto_platform.h */
}
mbedtls_platform_context;
void mbedtls_platform_init( mbedtls_platform_context* ctx);
/**
* \brief This function performs any partner platform initialization operations,
* needed top enable CryptoCell.
* \brief This function performs any platform initialization operations,
* needed for setting up cryptographic modules.
*
* \param ctx The platform specific context.
*
* \return \c 0 on success.
*
* \note This function is intended to allow platform-specific initialization for CryptoCell,
* and is called before initializing the CC library(SaSi_LibInit). Its
* \note This function is intended to allow platform-specific initialization for Mbed TLS,
* and is called before initializing the Mbed TLS functions. Its
* implementation is platform-specific, and its implementation MUST be provided.
*
*/
int cc_platform_setup( cc_platform_ctx *ctx );
int crypto_platform_setup( crypto_platform_ctx *ctx );
/**
* \brief This function performs any partner platform teardown operations, to disable CryptoCell.
* \brief This function performs any platform teardown operations, to disable cryptographic operations.
*
* \param ctx The platform specific context.
*
* \note This function is called after terminating CC library(SaSi_LibFini)
* and intended to free any resource used for CryptoCell by the platform.
* \note This function is intended to free any resource used Mbed TLS by the platform.
* Its implementation is platform-specific,and its implementation MUST be provided.
*
*/
void cc_platform_terminate( cc_platform_ctx *ctx );
void crypto_platform_terminate( crypto_platform_ctx *ctx );
#endif
#endif /* __PLATFORM_ALT__ */

View File

@ -0,0 +1,59 @@
/*
* platform_alt.c
*
* Copyright (C) 2018, Arm Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "mbedtls/platform.h"
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
static int reference_count = 0;
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
{
int ret = 0;
if( ctx == NULL )
return ( -1 );
reference_count++;
if( reference_count == 1 )
{
/* call platform specific code to setup crypto driver*/
ret = crypto_platform_setup( &ctx->platform_impl_ctx );
}
return ( ret );
}
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
{
if( ctx == NULL )
return;
if( reference_count == 0 )
return;
reference_count--;
if( reference_count == 0 )
{
/* call platform specific code to terminate crypto driver*/
crypto_platform_terminate( &ctx->platform_impl_ctx );
}
}
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/