Skip to content

Commit cfdc3a8

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

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
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: 89 additions & 0 deletions
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"
@@ -122,6 +123,9 @@ int whTest_SheClientConfig(whClientConfig* config)
122123
0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
123124
uint8_t zeros[WH_SHE_BOOT_MAC_PREFIX_LEN] = {0};
124125
uint8_t bootloader[512];
126+
uint8_t bootloaderBoundary[
127+
WOLFHSM_CFG_COMM_DATA_LEN -
128+
sizeof(whMessageShe_SecureBootUpdateRequest)];
125129
uint8_t bootMacDigest[16] = {0};
126130
uint8_t vectorMasterEcuKey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
127131
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
@@ -155,6 +159,10 @@ int whTest_SheClientConfig(whClientConfig* config)
155159
uint8_t messageFive[WH_SHE_M5_SZ];
156160
uint32_t outClientId = 0;
157161
uint32_t outServerId = 0;
162+
uint32_t serverCommDataLen = WOLFHSM_CFG_COMM_DATA_LEN;
163+
uint32_t maxBoundaryUpdateChunk =
164+
WOLFHSM_CFG_COMM_DATA_LEN -
165+
sizeof(whMessageShe_SecureBootUpdateRequest);
158166
const uint32_t SHE_TEST_VECTOR_KEY_ID = 4;
159167

160168
if (config == NULL) {
@@ -163,6 +171,20 @@ int whTest_SheClientConfig(whClientConfig* config)
163171

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

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

0 commit comments

Comments
 (0)