mirror of https://github.com/ARMmbed/mbed-os.git
M2354: Support TRNG as entropy source on TF-M
1. Update TF-M Secure bin enabling TRNG as entropy source 2. Replaced with above, remove TRNG HAL stuff on Mbed, including platform extra secure functions, cmake, etc.pull/14441/head
parent
2c3fb3d727
commit
7db441401b
|
@ -72,7 +72,6 @@ target_sources(mbed-m2354
|
|||
serial_api.c
|
||||
sleep.c
|
||||
spi_api.c
|
||||
trng_api.cpp
|
||||
us_ticker.c
|
||||
)
|
||||
|
||||
|
|
Binary file not shown.
|
@ -894,80 +894,3 @@ NU_PLAT_XTRA_SEC_HDLR(nu_rtc_isenabled_s)
|
|||
return TFM_PLATFORM_ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __MBED__
|
||||
void nu_trng_init_s(void)
|
||||
{
|
||||
/* Invoke NSC function */
|
||||
PLAT_NSC_CALL(nu_trng_init_s, NULL, 0, NULL, 0);
|
||||
}
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_init_s)
|
||||
{
|
||||
/* Check parameter validity */
|
||||
NU_CHK_PARAM_VAL(0, 0);
|
||||
|
||||
CLK_EnableModuleClock(TRNG_MODULE);
|
||||
SYS_ResetModule(TRNG_RST);
|
||||
TRNG_S->ACT |= TRNG_ACT_ACT_Msk;
|
||||
while (!(TRNG_S->CTL & TRNG_CTL_READY_Msk));
|
||||
|
||||
return TFM_PLATFORM_ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __MBED__
|
||||
void nu_trng_free_s(void)
|
||||
{
|
||||
/* Invoke NSC function */
|
||||
PLAT_NSC_CALL(nu_trng_free_s, NULL, 0, NULL, 0);
|
||||
}
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_free_s)
|
||||
{
|
||||
/* Check parameter validity */
|
||||
NU_CHK_PARAM_VAL(0, 0);
|
||||
|
||||
TRNG_S->ACT &= ~TRNG_ACT_ACT_Msk;
|
||||
CLK_DisableModuleClock(TRNG_MODULE);
|
||||
|
||||
return TFM_PLATFORM_ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __MBED__
|
||||
int32_t nu_trng_get_bytes_s(uint8_t *output, uint32_t length, uint32_t *output_length)
|
||||
{
|
||||
/* Check argument validity */
|
||||
if (!output && length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Invoke NSC function */
|
||||
PLAT_NSC_CALL(nu_trng_get_bytes_s, NULL, 0, output, length);
|
||||
|
||||
/* Finalize output */
|
||||
if (output_length) {
|
||||
*output_length = outvec.len;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_get_bytes_s)
|
||||
{
|
||||
/* Check parameter validity */
|
||||
NU_CHK_PARAM_VAL(0, INT_MAX);
|
||||
|
||||
uint8_t *output_ind = out_vec->base;
|
||||
uint8_t *output_end = output_ind + out_vec->len;
|
||||
|
||||
for (; output_ind != output_end; output_ind ++) {
|
||||
TRNG_S->CTL |= TRNG_CTL_TRNGEN_Msk;
|
||||
while (!(TRNG_S->CTL & TRNG_CTL_DVIF_Msk));
|
||||
*output_ind = TRNG_S->DATA & 0xff;
|
||||
}
|
||||
|
||||
return TFM_PLATFORM_ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -77,9 +77,6 @@ typedef enum {
|
|||
NU_PLAT_XTRA_SEC_REQ(nu_rtc_read_spare_register_s),
|
||||
NU_PLAT_XTRA_SEC_REQ(nu_rtc_write_spare_register_s),
|
||||
NU_PLAT_XTRA_SEC_REQ(nu_rtc_isenabled_s),
|
||||
NU_PLAT_XTRA_SEC_REQ(nu_trng_init_s),
|
||||
NU_PLAT_XTRA_SEC_REQ(nu_trng_free_s),
|
||||
NU_PLAT_XTRA_SEC_REQ(nu_trng_get_bytes_s),
|
||||
|
||||
/* Max request code, plays as number of valid request code */
|
||||
NU_PLAT_XTRA_SEC_REQ(MAX),
|
||||
|
@ -346,39 +343,6 @@ int32_t nu_rtc_isenabled_s(void);
|
|||
NU_PLAT_XTRA_SEC_HDLR(nu_rtc_isenabled_s);
|
||||
#endif
|
||||
|
||||
/* Secure trng_init
|
||||
*
|
||||
* Its synopsis is the same as normal version except change of return/argument type for
|
||||
* binary-compatible across compilers.
|
||||
*/
|
||||
#ifdef __MBED__
|
||||
void nu_trng_init_s(void);
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_init_s);
|
||||
#endif
|
||||
|
||||
/* Secure trng_free
|
||||
*
|
||||
* Its synopsis is the same as normal version except change of return/argument type for
|
||||
* binary-compatible across compilers.
|
||||
*/
|
||||
#ifdef __MBED__
|
||||
void nu_trng_free_s(void);
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_free_s);
|
||||
#endif
|
||||
|
||||
/* Secure trng_get_bytes
|
||||
*
|
||||
* Its synopsis is the same as normal version except change of return/argument type for
|
||||
* binary-compatible across compilers.
|
||||
*/
|
||||
#ifdef __MBED__
|
||||
int32_t nu_trng_get_bytes_s(uint8_t *output, uint32_t length, uint32_t *output_length);
|
||||
#else
|
||||
NU_PLAT_XTRA_SEC_HDLR(nu_trng_get_bytes_s);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Nuvoton Technology Corporation
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if DEVICE_TRNG
|
||||
|
||||
#include "cmsis.h"
|
||||
#include <limits.h>
|
||||
#include "crypto-misc.h"
|
||||
#include "hal/trng_api.h"
|
||||
#include "platform/mbed_toolchain.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
#include "platform/mbed_error.h"
|
||||
#include "nu_modutil.h"
|
||||
|
||||
/* TRNG init counter. TRNG is kept active as it is non-zero. */
|
||||
static uint16_t trng_init_counter = 0U;
|
||||
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
(void) obj;
|
||||
|
||||
core_util_critical_section_enter();
|
||||
if (trng_init_counter == USHRT_MAX) {
|
||||
core_util_critical_section_exit();
|
||||
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), \
|
||||
"TRNG initialization counter would overflow");
|
||||
}
|
||||
++ trng_init_counter;
|
||||
if (trng_init_counter == 1) {
|
||||
nu_trng_init_s();
|
||||
}
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
{
|
||||
(void) obj;
|
||||
|
||||
core_util_critical_section_enter();
|
||||
if (trng_init_counter == 0) {
|
||||
core_util_critical_section_exit();
|
||||
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), \
|
||||
"TRNG initialization counter would underflow");
|
||||
}
|
||||
-- trng_init_counter;
|
||||
if (trng_init_counter == 0) {
|
||||
nu_trng_free_s();
|
||||
}
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
||||
{
|
||||
(void) obj;
|
||||
|
||||
uint32_t output_length_;
|
||||
int32_t rc = nu_trng_get_bytes_s(output, (uint32_t) length, &output_length_);
|
||||
if (output_length) {
|
||||
*output_length = output_length_;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
#endif /* #if DEVICE_TRNG */
|
Loading…
Reference in New Issue