Skip to content

Commit e3056e6

Browse files
committed
Implement WOLFCRYPT_TZ_PKCS11 on MCXN
1 parent b5fab30 commit e3056e6

4 files changed

Lines changed: 164 additions & 22 deletions

File tree

config/examples/mcxn-tz.config

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ MCUXPRESSO_CPU?=MCXN947VDF_cm33_core0
1010
MCUXPRESSO_DRIVERS?=$(MCUXPRESSO)/devices/MCX/MCXN/MCXN947
1111
MCUXPRESSO_PROJECT_TEMPLATE?=$(MCUXPRESSO)/examples/_boards/frdmmcxn947/project_template
1212
DEBUG?=0
13-
DEBUG_UART?=0
13+
DEBUG_UART?=1
1414
VTOR?=1
1515
CORTEX_M0?=0
1616
CORTEX_M33?=1
@@ -27,29 +27,28 @@ SPMATH?=1
2727
RAM_CODE?=1
2828
DUALBANK_SWAP?=0
2929
PKA?=1
30-
WOLFCRYPT_TZ?=1
3130

3231
# 8KB sectors
3332
WOLFBOOT_SECTOR_SIZE?=0x2000
3433

3534
# Default configuration
36-
# 64KB boot, 80KB keyvault, 8KB NSC, 60KB partitions, 8KB swap
37-
WOLFBOOT_KEYVAULT_ADDRESS?=0x12000
38-
WOLFBOOT_KEYVAULT_SIZE?=0x14000
39-
WOLFBOOT_NSC_ADDRESS?=0x26000
35+
# 40KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
36+
WOLFBOOT_KEYVAULT_ADDRESS?=0xA000
37+
WOLFBOOT_KEYVAULT_SIZE?=0
38+
WOLFBOOT_NSC_ADDRESS?=0xA000
4039
WOLFBOOT_NSC_SIZE?=0x2000
41-
WOLFBOOT_PARTITION_SIZE?=0xE000
42-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x28000
43-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x36000
44-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x44000
40+
WOLFBOOT_PARTITION_SIZE?=0x10000
41+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0xC000
42+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1C000
43+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x2C000
4544

4645
# Alternate larger configuration for debugging or ARMASM
47-
# 128KB boot, 80KB keyvault, 8KB NSC, 60KB partitions, 8KB swap
46+
# 128KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
4847
#WOLFBOOT_KEYVAULT_ADDRESS?=0x20000
49-
#WOLFBOOT_KEYVAULT_SIZE?=0x14000
50-
#WOLFBOOT_NSC_ADDRESS?=0x34000
48+
#WOLFBOOT_KEYVAULT_SIZE?=0
49+
#WOLFBOOT_NSC_ADDRESS?=0x20000
5150
#WOLFBOOT_NSC_SIZE?=0x2000
52-
#WOLFBOOT_PARTITION_SIZE?=0xE000
53-
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x36000
54-
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x45000
55-
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x54000
51+
#WOLFBOOT_PARTITION_SIZE?=0x10000
52+
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x22000
53+
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x32000
54+
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x42000
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
ARCH?=ARM
2+
TZEN?=1
3+
TARGET?=mcxn
4+
SIGN?=ECC384
5+
HASH?=SHA384
6+
MCUXSDK?=1
7+
MCUXPRESSO?=$(PWD)/../NXP/mcuxpresso-sdk/mcuxsdk
8+
MCUXPRESSO_CMSIS?=$(PWD)/../NXP/CMSIS_5/CMSIS
9+
MCUXPRESSO_CPU?=MCXN947VDF_cm33_core0
10+
MCUXPRESSO_DRIVERS?=$(MCUXPRESSO)/devices/MCX/MCXN/MCXN947
11+
MCUXPRESSO_PROJECT_TEMPLATE?=$(MCUXPRESSO)/examples/_boards/frdmmcxn947/project_template
12+
DEBUG?=0
13+
DEBUG_UART?=1
14+
VTOR?=1
15+
CORTEX_M0?=0
16+
CORTEX_M33?=1
17+
NO_ASM?=0
18+
NO_MPU=1
19+
EXT_FLASH?=0
20+
SPI_FLASH?=0
21+
ALLOW_DOWNGRADE?=0
22+
NVM_FLASH_WRITEONCE?=1
23+
NO_ARM_ASM=1
24+
WOLFBOOT_VERSION?=0
25+
V?=0
26+
SPMATH?=1
27+
RAM_CODE?=1
28+
DUALBANK_SWAP?=0
29+
PKA?=1
30+
WOLFCRYPT_TZ?=1
31+
WOLFCRYPT_TZ_PKCS11?=1
32+
33+
# 8KB sectors
34+
WOLFBOOT_SECTOR_SIZE?=0x2000
35+
36+
# Default configuration
37+
# 192KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
38+
WOLFBOOT_KEYVAULT_ADDRESS?=0x30000
39+
WOLFBOOT_KEYVAULT_SIZE?=0x18000
40+
WOLFBOOT_NSC_ADDRESS?=0x48000
41+
WOLFBOOT_NSC_SIZE?=0x2000
42+
WOLFBOOT_PARTITION_SIZE?=0x10000
43+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x4A000
44+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x5A000
45+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x6A000
46+
47+
# Alternate larger configuration for debugging or ARMASM
48+
# 320KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
49+
#WOLFBOOT_KEYVAULT_ADDRESS?=0x50000
50+
#WOLFBOOT_KEYVAULT_SIZE?=0x18000
51+
#WOLFBOOT_NSC_ADDRESS?=0x68000
52+
#WOLFBOOT_NSC_SIZE?=0x2000
53+
#WOLFBOOT_PARTITION_SIZE?=0x10000
54+
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x6A000
55+
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x7A000
56+
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x8A000

