Skip to content

Commit c0df7c4

Browse files
committed
Phase 3: keystore-persist test mirrors PKCS11's first/second-boot pattern
1 parent 7c2fa8a commit c0df7c4

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

test-app/app_stm32h5.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static int cmd_tpm_quote(const char *args);
219219
static int cmd_fwtpm_test(const char *args);
220220
#endif
221221
#ifdef WOLFCRYPT_TZ_WOLFHSM
222-
extern int cmd_wolfhsm_test(const char *args);
222+
#include "wcs/wolfhsm_test.h"
223223
#endif
224224

225225

@@ -1511,15 +1511,17 @@ void main(void)
15111511
#ifdef WOLFCRYPT_TZ_WOLFHSM
15121512
ret = cmd_wolfhsm_test(NULL);
15131513
#ifdef WOLFBOOT_TZ_TEST_NO_BKPT
1514-
if (ret == 0) {
1514+
if (ret == WOLFHSM_TEST_FIRST_BOOT_OK || ret == WOLFHSM_TEST_SECOND_BOOT_OK) {
15151515
printf("WOLFHSM_TZ_TEST_PASS\r\n");
15161516
while (1) { }
15171517
} else {
15181518
printf("WOLFHSM_TZ_TEST_FAIL\r\n");
15191519
while (1) { }
15201520
}
15211521
#else
1522-
if (ret == 0)
1522+
if (ret == WOLFHSM_TEST_FIRST_BOOT_OK)
1523+
asm volatile ("bkpt #0x7d");
1524+
else if (ret == WOLFHSM_TEST_SECOND_BOOT_OK)
15231525
asm volatile ("bkpt #0x7f");
15241526
else
15251527
asm volatile ("bkpt #0x7e");

test-app/wcs/wolfhsm_test.c

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
#include <stdio.h>
1212
#include <string.h>
1313

14+
#include "wolfhsm_test.h"
15+
1416
#include "wolfhsm/wh_client.h"
1517
#include "wolfhsm/wh_client_crypto.h"
1618
#include "wolfhsm/wh_common.h"
1719
#include "wolfhsm/wh_comm.h"
1820
#include "wolfhsm/wh_error.h"
21+
#include "wolfhsm/wh_keyid.h"
1922

2023
#include "wolfssl/wolfcrypt/aes.h"
2124
#include "wolfssl/wolfcrypt/random.h"
@@ -168,10 +171,44 @@ static int wolfhsm_test_aes_cached(whClientContext *client)
168171
return rc;
169172
}
170173

