Skip to content

Commit 6a06299

Browse files
committed
Address chunk size issue in wh_Client_SheSecureBoot
1 parent 95cb29c commit 6a06299

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

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);

src/wh_server_nvm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include "wolfhsm/wh_server.h"
4444
#include "wolfhsm/wh_server_nvm.h"
4545

46+
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
47+
#include "wolfhsm/wh_server_keystore.h"
48+
#endif
49+
4650
/* Handle NVM read, do access checking and clamping */
4751
static int _HandleNvmRead(whServerContext* server, uint8_t* out_data,
4852
whNvmSize offset, whNvmSize len, whNvmSize* out_len,
@@ -290,6 +294,17 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
290294
/* Process the DestroyObjects action */
291295
rc = wh_Nvm_DestroyObjectsChecked(server->nvm,
292296
req.list_count, req.list);
297+
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
298+
if (rc == WH_ERROR_OK) {
299+
whNvmId i;
300+
for (i = 0; i < req.list_count; i++) {
301+
/* Best-effort cache invalidation to prevent stale
302+
* key material after object destroy/recreate. */
303+
(void)wh_Server_KeystoreEvictKey(server,
304+
req.list[i]);
305+
}
306+
}
307+
#endif
293308

294309
(void)WH_SERVER_NVM_UNLOCK(server);
295310
} /* WH_SERVER_NVM_LOCK() */

