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