hal/mcxn.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
#include "hal/armv8m_tz.h"
4141
#endif
4242

43+
#ifdef WOLFCRYPT_SECURE_MODE
44+
void hal_trng_init(void);
45+
int hal_trng_get_entropy(unsigned char *out, unsigned int len);
46+
#endif
47+
4348
static flash_config_t pflash;
4449
static uint32_t pflash_sector_size = WOLFBOOT_SECTOR_SIZE;
4550
uint32_t SystemCoreClock;
@@ -104,6 +109,7 @@ void hal_init(void)
104109
#if defined(TZEN) && !defined(NONSECURE_APP)
105110
hal_sau_init();
106111
#endif
112+
107113
}
108114

109115
#ifdef __WOLFBOOT
@@ -207,21 +213,71 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
207213
}
208214

209215
#ifdef WOLFCRYPT_SECURE_MODE
210-
/* These functions are stubs for now, because the MCUXpresso SDK doesn't
211-
* implement drivers for the MCXN's TRNG. */
216+
#define ELS_CMD_RND_REQ 24U
217+
212218
void hal_trng_init(void)
213219
{
220+
/* Enable ELS and wait for it to be ready */
221+
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1);
222+
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
223+
;
214224
}
215225

216226
void hal_trng_fini(void)
217227
{
228+
/* Don't disable ELS, it might be used by other actors */
218229
}
219230

220231
int hal_trng_get_entropy(unsigned char *out, unsigned int len)
221232
{
222-
(void)out;
223-
(void)len;
224-
return -1;
233+
/* Implemented as a RND_REQ command to the ELS */
234+
235+
uint32_t aligned_len = len & ~3U;
236+
uint32_t status;
237+
238+
/* Wait for ELS to be ready */
239+
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
240+
;
241+
242+
/* Handle the word-aligned portion */
243+
if (aligned_len > 0) {
244+
ELS->ELS_DMA_RES0 = (uint32_t)(uintptr_t)out;
245+
ELS->ELS_DMA_RES0_LEN = aligned_len;
246+
ELS->ELS_CMDCFG0 = 0;
247+
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1)
248+
| S50_ELS_CTRL_ELS_START(1)
249+
| S50_ELS_CTRL_ELS_CMD(ELS_CMD_RND_REQ);
250+
251+
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
252+
;
253+
254+
status = ELS->ELS_STATUS;
255+
if (status & S50_ELS_STATUS_ELS_ERR_MASK)
256+
return -1;
257+
}
258+
259+
/* Handle remaining bytes (1-3) with a temporary word */
260+
if (len > aligned_len) {
261+
uint32_t tmp;
262+
263+
ELS->ELS_DMA_RES0 = (uint32_t)(uintptr_t)&tmp;
264+
ELS->ELS_DMA_RES0_LEN = 4;
265+
ELS->ELS_CMDCFG0 = 0;
266+
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1)
267+
| S50_ELS_CTRL_ELS_START(1)
268+
| S50_ELS_CTRL_ELS_CMD(ELS_CMD_RND_REQ);
269+
270+
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
271+
;
272+
273+
status = ELS->ELS_STATUS;
274+
if (status & S50_ELS_STATUS_ELS_ERR_MASK)
275+
return -1;
276+
277+
memcpy(out + aligned_len, &tmp, len - aligned_len);
278+
}
279+
280+
return 0;
225281
}
226282
#endif
227283

test-app/app_mcxn.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include "wolfboot/wolfboot.h"
2929
#include "printf.h"
3030

31+
#ifdef WOLFCRYPT_SECURE_MODE
32+
#include "wolfssl/wolfcrypt/types.h"
33+
#include "wolfssl/wolfcrypt/random.h"
34+
#endif
35+
3136
extern void hal_init(void);
3237

3338
static void gpio_init_output(GPIO_Type *gpio, PORT_Type *port,
@@ -77,6 +82,28 @@ static void gpio_init_output(GPIO_Type *gpio, PORT_Type *port,
7782
PORT_SetPinConfig(port, pin, &pin_config);
7883
}
7984

85+
#ifdef WOLFCRYPT_SECURE_MODE
86+
void print_random_number(void)
87+
{
88+
WC_RNG rng;
89+
uint32_t rnd;
90+
int ret;
91+
92+
ret = wc_InitRng(&rng);
93+
if (ret != 0) {
94+
wolfBoot_printf("Random number: init failed (%d)\n", ret);
95+
}
96+
else {
97+
ret = wc_RNG_GenerateBlock(&rng, (byte *)&rnd, sizeof(rnd));
98+
if (ret != 0)
99+
wolfBoot_printf("Random number: generate failed (%d)\n", ret);
100+
else
101+
wolfBoot_printf("Today's lucky number: 0x%08lx\n", (unsigned long)rnd);
102+
wc_FreeRng(&rng);
103+
}
104+
}
105+
#endif
106+
80107
void main(void)
81108
{
82109
uint32_t boot_ver;
@@ -91,6 +118,10 @@ void main(void)
91118

92119
wolfBoot_printf("Hello from firmware version %d\n", boot_ver);
93120

121+
#ifdef WOLFCRYPT_SECURE_MODE
122+
print_random_number();
123+
#endif
124+
94125
if (boot_ver == 1) {
95126
/* Red off */
96127
gpio_init_output(GPIO0, PORT0, kCLOCK_Gpio0, kCLOCK_Port0, 10U, 1U);

0 commit comments

Comments
 (0)