src/wh_server_she.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,18 @@ static int _SecureBootInit(whServerContext* server, uint16_t magic,
232232

233233
(void)wh_MessageShe_TranslateSecureBootInitRequest(magic, req_packet, &req);
234234

235-
/* if we aren't looking for init return error */
236-
if (server->she->sbState != WH_SHE_SB_INIT) {
235+
/* Allow a new secure-boot session after a completed prior session.
236+
* Only reject init while an active session is in progress.
237+
*/
238+
if (server->she->sbState == WH_SHE_SB_UPDATE ||
239+
server->she->sbState == WH_SHE_SB_FINISH) {
237240
ret = WH_SHE_ERC_SEQUENCE_ERROR;
238241
}
239242
if (ret == 0) {
240243
/* set the expected size */
241244
server->she->blSize = req.sz;
245+
server->she->blSizeReceived = 0;
246+
server->she->cmacKeyFound = 0;
242247
/* check if the boot mac key is empty */
243248
keySz = sizeof(macKey);
244249
ret = wh_Server_KeystoreReadKey(server,

test/wh_test_she.c

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include "wolfhsm/wh_comm.h"
3939
#include "wolfhsm/wh_message.h"
40+
#include "wolfhsm/wh_message_she.h"
4041

4142
#ifdef WOLFHSM_CFG_ENABLE_SERVER
4243
#include "wolfhsm/wh_server.h"
@@ -74,7 +75,8 @@
7475
enum {
7576
REQ_SIZE = 32,
7677
RESP_SIZE = 64,
77-
BUFFER_SIZE = 4096,
78+
BUFFER_SIZE = sizeof(whTransportMemCsr) + sizeof(whCommHeader) +
79+
WOLFHSM_CFG_COMM_DATA_LEN,
7880
};
7981

8082
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
@@ -122,6 +124,9 @@ int whTest_SheClientConfig(whClientConfig* config)
122124
0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
123125
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
124126
uint8_t bootloader[512];
127+
uint8_t bootloaderBoundary[
128+
WOLFHSM_CFG_COMM_DATA_LEN -
129+
sizeof(whMessageShe_SecureBootUpdateRequest)];
125130
uint8_t bootMacDigest[16] = {0};
126131
uint8_t vectorMasterEcuKey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
127132
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
@@ -155,6 +160,10 @@ int whTest_SheClientConfig(whClientConfig* config)
155160
uint8_t messageFive[WH_SHE_M5_SZ];
156161
uint32_t outClientId = 0;
157162
uint32_t outServerId = 0;
163+
uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN;
164+
uint32_t maxBoundaryUpdateChunk =
165+
WOLFHSM_CFG_COMM_DATA_LEN -
166+
sizeof(whMessageShe_SecureBootUpdateRequest);
158167
const uint32_t SHE_TEST_VECTOR_KEY_ID = 4;
159168

160169
if (config == NULL) {
@@ -163,6 +172,20 @@ int whTest_SheClientConfig(whClientConfig* config)
163172

164173
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, config));
165174
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInit(client, &outClientId, &outServerId));
175+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInfo(client, NULL, NULL,
176+
&serverCommDataLen, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
177+
NULL, NULL, NULL, NULL));
178+
179+
if (serverCommDataLen <= sizeof(whMessageShe_SecureBootUpdateRequest)) {
180+
WH_ERROR_PRINT("Invalid server cfg_comm_data_len %u\n",
181+
(unsigned int)serverCommDataLen);
182+
ret = WH_ERROR_ABORTED;
183+
goto exit;
184+
}
185+
if (serverCommDataLen < WOLFHSM_CFG_COMM_DATA_LEN) {
186+
maxBoundaryUpdateChunk =
187+
serverCommDataLen - sizeof(whMessageShe_SecureBootUpdateRequest);
188+
}
166189

167190
{
168191
int32_t server_rc = 0;
@@ -196,6 +219,11 @@ int whTest_SheClientConfig(whClientConfig* config)
196219
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
197220
goto exit;
198221
}
222+
if ((ret = wc_RNG_GenerateBlock(rng, bootloaderBoundary,
223+
maxBoundaryUpdateChunk)) != 0) {
224+
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
225+
goto exit;
226+
}
199227
/* Done generating test data, free RNG */
200228
wc_FreeRng(rng);
201229
/* cmac 0..0 | size | bootloader */
@@ -253,6 +281,68 @@ int whTest_SheClientConfig(whClientConfig* config)
253281
goto exit;
254282
}
255283
WH_TEST_PRINT("SHE secure boot SUCCESS\n");
284+
285+
/* verify bootloader at exact max update chunk boundary */
286+
bootloaderSz = maxBoundaryUpdateChunk;
287+
memset(bootMacDigest, 0, sizeof(bootMacDigest));
288+
if ((ret = wc_InitCmac(cmac, key, sizeof(key), WC_CMAC_AES, NULL)) != 0) {
289+
WH_ERROR_PRINT("Failed to wc_InitCmac %d\n", ret);
290+
goto exit;
291+
}
292+
if ((ret = wc_CmacUpdate(cmac, zeros, sizeof(zeros))) != 0) {
293+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
294+
goto exit;
295+
}
296+
if ((ret = wc_CmacUpdate(cmac, (uint8_t*)&bootloaderSz,
297+
sizeof(bootloaderSz))) != 0) {
298+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
299+
goto exit;
300+
}
301+
if ((ret = wc_CmacUpdate(cmac, bootloaderBoundary,
302+
bootloaderSz)) != 0) {
303+
WH_ERROR_PRINT("Failed to wc_CmacUpdate %d\n", ret);
304+
goto exit;
305+
}
306+
digestSz = AES_BLOCK_SIZE;
307+
if ((ret = wc_CmacFinal(cmac, bootMacDigest, (word32*)&digestSz)) != 0) {
308+
WH_ERROR_PRINT("Failed to wc_CmacFinal %d\n", ret);
309+
goto exit;
310+
}
311+
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC_KEY_ID)) != 0) {
312+
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
313+
goto exit;
314+
}
315+
if ((ret = _destroySheKey(client, WH_SHE_BOOT_MAC)) != 0) {
316+
WH_ERROR_PRINT("Failed to _destroySheKey, ret=%d\n", ret);
317+
goto exit;
318+
}
319+
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC_KEY_ID, 0,
320+
key, sizeof(key))) != 0) {
321+
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
322+
goto exit;
323+
}
324+
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_BOOT_MAC, 0,
325+
bootMacDigest,
326+
sizeof(bootMacDigest))) != 0) {
327+
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);
328+
goto exit;
329+
}
330+
if ((ret = wh_Client_SheSecureBoot(client, bootloaderBoundary,
331+
bootloaderSz)) != 0) {
332+
WH_ERROR_PRINT("Failed to wh_Client_SheSecureBoot boundary %d\n", ret);
333+
goto exit;
334+
}
335+
if ((ret = wh_Client_SheGetStatus(client, &sreg)) != 0) {
336+
WH_ERROR_PRINT("Failed to wh_Client_SheGetStatus %d\n", ret);
337+
goto exit;
338+
}
339+
if ((sreg & WH_SHE_SREG_BOOT_OK) == 0 ||
340+
(sreg & WH_SHE_SREG_BOOT_FINISHED) == 0 ||
341+
(sreg & WH_SHE_SREG_SECURE_BOOT) == 0) {
342+
WH_ERROR_PRINT("Failed secureBoot boundary with SHE CMAC\n");
343+
goto exit;
344+
}
345+
WH_TEST_PRINT("SHE secure boot boundary SUCCESS\n");
256346
/* load the secret key using pre program */
257347
if ((ret = wh_Client_ShePreProgramKey(client, WH_SHE_SECRET_KEY_ID, 0, secretKey, sizeof(secretKey))) != 0) {
258348
WH_ERROR_PRINT("Failed to wh_Client_ShePreProgramKey %d\n", ret);

0 commit comments

Comments
 (0)