Skip to content

Commit f5a524e

Browse files
committed
NUVOTON: Re-implement TRNG HAL with TRNG H/W
Targets supporting TRNG H/W: - NU_PFM_M2351_* - NUMAKER_IOT_M263A
1 parent 3f9ba9e commit f5a524e

File tree

12 files changed

+298
-195
lines changed

12 files changed

+298
-195
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/PeripheralNames.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ typedef enum {
328328

329329
} CANName;
330330

331+
typedef enum {
332+
#if defined(SCU_INIT_PNSSET5_VAL) && (SCU_INIT_PNSSET5_VAL & (1 << 25))
333+
TRNG_0 = (int) NU_MODNAME(TRNG_BASE + NS_OFFSET, 0, 0)
334+
#else
335+
TRNG_0 = (int) NU_MODNAME(TRNG_BASE, 0, 0)
336+
#endif
337+
338+
} TRNGName;
339+
331340
#ifdef __cplusplus
332341
}
333342
#endif

targets/TARGET_NUVOTON/TARGET_M2351/crypto/crypto-misc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "platform/SingletonPtr.h"
2828
#include "platform/PlatformMutex.h"
2929

30-
#if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT)
30+
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
3131

3232
/* Consideration for choosing proper synchronization mechanism
3333
*
@@ -345,4 +345,4 @@ extern "C" void CRPT_IRQHandler()
345345
}
346346
}
347347

348-
#endif /* #if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT) */
348+
#endif /* #if defined(MBEDTLS_CONFIG_HW_SUPPORT) */

targets/TARGET_NUVOTON/TARGET_M2351/crypto/crypto-misc.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@
2424
*
2525
* There's only one CRYPTO/CRPT module and we have the following policy for configuring its secure attribute:
2626
*
27-
* 1. TRNG or mbedtls H/W support can be enabled on either secure target or non-secure target, but not both.
28-
* 2. TRNG and mbedtls H/W supports cannot be enabled on different targets.
29-
* 3. On secure target, if TRNG or mbedtls H/W support is enabled, CRYPTO/CRPT must configure to secure.
30-
* 4. On non-secure target, if TRNG or mbedtls H/W support is enabled, CRYPTO/CRPT must configure to non-secure.
27+
* 1. mbedtls H/W support can be enabled on either secure target or non-secure target, but not both.
28+
* 2. On secure target, if mbedtls H/W support is enabled, CRYPTO/CRPT must configure to secure.
29+
* 3. On non-secure target, if mbedtls H/W support is enabled, CRYPTO/CRPT must configure to non-secure.
3130
*/
32-
#if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT)
31+
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
3332
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
3433
#if defined(SCU_INIT_PNSSET1_VAL) && (SCU_INIT_PNSSET1_VAL & (1 << 18))
35-
#error("CRYPTO/CRPT must configure to secure for secure target which supports TRNG or mbedtls H/W")
34+
#error("CRYPTO/CRPT must configure to secure for secure target which supports mbedtls H/W")
3635
#endif
3736
#else
3837
#if (! defined(SCU_INIT_PNSSET1_VAL)) || (! (SCU_INIT_PNSSET1_VAL & (1 << 18)))
39-
#error("CRYPTO/CRPT must configure to non-secure for non-secure target which supports TRNG or mbedtls H/W")
38+
#error("CRYPTO/CRPT must configure to non-secure for non-secure target which supports mbedtls H/W")
4039
#endif
4140
#endif
4241
#endif
4342

44-
#if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT)
43+
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
4544

4645
#ifdef __cplusplus
4746
extern "C" {
@@ -132,6 +131,6 @@ bool crypto_dma_buffs_overlap(const void *in_buff, size_t in_buff_size, const vo
132131
}
133132
#endif
134133

135-
#endif /* #if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT) */
134+
#endif /* defined(MBEDTLS_CONFIG_HW_SUPPORT) */
136135

137136
#endif

targets/TARGET_NUVOTON/TARGET_M2351/hal_secure.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ int64_t rtc_read_s(void);
7272
__NONSECURE_ENTRY
7373
void rtc_write_s(int64_t t);
7474

75+
/* trng_init (secure version)
76+
*
77+
* Its synopsis is the same as normal version except change of return/argument type for
78+
* binary-compatible across compilers.
79+
*/
80+
__NONSECURE_ENTRY
81+
void trng_init_s(void *obj);
82+
83+
/* trng_free (secure version)
84+
*
85+
* Its synopsis is the same as normal version except change of return/argument type for
86+
* binary-compatible across compilers.
87+
*/
88+
__NONSECURE_ENTRY
89+
void trng_free_s(void *obj);
90+
91+
/* trng_get_bytes (secure version)
92+
*
93+
* Its synopsis is the same as normal version except change of return/argument type for
94+
* binary-compatible across compilers.
95+
*/
96+
__NONSECURE_ENTRY
97+
int32_t trng_get_bytes_s(void *obj, uint8_t *output, uint32_t length, uint32_t *output_length);
98+
7599
#ifdef __cplusplus
76100
}
77101
#endif

