Skip to content

Commit 2aef0ed

Browse files
authored
Merge pull request #731 from danielinux/wolfPKCS11_test
wolfPKCS11 test
2 parents 51d9c68 + 29450d5 commit 2aef0ed

File tree

11 files changed

+737
-205
lines changed

11 files changed

+737
-205
lines changed

.github/workflows/trustzone-emulator-tests.yml

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
trustzone-emulator-tests:
99
runs-on: ubuntu-latest
1010
container:
11-
image: ghcr.io/danielinux/m33mu-ci:1.5
11+
image: ghcr.io/wolfssl/m33mu-ci:1.9
1212
steps:
1313
- uses: actions/checkout@v4
1414

@@ -27,6 +27,56 @@ jobs:
2727
run: |
2828
./test.sh
2929
30+
- name: Clean build tree for PKCS11 test (stm32h5)
31+
run: |
32+
make clean distclean
33+
34+
- name: Configure PKCS11 test (stm32h5)
35+
run: |
36+
cp config/examples/stm32h5-tz.config .config
37+
38+
- name: Build persistent PKCS11 test app (stm32h5)
39+
run: |
40+
make PKCS11_TESTAPP=1
41+
42+
- name: Prepare PKCS11 persistence directory
43+
run: |
44+
rm -rf /tmp/m33mu-pkcs11-persist
45+
mkdir -p /tmp/m33mu-pkcs11-persist
46+
rm -f /tmp/m33mu-pkcs11-first.log /tmp/m33mu-pkcs11-second.log
47+
48+
- name: Run PKCS11 first boot (stm32h5)
49+
run: |
50+
cd /tmp/m33mu-pkcs11-persist
51+
m33mu "$GITHUB_WORKSPACE/wolfboot.bin" \
52+
"$GITHUB_WORKSPACE/test-app/image_v1_signed.bin:0x60000" \
53+
--persist --uart-stdout --timeout 120 --expect-bkpt 0x7d \
54+
| tee /tmp/m33mu-pkcs11-first.log
55+
56+
- name: Verify PKCS11 first boot (stm32h5)
57+
run: |
58+
grep -q "pkcs11: first boot path, creating persistent objects" /tmp/m33mu-pkcs11-first.log
59+
grep -q "pkcs11: created persistent PKCS11 objects" /tmp/m33mu-pkcs11-first.log
60+
grep -q "pkcs11: success" /tmp/m33mu-pkcs11-first.log
61+
grep -q "\\[BKPT\\] imm=0x7d" /tmp/m33mu-pkcs11-first.log
62+
grep -q "\\[EXPECT BKPT\\] Success" /tmp/m33mu-pkcs11-first.log
63+
64+
- name: Run PKCS11 second boot (stm32h5)
65+
run: |
66+
cd /tmp/m33mu-pkcs11-persist
67+
m33mu "$GITHUB_WORKSPACE/wolfboot.bin" \
68+
"$GITHUB_WORKSPACE/test-app/image_v1_signed.bin:0x60000" \
69+
--persist --uart-stdout --timeout 120 --expect-bkpt 0x7f \
70+
| tee /tmp/m33mu-pkcs11-second.log
71+
72+
- name: Verify PKCS11 second boot (stm32h5)
73+
run: |
74+
grep -q "pkcs11: second boot path, restoring persistent objects" /tmp/m33mu-pkcs11-second.log
75+
grep -q "pkcs11: restored persistent PKCS11 objects" /tmp/m33mu-pkcs11-second.log
76+
grep -q "pkcs11: success" /tmp/m33mu-pkcs11-second.log
77+
grep -q "\\[BKPT\\] imm=0x7f" /tmp/m33mu-pkcs11-second.log
78+
grep -q "\\[EXPECT BKPT\\] Success" /tmp/m33mu-pkcs11-second.log
79+
3080
- name: Clean and build stm32u5 (TZ + wolfcrypt)
3181
run: |
3282
make clean distclean
@@ -44,7 +94,7 @@ jobs:
4494
cp config/examples/stm32l5-wolfcrypt-tz.config .config
4595
make wolfboot.bin
4696
47-
- name: Run emu test (stm32u5)
97+
- name: Run emu test (stm32l5)
4898
working-directory: test-app/emu-test-apps
4999
run: |
50100
TARGET=stm32l5 ./test.sh