171-
/* Initializes the wolfHSM client (auto-registers the wolfCrypt cryptocb
172-
* under WH_DEV_ID), runs the CommInit handshake, exercises crypto
173-
* round-trips (RNG, SHA256, AES with cached key) through the
174-
* secure-side server. */
174+
static int wolfhsm_test_persist(whClientContext *client, int *boot_state)
175+
{
176+
static const uint8_t persist_key[16] = {
177+
0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
178+
0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x00
179+
};
180+
uint16_t keyId = WH_MAKE_KEYID(0, WCS_WOLFHSM_CLIENT_ID, 1);
181+
uint8_t out[sizeof(persist_key)];
182+
uint16_t outSz = (uint16_t)sizeof(out);
183+
int rc;
184+
185+
memset(out, 0, sizeof(out));
186+
rc = wh_Client_KeyExport(client, keyId, NULL, 0, out, &outSz);
187+
if (rc == WH_ERROR_OK && outSz == sizeof(persist_key) &&
188+
memcmp(out, persist_key, sizeof(persist_key)) == 0) {
189+
printf("wolfHSM second boot path, restored persisted key\r\n");
190+
*boot_state = WOLFHSM_TEST_SECOND_BOOT_OK;
191+
return 0;
192+
}
193+
194+
printf("wolfHSM first boot path, committing key to NVM\r\n");
195+
keyId = WH_MAKE_KEYID(0, WCS_WOLFHSM_CLIENT_ID, 1);
196+
rc = wh_Client_KeyCache(client, WH_NVM_FLAGS_USAGE_ENCRYPT, NULL, 0,
197+
persist_key, (uint16_t)sizeof(persist_key),
198+
&keyId);
199+
if (rc != WH_ERROR_OK) {
200+
printf("wolfHSM persist KeyCache failed: %d\r\n", rc);
201+
return rc;
202+
}
203+
rc = wh_Client_KeyCommit(client, keyId);
204+
if (rc != WH_ERROR_OK) {
205+
printf("wolfHSM persist KeyCommit failed: %d\r\n", rc);
206+
return rc;
207+
}
208+
*boot_state = WOLFHSM_TEST_FIRST_BOOT_OK;
209+
return 0;
210+
}
211+
175212
int cmd_wolfhsm_test(const char *args)
176213
{
177214
static const whTransportNscClientConfig nsc_cfg = { 0 };
@@ -180,6 +217,7 @@ int cmd_wolfhsm_test(const char *args)
180217
whClientContext client;
181218
uint32_t out_clientid = 0;
182219
uint32_t out_serverid = 0;
220+
int boot_state = WOLFHSM_TEST_FAIL;
183221
int rc;
184222

185223
(void)args;
@@ -198,14 +236,14 @@ int cmd_wolfhsm_test(const char *args)
198236
rc = wh_Client_Init(&client, &cfg);
199237
if (rc != WH_ERROR_OK) {
200238
printf("wolfHSM Init failed: %d\r\n", rc);
201-
return rc;
239+
return WOLFHSM_TEST_FAIL;
202240
}
203241

204242
rc = wh_Client_CommInit(&client, &out_clientid, &out_serverid);
205243
if (rc != WH_ERROR_OK) {
206244
printf("wolfHSM CommInit failed: %d\r\n", rc);
207245
(void)wh_Client_Cleanup(&client);
208-
return rc;
246+
return WOLFHSM_TEST_FAIL;
209247
}
210248

211249
printf("wolfHSM CommInit ok (client=%u server=%u)\r\n",
@@ -214,25 +252,31 @@ int cmd_wolfhsm_test(const char *args)
214252
rc = wolfhsm_test_rng();
215253
if (rc != 0) {
216254
(void)wh_Client_Cleanup(&client);
217-
return rc;
255+
return WOLFHSM_TEST_FAIL;
218256
}
219257

220258
rc = wolfhsm_test_sha256();
221259
if (rc != 0) {
222260
(void)wh_Client_Cleanup(&client);
223-
return rc;
261+
return WOLFHSM_TEST_FAIL;
224262
}
225263

226264
rc = wolfhsm_test_aes_cached(&client);
227265
if (rc != 0) {
228266
(void)wh_Client_Cleanup(&client);
229-
return rc;
267+
return WOLFHSM_TEST_FAIL;
268+
}
269+
270+
rc = wolfhsm_test_persist(&client, &boot_state);
271+
if (rc != 0) {
272+
(void)wh_Client_Cleanup(&client);
273+
return WOLFHSM_TEST_FAIL;
230274
}
231275

232276
printf("wolfHSM NSC tests passed\r\n");
233277

234278
(void)wh_Client_Cleanup(&client);
235-
return 0;
279+
return boot_state;
236280
}
237281

238282
#endif /* WOLFCRYPT_TZ_WOLFHSM */

test-app/wcs/wolfhsm_test.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* wolfhsm_test.h
2+
*
3+
* Copyright (C) 2026 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*/
7+
8+
#ifndef WOLFBOOT_TEST_WOLFHSM_H
9+
#define WOLFBOOT_TEST_WOLFHSM_H
10+
11+
enum wolfhsm_test_result {
12+
WOLFHSM_TEST_FAIL = -1,
13+
WOLFHSM_TEST_FIRST_BOOT_OK = 1,
14+
WOLFHSM_TEST_SECOND_BOOT_OK = 2
15+
};
16+
17+
int cmd_wolfhsm_test(const char *args);
18+
19+
#endif /* WOLFBOOT_TEST_WOLFHSM_H */

0 commit comments

Comments
 (0)