|
51 | 51 | #include "wolfhsm/wh_error.h" |
52 | 52 | #include "wolfhsm/wh_client.h" |
53 | 53 | #include "port/posix/posix_transport_tcp.h" |
54 | | -#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */ |
| 54 | +#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/ |
| 55 | +#include "wolfhsm/wh_error.h" |
| 56 | +#include "wolfhsm/wh_server.h" |
| 57 | +#include "wolfhsm/wh_server_keystore.h" |
| 58 | +#include "wolfhsm/wh_nvm.h" |
| 59 | +#include "wolfhsm/wh_nvm_flash.h" |
| 60 | +#include "wolfhsm/wh_transport_mem.h" |
| 61 | +#include "port/posix/posix_flash_file.h" |
| 62 | +#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */ |
55 | 63 |
|
56 | 64 | /* Global pointer to the internal and external flash base */ |
57 | 65 | uint8_t *sim_ram_base; |
@@ -107,7 +115,76 @@ const whNvmId hsmClientNvmIdCertRootCA = 1; |
107 | 115 | int hal_hsm_init_connect(void); |
108 | 116 | int hal_hsm_disconnect(void); |
109 | 117 |
|
110 | | -#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */ |
| 118 | +#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/ |
| 119 | + |
| 120 | +/* HAL Flash state and configuration */ |
| 121 | +const whFlashCb fcb[1] = {POSIX_FLASH_FILE_CB}; |
| 122 | +posixFlashFileContext fc[1] = {0}; |
| 123 | +posixFlashFileConfig fc_conf[1] = {{ |
| 124 | + .filename = "wolfBoot_wolfHSM_NVM.bin", |
| 125 | + .partition_size = 16384, |
| 126 | + .erased_byte = (uint8_t)0, |
| 127 | +}}; |
| 128 | +/* NVM Configuration using PosixSim HAL Flash */ |
| 129 | +whNvmFlashConfig nf_conf[1] = {{ |
| 130 | + .cb = fcb, |
| 131 | + .context = fc, |
| 132 | + .config = fc_conf, |
| 133 | +}}; |
| 134 | +whNvmFlashContext nfc[1] = {0}; |
| 135 | +whNvmCb nfcb[1] = {WH_NVM_FLASH_CB}; |
| 136 | + |
| 137 | +whNvmConfig n_conf[1] = {{ |
| 138 | + .cb = nfcb, |
| 139 | + .context = nfc, |
| 140 | + .config = nf_conf, |
| 141 | +}}; |
| 142 | +whNvmContext nvm[1] = {{0}}; |
| 143 | + |
| 144 | +static uint8_t req[] = {0}; |
| 145 | +static uint8_t resp[] = {0}; |
| 146 | + |
| 147 | +whTransportMemConfig tmcf[1] = {{ |
| 148 | + .req = (whTransportMemCsr*)req, |
| 149 | + .req_size = sizeof(req), |
| 150 | + .resp = (whTransportMemCsr*)resp, |
| 151 | + .resp_size = sizeof(resp), |
| 152 | +}}; |
| 153 | +whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB}; |
| 154 | +whTransportMemServerContext tmsc[1] = {0}; |
| 155 | +/* Dummy comm server config */ |
| 156 | +whCommServerConfig cs_conf[1] = {{ |
| 157 | + .transport_cb = tscb, |
| 158 | + .transport_context = &tmsc, |
| 159 | + .transport_config = &tmcf, |
| 160 | + .server_id = 0, |
| 161 | +}}; |
| 162 | + |
| 163 | +/* Crypto context */ |
| 164 | +whServerCryptoContext crypto[1] = {{ |
| 165 | + .devId = INVALID_DEVID, |
| 166 | +}}; |
| 167 | + |
| 168 | +#if defined(WOLFHSM_CFG_SHE_EXTENSION) |
| 169 | +whServerSheContext she[1] = {{0}}; |
| 170 | +#endif |
| 171 | + |
| 172 | +whServerConfig s_conf[1] = {{ |
| 173 | + .comm_config = cs_conf, |
| 174 | + .nvm = nvm, |
| 175 | + .crypto = crypto, |
| 176 | +}}; |
| 177 | + |
| 178 | +whServerContext hsmServerCtx = {0}; |
| 179 | + |
| 180 | +const int hsmServerDevIdHash = INVALID_DEVID; |
| 181 | +const int hsmServerDevIdPubKey = INVALID_DEVID; |
| 182 | +const whNvmId hsmServerNvmIdCertRootCA = 1; |
| 183 | + |
| 184 | +int hal_hsm_server_init(void); |
| 185 | +int hal_hsm_server_cleanup(void); |
| 186 | + |
| 187 | +#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER*/ |
111 | 188 |
|
112 | 189 | static int mmap_file(const char *path, uint8_t *address, uint8_t** ret_address) |
113 | 190 | { |
@@ -410,7 +487,6 @@ int wolfBoot_dualboot_candidate(void) |
410 | 487 |
|
411 | 488 | #ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT |
412 | 489 |
|
413 | | - |
414 | 490 | int hal_hsm_init_connect(void) |
415 | 491 | { |
416 | 492 | int rc = 0; |
@@ -449,4 +525,57 @@ int hal_hsm_disconnect(void) |
449 | 525 | return rc; |
450 | 526 | } |
451 | 527 |
|
452 | | -#endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */ |
| 528 | +#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) /*WOLFBOOT_ENABLE_WOLFHSM_CLIENT*/ |
| 529 | + |
| 530 | +int hal_hsm_server_init(void) |
| 531 | +{ |
| 532 | + int rc = 0; |
| 533 | + |
| 534 | + rc = wh_Nvm_Init(nvm, n_conf); |
| 535 | + if (rc != 0) { |
| 536 | + fprintf(stderr, "Failed to initialize NVM: %d\n", rc); |
| 537 | + exit(-1); |
| 538 | + } |
| 539 | + |
| 540 | + wolfCrypt_Init(); |
| 541 | + |
| 542 | + rc = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID); |
| 543 | + if (rc != 0) { |
| 544 | + fprintf(stderr, "Failed to initialize RNG: %d\n", rc); |
| 545 | + exit(-1); |
| 546 | + } |
| 547 | + |
| 548 | + rc = wh_Server_Init(&hsmServerCtx, s_conf); |
| 549 | + if (rc != 0) { |
| 550 | + fprintf(stderr, "Failed to initialize HSM server: %d\n", rc); |
| 551 | + exit(-1); |
| 552 | + } |
| 553 | + |
| 554 | + return rc; |
| 555 | +} |
| 556 | + |
| 557 | +int hal_hsm_server_cleanup(void) |
| 558 | +{ |
| 559 | + int rc = 0; |
| 560 | + |
| 561 | + rc = wh_Server_Cleanup(&hsmServerCtx); |
| 562 | + if (rc != 0) { |
| 563 | + fprintf(stderr, "Failed to cleanup HSM server: %d\n", rc); |
| 564 | + exit(-1); |
| 565 | + } |
| 566 | + |
| 567 | + rc = wc_FreeRng(crypto->rng); |
| 568 | + if (rc != 0) { |
| 569 | + fprintf(stderr, "Failed to cleanup RNG: %d\n", rc); |
| 570 | + exit(-1); |
| 571 | + } |
| 572 | + |
| 573 | + rc = wolfCrypt_Cleanup(); |
| 574 | + if (rc != 0) { |
| 575 | + fprintf(stderr, "Failed to cleanup wolfCrypt: %d\n", rc); |
| 576 | + exit(-1); |
| 577 | + } |
| 578 | + |
| 579 | + return rc; |
| 580 | +} |
| 581 | +#endif /* WOLFBOOT_ENABLE_WOLFHSM_SERVER */ |
0 commit comments