targets/TARGET_NUVOTON/TARGET_M2351/trng_api.c

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2018 Nuvoton
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if DEVICE_TRNG
18+
19+
#include "cmsis.h"
20+
#include <limits.h>
21+
#include "crypto-misc.h"
22+
#include "hal/trng_api.h"
23+
#include "platform/mbed_toolchain.h"
24+
#include "platform/mbed_critical.h"
25+
#include "platform/mbed_error.h"
26+
#include "nu_modutil.h"
27+
#include "hal_secure.h"
28+
#include "partition_M2351.h"
29+
30+
#if defined(SCU_INIT_PNSSET5_VAL) && (SCU_INIT_PNSSET5_VAL & (1 << 25))
31+
#error("We just support secure TRNG")
32+
#endif
33+
34+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3L)
35+
36+
/* Module init definition: modname, clkidx, clksrc, clkdiv, rstidx, irqnum, misc */
37+
static const struct nu_modinit_s trng_modinit = {TRNG_0, TRNG_MODULE, 0, 0, TRNG_RST, TRNG_IRQn, NULL};
38+
39+
/* TRNG init counter. TRNG is kept active as it is non-zero. */
40+
static uint16_t trng_init_counter = 0U;
41+
42+
#endif
43+
44+
void trng_init(trng_t *obj)
45+
{
46+
trng_init_s(obj);
47+
}
48+
49+
void trng_free(trng_t *obj)
50+
{
51+
trng_free_s(obj);
52+
}
53+
54+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
55+
{
56+
uint32_t output_length_;
57+
int32_t rc = trng_get_bytes_s(obj, output, (uint32_t) length, &output_length_);
58+
if (output_length) {
59+
*output_length = output_length_;
60+
}
61+
return rc;
62+
}
63+
64+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
65+
66+
__NONSECURE_ENTRY
67+
extern "C"
68+
void trng_init_s(MBED_UNUSED void *obj)
69+
{
70+
core_util_critical_section_enter();
71+
if (trng_init_counter == USHRT_MAX) {
72+
core_util_critical_section_exit();
73+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), \
74+
"TRNG initialization counter would overflow");
75+
}
76+
++ trng_init_counter;
77+
if (trng_init_counter == 1) {
78+
/* Enable IP clock (secure version) */
79+
CLK_EnableModuleClock_S(trng_modinit.clkidx);
80+
81+
/* Reset IP (secure version) */
82+
SYS_ResetModule_S(trng_modinit.rsetidx);
83+
84+
TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname);
85+
86+
trng_base->ACT |= TRNG_ACT_ACT_Msk;
87+
while (!(trng_base->CTL & TRNG_CTL_READY_Msk));
88+
}
89+
core_util_critical_section_exit();
90+
}
91+
92+
__NONSECURE_ENTRY
93+
extern "C"
94+
void trng_free_s(MBED_UNUSED void *obj)
95+
{
96+
core_util_critical_section_enter();
97+
if (trng_init_counter == 0) {
98+
core_util_critical_section_exit();
99+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), \
100+
"TRNG initialization counter would underflow");
101+
}
102+
-- trng_init_counter;
103+
if (trng_init_counter == 0) {
104+
TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname);
105+
106+
trng_base->ACT &= ~TRNG_ACT_ACT_Msk;
107+
108+
/* Disable IP clock (secure version) */
109+
CLK_DisableModuleClock_S(trng_modinit.clkidx);
110+
}
111+
core_util_critical_section_exit();
112+
}
113+
114+
__NONSECURE_ENTRY
115+
extern "C"
116+
int32_t trng_get_bytes_s(MBED_UNUSED void *obj, uint8_t *output, uint32_t length, uint32_t *output_length)
117+
{
118+
/* Check augument validity */
119+
if (!output && length) {
120+
return -1;
121+
}
122+
123+
uint8_t *output_ind = output;
124+
uint8_t *output_end = output + length;
125+
126+
TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname);
127+
128+
for (; output_ind != output_end; output_ind ++) {
129+
trng_base->CTL |= TRNG_CTL_TRNGEN_Msk;
130+
while (!(trng_base->CTL & TRNG_CTL_DVIF_Msk));
131+
*output_ind = trng_base->DATA & 0xff;
132+
}
133+
134+
if (output_length) {
135+
*output_length = length;
136+
}
137+
138+
return 0;
139+
}
140+
141+
#endif
142+
#endif

targets/TARGET_NUVOTON/TARGET_M261/PeripheralNames.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ typedef enum {
144144

145145
} CANName;
146146

147+
typedef enum {
148+
TRNG_0 = (int) NU_MODNAME(TRNG_BASE, 0, 0)
149+
150+
} TRNGName;
151+
147152
#ifdef __cplusplus
148153
}
149154
#endif

targets/TARGET_NUVOTON/TARGET_M261/crypto/crypto-misc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "platform/SingletonPtr.h"
2828
#include "platform/PlatformMutex.h"
2929

30-
#if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT)
30+
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
3131

3232
/* Consideration for choosing proper synchronization mechanism
3333
*
@@ -342,4 +342,4 @@ extern "C" void CRPT_IRQHandler()
342342
}
343343
}
344344

345-
#endif /* #if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT) */
345+
#endif /* #if defined(MBEDTLS_CONFIG_HW_SUPPORT) */

targets/TARGET_NUVOTON/TARGET_M261/crypto/crypto-misc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <stdbool.h>
2222

2323

24-
#if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT)
24+
#if defined(MBEDTLS_CONFIG_HW_SUPPORT)
2525

2626
#ifdef __cplusplus
2727
extern "C" {
@@ -108,6 +108,6 @@ bool crypto_dma_buffs_overlap(const void *in_buff, size_t in_buff_size, const vo
108108
}
109109
#endif
110110

111-
#endif /* #if DEVICE_TRNG || defined(MBEDTLS_CONFIG_HW_SUPPORT) */
111+
#endif /* #if defined(MBEDTLS_CONFIG_HW_SUPPORT) */
112112

113113
#endif

0 commit comments

Comments
 (0)