config/examples/mcxn-tz.config

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ PKA?=1
3232
WOLFBOOT_SECTOR_SIZE?=0x2000
3333

3434
# Default configuration
35-
# 40KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
35+
# 40KB boot, no keyvault, 8KB NSC, 72KB partitions, 8KB swap
3636
WOLFBOOT_KEYVAULT_ADDRESS?=0xA000
3737
WOLFBOOT_KEYVAULT_SIZE?=0
3838
WOLFBOOT_NSC_ADDRESS?=0xA000
3939
WOLFBOOT_NSC_SIZE?=0x2000
40-
WOLFBOOT_PARTITION_SIZE?=0x10000
40+
WOLFBOOT_PARTITION_SIZE?=0x12000
4141
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0xC000
42-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1C000
43-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x2C000
42+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x1E000
43+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x30000
4444

4545
# Alternate larger configuration for debugging or ARMASM
46-
# 128KB boot, no keyvault, 8KB NSC, 64KB partitions, 8KB swap
46+
# 128KB boot, no keyvault, 8KB NSC, 72KB partitions, 8KB swap
4747
#WOLFBOOT_KEYVAULT_ADDRESS?=0x20000
4848
#WOLFBOOT_KEYVAULT_SIZE?=0
4949
#WOLFBOOT_NSC_ADDRESS?=0x20000
5050
#WOLFBOOT_NSC_SIZE?=0x2000
51-
#WOLFBOOT_PARTITION_SIZE?=0x10000
51+
#WOLFBOOT_PARTITION_SIZE?=0x12000
5252
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x22000
53-
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x32000
54-
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x42000
53+
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x34000
54+
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x46000

config/examples/mcxn-wolfcrypt-tz.config

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ WOLFCRYPT_TZ_PKCS11?=1
3434
WOLFBOOT_SECTOR_SIZE?=0x2000
3535

3636
# Default configuration
37-
# 192KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
37+
# 192KB boot, 96KB keyvault, 8KB NSC, 72KB partitions, 8KB swap
3838
WOLFBOOT_KEYVAULT_ADDRESS?=0x30000
3939
WOLFBOOT_KEYVAULT_SIZE?=0x18000
4040
WOLFBOOT_NSC_ADDRESS?=0x48000
4141
WOLFBOOT_NSC_SIZE?=0x2000
42-
WOLFBOOT_PARTITION_SIZE?=0x10000
42+
WOLFBOOT_PARTITION_SIZE?=0x12000
4343
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x4A000
44-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x5A000
45-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x6A000
44+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x5C000
45+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x6E000
4646

4747
# Alternate larger configuration for debugging or ARMASM
48-
# 320KB boot, 96KB keyvault, 8KB NSC, 64KB partitions, 8KB swap
48+
# 320KB boot, 96KB keyvault, 8KB NSC, 72KB partitions, 8KB swap
4949
#WOLFBOOT_KEYVAULT_ADDRESS?=0x50000
5050
#WOLFBOOT_KEYVAULT_SIZE?=0x18000
5151
#WOLFBOOT_NSC_ADDRESS?=0x68000
5252
#WOLFBOOT_NSC_SIZE?=0x2000
53-
#WOLFBOOT_PARTITION_SIZE?=0x10000
53+
#WOLFBOOT_PARTITION_SIZE?=0x12000
5454
#WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x6A000
55-
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x7A000
56-
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x8A000
55+
#WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x7C000
56+
#WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x8E000

