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+
175212int 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 */
0 commit comments