Implement WOLFCRYPT_TZ_PKCS11 on MCXN

pull/727/head
Mattia Moffa 2026-03-18 21:42:32 +01:00
parent b5fab30310
commit e3056e6d7f
4 changed files with 164 additions and 22 deletions

View File

@ -10,7 +10,7 @@ MCUXPRESSO_CPU?=MCXN947VDF_cm33_core0
MCUXPRESSO_DRIVERS?=$(MCUXPRESSO)/devices/MCX/MCXN/MCXN947
MCUXPRESSO_PROJECT_TEMPLATE?=$(MCUXPRESSO)/examples/_boards/frdmmcxn947/project_template
DEBUG?=0
DEBUG_UART?=0
DEBUG_UART?=1
VTOR?=1
CORTEX_M0?=0
CORTEX_M33?=1
@ -27,29 +27,28 @@ SPMATH?=1
RAM_CODE?=1
DUALBANK_SWAP?=0
PKA?=1
WOLFCRYPT_TZ?=1
# 8KB sectors
WOLFBOOT_SECTOR_SIZE?=0x2000
# Default configuration
# 64KB boot, 80KB keyvault, 8KB NSC, 60KB partitions, 8KB swap
WOLFBOOT_KEYVAULT_ADDRESS?=0x12000
WOLFBOOT_KEYVAULT_SIZE?=0x14000
WOLFBOOT_NSC_ADDRESS?=0x26000
# 40KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
WOLFBOOT_KEYVAULT_ADDRESS?=0xA000
WOLFBOOT_KEYVAULT_SIZE?=0
WOLFBOOT_NSC_ADDRESS?=0xA000
WOLFBOOT_NSC_SIZE?=0x2000
WOLFBOOT_PARTITION_SIZE?=0xE000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x28000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x36000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x44000
WOLFBOOT_PARTITION_SIZE?=0x10000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0xC000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1C000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x2C000
# Alternate larger configuration for debugging or ARMASM
# 128KB boot, 80KB keyvault, 8KB NSC, 60KB partitions, 8KB swap
# 128KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
#WOLFBOOT_KEYVAULT_ADDRESS?=0x20000
#WOLFBOOT_KEYVAULT_SIZE?=0x14000
#WOLFBOOT_NSC_ADDRESS?=0x34000
#WOLFBOOT_KEYVAULT_SIZE?=0
#WOLFBOOT_NSC_ADDRESS?=0x20000
#WOLFBOOT_NSC_SIZE?=0x2000
#WOLFBOOT_PARTITION_SIZE?=0xE000
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x36000
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x45000
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x54000
#WOLFBOOT_PARTITION_SIZE?=0x10000
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x22000
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x32000
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x42000

View File

@ -0,0 +1,56 @@
ARCH?=ARM
TZEN?=1
TARGET?=mcxn
SIGN?=ECC384
HASH?=SHA384
MCUXSDK?=1
MCUXPRESSO?=$(PWD)/../NXP/mcuxpresso-sdk/mcuxsdk
MCUXPRESSO_CMSIS?=$(PWD)/../NXP/CMSIS_5/CMSIS
MCUXPRESSO_CPU?=MCXN947VDF_cm33_core0
MCUXPRESSO_DRIVERS?=$(MCUXPRESSO)/devices/MCX/MCXN/MCXN947
MCUXPRESSO_PROJECT_TEMPLATE?=$(MCUXPRESSO)/examples/_boards/frdmmcxn947/project_template
DEBUG?=0
DEBUG_UART?=1
VTOR?=1
CORTEX_M0?=0
CORTEX_M33?=1
NO_ASM?=0
NO_MPU=1
EXT_FLASH?=0
SPI_FLASH?=0
ALLOW_DOWNGRADE?=0
NVM_FLASH_WRITEONCE?=1
NO_ARM_ASM=1
WOLFBOOT_VERSION?=0
V?=0
SPMATH?=1
RAM_CODE?=1
DUALBANK_SWAP?=0
PKA?=1
WOLFCRYPT_TZ?=1
WOLFCRYPT_TZ_PKCS11?=1
# 8KB sectors
WOLFBOOT_SECTOR_SIZE?=0x2000
# Default configuration
# 192KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
WOLFBOOT_KEYVAULT_ADDRESS?=0x30000
WOLFBOOT_KEYVAULT_SIZE?=0x18000
WOLFBOOT_NSC_ADDRESS?=0x48000
WOLFBOOT_NSC_SIZE?=0x2000
WOLFBOOT_PARTITION_SIZE?=0x10000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x4A000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x5A000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x6A000
# Alternate larger configuration for debugging or ARMASM
# 320KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
#WOLFBOOT_KEYVAULT_ADDRESS?=0x50000
#WOLFBOOT_KEYVAULT_SIZE?=0x18000
#WOLFBOOT_NSC_ADDRESS?=0x68000
#WOLFBOOT_NSC_SIZE?=0x2000
#WOLFBOOT_PARTITION_SIZE?=0x10000
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x6A000
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x7A000
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x8A000