hal/stm32l5.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,17 @@ static void clock_pll_on(int powersave)
400400
#if TZ_SECURE()
401401
static void periph_unsecure()
402402
{
403+
volatile uint32_t reg;
404+
403405
/*Enable clock for User LED GPIOs */
404406
RCC_AHB2_CLOCK_ER|= LED_AHB2_ENABLE;
405407

406408
/* Enable clock for LPUART1 */
407409
RCC_APB1_CLOCK_ER |= UART1_APB1_CLOCK_ER_VAL;
410+
/* Enable clock for USART3 used by emu-test-apps on PD8/PD9 */
411+
RCC_APB1_CLOCK_ER |= UART3_APB1_CLOCK_ER_VAL;
412+
/* Enable clock for GPIO D (USART3 pins) */
413+
RCC_AHB2_CLOCK_ER |= GPIOD_AHB2_CLOCK_ER;
408414

409415

410416
PWR_CR2 |= PWR_CR2_IOSV;
@@ -423,6 +429,16 @@ static void periph_unsecure()
423429
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_TX_PIN);
424430
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_RX_PIN);
425431

432+
/* Unsecure USART3 and its pins for the STM32L5 emulator app path. */
433+
reg = TZSC_SECCFGR1;
434+
if (reg & TZSC_SECCFGR1_USART3SEC) {
435+
reg &= ~TZSC_SECCFGR1_USART3SEC;
436+
DMB();
437+
TZSC_SECCFGR1 = reg;
438+
}
439+
GPIO_SECCFGR(GPIOD_BASE) &= ~(1u << 8);
440+
GPIO_SECCFGR(GPIOD_BASE) &= ~(1u << 9);
441+
426442
}
427443
#endif
428444

hal/stm32l5.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,10 @@
181181
#define FLASH_NS_SR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x20))
182182
#define FLASH_NS_CR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x28))
183183

184-
#define TZSC_PRIVCFGR1 *((uint32_t *)(0x50032420))
185-
#define TZSC_PRIVCFG1_LPUARTPRIV (1 << 21)
184+
#define TZSC_PRIVCFGR1 (*(volatile uint32_t *)(0x50032420))
185+
#define TZSC_PRIVCFG1_LPUARTPRIV (1u << 21)
186+
#define TZSC_SECCFGR1 (*(volatile uint32_t *)(0x50032410))
187+
#define TZSC_SECCFGR1_USART3SEC (1u << 11)
186188

187189

188190
#else
@@ -257,6 +259,7 @@
257259

258260
#define RCC_APB1_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x5C ))
259261
#define UART1_APB1_CLOCK_ER_VAL (1 << 0)
262+
#define UART3_APB1_CLOCK_ER_VAL (1 << 18)
260263

261264
#define UART1_PIN_AF 8
262265
#define UART1_RX_PIN 8

test-app/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ endif()
122122

123123
if(BUILD_TEST_APPS)
124124
message(STATUS "Building wolfBoot test apps")
125+
if(WOLFCRYPT_TZ_PKCS11 AND NOT TZEN)
126+
message(FATAL_ERROR "WOLFCRYPT_TZ_PKCS11 requires TZEN=ON for test-app builds")
127+
endif()
125128
add_executable(image)
126129

127130
target_sources(image PRIVATE ${APP_SOURCES})
@@ -176,7 +179,7 @@ if(BUILD_TEST_APPS)
176179
target_include_directories(image PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/wcs)
177180
endif()
178181

179-
if(WOLFCRYPT_TZ_PKCS11)
182+
if(TZEN AND WOLFCRYPT_TZ_PKCS11)
180183
target_include_directories(image PRIVATE ../lib/wolfPKCS11)
181184
endif()
182185

@@ -204,11 +207,18 @@ if(BUILD_TEST_APPS)
204207
list(APPEND TEST_APP_COMPILE_DEFINITIONS WOLFBOOT_SECURE_CALLS)
205208
endif()
206209

