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,148 @@ int whTest_SheClientConfig(whClientConfig* config)
422430
423431 return ret ;
424432}
433+
434+ static int whTest_SheClientConfigBoundarySecureBoot (whClientConfig * config )
435+ {
436+ int ret = 0 ;
437+ WC_RNG rng [1 ];
438+ Cmac cmac [1 ];
439+ whClientContext client [1 ] = {0 };
440+ uint8_t key [16 ] = {0 };
441+ uint8_t zeros [WH_SHE_BOOT_MAC_PREFIX_LEN ] = {0 };
442+ uint8_t sheUid [] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
443+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 };
444+ uint8_t bootMacDigest [16 ] = {0 };
445+ uint8_t sreg = 0 ;
446+ uint8_t bootloaderBoundary [
447+ WOLFHSM_CFG_COMM_DATA_LEN -
448+ sizeof (whMessageShe_SecureBootUpdateRequest )];
449+ uint32_t digestSz = sizeof (bootMacDigest );
450+ uint32_t bootloaderSz ;
451+ uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN ;
452+ uint32_t maxBoundaryUpdateChunk =
453+ WOLFHSM_CFG_COMM_DATA_LEN -
454+ sizeof (whMessageShe_SecureBootUpdateRequest );
455+ uint32_t outClientId = 0 ;
456+ uint32_t outServerId = 0 ;
457+
458+ if (config == NULL ) {
459+ return WH_ERROR_BADARGS ;
460+ }
461+
462+ WH_TEST_RETURN_ON_FAIL (wh_Client_Init (client , config ));
463+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommInit (client , & outClientId , & outServerId ));
464+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommInfo (client , NULL , NULL ,
465+ & serverCommDataLen , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
466+ NULL , NULL , NULL , NULL ));
467+
468+ if (serverCommDataLen <= sizeof (whMessageShe_SecureBootUpdateRequest )) {
469+ WH_ERROR_PRINT ("Invalid server cfg_comm_data_len %u\n" ,
470+ (unsigned int )serverCommDataLen );
471+ ret = WH_ERROR_ABORTED ;
472+ goto exit_boundary ;
473+ }
474+ if (serverCommDataLen < WOLFHSM_CFG_COMM_DATA_LEN ) {
475+ maxBoundaryUpdateChunk =
476+ serverCommDataLen - sizeof (whMessageShe_SecureBootUpdateRequest );
477+ }
478+
479+ bootloaderSz = maxBoundaryUpdateChunk ;
480+
481+ if ((ret = wc_InitRng_ex (rng , NULL , WH_DEV_ID )) != 0 ) {
482+ WH_ERROR_PRINT ("Failed to wc_InitRng_ex %d\n" , ret );
483+ goto exit_boundary ;
484+ }
485+ if ((ret = wc_RNG_GenerateBlock (rng , key , sizeof (key ))) != 0 ) {
486+ WH_ERROR_PRINT ("Failed to wc_RNG_GenerateBlock %d\n" , ret );
487+ goto exit_boundary ;
488+ }
489+ if ((ret = wc_RNG_GenerateBlock (rng , bootloaderBoundary ,
490+ maxBoundaryUpdateChunk )) != 0 ) {
491+ WH_ERROR_PRINT ("Failed to wc_RNG_GenerateBlock %d\n" , ret );
492+ goto exit_boundary ;
493+ }
494+ wc_FreeRng (rng );
495+
496+ if ((ret = wc_InitCmac (cmac , key , sizeof (key ), WC_CMAC_AES , NULL )) != 0 ) {
497+ WH_ERROR_PRINT ("Failed to wc_InitCmac %d\n" , ret );
498+ goto exit_boundary ;
499+ }
500+ if ((ret = wc_CmacUpdate (cmac , zeros , sizeof (zeros ))) != 0 ) {
501+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
502+ goto exit_boundary ;
503+ }
504+ if ((ret = wc_CmacUpdate (cmac , (uint8_t * )& bootloaderSz ,
505+ sizeof (bootloaderSz ))) != 0 ) {
506+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
507+ goto exit_boundary ;
508+ }
509+ if ((ret = wc_CmacUpdate (cmac , bootloaderBoundary ,
510+ bootloaderSz )) != 0 ) {
511+ WH_ERROR_PRINT ("Failed to wc_CmacUpdate %d\n" , ret );
512+ goto exit_boundary ;
513+ }
514+ digestSz = AES_BLOCK_SIZE ;
515+ if ((ret = wc_CmacFinal (cmac , bootMacDigest , (word32 * )& digestSz )) != 0 ) {
516+ WH_ERROR_PRINT ("Failed to wc_CmacFinal %d\n" , ret );
517+ goto exit_boundary ;
518+ }
519+
520+ if ((ret = wh_Client_ShePreProgramKey (client , WH_SHE_BOOT_MAC_KEY_ID , 0 ,
521+ key , sizeof (key ))) != 0 ) {
522+ WH_ERROR_PRINT ("Failed to wh_Client_ShePreProgramKey %d\n" , ret );
523+ goto exit_boundary ;
524+ }
525+ if ((ret = wh_Client_ShePreProgramKey (client , WH_SHE_BOOT_MAC , 0 ,
526+ bootMacDigest ,
527+ sizeof (bootMacDigest ))) != 0 ) {
528+ WH_ERROR_PRINT ("Failed to wh_Client_ShePreProgramKey %d\n" , ret );
529+ goto exit_boundary ;
530+ }
531+ if ((ret = wh_Client_SheSetUid (client , sheUid , sizeof (sheUid ))) != 0 ) {
532+ WH_ERROR_PRINT ("Failed to wh_Client_SheSetUid %d\n" , ret );
533+ goto exit_boundary ;
534+ }
535+ if ((ret = wh_Client_SheSecureBoot (client , bootloaderBoundary ,
536+ bootloaderSz )) != 0 ) {
537+ WH_ERROR_PRINT ("Failed to wh_Client_SheSecureBoot boundary %d\n" , ret );
538+ goto exit_boundary ;
539+ }
540+ if ((ret = wh_Client_SheGetStatus (client , & sreg )) != 0 ) {
541+ WH_ERROR_PRINT ("Failed to wh_Client_SheGetStatus %d\n" , ret );
542+ goto exit_boundary ;
543+ }
544+ if ((sreg & WH_SHE_SREG_BOOT_OK ) == 0 ||
545+ (sreg & WH_SHE_SREG_BOOT_FINISHED ) == 0 ||
546+ (sreg & WH_SHE_SREG_SECURE_BOOT ) == 0 ) {
547+ ret = WH_ERROR_ABORTED ;
548+ WH_ERROR_PRINT ("Failed secureBoot boundary with SHE CMAC\n" );
549+ goto exit_boundary ;
550+ }
551+ WH_TEST_PRINT ("SHE secure boot boundary SUCCESS\n" );
552+
553+ if ((ret = _destroySheKey (client , WH_SHE_BOOT_MAC_KEY_ID )) != 0 ) {
554+ WH_ERROR_PRINT ("Failed to _destroySheKey, ret=%d\n" , ret );
555+ goto exit_boundary ;
556+ }
557+ if ((ret = _destroySheKey (client , WH_SHE_BOOT_MAC )) != 0 ) {
558+ WH_ERROR_PRINT ("Failed to _destroySheKey, ret=%d\n" , ret );
559+ goto exit_boundary ;
560+ }
561+
562+ exit_boundary :
563+ /* Tell server to close */
564+ WH_TEST_RETURN_ON_FAIL (wh_Client_CommClose (client ));
565+
566+ if (ret == 0 ) {
567+ WH_TEST_RETURN_ON_FAIL (wh_Client_Cleanup (client ));
568+ }
569+ else {
570+ wh_Client_Cleanup (client );
571+ }
572+
573+ return ret ;
574+ }
425575#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
426576
427577#ifdef WOLFHSM_CFG_ENABLE_SERVER
@@ -459,9 +609,17 @@ int whTest_SheServerConfig(whServerConfig* config)
459609
460610#if defined(WOLFHSM_CFG_TEST_POSIX ) && defined(WOLFHSM_CFG_ENABLE_CLIENT ) && \
461611 !defined(WOLFHSM_CFG_TEST_CLIENT_ONLY )
612+ typedef int (* whTestSheClientFn )(whClientConfig * config );
613+
614+ typedef struct {
615+ whClientConfig * clientConfig ;
616+ whTestSheClientFn clientFn ;
617+ } whTestSheClientTaskCtx ;
618+
462619static void * _whClientTask (void * cf )
463620{
464- WH_TEST_ASSERT (0 == whTest_SheClientConfig (cf ));
621+ whTestSheClientTaskCtx * ctx = (whTestSheClientTaskCtx * )cf ;
622+ WH_TEST_ASSERT (0 == ctx -> clientFn (ctx -> clientConfig ));
465623 return NULL ;
466624}
467625#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
@@ -478,17 +636,22 @@ static void* _whServerTask(void* cf)
478636#if defined(WOLFHSM_CFG_TEST_POSIX ) && defined(WOLFHSM_CFG_ENABLE_CLIENT ) && \
479637 defined(WOLFHSM_CFG_ENABLE_SERVER )
480638static void _whClientServerThreadTest (whClientConfig * c_conf ,
481- whServerConfig * s_conf )
639+ whServerConfig * s_conf ,
640+ whTestSheClientFn clientFn )
482641{
483642 pthread_t cthread = {0 };
484643 pthread_t sthread = {0 };
644+ whTestSheClientTaskCtx cTaskCtx = {
645+ .clientConfig = c_conf ,
646+ .clientFn = clientFn ,
647+ };
485648
486649 void * retval ;
487650 int rc = 0 ;
488651
489652 rc = pthread_create (& sthread , NULL , _whServerTask , s_conf );
490653 if (rc == 0 ) {
491- rc = pthread_create (& cthread , NULL , _whClientTask , c_conf );
654+ rc = pthread_create (& cthread , NULL , _whClientTask , & cTaskCtx );
492655 if (rc == 0 ) {
493656 /* All good. Block on joining */
494657 pthread_join (cthread , & retval );
@@ -502,7 +665,7 @@ static void _whClientServerThreadTest(whClientConfig* c_conf,
502665 }
503666}
504667
505- static int wh_ClientServer_MemThreadTest (void )
668+ static int wh_ClientServer_MemThreadTest (whTestSheClientFn clientFn )
506669{
507670 uint8_t req [BUFFER_SIZE ] = {0 };
508671 uint8_t resp [BUFFER_SIZE ] = {0 };
@@ -584,7 +747,7 @@ static int wh_ClientServer_MemThreadTest(void)
584747 WH_TEST_RETURN_ON_FAIL (wolfCrypt_Init ());
585748 WH_TEST_RETURN_ON_FAIL (wc_InitRng_ex (crypto -> rng , NULL , crypto -> devId ));
586749
587- _whClientServerThreadTest (c_conf , s_conf );
750+ _whClientServerThreadTest (c_conf , s_conf , clientFn );
588751
589752 wh_Nvm_Cleanup (nvm );
590753 wc_FreeRng (crypto -> rng );
@@ -708,8 +871,11 @@ int whTest_She(void)
708871{
709872 WH_TEST_PRINT ("Testing SHE: master ECU key fallback...\n" );
710873 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 ());
874+ WH_TEST_PRINT ("Testing SHE: (pthread) mem core flow...\n" );
875+ WH_TEST_RETURN_ON_FAIL (wh_ClientServer_MemThreadTest (whTest_SheClientConfig ));
876+ WH_TEST_PRINT ("Testing SHE: (pthread) mem boundary secure boot...\n" );
877+ WH_TEST_RETURN_ON_FAIL (
878+ wh_ClientServer_MemThreadTest (whTest_SheClientConfigBoundarySecureBoot ));
713879 return 0 ;
714880}
715881#endif
0 commit comments