3838
3939#include "wolfhsm/wh_comm.h"
4040#include "wolfhsm/wh_message.h"
41+ #include "wolfhsm/wh_message_she.h"
4142
4243#ifdef WOLFHSM_CFG_ENABLE_SERVER
4344#include "wolfhsm/wh_server.h"
7576enum {
7677 REQ_SIZE = 32 ,
7778 RESP_SIZE = 64 ,
78- BUFFER_SIZE = 4096 ,
79+ BUFFER_SIZE = sizeof (whTransportMemCsr ) + sizeof (whCommHeader ) +
80+ WOLFHSM_CFG_COMM_DATA_LEN ,
7981 };
8082
8183#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
@@ -250,6 +252,7 @@ int whTest_SheClientConfig(whClientConfig* config)
250252 if ((sreg & WH_SHE_SREG_BOOT_OK ) == 0 ||
251253 (sreg & WH_SHE_SREG_BOOT_FINISHED ) == 0 ||
252254 (sreg & WH_SHE_SREG_SECURE_BOOT ) == 0 ) {
255+ ret = WH_ERROR_ABORTED ;
253256 WH_ERROR_PRINT ("Failed to secureBoot with SHE CMAC\n" );
254257 goto exit ;
255258 }
@@ -283,6 +286,7 @@ int whTest_SheClientConfig(whClientConfig* config)
283286 memcmp (messageThree , vectorMessageThree , sizeof (vectorMessageThree )) != 0 ||
284287 memcmp (messageFour , vectorMessageFour , sizeof (vectorMessageFour )) != 0 ||
285288 memcmp (messageFive , vectorMessageFive , sizeof (vectorMessageFive )) != 0 ) {
289+ ret = WH_ERROR_ABORTED ;
286290 WH_ERROR_PRINT ("Failed to generate a loadable key to match the vector\n" );
287291 goto exit ;
288292 }
@@ -295,6 +299,7 @@ int whTest_SheClientConfig(whClientConfig* config)
295299 if (memcmp (outMessageFour , vectorMessageFour , sizeof (vectorMessageFour ))
296300 != 0 || memcmp (outMessageFive , vectorMessageFive ,
297301 sizeof (vectorMessageFive )) != 0 ) {
302+ ret = WH_ERROR_ABORTED ;
298303 WH_ERROR_PRINT ("wh_Client_SheLoadKey FAILED TO MATCH\n" );
299304 goto exit ;
300305 }
@@ -333,6 +338,7 @@ int whTest_SheClientConfig(whClientConfig* config)
333338 goto exit ;
334339 }
335340 if (memcmp (finalText , plainText , sizeof (plainText )) != 0 ) {
341+ ret = WH_ERROR_ABORTED ;
336342 WH_ERROR_PRINT ("SHE ECB FAILED TO MATCH\n" );
337343 goto exit ;
338344 }
@@ -346,6 +352,7 @@ int whTest_SheClientConfig(whClientConfig* config)
346352 goto exit ;
347353 }
348354 if (memcmp (finalText , plainText , sizeof (plainText )) != 0 ) {
355+ ret = WH_ERROR_ABORTED ;
349356 WH_ERROR_PRINT ("SHE CBC FAILED TO MATCH\n" );
350357 goto exit ;
351358 }
@@ -359,6 +366,7 @@ int whTest_SheClientConfig(whClientConfig* config)
359366 goto exit ;
360367 }
361368 if (sreg != 0 ) {
369+ ret = WH_ERROR_ABORTED ;
362370 WH_ERROR_PRINT ("SHE CMAC FAILED TO VERIFY\n" );
363371 goto exit ;
364372 }
@@ -422,6 +430,150 @@ int whTest_SheClientConfig(whClientConfig* config)
422430
423431 return ret ;
424432}
433+
434+ #ifdef WOLFHSM_CFG_ENABLE_SERVER
435+ static int whTest_SheClientConfigBoundarySecureBoot (whClientConfig * config )
436+ {
437+ int ret = 0 ;
438+ WC_RNG rng [1 ];
439+ Cmac cmac [1 ];
440+ whClientContext client [1 ] = {0 };
441+ uint8_t key [16 ] = {0 };
442+ uint8_t zeros [WH_SHE_BOOT_MAC_PREFIX_LEN ] = {0 };
443+ uint8_t sheUid [] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
444+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 };
445+ uint8_t bootMacDigest [16 ] = {0 };
446+ uint8_t sreg = 0 ;
447+ uint8_t bootloaderBoundary [
448+ WOLFHSM_CFG_COMM_DATA_LEN -
449+ sizeof (whMessageShe_SecureBootUpdateRequest )];
450+ uint32_t digestSz = sizeof (bootMacDigest );
451+ uint32_t bootloaderSz ;
452+ uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN ;
453+ uint32_t maxBoundaryUpdateChunk =
454+ WOLFHSM_CFG_COMM_DATA_LEN -
455+ sizeof (whMessageShe_SecureBootUpdateRequest );
456+ uint32_t outClientId = 0 ;
457+ uint32_t outServerId = 0 ;
458+
459+ if (config == NULL ) {
460+ return WH_ERROR_BADARGS ;
461+ }
462+
463+ WH_TEST_RETURN_ON_FAIL (wh_Client_Init (client , config ));
464+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommInit (client , & outClientId , & outServerId ));
465+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommInfo (client , NULL , NULL ,
466+ & serverCommDataLen , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
467+ NULL , NULL , NULL , NULL ));
468+
469+ if (serverCommDataLen <= sizeof (whMessageShe_SecureBootUpdateRequest )) {
470+ WH_ERROR_PRINT ("Invalid server cfg_comm_data_len %u\n" ,
471+ (unsigned int )serverCommDataLen );
472+ ret = WH_ERROR_ABORTED ;
473+ goto exit_boundary ;
474+ }
475+ if (serverCommDataLen < WOLFHSM_CFG_COMM_DATA_LEN ) {
476+ maxBoundaryUpdateChunk =
477+ serverCommDataLen - sizeof (whMessageShe_SecureBootUpdateRequest );
478+ }
479+
480+ bootloaderSz = maxBoundaryUpdateChunk ;
481+
482+ if ((ret = wc_InitRng_ex (rng , NULL , WH_DEV_ID )) != 0 ) {
483+ WH_ERROR_PRINT ("Failed to wc_InitRng_ex %d\n" , ret );
484+ goto exit_boundary ;
485+ }
486+ if ((ret = wc_RNG_GenerateBlock (rng , key , sizeof (key ))) != 0 ) {
487+ WH_ERROR_PRINT ("Failed to wc_RNG_GenerateBlock %d\n" , ret );
488+ goto exit_boundary ;
489+ }
490+ if ((ret = wc_RNG_GenerateBlock (rng , bootloaderBoundary ,
491+ maxBoundaryUpdateChunk )) != 0 ) {
492+ WH_ERROR_PRINT ("Failed to wc_RNG_GenerateBlock %d\n" , ret );
493+ goto exit_boundary ;
494+ }
495+ wc_FreeRng (rng );
496+
497+ if ((ret = wc_InitCmac (cmac , key , sizeof (key ), WC_CMAC_AES , NULL )) != 0 ) {
498+ WH_ERROR_PRINT ("Failed to wc_InitCmac %d\n" , ret );
499+ goto exit_boundary ;
500+ }
501+ if ((ret = wc_CmacUpdate (cmac , zeros , sizeof (zeros ))) != 0 ) {
502+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
503+ goto exit_boundary ;
504+ }
505+ if ((ret = wc_CmacUpdate (cmac , (uint8_t * )& bootloaderSz ,
506+ sizeof (bootloaderSz ))) != 0 ) {
507+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
508+ goto exit_boundary ;
509+ }
510+ if ((ret = wc_CmacUpdate (cmac , bootloaderBoundary ,
511+ bootloaderSz )) != 0 ) {
512+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
513+ goto exit_boundary ;
514+ }
515+ digestSz = AES_BLOCK_SIZE ;
516+ if ((ret = wc_CmacFinal (cmac , bootMacDigest , (word32 * )& digestSz )) != 0 ) {
517+ WH_ERROR_PRINT ("Failed to wc_CmacFinal %d\n" , ret );
518+ goto exit_boundary ;
519+ }
520+
521+ if ((ret = wh_Client_ShePreProgramKey (client , WH_SHE_BOOT_MAC_KEY_ID , 0 ,
522+ key , sizeof (key ))) != 0 ) {
523+ WH_ERROR_PRINT ("Failed to wh_Client_ShePreProgramKey %d\n" , ret );
524+ goto exit_boundary ;
525+ }
526+ if ((ret = wh_Client_ShePreProgramKey (client , WH_SHE_BOOT_MAC , 0 ,
527+ bootMacDigest ,
528+ sizeof (bootMacDigest ))) != 0 ) {
529+ WH_ERROR_PRINT ("Failed to wh_Client_ShePreProgramKey %d\n" , ret );
530+ goto exit_boundary ;
531+ }
532+ if ((ret = wh_Client_SheSetUid (client , sheUid , sizeof (sheUid ))) != 0 ) {
533+ WH_ERROR_PRINT ("Failed to wh_Client_SheSetUid %d\n" , ret );
534+ goto exit_boundary ;
535+ }
536+ if ((ret = wh_Client_SheSecureBoot (client , bootloaderBoundary ,
537+ bootloaderSz )) != 0 ) {
538+ WH_ERROR_PRINT ("Failed to wh_Client_SheSecureBoot boundary %d\n" , ret );
539+ goto exit_boundary ;
540+ }
541+ if ((ret = wh_Client_SheGetStatus (client , & sreg )) != 0 ) {
542+ WH_ERROR_PRINT ("Failed to wh_Client_SheGetStatus %d\n" , ret );
543+ goto exit_boundary ;
544+ }
545+ if ((sreg & WH_SHE_SREG_BOOT_OK ) == 0 ||
546+ (sreg & WH_SHE_SREG_BOOT_FINISHED ) == 0 ||
547+ (sreg & WH_SHE_SREG_SECURE_BOOT ) == 0 ) {
548+ ret = WH_ERROR_ABORTED ;
549+ WH_ERROR_PRINT ("Failed secureBoot boundary with SHE CMAC\n" );
550+ goto exit_boundary ;
551+ }
552+ WH_TEST_PRINT ("SHE secure boot boundary SUCCESS\n" );
553+
554+ if ((ret = _destroySheKey (client , WH_SHE_BOOT_MAC_KEY_ID )) != 0 ) {
555+ WH_ERROR_PRINT ("Failed to _destroySheKey, ret=%d\n" , ret );
556+ goto exit_boundary ;
557+ }
558+ if ((ret = _destroySheKey (client , WH_SHE_BOOT_MAC )) != 0 ) {
559+ WH_ERROR_PRINT ("Failed to _destroySheKey, ret=%d\n" , ret );
560+ goto exit_boundary ;
561+ }
562+
563+ exit_boundary :
564+ /* Tell server to close */
565+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommClose (client ));
566+
567+ if (ret == 0 ) {
568+ WH_TEST_RETURN_ON_FAIL (wh_Client_Cleanup (client ));
569+ }
570+ else {
571+ wh_Client_Cleanup (client );
572+ }
573+
574+ return ret ;
575+ }
576+ #endif /* WOLFHSM_CFG_ENABLE_SERVER */
425577#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
426578
427579#ifdef WOLFHSM_CFG_ENABLE_SERVER
@@ -459,9 +611,17 @@ int whTest_SheServerConfig(whServerConfig* config)
459611
460612#if defined(WOLFHSM_CFG_TEST_POSIX ) && defined(WOLFHSM_CFG_ENABLE_CLIENT ) && \
461613 !defined(WOLFHSM_CFG_TEST_CLIENT_ONLY )
614+ typedef int (* whTestSheClientFn )(whClientConfig * config );
615+
616+ typedef struct {
617+ whClientConfig * clientConfig ;
618+ whTestSheClientFn clientFn ;
619+ } whTestSheClientTaskCtx ;
620+
462621static void * _whClientTask (void * cf )
463622{
464- WH_TEST_ASSERT (0 == whTest_SheClientConfig (cf ));
623+ whTestSheClientTaskCtx * ctx = (whTestSheClientTaskCtx * )cf ;
624+ WH_TEST_ASSERT (0 == ctx -> clientFn (ctx -> clientConfig ));
465625 return NULL ;
466626}
467627#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
@@ -478,17 +638,22 @@ static void* _whServerTask(void* cf)
478638#if defined(WOLFHSM_CFG_TEST_POSIX ) && defined(WOLFHSM_CFG_ENABLE_CLIENT ) && \
479639 defined(WOLFHSM_CFG_ENABLE_SERVER )
480640static void _whClientServerThreadTest (whClientConfig * c_conf ,
481- whServerConfig * s_conf )
641+ whServerConfig * s_conf ,
642+ whTestSheClientFn clientFn )
482643{
483644 pthread_t cthread = {0 };
484645 pthread_t sthread = {0 };
646+ whTestSheClientTaskCtx cTaskCtx = {
647+ .clientConfig = c_conf ,
648+ .clientFn = clientFn ,
649+ };
485650
486651 void * retval ;
487652 int rc = 0 ;
488653
489654 rc = pthread_create (& sthread , NULL , _whServerTask , s_conf );
490655 if (rc == 0 ) {
491- rc = pthread_create (& cthread , NULL , _whClientTask , c_conf );
656+ rc = pthread_create (& cthread , NULL , _whClientTask , & cTaskCtx );
492657 if (rc == 0 ) {
493658 /* All good. Block on joining */
494659 pthread_join (cthread , & retval );
@@ -502,7 +667,7 @@ static void _whClientServerThreadTest(whClientConfig* c_conf,
502667 }
503668}
504669
505- static int wh_ClientServer_MemThreadTest (void )
670+ static int wh_ClientServer_MemThreadTest (whTestSheClientFn clientFn )
506671{
507672 uint8_t req [BUFFER_SIZE ] = {0 };
508673 uint8_t resp [BUFFER_SIZE ] = {0 };
@@ -584,7 +749,7 @@ static int wh_ClientServer_MemThreadTest(void)
584749 WH_TEST_RETURN_ON_FAIL (wolfCrypt_Init ());
585750 WH_TEST_RETURN_ON_FAIL (wc_InitRng_ex (crypto -> rng , NULL , crypto -> devId ));
586751
587- _whClientServerThreadTest (c_conf , s_conf );
752+ _whClientServerThreadTest (c_conf , s_conf , clientFn );
588753
589754 wh_Nvm_Cleanup (nvm );
590755 wc_FreeRng (crypto -> rng );
@@ -708,8 +873,11 @@ int whTest_She(void)
708873{
709874 WH_TEST_PRINT ("Testing SHE: master ECU key fallback...\n" );
710875 WH_TEST_RETURN_ON_FAIL (wh_She_TestMasterEcuKeyFallback ());
711- WH_TEST_PRINT ("Testing SHE: (pthread) mem...\n" );
712- WH_TEST_RETURN_ON_FAIL (wh_ClientServer_MemThreadTest ());
876+ WH_TEST_PRINT ("Testing SHE: (pthread) mem core flow...\n" );
877+ WH_TEST_RETURN_ON_FAIL (wh_ClientServer_MemThreadTest (whTest_SheClientConfig ));
878+ WH_TEST_PRINT ("Testing SHE: (pthread) mem boundary secure boot...\n" );
879+ WH_TEST_RETURN_ON_FAIL (
880+ wh_ClientServer_MemThreadTest (whTest_SheClientConfigBoundarySecureBoot ));
713881 return 0 ;
714882}
715883#endif
0 commit comments