207-
if(WOLFCRYPT_TZ_PKCS11)
208-
list(APPEND TEST_APP_COMPILE_DEFINITIONS WOLFBOOT_PKCS11_APP SECURE_PKCS11 WOLFPKCS11_USER_SETTINGS)
210+
if(TZEN AND WOLFCRYPT_TZ_PKCS11)
211+
list(APPEND TEST_APP_COMPILE_DEFINITIONS
212+
WOLFBOOT_PKCS11_APP
213+
SECURE_PKCS11
214+
WOLFBOOT_TZ_PKCS11
215+
WOLFPKCS11_USER_SETTINGS)
216+
if(PKCS11_TESTAPP)
217+
list(APPEND TEST_APP_COMPILE_DEFINITIONS WOLFBOOT_PKCS11_TESTAPP)
218+
endif()
209219
set(WOLFSSL_PKCS11_SOURCES
220+
test_pkcs11.c
210221
wcs/pkcs11_stub.c
211-
wcs/pkcs11_test_ecc.c
212222
../lib/wolfssl/wolfcrypt/src/ecc.c
213223
../lib/wolfssl/wolfcrypt/src/rsa.c
214224
../lib/wolfssl/wolfcrypt/src/asn.c

test-app/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ ifeq ($(TZEN),1)
247247
ifeq ($(WOLFCRYPT_TZ_PKCS11),1)
248248
CFLAGS+=-DWOLFSSL_USER_SETTINGS -DWOLFTPM_USER_SETTINGS
249249
CFLAGS+=-DWOLFBOOT_PKCS11_APP -DSECURE_PKCS11 -DWOLFBOOT_TZ_PKCS11
250+
ifeq ($(PKCS11_TESTAPP),1)
251+
CFLAGS+=-DWOLFBOOT_PKCS11_TESTAPP
252+
endif
250253
CFLAGS+=-I"$(WOLFBOOT_LIB_WOLFPKCS11)"
251-
APP_OBJS+=./wcs/pkcs11_test_ecc.o
254+
APP_OBJS+=./test_pkcs11.o
252255
APP_OBJS+=./wcs/pkcs11_stub.o
253256
APP_OBJS+=./wcs/ecc.o
254257
APP_OBJS+=./wcs/rsa.o

test-app/app_stm32h5.c

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@
4141
#endif
4242

4343
#ifdef WOLFBOOT_TZ_PKCS11
44-
#include "wcs/user_settings.h"
45-
#include "wolfssl/wolfcrypt/settings.h"
46-
#include "wolfssl/wolfcrypt/wc_pkcs11.h"
47-
#include "wolfssl/wolfcrypt/random.h"
48-
extern const char pkcs11_library_name[];
49-
extern const CK_FUNCTION_LIST wolfpkcs11nsFunctionList;
44+
#include "test_pkcs11.h"
5045
#endif
5146

5247
#ifdef WOLFCRYPT_SECURE_MODE
@@ -192,8 +187,6 @@ void extra_led_off(void)
192187
GPIOF_BSRR |= (1 << (LED_EXTRA_PIN + 16));
193188
}
194189

