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"
7475enum {
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