View File

@ -40,6 +40,11 @@
#include "hal/armv8m_tz.h"
#endif
#ifdef WOLFCRYPT_SECURE_MODE
void hal_trng_init(void);
int hal_trng_get_entropy(unsigned char *out, unsigned int len);
#endif
static flash_config_t pflash;
static uint32_t pflash_sector_size = WOLFBOOT_SECTOR_SIZE;
uint32_t SystemCoreClock;
@ -104,6 +109,7 @@ void hal_init(void)
#if defined(TZEN) && !defined(NONSECURE_APP)
hal_sau_init();
#endif
}
#ifdef __WOLFBOOT
@ -207,21 +213,71 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
}
#ifdef WOLFCRYPT_SECURE_MODE
/* These functions are stubs for now, because the MCUXpresso SDK doesn't
* implement drivers for the MCXN's TRNG. */
#define ELS_CMD_RND_REQ 24U
void hal_trng_init(void)
{
/* Enable ELS and wait for it to be ready */
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1);
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
;
}
void hal_trng_fini(void)
{
/* Don't disable ELS, it might be used by other actors */
}
int hal_trng_get_entropy(unsigned char *out, unsigned int len)
{
(void)out;
(void)len;
return -1;
/* Implemented as a RND_REQ command to the ELS */
uint32_t aligned_len = len & ~3U;
uint32_t status;
/* Wait for ELS to be ready */
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
;
/* Handle the word-aligned portion */
if (aligned_len > 0) {
ELS->ELS_DMA_RES0 = (uint32_t)(uintptr_t)out;
ELS->ELS_DMA_RES0_LEN = aligned_len;
ELS->ELS_CMDCFG0 = 0;
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1)
| S50_ELS_CTRL_ELS_START(1)
| S50_ELS_CTRL_ELS_CMD(ELS_CMD_RND_REQ);
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
;
status = ELS->ELS_STATUS;
if (status & S50_ELS_STATUS_ELS_ERR_MASK)
return -1;
}
/* Handle remaining bytes (1-3) with a temporary word */
if (len > aligned_len) {
uint32_t tmp;
ELS->ELS_DMA_RES0 = (uint32_t)(uintptr_t)&tmp;
ELS->ELS_DMA_RES0_LEN = 4;
ELS->ELS_CMDCFG0 = 0;
ELS->ELS_CTRL = S50_ELS_CTRL_ELS_EN(1)
| S50_ELS_CTRL_ELS_START(1)
| S50_ELS_CTRL_ELS_CMD(ELS_CMD_RND_REQ);
while (ELS->ELS_STATUS & S50_ELS_STATUS_ELS_BUSY_MASK)
;
status = ELS->ELS_STATUS;
if (status & S50_ELS_STATUS_ELS_ERR_MASK)
return -1;
memcpy(out + aligned_len, &tmp, len - aligned_len);
}
return 0;
}
#endif

View File

@ -28,6 +28,11 @@
#include "wolfboot/wolfboot.h"
#include "printf.h"
#ifdef WOLFCRYPT_SECURE_MODE
#include "wolfssl/wolfcrypt/types.h"
#include "wolfssl/wolfcrypt/random.h"
#endif
extern void hal_init(void);
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,
PORT_SetPinConfig(port, pin, &pin_config);
}
#ifdef WOLFCRYPT_SECURE_MODE
void print_random_number(void)
{
WC_RNG rng;
uint32_t rnd;
int ret;
ret = wc_InitRng(&rng);
if (ret != 0) {
wolfBoot_printf("Random number: init failed (%d)\n", ret);
}
else {
ret = wc_RNG_GenerateBlock(&rng, (byte *)&rnd, sizeof(rnd));
if (ret != 0)
wolfBoot_printf("Random number: generate failed (%d)\n", ret);
else
wolfBoot_printf("Today's lucky number: 0x%08lx\n", (unsigned long)rnd);
wc_FreeRng(&rng);
}
}
#endif
void main(void)
{
uint32_t boot_ver;
@ -91,6 +118,10 @@ void main(void)
wolfBoot_printf("Hello from firmware version %d\n", boot_ver);
#ifdef WOLFCRYPT_SECURE_MODE
print_random_number();
#endif
if (boot_ver == 1) {
/* Red off */
gpio_init_output(GPIO0, PORT0, kCLOCK_Gpio0, kCLOCK_Port0, 10U, 1U);