Skip to content

Commit a018af2

Browse files
committed
Address chunk size issue in wh_Client_SheSecureBoot
1 parent 3d6f973 commit a018af2

2 files changed

Lines changed: 183 additions & 10 deletions

File tree

src/wh_client_she.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader,
156156
while (ret == 0 && bootloaderSent < bootloaderLen) {
157157
uint8_t* in;
158158
uint32_t justSent;
159+
uint32_t remaining;
160+
static const uint32_t maxChunk =
161+
WOLFHSM_CFG_COMM_DATA_LEN - sizeof(*updateReq);
159162

160163
if (initResp->rc != WH_SHE_ERC_NO_ERROR) {
161164
return initResp->rc;
@@ -168,8 +171,8 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader,
168171
in = (uint8_t*)(updateReq + 1);
169172

170173
/* send what's left in the size available */
171-
updateReq->sz = ((bootloaderLen - bootloaderSent) %
172-
(WOLFHSM_CFG_COMM_DATA_LEN - sizeof(*updateReq)));
174+
remaining = bootloaderLen - bootloaderSent;
175+
updateReq->sz = (remaining > maxChunk) ? maxChunk : remaining;
173176

174177
justSent = updateReq->sz;
175178
memcpy(in, bootloader + bootloaderSent, updateReq->sz);

test/wh_test_she.c

Lines changed: 178 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
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"
@@ -75,7 +76,8 @@
7576
enum {
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,152 @@ int whTest_SheClientConfig(whClientConfig* config)
422430

423431
return ret;
424432
}
433+
434+
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
435+
defined(WOLFHSM_CFG_ENABLE_SERVER)
436+
static int whTest_SheClientConfigBoundarySecureBoot(whClientConfig* config)
437+
{
438+
int ret = 0;
439+
WC_RNG rng[1];
440+
Cmac cmac[1];
441+
whClientContext client[1] = {0};
442+
uint8_t key[16] = {0};
443+
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
444+
uint8_t sheUid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
445+
0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
446+
uint8_t bootMacDigest[16] = {0};
447+
uint8_t sreg = 0;
448+
uint8_t bootloaderBoundary[
449+
WOLFHSM_CFG_COMM_DATA_LEN -
450+
sizeof(whMessageShe_SecureBootUpdateRequest)];
451+
uint32_t digestSz = sizeof(bootMacDigest);
452+
uint32_t bootloaderSz;
453+
uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN;
454+
uint32_t maxBoundaryUpdateChunk =
455+
WOLFHSM_CFG_COMM_DATA_LEN -
456+
sizeof(whMessageShe_SecureBootUpdateRequest);
457+
uint32_t outClientId = 0;
458+
uint32_t outServerId = 0;
459+
460+
if (config == NULL) {
461+
return WH_ERROR_BADARGS;
462+
}
463+
464+
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, config));
465+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInit(client, &outClientId, &outServerId));
466+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInfo(client, NULL, NULL,
467+
&serverCommDataLen, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
468+
NULL, NULL, NULL, NULL));
469+
470+
if (serverCommDataLen <= sizeof(whMessageShe_SecureBootUpdateRequest)) {
471+
WH_ERROR_PRINT("Invalid server cfg_comm_data_len %u\n",
472+
(unsigned int)serverCommDataLen);
473+
ret = WH_ERROR_ABORTED;
474+
goto exit_boundary;
475+
}
476+
if (serverCommDataLen < WOLFHSM_CFG_COMM_DATA_LEN) {
477+
maxBoundaryUpdateChunk =
478+
serverCommDataLen - sizeof(whMessageShe_SecureBootUpdateRequest);
479+
}
480+
481+
bootloaderSz = maxBoundaryUpdateChunk;
482+
483+
if ((ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID)) != 0) {
484+
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
485+
goto exit_boundary;
486+
}
487+
if ((ret = wc_RNG_GenerateBlock(rng, key, sizeof(key))) != 0) {
488+
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
489+
goto exit_boundary;
490+
}
491+
if ((ret = wc_RNG_GenerateBlock(rng, bootloaderBoundary,
492+
maxBoundaryUpdateChunk)) != 0) {
493+
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
494+
goto exit_boundary;
495+
}
496+
wc_FreeRng(rng);
497+
498+
if ((ret = wc_InitCmac(cmac, key, sizeof(key), WC_CMAC_AES, NULL)) != 0) {
499+
WH_ERROR_PRINT("Failed to wc_InitCmac %d\n", ret);
500+
goto exit_boundary;
501+
}
502+
if ((ret = wc_CmacUpdate(cmac, zeros, sizeof(zeros))) != 0) {
503+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
504+
goto exit_boundary;
505+
}
506+
if ((ret = wc_CmacUpdate(cmac, (uint8_t*)&bootloaderSz,
507+
sizeof(bootloaderSz))) != 0) {
508+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
509+
goto exit_boundary;
510+
}
511+
if ((ret = wc_CmacUpdate(cmac, bootloaderBoundary,
512+
bootloaderSz)) != 0) {
513+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
514+
goto exit_boundary;
515+
}
516+
digestSz = AES_BLOCK_SIZE;
517+
if ((ret = wc_CmacFinal(cmac, bootMacDigest, (word32*)&digestSz)) != 0) {
518+
WH_ERROR_PRINT("Failed to wc_CmacFinal %d\n", ret);
519+
goto exit_boundary;
520+
}
521+
522+
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC_KEY_ID, 0,
523+
key, sizeof(key))) != 0) {
524+
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
525+
goto exit_boundary;
526+
}
527+
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC, 0,
528+
bootMacDigest,
529+
sizeof(bootMacDigest))) != 0) {
530+
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
531+
goto exit_boundary;
532+
}
533+
if ((ret = wh_Client_SheSetUid(client, sheUid, sizeof(sheUid))) != 0) {
534+
WH_ERROR_PRINT("Failed to wh_Client_SheSetUid %d\n", ret);
535+
goto exit_boundary;
536+
}
537+
if ((ret = wh_Client_SheSecureBoot(client, bootloaderBoundary,
538+
bootloaderSz)) != 0) {
539+
WH_ERROR_PRINT("Failed to wh_Client_SheSecureBoot boundary %d\n", ret);
540+
goto exit_boundary;
541+
}
542+
if ((ret = wh_Client_SheGetStatus(client, &sreg)) != 0) {
543+
WH_ERROR_PRINT("Failed to wh_Client_SheGetStatus %d\n", ret);
544+
goto exit_boundary;
545+
}
546+
if ((sreg & WH_SHE_SREG_BOOT_OK) == 0 ||
547+
(sreg & WH_SHE_SREG_BOOT_FINISHED) == 0 ||
548+
(sreg & WH_SHE_SREG_SECURE_BOOT) == 0) {
549+
ret = WH_ERROR_ABORTED;
550+
WH_ERROR_PRINT("Failed secureBoot boundary with SHE CMAC\n");
551+
goto exit_boundary;
552+
}
553+
WH_TEST_PRINT("SHE secure boot boundary SUCCESS\n");
554+
555+
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC_KEY_ID)) != 0) {
556+
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
557+
goto exit_boundary;
558+
}
559+
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC)) != 0) {
560+
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
561+
goto exit_boundary;
562+
}
563+
564+
exit_boundary:
565+
/* Tell server to close */
566+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommClose(client));
567+
568+
if (ret == 0) {
569+
WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client));
570+
}
571+
else {
572+
wh_Client_Cleanup(client);
573+
}
574+
575+
return ret;
576+
}
577+
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
578+
WOLFHSM_CFG_ENABLE_SERVER */
425579
#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
426580

