Skip to content

Commit a1bd17a

Browse files
committed
Move pkcs11/tz test to its own module in test-app
+ added m33mu two-runs test for persistent storage coverage
1 parent 16dadd6 commit a1bd17a

File tree

7 files changed

+584
-103
lines changed

7 files changed

+584
-103
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,47 @@ jobs:
2727
run: |
2828
./test.sh
2929
30+
- name: Build and run persistent PKCS11 test app (stm32h5)
31+
run: |
32+
set -euo pipefail
33+
34+
make clean distclean
35+
cp config/examples/stm32h5-tz.config .config
36+
make wolfboot.bin PKCS11_TESTAPP=1
37+
38+
first_log=/tmp/m33mu-pkcs11-first.log
39+
second_log=/tmp/m33mu-pkcs11-second.log
40+
persist_dir=/tmp/m33mu-pkcs11-persist
41+
rm -f "$first_log" "$second_log"
42+
rm -rf "$persist_dir"
43+
mkdir -p "$persist_dir"
44+
45+
(
46+
cd "$persist_dir"
47+
m33mu "$GITHUB_WORKSPACE/wolfboot.bin" \
48+
"$GITHUB_WORKSPACE/test-app/image_v1_signed.bin:0x60000" \
49+
--persist --uart-stdout --timeout 120 --expect-bkpt 0x7f \
50+
>"$first_log" 2>&1
51+
)
52+
53+
grep -q "pkcs11: first boot path, creating persistent objects" "$first_log"
54+
grep -q "pkcs11: created persistent PKCS11 objects" "$first_log"
55+
grep -q "pkcs11: success" "$first_log"
56+
grep -q "\\[EXPECT BKPT\\] Success" "$first_log"
57+
58+
(
59+
cd "$persist_dir"
60+
m33mu "$GITHUB_WORKSPACE/wolfboot.bin" \
61+
"$GITHUB_WORKSPACE/test-app/image_v1_signed.bin:0x60000" \
62+
--persist --uart-stdout --timeout 120 --expect-bkpt 0x7f \
63+
>"$second_log" 2>&1
64+
)
65+
66+
grep -q "pkcs11: second boot path, restoring persistent objects" "$second_log"
67+
grep -q "pkcs11: restored persistent PKCS11 objects" "$second_log"
68+
grep -q "pkcs11: success" "$second_log"
69+
grep -q "\\[EXPECT BKPT\\] Success" "$second_log"
70+
3071
- name: Clean and build stm32u5 (TZ + wolfcrypt)
3172
run: |
3273
make clean distclean

lib/wolfssl

Submodule wolfssl updated 97 files

test-app/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ if(BUILD_TEST_APPS)
206206

207207
if(WOLFCRYPT_TZ_PKCS11)
208208
list(APPEND TEST_APP_COMPILE_DEFINITIONS WOLFBOOT_PKCS11_APP SECURE_PKCS11 WOLFPKCS11_USER_SETTINGS)
209+
if(PKCS11_TESTAPP)
210+
list(APPEND TEST_APP_COMPILE_DEFINITIONS WOLFBOOT_PKCS11_TESTAPP)
211+
endif()
209212
set(WOLFSSL_PKCS11_SOURCES
213+
test_pkcs11.c
210214
wcs/pkcs11_stub.c
211-
wcs/pkcs11_test_ecc.c
212215
../lib/wolfssl/wolfcrypt/src/ecc.c
213216
../lib/wolfssl/wolfcrypt/src/rsa.c
214217
../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: 11 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,14 @@ 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 == 0)
1286+
asm volatile ("bkpt #0x7f");
1287+
else
1288+
asm volatile ("bkpt #0x7e");
1289+
#endif
1290+
13801291
console_loop();
13811292

13821293
while(1)

0 commit comments

Comments
 (0)