|
33 | 33 | #include "wolfhsm/wh_nvm.h" |
34 | 34 | #include "wolfhsm/wh_nvm_flash.h" |
35 | 35 | #include "wolfhsm/wh_flash_ramsim.h" |
| 36 | +#include "wolfhsm/wh_server_keystore.h" |
36 | 37 | #endif |
37 | 38 |
|
38 | 39 | #include "wolfhsm/wh_comm.h" |
@@ -594,10 +595,119 @@ static int wh_ClientServer_MemThreadTest(void) |
594 | 595 | #endif /* WOLFHSM_CFG_TEST_POSIX && WOLFHSM_CFG_ENABLE_CLIENT && \ |
595 | 596 | WOLFHSM_CFG_ENABLE_SERVER */ |
596 | 597 |
|
| 598 | +#if defined(WOLFHSM_CFG_ENABLE_SERVER) |
| 599 | +static int wh_She_TestMasterEcuKeyFallback(void) |
| 600 | +{ |
| 601 | + int ret = 0; |
| 602 | + whServerContext server[1] = {0}; |
| 603 | + whNvmMetadata outMeta[1] = {0}; |
| 604 | + uint8_t keyBuf[WH_SHE_KEY_SZ] = {0}; |
| 605 | + uint32_t keySz = sizeof(keyBuf); |
| 606 | + uint8_t zeros[WH_SHE_KEY_SZ] = {0}; |
| 607 | + whKeyId masterEcuKeyId; |
| 608 | + |
| 609 | + /* Transport (not used, but required for server init) */ |
| 610 | + uint8_t reqBuf[BUFFER_SIZE] = {0}; |
| 611 | + uint8_t respBuf[BUFFER_SIZE] = {0}; |
| 612 | + whTransportMemConfig tmcf[1] = {{ |
| 613 | + .req = (whTransportMemCsr*)reqBuf, |
| 614 | + .req_size = sizeof(reqBuf), |
| 615 | + .resp = (whTransportMemCsr*)respBuf, |
| 616 | + .resp_size = sizeof(respBuf), |
| 617 | + }}; |
| 618 | + whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB}; |
| 619 | + whTransportMemServerContext tmsc[1] = {0}; |
| 620 | + whCommServerConfig cs_conf[1] = {{ |
| 621 | + .transport_cb = tscb, |
| 622 | + .transport_context = (void*)tmsc, |
| 623 | + .transport_config = (void*)tmcf, |
| 624 | + .server_id = 124, |
| 625 | + }}; |
| 626 | + |
| 627 | + /* RamSim Flash state and configuration */ |
| 628 | + uint8_t memory[FLASH_RAM_SIZE] = {0}; |
| 629 | + whFlashRamsimCtx fc[1] = {0}; |
| 630 | + whFlashRamsimCfg fc_conf[1] = {{ |
| 631 | + .size = FLASH_RAM_SIZE, |
| 632 | + .sectorSize = FLASH_SECTOR_SIZE, |
| 633 | + .pageSize = FLASH_PAGE_SIZE, |
| 634 | + .erasedByte = ~(uint8_t)0, |
| 635 | + .memory = memory, |
| 636 | + }}; |
| 637 | + const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; |
| 638 | + |
| 639 | + /* NVM */ |
| 640 | + whNvmFlashConfig nf_conf[1] = {{ |
| 641 | + .cb = fcb, |
| 642 | + .context = fc, |
| 643 | + .config = fc_conf, |
| 644 | + }}; |
| 645 | + whNvmFlashContext nfc[1] = {0}; |
| 646 | + whNvmCb nfcb[1] = {WH_NVM_FLASH_CB}; |
| 647 | + whNvmConfig n_conf[1] = {{ |
| 648 | + .cb = nfcb, |
| 649 | + .context = nfc, |
| 650 | + .config = nf_conf, |
| 651 | + }}; |
| 652 | + whNvmContext nvm[1] = {{0}}; |
| 653 | + |
| 654 | + /* Crypto context */ |
| 655 | + whServerCryptoContext crypto[1] = {{ |
| 656 | + .devId = INVALID_DEVID, |
| 657 | + }}; |
| 658 | + |
| 659 | + whServerSheContext she[1]; |
| 660 | + memset(she, 0, sizeof(she)); |
| 661 | + |
| 662 | + whServerConfig s_conf[1] = {{ |
| 663 | + .comm_config = cs_conf, |
| 664 | + .nvm = nvm, |
| 665 | + .crypto = crypto, |
| 666 | + .she = she, |
| 667 | + .devId = INVALID_DEVID, |
| 668 | + }}; |
| 669 | + |
| 670 | + WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); |
| 671 | + WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); |
| 672 | + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); |
| 673 | + WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, s_conf)); |
| 674 | + WH_TEST_RETURN_ON_FAIL( |
| 675 | + wh_Server_SetConnected(server, WH_COMM_CONNECTED)); |
| 676 | + |
| 677 | + masterEcuKeyId = WH_MAKE_KEYID(WH_KEYTYPE_SHE, |
| 678 | + server->comm->client_id, |
| 679 | + WH_SHE_MASTER_ECU_KEY_ID); |
| 680 | + |
| 681 | + /* Fill keyBuf with non-zero to ensure it gets overwritten */ |
| 682 | + memset(keyBuf, 0xFF, sizeof(keyBuf)); |
| 683 | + |
| 684 | + /* Read master ECU key when it has never been provisioned */ |
| 685 | + ret = wh_Server_KeystoreReadKey(server, masterEcuKeyId, outMeta, |
| 686 | + keyBuf, &keySz); |
| 687 | + |
| 688 | + WH_TEST_ASSERT_RETURN(ret == 0); |
| 689 | + WH_TEST_ASSERT_RETURN(keySz == WH_SHE_KEY_SZ); |
| 690 | + WH_TEST_ASSERT_RETURN(memcmp(keyBuf, zeros, WH_SHE_KEY_SZ) == 0); |
| 691 | + WH_TEST_ASSERT_RETURN(outMeta->len == WH_SHE_KEY_SZ); |
| 692 | + WH_TEST_ASSERT_RETURN(outMeta->id == masterEcuKeyId); |
| 693 | + |
| 694 | + WH_TEST_PRINT("SHE master ECU key fallback metadata test SUCCESS\n"); |
| 695 | + |
| 696 | + wh_Server_Cleanup(server); |
| 697 | + wh_Nvm_Cleanup(nvm); |
| 698 | + wc_FreeRng(crypto->rng); |
| 699 | + wolfCrypt_Cleanup(); |
| 700 | + |
| 701 | + return 0; |
| 702 | +} |
| 703 | +#endif /* WOLFHSM_CFG_ENABLE_SERVER */ |
| 704 | + |
597 | 705 | #if defined(WOLFHSM_CFG_TEST_POSIX) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \ |
598 | 706 | defined(WOLFHSM_CFG_ENABLE_SERVER) |
599 | 707 | int whTest_She(void) |
600 | 708 | { |
| 709 | + WH_TEST_PRINT("Testing SHE: master ECU key fallback...\n"); |
| 710 | + WH_TEST_RETURN_ON_FAIL(wh_She_TestMasterEcuKeyFallback()); |
601 | 711 | WH_TEST_PRINT("Testing SHE: (pthread) mem...\n"); |
602 | 712 | WH_TEST_RETURN_ON_FAIL(wh_ClientServer_MemThreadTest()); |
603 | 713 | return 0; |
|
0 commit comments