427581
#ifdef WOLFHSM_CFG_ENABLE_SERVER
@@ -459,9 +613,17 @@ int whTest_SheServerConfig(whServerConfig* config)
459613

460614
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
461615
!defined(WOLFHSM_CFG_TEST_CLIENT_ONLY)
616+
typedef int (*whTestSheClientFn)(whClientConfig* config);
617+
618+
typedef struct {
619+
whClientConfig* clientConfig;
620+
whTestSheClientFn clientFn;
621+
} whTestSheClientTaskCtx;
622+
462623
static void* _whClientTask(void* cf)
463624
{
464-
WH_TEST_ASSERT(0 == whTest_SheClientConfig(cf));
625+
whTestSheClientTaskCtx* ctx = (whTestSheClientTaskCtx*)cf;
626+
WH_TEST_ASSERT(0 == ctx->clientFn(ctx->clientConfig));
465627
return NULL;
466628
}
467629
#endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \
@@ -478,17 +640,22 @@ static void* _whServerTask(void* cf)
478640
#if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
479641
defined(WOLFHSM_CFG_ENABLE_SERVER)
480642
static void _whClientServerThreadTest(whClientConfig* c_conf,
481-
whServerConfig* s_conf)
643+
whServerConfig* s_conf,
644+
whTestSheClientFn clientFn)
482645
{
483646
pthread_t cthread = {0};
484647
pthread_t sthread = {0};
648+
whTestSheClientTaskCtx cTaskCtx = {
649+
.clientConfig = c_conf,
650+
.clientFn = clientFn,
651+
};
485652

486653
void* retval;
487654
int rc = 0;
488655

489656
rc = pthread_create(&sthread, NULL, _whServerTask, s_conf);
490657
if (rc == 0) {
491-
rc = pthread_create(&cthread, NULL, _whClientTask, c_conf);
658+
rc = pthread_create(&cthread, NULL, _whClientTask, &cTaskCtx);
492659
if (rc == 0) {
493660
/* All good. Block on joining */
494661
pthread_join(cthread, &retval);
@@ -502,7 +669,7 @@ static void _whClientServerThreadTest(whClientConfig* c_conf,
502669
}
503670
}
504671

505-
static int wh_ClientServer_MemThreadTest(void)
672+
static int wh_ClientServer_MemThreadTest(whTestSheClientFn clientFn)
506673
{
507674
uint8_t req[BUFFER_SIZE] = {0};
508675
uint8_t resp[BUFFER_SIZE] = {0};
@@ -584,7 +751,7 @@ static int wh_ClientServer_MemThreadTest(void)
584751
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
585752
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId));
586753

587-
_whClientServerThreadTest(c_conf, s_conf);
754+
_whClientServerThreadTest(c_conf, s_conf, clientFn);
588755

589756
wh_Nvm_Cleanup(nvm);
590757
wc_FreeRng(crypto->rng);
@@ -708,8 +875,11 @@ int whTest_She(void)
708875
{
709876
WH_TEST_PRINT("Testing SHE: master ECU key fallback...\n");
710877
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());
878+
WH_TEST_PRINT("Testing SHE: (pthread) mem core flow...\n");
879+
WH_TEST_RETURN_ON_FAIL(wh_ClientServer_MemThreadTest(whTest_SheClientConfig));
880+
WH_TEST_PRINT("Testing SHE: (pthread) mem boundary secure boot...\n");
881+
WH_TEST_RETURN_ON_FAIL(
882+
wh_ClientServer_MemThreadTest(whTest_SheClientConfigBoundarySecureBoot));
713883
return 0;
714884
}
715885
#endif

0 commit comments

Comments
 (0)