195-
extern int ecdsa_sign_verify(int devId);
196-
197190
/* Command line commands */
198191
static int cmd_help(const char *args);
199192
static int cmd_info(const char *args);
@@ -837,98 +830,8 @@ static int run_psa_boot_attestation(void)
837830
#ifdef WOLFBOOT_TZ_PKCS11
838831
static int cmd_login_pkcs11(const char *args)
839832
{
840-
int ret = -1;
841-
unsigned int devId = 0;
842-
Pkcs11Token token;
843-
Pkcs11Dev PKCS11_d;
844-
unsigned long session;
845-
char TokenPin[] = "0123456789ABCDEF";
846-
char UserPin[] = "ABCDEF0123456789";
847-
char SoPinName[] = "SO-PIN";
848-
static int pkcs11_initialized = 0;
849-
850-
if (pkcs11_initialized) {
851-
printf("PKCS11 already initialized.\r\n");
852-
return 0;
853-
}
854-
855-
printf("PKCS11 Login\r\n");
856-
857-
printf("Initializing wolfCrypt...");
858-
fflush(stdout);
859-
wolfCrypt_Init();
860-
printf("Done.\r\n");
861-
862-
PKCS11_d.heap = NULL,
863-
PKCS11_d.func = (CK_FUNCTION_LIST *)&wolfpkcs11nsFunctionList;
864-
865-
printf("Initializing EccKey token...");
866-
fflush(stdout);
867-
ret = wc_Pkcs11Token_Init(&token, &PKCS11_d, 1, "EccKey",
868-
(const byte*)TokenPin, strlen(TokenPin));
869-
870-
if (ret == 0) {
871-
printf("Done.\r\n");
872-
printf("Initializing token...");
873-
fflush(stdout);
874-
ret = wolfpkcs11nsFunctionList.C_InitToken(1,
875-
(byte *)TokenPin, strlen(TokenPin), (byte *)SoPinName);
876-
}
877-
if (ret == 0) {
878-
printf("Done.\r\n");
879-
printf("Opening session...");
880-
fflush(stdout);
881-
ret = wolfpkcs11nsFunctionList.C_OpenSession(1,
882-
CKF_SERIAL_SESSION | CKF_RW_SESSION,
883-
NULL, NULL, &session);
884-
}
885-
886-
if (ret == 0) {
887-
printf("Done.\r\n");
888-
printf("Logging in as SO...");
889-
ret = wolfpkcs11nsFunctionList.C_Login(session, CKU_SO,
890-
(byte *)TokenPin,
891-
strlen(TokenPin));
892-
}
893-
if (ret == 0) {
894-
extra_led_on();
895-
printf("Done.\r\n");
896-
printf("Setting PIN...");
897-
ret = wolfpkcs11nsFunctionList.C_InitPIN(session,
898-
(byte *)TokenPin,
899-
strlen(TokenPin));
900-
}
901-
if (ret == 0) {
902-
printf("Done.\r\n");
903-
printf("Logging out...");
904-
ret = wolfpkcs11nsFunctionList.C_Logout(session);
905-
}
906-
if (ret == 0) {
907-
printf("Done.\r\n");
908-
printf("Registering crypto calls with wolfCrypt...");
909-
ret = wc_CryptoDev_RegisterDevice(devId, wc_Pkcs11_CryptoDevCb,
910-
&token);
911-
}
912-
if (ret == 0) {
913-
printf("Done.\r\n");
914-
#ifdef HAVE_ECC
915-
printf("Testing ECC...");
916-
ret = ecdsa_sign_verify(devId);
917-
if (ret != 0) {
918-
ret = -1;
919-
printf("Failed.\r\n");
920-
}
921-
else {
922-
usr_led_on();
923-
printf("Done.\r\n");
924-
}
925-
#endif
926-
}
927-
if (ret == 0) {
928-
printf("PKCS11 initialization completed successfully.\r\n");
929-
pkcs11_initialized = 1;
930-
}
931-
return ret;
833+
(void)args;
834+
return test_pkcs11_start();
932835
}
933836
#endif /* WOLFBOOT_TZ_PKCS11 */
934837

@@ -1377,6 +1280,16 @@ void main(void)
13771280
(void)run_psa_boot_attestation();
13781281
#endif
13791282

1283+
#ifdef WOLFBOOT_PKCS11_TESTAPP
1284+
ret = test_pkcs11_start();
1285+
if (ret == PKCS11_TEST_FIRST_BOOT_OK)
1286+
asm volatile ("bkpt #0x7d");
1287+
else if (ret == PKCS11_TEST_SECOND_BOOT_OK)
1288+
asm volatile ("bkpt #0x7f");
1289+
else
1290+
asm volatile ("bkpt #0x7e");
1291+
#endif
1292+
13801293
console_loop();
13811294

13821295
while(1)

0 commit comments

Comments
 (0)