From 153ba3a5d650551fc12c09bba54742f021b389e3 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Wed, 4 Feb 2026 16:20:55 -0500 Subject: [PATCH 1/4] Implement HW/SW crypto affinity --- .gitignore | 1 + docs/draft/crypto_affinity.md | 92 ++++++ src/wh_client.c | 69 ++++ src/wh_message_comm.c | 21 ++ src/wh_server.c | 52 +++ test/wh_test.c | 5 + test/wh_test_check_struct_padding.c | 10 +- test/wh_test_crypto_affinity.c | 482 ++++++++++++++++++++++++++++ test/wh_test_crypto_affinity.h | 24 ++ wolfhsm/wh_client.h | 38 +++ wolfhsm/wh_error.h | 1 + wolfhsm/wh_message_comm.h | 35 +- wolfhsm/wh_server.h | 1 + 13 files changed, 821 insertions(+), 10 deletions(-) create mode 100644 docs/draft/crypto_affinity.md create mode 100644 test/wh_test_crypto_affinity.c create mode 100644 test/wh_test_crypto_affinity.h diff --git a/.gitignore b/.gitignore index 37f572851..c33a87daf 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ tools/testcertgen/*.der *.code-workspace .vscode compile_commands.json +.cache # Static analysis tools/static-analysis/reports/ diff --git a/docs/draft/crypto_affinity.md b/docs/draft/crypto_affinity.md new file mode 100644 index 000000000..0370219d2 --- /dev/null +++ b/docs/draft/crypto_affinity.md @@ -0,0 +1,92 @@ +# SetCryptoAffinity Client API + +The SetCryptoAffinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations. + +## Affinity Values + +```c +enum WH_CRYPTO_AFFINITY_ENUM { + WH_CRYPTO_AFFINITY_SW = 0, // Use software crypto (devId = INVALID_DEVID) + WH_CRYPTO_AFFINITY_HW = 1, // Use hardware crypto (devId = configured value) +}; +``` + +## Client API Functions + +### Blocking API (simplest) + +```c +int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, + int32_t* out_rc, uint32_t* out_affinity); +``` + +### Non-blocking (async) API + +```c +// Send request +int wh_Client_SetCryptoAffinityRequest(whClientContext* c, uint32_t affinity); + +// Receive response +int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity); +``` + +## Usage Example + +```c +int32_t server_rc; +uint32_t current_affinity; + +// Switch to software crypto +int rc = wh_Client_SetCryptoAffinity(client, + WH_CRYPTO_AFFINITY_SW, + &server_rc, + ¤t_affinity); + +if (rc == WH_ERROR_OK && server_rc == WH_ERROR_OK) { + // Server is now using software crypto + // current_affinity == WH_CRYPTO_AFFINITY_SW +} + +// Switch to hardware crypto +rc = wh_Client_SetCryptoAffinity(client, + WH_CRYPTO_AFFINITY_HW, + &server_rc, + ¤t_affinity); + +if (rc == WH_ERROR_OK) { + if (server_rc == WH_ERROR_OK) { + // Server is now using hardware crypto + } else if (server_rc == WH_ERROR_BADCONFIG) { + // HW crypto not available (server wasn't configured with a valid devId) + } +} +``` + +## Return Values + +| Value | Description | +|-------|-------------| +| `rc` (function return) | Transport/communication errors | +| `server_rc` (output parameter) | Server-side result | + +### Server Return Codes + +| Code | Description | +|------|-------------| +| `WH_ERROR_OK` | Affinity changed successfully | +| `WH_ERROR_BADCONFIG` | HW requested but no HW crypto configured | +| `WH_ERROR_BADARGS` | Invalid affinity value | +| `WH_ERROR_ABORTED` | Server crypto context is NULL | +| `WH_ERROR_NOTIMPL` | Affinity change not implemented (returned when `WOLF_CRYPTO_CB` is not defined and HW affinity is requested, or when `WOLFHSM_CFG_NO_CRYPTO` is defined) | + +## Server Behavior + +When affinity is set: + +| Affinity | Server Action | +|----------|---------------| +| `WH_CRYPTO_AFFINITY_SW` | `server->crypto->devId = INVALID_DEVID` (wolfCrypt uses software) | +| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->configDevId` (wolfCrypt uses registered crypto callback) | + +The `configDevId` is stored at server init from `config->devId`. diff --git a/src/wh_client.c b/src/wh_client.c index 132841b7b..732855f7f 100644 --- a/src/wh_client.c +++ b/src/wh_client.c @@ -410,6 +410,75 @@ int wh_Client_CommInfo(whClientContext* c, return rc; } +int wh_Client_SetCryptoAffinityRequest(whClientContext* c, uint32_t affinity) +{ + whMessageCommSetCryptoAffinityRequest msg = {0}; + + if (c == NULL) { + return WH_ERROR_BADARGS; + } + + msg.affinity = affinity; + + return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_COMM, + WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY, + sizeof(msg), &msg); +} + +int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity) +{ + int rc = 0; + whMessageCommSetCryptoAffinityResponse msg = {0}; + uint16_t resp_group = 0; + uint16_t resp_action = 0; + uint16_t resp_size = 0; + + if (c == NULL) { + return WH_ERROR_BADARGS; + } + + rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg); + if (rc == 0) { + /* Validate response */ + if ((resp_group != WH_MESSAGE_GROUP_COMM) || + (resp_action != WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY) || + (resp_size != sizeof(msg))) { + /* Invalid message */ + rc = WH_ERROR_ABORTED; + } + else { + /* Valid message */ + if (out_rc != NULL) { + *out_rc = msg.rc; + } + if (out_affinity != NULL) { + *out_affinity = msg.affinity; + } + } + } + return rc; +} + +int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, + int32_t* out_rc, uint32_t* out_affinity) +{ + int rc = 0; + if (c == NULL) { + return WH_ERROR_BADARGS; + } + do { + rc = wh_Client_SetCryptoAffinityRequest(c, affinity); + } while (rc == WH_ERROR_NOTREADY); + + if (rc == 0) { + do { + rc = wh_Client_SetCryptoAffinityResponse(c, out_rc, out_affinity); + } while (rc == WH_ERROR_NOTREADY); + } + return rc; +} + int wh_Client_CommCloseRequest(whClientContext* c) { diff --git a/src/wh_message_comm.c b/src/wh_message_comm.c index 1c42b03ae..0b7103494 100644 --- a/src/wh_message_comm.c +++ b/src/wh_message_comm.c @@ -83,4 +83,25 @@ int wh_MessageComm_TranslateInfoResponse(uint16_t magic, return 0; } +int wh_MessageComm_TranslateSetCryptoAffinityRequest( + uint16_t magic, const whMessageCommSetCryptoAffinityRequest* src, + whMessageCommSetCryptoAffinityRequest* dest) +{ + if ((src == NULL) || (dest == NULL)) { + return WH_ERROR_BADARGS; + } + WH_T32(magic, dest, src, affinity); + return 0; +} +int wh_MessageComm_TranslateSetCryptoAffinityResponse( + uint16_t magic, const whMessageCommSetCryptoAffinityResponse* src, + whMessageCommSetCryptoAffinityResponse* dest) +{ + if ((src == NULL) || (dest == NULL)) { + return WH_ERROR_BADARGS; + } + WH_T32(magic, dest, src, rc); + WH_T32(magic, dest, src, affinity); + return 0; +} diff --git a/src/wh_server.c b/src/wh_server.c index 5098a2da4..9bced9a66 100644 --- a/src/wh_server.c +++ b/src/wh_server.c @@ -81,8 +81,10 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config) if (server->crypto != NULL) { #if defined(WOLF_CRYPTO_CB) server->crypto->devId = config->devId; + server->crypto->configDevId = config->devId; #else server->crypto->devId = INVALID_DEVID; + server->crypto->configDevId = INVALID_DEVID; #endif } #ifdef WOLFHSM_CFG_SHE_EXTENSION @@ -247,6 +249,56 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, *out_resp_size = sizeof(resp); }; break; + case WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY: { + whMessageCommSetCryptoAffinityRequest req = {0}; + whMessageCommSetCryptoAffinityResponse resp = {0}; + + wh_MessageComm_TranslateSetCryptoAffinityRequest( + magic, (const whMessageCommSetCryptoAffinityRequest*)req_packet, + &req); + +#ifndef WOLFHSM_CFG_NO_CRYPTO + if (server->crypto == NULL) { + resp.rc = WH_ERROR_ABORTED; + resp.affinity = WH_CRYPTO_AFFINITY_SW; + } + else { + switch (req.affinity) { + case WH_CRYPTO_AFFINITY_SW: + server->crypto->devId = INVALID_DEVID; + resp.rc = WH_ERROR_OK; + break; + case WH_CRYPTO_AFFINITY_HW: +#ifdef WOLF_CRYPTO_CB + if (server->crypto->configDevId != INVALID_DEVID) { + server->crypto->devId = server->crypto->configDevId; + resp.rc = WH_ERROR_OK; + } + else { + resp.rc = WH_ERROR_BADCONFIG; + } + break; +#else + resp.rc = WH_ERROR_NOTIMPL; + break; +#endif + default: + resp.rc = WH_ERROR_BADARGS; + break; + } + resp.affinity = (server->crypto->devId == INVALID_DEVID) + ? WH_CRYPTO_AFFINITY_SW + : WH_CRYPTO_AFFINITY_HW; + } +#else + resp.rc = WH_ERROR_NOTIMPL; + resp.affinity = WH_CRYPTO_AFFINITY_SW; +#endif + + wh_MessageComm_TranslateSetCryptoAffinityResponse( + magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet); + *out_resp_size = sizeof(resp); + }; break; case WH_MESSAGE_COMM_ACTION_CLOSE: { diff --git a/test/wh_test.c b/test/wh_test.c index 3fd9a1bb5..c97a36897 100644 --- a/test/wh_test.c +++ b/test/wh_test.c @@ -42,6 +42,7 @@ #include "wh_test_log.h" #include "wh_test_lock.h" #include "wh_test_posix_threadsafe_stress.h" +#include "wh_test_crypto_affinity.h" #if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) #include "wh_test_cert.h" @@ -93,6 +94,10 @@ int whTest_Unit(void) /* Crypto Tests */ WH_TEST_ASSERT(0 == whTest_Crypto()); +#ifdef WOLF_CRYPTO_CB + WH_TEST_ASSERT(0 == whTest_CryptoAffinity()); +#endif + #if defined(WOLFHSM_CFG_SERVER_IMG_MGR) && !defined(WOLFHSM_CFG_NO_CRYPTO) /* Image Manager Tests */ WH_TEST_ASSERT(0 == whTest_ServerImgMgr(WH_NVM_TEST_BACKEND_FLASH)); diff --git a/test/wh_test_check_struct_padding.c b/test/wh_test_check_struct_padding.c index 530b3e7ef..5db7ca0d8 100644 --- a/test/wh_test_check_struct_padding.c +++ b/test/wh_test_check_struct_padding.c @@ -27,10 +27,12 @@ #include "wolfhsm/wh_message_comm.h" -whMessageComm_ErrorResponse whMessageComm_ErrorResponse_test; -whMessageCommInitRequest whMessageCommInitRequest_test; -whMessageCommInitResponse whMessageCommInitResponse_test; -whMessageCommInfoResponse whMessageCommInfoResponse_test; +whMessageComm_ErrorResponse whMessageComm_ErrorResponse_test; +whMessageCommInitRequest whMessageCommInitRequest_test; +whMessageCommInitResponse whMessageCommInitResponse_test; +whMessageCommInfoResponse whMessageCommInfoResponse_test; +whMessageCommSetCryptoAffinityRequest whMessageCommSetCryptoAffinityRequest_test; +whMessageCommSetCryptoAffinityResponse whMessageCommSetCryptoAffinityResponse_test; #include "wolfhsm/wh_message_customcb.h" whMessageCustomCb_Request whMessageCustomCb_Request_test; diff --git a/test/wh_test_crypto_affinity.c b/test/wh_test_crypto_affinity.c new file mode 100644 index 000000000..2e09640b3 --- /dev/null +++ b/test/wh_test_crypto_affinity.c @@ -0,0 +1,482 @@ +/* + * Copyright (C) 2024 wolfSSL Inc. + * + * This file is part of wolfHSM. + * + * wolfHSM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfHSM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wolfHSM. If not, see . + */ +/* + * test/wh_test_crypto_affinity.c + * + * Tests for the SetCryptoAffinity API, verifying that the server correctly + * switches between hardware and software crypto implementations. + */ + +#include "wolfhsm/wh_settings.h" + +/* Only compile if we have crypto, client, server, and crypto callbacks */ +#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(WOLF_CRYPTO_CB) + +#include +#include +#include + +#include "wolfssl/wolfcrypt/settings.h" +#include "wolfssl/wolfcrypt/types.h" +#include "wolfssl/wolfcrypt/cryptocb.h" +#include "wolfssl/wolfcrypt/random.h" + +#include "wolfhsm/wh_error.h" +#include "wolfhsm/wh_comm.h" +#include "wolfhsm/wh_transport_mem.h" +#include "wolfhsm/wh_message_comm.h" +#include "wolfhsm/wh_client.h" +#include "wolfhsm/wh_server.h" +#include "wolfhsm/wh_nvm.h" +#include "wolfhsm/wh_nvm_flash.h" +#include "wolfhsm/wh_flash_ramsim.h" + +#include "wh_test_common.h" +#include "wh_test_crypto_affinity.h" + +#define BUFFER_SIZE 4096 +#define FLASH_RAM_SIZE (1024 * 1024) +#define FLASH_SECTOR_SIZE (128 * 1024) +#define FLASH_PAGE_SIZE (8) +#define TEST_DEV_ID 0xCA + +/* Counter to track how many times the crypto callback is invoked */ +static int cryptoCbInvokeCount = 0; + +static whServerContext* cryptoAffinityTestServerCtx = NULL; + +/* Test crypto callback that just increments a counter and returns + * CRYPTOCB_UNAVAILABLE to fall back to software */ +static int _testCryptoCb(int devId, wc_CryptoInfo* info, void* ctx) +{ + (void)devId; + (void)info; + (void)ctx; + + cryptoCbInvokeCount++; + + /* Return CRYPTOCB_UNAVAILABLE to indicate we don't handle this operation + * and wolfCrypt should fall back to software implementation */ + return CRYPTOCB_UNAVAILABLE; +} + +static int _cryptoAffinityTestConnectCb(void* context, + whCommConnected connected) +{ + (void)context; + + if (cryptoAffinityTestServerCtx == NULL) { + WH_ERROR_PRINT("Client connect callback server context is NULL\n"); + WH_TEST_ASSERT_RETURN(0); + } + + /* Set server connect flag. In a "real" system, this should signal the + * server via out-of-band mechanism. The server app is responsible for + * receiving this signal and calling wh_Server_SetConnected() */ + return wh_Server_SetConnected(cryptoAffinityTestServerCtx, connected); +} + + +static int whTest_CryptoAffinityWithCb(void) +{ + int rc = 0; + int32_t server_rc = 0; + uint32_t affinity = 0; + + /* Transport memory configuration */ + uint8_t req[BUFFER_SIZE] = {0}; + uint8_t resp[BUFFER_SIZE] = {0}; + whTransportMemConfig tmcf[1] = {{ + .req = (whTransportMemCsr*)req, + .req_size = sizeof(req), + .resp = (whTransportMemCsr*)resp, + .resp_size = sizeof(resp), + }}; + + /* Client configuration/contexts */ + whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB}; + whTransportMemClientContext tmcc[1] = {0}; + whCommClientConfig cc_conf[1] = {{ + .transport_cb = tccb, + .transport_context = (void*)tmcc, + .transport_config = (void*)tmcf, + .client_id = 1, + .connect_cb = _cryptoAffinityTestConnectCb, + }}; + whClientConfig c_conf[1] = {{ + .comm = cc_conf, + }}; + whClientContext client[1] = {0}; + + /* Server configuration/contexts */ + whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB}; + whTransportMemServerContext tmsc[1] = {0}; + whCommServerConfig cs_conf[1] = {{ + .transport_cb = tscb, + .transport_context = (void*)tmsc, + .transport_config = (void*)tmcf, + .server_id = 123, + }}; + + /* Flash/NVM configuration */ + uint8_t flash_memory[FLASH_RAM_SIZE] = {0}; + whFlashRamsimCtx fc[1] = {0}; + whFlashRamsimCfg fc_conf[1] = {{ + .size = FLASH_RAM_SIZE, + .sectorSize = FLASH_SECTOR_SIZE, + .pageSize = FLASH_PAGE_SIZE, + .erasedByte = ~(uint8_t)0, + .memory = flash_memory, + }}; + const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; + + whNvmFlashContext nfc[1] = {0}; + whNvmFlashConfig nf_conf[1] = {{ + .cb = fcb, + .context = fc, + .config = fc_conf, + }}; + + + whNvmCb nfcb[1] = {WH_NVM_FLASH_CB}; + whNvmConfig n_conf[1] = {{ + .cb = nfcb, + .context = nfc, + .config = nf_conf, + }}; + whNvmContext nvm[1] = {0}; + + /* Crypto context - configure with our test device ID */ + whServerCryptoContext crypto[1] = {{ + .devId = TEST_DEV_ID, + }}; + + whServerConfig s_conf[1] = {{ + .comm_config = cs_conf, + .nvm = nvm, + .crypto = crypto, + .devId = TEST_DEV_ID, + }}; + whServerContext server[1] = {0}; + + cryptoAffinityTestServerCtx = server; + + WH_TEST_PRINT(" whTest_CryptoAffinityWithCb..."); + + /* Initialize wolfCrypt and register our test crypto callback */ + WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); + WH_TEST_RETURN_ON_FAIL( + wc_CryptoCb_RegisterDevice(TEST_DEV_ID, _testCryptoCb, NULL)); + + /* Initialize NVM */ + WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); + + /* Initialize RNG */ + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); + + /* Initialize server and client */ + WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, s_conf)); + WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, c_conf)); + + /* Check that the server side is ready to recv */ + WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == + wh_Server_HandleRequestMessage(server)); + + /* Send comm init */ + WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitResponse(client, NULL, NULL)); + + /* Verify initial state - should be HW since we configured with valid devId + */ + WH_TEST_ASSERT_RETURN(server->crypto->devId == TEST_DEV_ID); + WH_TEST_ASSERT_RETURN(server->crypto->configDevId == TEST_DEV_ID); + + /* Test 1: Set SW affinity */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); + WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + + /* Test 2: Set HW affinity - should succeed since we have valid configDevId + */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_HW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_HW); + WH_TEST_ASSERT_RETURN(server->crypto->devId == TEST_DEV_ID); + + /* Test 3: Invalid affinity value - should return BADARGS */ + WH_TEST_RETURN_ON_FAIL(wh_Client_SetCryptoAffinityRequest(client, 0xFF)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADARGS); + + /* Test 4: Verify crypto callback is invoked when HW affinity is set */ + cryptoCbInvokeCount = 0; + + /* Set HW affinity */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_HW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + + /* Do a crypto operation using the server's devId - this should hit our + * callback */ + { + WC_RNG testRng[1]; + uint8_t randomBytes[16]; + rc = wc_InitRng_ex(testRng, NULL, server->crypto->devId); + if (rc == 0) { + (void)wc_RNG_GenerateBlock(testRng, randomBytes, + sizeof(randomBytes)); + wc_FreeRng(testRng); + } + } + + /* Crypto callback should have been invoked at least once */ + WH_TEST_ASSERT_RETURN(cryptoCbInvokeCount > 0); + + /* Test 5: Verify crypto callback is NOT invoked when SW affinity is set */ + cryptoCbInvokeCount = 0; + + /* Set SW affinity */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + + /* Do a crypto operation using the server's devId (now INVALID_DEVID) */ + { + WC_RNG testRng[1]; + uint8_t randomBytes[16]; + rc = wc_InitRng_ex(testRng, NULL, server->crypto->devId); + if (rc == 0) { + (void)wc_RNG_GenerateBlock(testRng, randomBytes, + sizeof(randomBytes)); + wc_FreeRng(testRng); + } + } + + /* Crypto callback should NOT have been invoked */ + WH_TEST_ASSERT_RETURN(cryptoCbInvokeCount == 0); + + /* Cleanup */ + WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseResponse(client)); + + WH_TEST_RETURN_ON_FAIL(wh_Server_Cleanup(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client)); + + wc_FreeRng(crypto->rng); + wh_Nvm_Cleanup(nvm); + wc_CryptoCb_UnRegisterDevice(TEST_DEV_ID); + wolfCrypt_Cleanup(); + + WH_TEST_PRINT("PASS\n"); + return WH_ERROR_OK; +} + + +static int whTest_CryptoAffinityNoCb(void) +{ + int rc = 0; + int32_t server_rc = 0; + uint32_t affinity = 0; + + /* Transport memory configuration */ + uint8_t req[BUFFER_SIZE] = {0}; + uint8_t resp[BUFFER_SIZE] = {0}; + whTransportMemConfig tmcf[1] = {{ + .req = (whTransportMemCsr*)req, + .req_size = sizeof(req), + .resp = (whTransportMemCsr*)resp, + .resp_size = sizeof(resp), + }}; + + /* Client configuration/contexts */ + whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB}; + whTransportMemClientContext tmcc[1] = {0}; + whCommClientConfig cc_conf[1] = {{ + .transport_cb = tccb, + .transport_context = (void*)tmcc, + .transport_config = (void*)tmcf, + .client_id = 1, + .connect_cb = _cryptoAffinityTestConnectCb, + }}; + whClientConfig c_conf[1] = {{ + .comm = cc_conf, + }}; + whClientContext client[1] = {0}; + + /* Server configuration/contexts */ + whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB}; + whTransportMemServerContext tmsc[1] = {0}; + whCommServerConfig cs_conf[1] = {{ + .transport_cb = tscb, + .transport_context = (void*)tmsc, + .transport_config = (void*)tmcf, + .server_id = 123, + }}; + + /* Flash/NVM configuration */ + uint8_t flash_memory[FLASH_RAM_SIZE] = {0}; + whFlashRamsimCtx fc[1] = {0}; + whFlashRamsimCfg fc_conf[1] = {{ + .size = FLASH_RAM_SIZE, + .sectorSize = FLASH_SECTOR_SIZE, + .pageSize = FLASH_PAGE_SIZE, + .erasedByte = ~(uint8_t)0, + .memory = flash_memory, + }}; + const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; + + whNvmFlashContext nfc[1] = {0}; + whNvmFlashConfig nf_conf[1] = {{ + .cb = fcb, + .context = fc, + .config = fc_conf, + }}; + + + whNvmCb nfcb[1] = {WH_NVM_FLASH_CB}; + whNvmConfig n_conf[1] = {{ + .cb = nfcb, + .context = nfc, + .config = nf_conf, + }}; + whNvmContext nvm[1] = {0}; + + /* Crypto context - configure with INVALID_DEVID (no HW crypto) */ + whServerCryptoContext crypto[1] = {{ + .devId = INVALID_DEVID, + }}; + + whServerConfig s_conf[1] = {{ + .comm_config = cs_conf, + .nvm = nvm, + .crypto = crypto, + .devId = INVALID_DEVID, + }}; + whServerContext server[1] = {0}; + + cryptoAffinityTestServerCtx = server; + + WH_TEST_PRINT(" whTest_CryptoAffinityNoCb..."); + + /* Initialize wolfCrypt */ + WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); + + /* Initialize NVM */ + WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); + + /* Initialize RNG */ + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); + + /* Initialize server and client */ + WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, s_conf)); + WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, c_conf)); + + /* Check that the server side is ready to recv */ + WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == + wh_Server_HandleRequestMessage(server)); + + /* Send comm init */ + WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitResponse(client, NULL, NULL)); + + /* Verify initial state - should be SW since we configured with INVALID_DEVID + */ + WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->crypto->configDevId == INVALID_DEVID); + + /* Test 1: Set SW affinity - should succeed */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); + WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + + /* Test 2: Set HW affinity - should fail with BADCONFIG since no HW + * configured */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_HW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADCONFIG); + /* Affinity should remain SW after failed HW request */ + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); + WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + + /* Test 3: Verify SW affinity still works after failed HW request */ + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); + + /* Cleanup */ + WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseResponse(client)); + + WH_TEST_RETURN_ON_FAIL(wh_Server_Cleanup(server)); + WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client)); + + wc_FreeRng(crypto->rng); + wh_Nvm_Cleanup(nvm); + wolfCrypt_Cleanup(); + + (void)rc; + + WH_TEST_PRINT("PASS\n"); + return WH_ERROR_OK; +} + + +int whTest_CryptoAffinity(void) +{ + WH_TEST_PRINT("Testing Crypto Affinity...\n"); + WH_TEST_RETURN_ON_FAIL(whTest_CryptoAffinityWithCb()); + WH_TEST_RETURN_ON_FAIL(whTest_CryptoAffinityNoCb()); + + return WH_ERROR_OK; +} + +#endif /* !WOLFHSM_CFG_NO_CRYPTO && WOLF_CRYPTO_CB */ diff --git a/test/wh_test_crypto_affinity.h b/test/wh_test_crypto_affinity.h new file mode 100644 index 000000000..8254c50f2 --- /dev/null +++ b/test/wh_test_crypto_affinity.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 wolfSSL Inc. + * + * This file is part of wolfHSM. + * + * wolfHSM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfHSM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wolfHSM. If not, see . + */ +#ifndef WH_TEST_CRYPTO_AFFINITY_H_ +#define WH_TEST_CRYPTO_AFFINITY_H_ + +int whTest_CryptoAffinity(void); + +#endif /* WH_TEST_CRYPTO_AFFINITY_H_ */ diff --git a/wolfhsm/wh_client.h b/wolfhsm/wh_client.h index 2990ed297..df80d0312 100644 --- a/wolfhsm/wh_client.h +++ b/wolfhsm/wh_client.h @@ -48,6 +48,7 @@ /* Component includes */ #include "wolfhsm/wh_comm.h" +#include "wolfhsm/wh_message_comm.h" #include "wolfhsm/wh_message_customcb.h" #ifdef WOLFHSM_CFG_DMA #include "wolfhsm/wh_dma.h" @@ -336,6 +337,43 @@ int wh_Client_CommInfo(whClientContext* c, uint32_t *out_lifecycle_state, uint32_t *out_nvm_state); +/** + * @brief Sends a crypto affinity request to the server. + * + * This function prepares and sends a request to select software or hardware + * crypto on the server. + * + * @param[in] c Pointer to the client context. + * @param[in] affinity Requested crypto affinity. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_SetCryptoAffinityRequest(whClientContext* c, uint32_t affinity); + +/** + * @brief Receives a crypto affinity response from the server. + * + * This function waits for and processes the response message from the server. + * + * @param[in] c Pointer to the client context. + * @param[out] out_rc Pointer to store the server result code. + * @param[out] out_affinity Pointer to store the active crypto affinity. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity); + +/** + * @brief Sets the crypto affinity with a blocking call. + * + * @param[in] c Pointer to the client context. + * @param[in] affinity Requested crypto affinity. + * @param[out] out_rc Pointer to store the server result code. + * @param[out] out_affinity Pointer to store the active crypto affinity. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, + int32_t* out_rc, uint32_t* out_affinity); + /** * @brief Sends a communication close request to the server. * diff --git a/wolfhsm/wh_error.h b/wolfhsm/wh_error.h index 5ce75cdde..c765c2313 100644 --- a/wolfhsm/wh_error.h +++ b/wolfhsm/wh_error.h @@ -45,6 +45,7 @@ enum WH_ERROR_ENUM { compile-time configuration */ WH_ERROR_USAGE = -2009, /* Operation not permitted based on object/key usage flags */ + WH_ERROR_BADCONFIG = -2010, /* Failed due to runtime configuration */ /* NVM and keystore specific status returns */ WH_ERROR_LOCKED = -2100, /* Unlock and retry if necessary */ diff --git a/wolfhsm/wh_message_comm.h b/wolfhsm/wh_message_comm.h index 711f2998e..f645f44a8 100644 --- a/wolfhsm/wh_message_comm.h +++ b/wolfhsm/wh_message_comm.h @@ -35,12 +35,18 @@ /* Comm component message kinds */ enum WH_MESSAGE_COMM_ACTION_ENUM { - WH_MESSAGE_COMM_ACTION_NONE = 0x00, - WH_MESSAGE_COMM_ACTION_INIT = 0x01, - WH_MESSAGE_COMM_ACTION_KEEPALIVE = 0x02, - WH_MESSAGE_COMM_ACTION_CLOSE = 0x03, - WH_MESSAGE_COMM_ACTION_INFO = 0x04, - WH_MESSAGE_COMM_ACTION_ECHO = 0x05, + WH_MESSAGE_COMM_ACTION_NONE = 0x00, + WH_MESSAGE_COMM_ACTION_INIT = 0x01, + WH_MESSAGE_COMM_ACTION_KEEPALIVE = 0x02, + WH_MESSAGE_COMM_ACTION_CLOSE = 0x03, + WH_MESSAGE_COMM_ACTION_INFO = 0x04, + WH_MESSAGE_COMM_ACTION_ECHO = 0x05, + WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY = 0x06, +}; + +enum WH_CRYPTO_AFFINITY_ENUM { + WH_CRYPTO_AFFINITY_SW = 0, + WH_CRYPTO_AFFINITY_HW = 1, }; /* Info request/response data sizes*/ @@ -100,5 +106,22 @@ int wh_MessageComm_TranslateInfoResponse(uint16_t magic, const whMessageCommInfoResponse* src, whMessageCommInfoResponse* dest); +typedef struct { + uint32_t affinity; +} whMessageCommSetCryptoAffinityRequest; + +int wh_MessageComm_TranslateSetCryptoAffinityRequest( + uint16_t magic, const whMessageCommSetCryptoAffinityRequest* src, + whMessageCommSetCryptoAffinityRequest* dest); + +typedef struct { + int32_t rc; + uint32_t affinity; +} whMessageCommSetCryptoAffinityResponse; + +int wh_MessageComm_TranslateSetCryptoAffinityResponse( + uint16_t magic, const whMessageCommSetCryptoAffinityResponse* src, + whMessageCommSetCryptoAffinityResponse* dest); + #endif /* !WOLFHSM_WH_MESSAGE_COMM_H_ */ diff --git a/wolfhsm/wh_server.h b/wolfhsm/wh_server.h index 6cf08d3d3..23c63f570 100644 --- a/wolfhsm/wh_server.h +++ b/wolfhsm/wh_server.h @@ -67,6 +67,7 @@ typedef struct whServerContext_t whServerContext; typedef struct whServerCryptoContext { int devId; + int configDevId; #ifndef WC_NO_RNG WC_RNG rng[1]; #endif From c1e2cbdb555f1999d4ac01f135c8ceeec039848b Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 10 Feb 2026 11:55:32 -0500 Subject: [PATCH 2/4] Address comments --- docs/draft/crypto_affinity.md | 60 +++++++++++++++++---- src/wh_client.c | 69 +++++++++++++++++++++++- src/wh_message_comm.c | 12 +++++ src/wh_server.c | 83 +++++++++++++++++++---------- test/wh_test_check_struct_padding.c | 1 + test/wh_test_crypto_affinity.c | 36 ++++++++++--- wolfhsm/wh_client.h | 35 ++++++++++++ wolfhsm/wh_common.h | 5 ++ wolfhsm/wh_error.h | 1 - wolfhsm/wh_message_comm.h | 15 ++++-- wolfhsm/wh_server.h | 2 +- 11 files changed, 264 insertions(+), 55 deletions(-) diff --git a/docs/draft/crypto_affinity.md b/docs/draft/crypto_affinity.md index 0370219d2..91afe9874 100644 --- a/docs/draft/crypto_affinity.md +++ b/docs/draft/crypto_affinity.md @@ -1,17 +1,17 @@ -# SetCryptoAffinity Client API +# Crypto Affinity Client API -The SetCryptoAffinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations. +The crypto affinity feature allows a client to control and query whether the server uses **software** or **hardware** cryptographic implementations. ## Affinity Values ```c enum WH_CRYPTO_AFFINITY_ENUM { WH_CRYPTO_AFFINITY_SW = 0, // Use software crypto (devId = INVALID_DEVID) - WH_CRYPTO_AFFINITY_HW = 1, // Use hardware crypto (devId = configured value) + WH_CRYPTO_AFFINITY_HW = 1, // Attmept to use hardware crypto (devId = configured value) }; ``` -## Client API Functions +## SetCryptoAffinity ### Blocking API (simplest) @@ -31,14 +31,43 @@ int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, uint32_t* out_affinity); ``` +## GetCryptoAffinity + +### Blocking API (simplest) + +```c +int wh_Client_GetCryptoAffinity(whClientContext* c, + int32_t* out_rc, uint32_t* out_affinity); +``` + +### Non-blocking (async) API + +```c +// Send request +int wh_Client_GetCryptoAffinityRequest(whClientContext* c); + +// Receive response +int wh_Client_GetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity); +``` + ## Usage Example ```c int32_t server_rc; uint32_t current_affinity; +// Query current crypto affinity +int rc = wh_Client_GetCryptoAffinity(client, + &server_rc, + ¤t_affinity); + +if (rc == WH_ERROR_OK && server_rc == WH_ERROR_OK) { + // current_affinity contains the active affinity +} + // Switch to software crypto -int rc = wh_Client_SetCryptoAffinity(client, +rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW, &server_rc, ¤t_affinity); @@ -57,7 +86,7 @@ rc = wh_Client_SetCryptoAffinity(client, if (rc == WH_ERROR_OK) { if (server_rc == WH_ERROR_OK) { // Server is now using hardware crypto - } else if (server_rc == WH_ERROR_BADCONFIG) { + } else if (server_rc == WH_ERROR_NOTIMPL) { // HW crypto not available (server wasn't configured with a valid devId) } } @@ -70,15 +99,22 @@ if (rc == WH_ERROR_OK) { | `rc` (function return) | Transport/communication errors | | `server_rc` (output parameter) | Server-side result | -### Server Return Codes +### Server Return Codes (SetCryptoAffinity) | Code | Description | |------|-------------| | `WH_ERROR_OK` | Affinity changed successfully | -| `WH_ERROR_BADCONFIG` | HW requested but no HW crypto configured | +| `WH_ERROR_NOTIMPL` | HW requested but no HW crypto configured, `WOLF_CRYPTO_CB` is not defined, or `WOLFHSM_CFG_NO_CRYPTO` is defined | | `WH_ERROR_BADARGS` | Invalid affinity value | | `WH_ERROR_ABORTED` | Server crypto context is NULL | -| `WH_ERROR_NOTIMPL` | Affinity change not implemented (returned when `WOLF_CRYPTO_CB` is not defined and HW affinity is requested, or when `WOLFHSM_CFG_NO_CRYPTO` is defined) | + +### Server Return Codes (GetCryptoAffinity) + +| Code | Description | +|------|-------------| +| `WH_ERROR_OK` | Affinity queried successfully | +| `WH_ERROR_ABORTED` | Server crypto context is NULL | +| `WH_ERROR_NOTIMPL` | Not implemented (returned when `WOLFHSM_CFG_NO_CRYPTO` is defined) | ## Server Behavior @@ -87,6 +123,8 @@ When affinity is set: | Affinity | Server Action | |----------|---------------| | `WH_CRYPTO_AFFINITY_SW` | `server->crypto->devId = INVALID_DEVID` (wolfCrypt uses software) | -| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->configDevId` (wolfCrypt uses registered crypto callback) | +| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->defaultDevId` (wolfCrypt uses registered crypto callback) | + +When affinity is queried (Get), the server reads the current `devId` and returns the corresponding affinity value without modifying any state. -The `configDevId` is stored at server init from `config->devId`. +The `defaultDevId` is stored at server init from `config->devId`. diff --git a/src/wh_client.c b/src/wh_client.c index 732855f7f..8093300da 100644 --- a/src/wh_client.c +++ b/src/wh_client.c @@ -439,7 +439,7 @@ int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg); - if (rc == 0) { + if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_COMM) || (resp_action != WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY) || @@ -471,7 +471,7 @@ int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, rc = wh_Client_SetCryptoAffinityRequest(c, affinity); } while (rc == WH_ERROR_NOTREADY); - if (rc == 0) { + if (rc == WH_ERROR_OK) { do { rc = wh_Client_SetCryptoAffinityResponse(c, out_rc, out_affinity); } while (rc == WH_ERROR_NOTREADY); @@ -479,6 +479,71 @@ int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, return rc; } +int wh_Client_GetCryptoAffinityRequest(whClientContext* c) +{ + if (c == NULL) { + return WH_ERROR_BADARGS; + } + + return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_COMM, + WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY, + 0, NULL); +} + +int wh_Client_GetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity) +{ + int rc = 0; + whMessageCommGetCryptoAffinityResponse msg = {0}; + uint16_t resp_group = 0; + uint16_t resp_action = 0; + uint16_t resp_size = 0; + + if (c == NULL) { + return WH_ERROR_BADARGS; + } + + rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg); + if (rc == 0) { + /* Validate response */ + if ((resp_group != WH_MESSAGE_GROUP_COMM) || + (resp_action != WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY) || + (resp_size != sizeof(msg))) { + /* Invalid message */ + rc = WH_ERROR_ABORTED; + } + else { + /* Valid message */ + if (out_rc != NULL) { + *out_rc = msg.rc; + } + if (out_affinity != NULL) { + *out_affinity = msg.affinity; + } + } + } + return rc; +} + +int wh_Client_GetCryptoAffinity(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity) +{ + int rc = 0; + if (c == NULL) { + return WH_ERROR_BADARGS; + } + do { + rc = wh_Client_GetCryptoAffinityRequest(c); + } while (rc == WH_ERROR_NOTREADY); + + if (rc == 0) { + do { + rc = wh_Client_GetCryptoAffinityResponse(c, out_rc, out_affinity); + } while (rc == WH_ERROR_NOTREADY); + } + return rc; +} + int wh_Client_CommCloseRequest(whClientContext* c) { diff --git a/src/wh_message_comm.c b/src/wh_message_comm.c index 0b7103494..a8062d7ee 100644 --- a/src/wh_message_comm.c +++ b/src/wh_message_comm.c @@ -105,3 +105,15 @@ int wh_MessageComm_TranslateSetCryptoAffinityResponse( WH_T32(magic, dest, src, affinity); return 0; } + +int wh_MessageComm_TranslateGetCryptoAffinityResponse( + uint16_t magic, const whMessageCommGetCryptoAffinityResponse* src, + whMessageCommGetCryptoAffinityResponse* dest) +{ + if ((src == NULL) || (dest == NULL)) { + return WH_ERROR_BADARGS; + } + WH_T32(magic, dest, src, rc); + WH_T32(magic, dest, src, affinity); + return 0; +} diff --git a/src/wh_server.c b/src/wh_server.c index 9bced9a66..ed8522888 100644 --- a/src/wh_server.c +++ b/src/wh_server.c @@ -79,13 +79,8 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config) #ifndef WOLFHSM_CFG_NO_CRYPTO server->crypto = config->crypto; if (server->crypto != NULL) { -#if defined(WOLF_CRYPTO_CB) server->crypto->devId = config->devId; - server->crypto->configDevId = config->devId; -#else - server->crypto->devId = INVALID_DEVID; - server->crypto->configDevId = INVALID_DEVID; -#endif + server->crypto->defaultDevId = config->devId; } #ifdef WOLFHSM_CFG_SHE_EXTENSION server->she = config->she; @@ -253,7 +248,16 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, whMessageCommSetCryptoAffinityRequest req = {0}; whMessageCommSetCryptoAffinityResponse resp = {0}; - wh_MessageComm_TranslateSetCryptoAffinityRequest( + if (req_size != sizeof(whMessageCommSetCryptoAffinityRequest)) { + resp.rc = WH_ERROR_ABORTED; + + (void)wh_MessageComm_TranslateSetCryptoAffinityResponse( + magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet); + *out_resp_size = sizeof(resp); + break; + } + + (void)wh_MessageComm_TranslateSetCryptoAffinityRequest( magic, (const whMessageCommSetCryptoAffinityRequest*)req_packet, &req); @@ -263,28 +267,27 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, resp.affinity = WH_CRYPTO_AFFINITY_SW; } else { - switch (req.affinity) { - case WH_CRYPTO_AFFINITY_SW: - server->crypto->devId = INVALID_DEVID; + if (req.affinity == WH_CRYPTO_AFFINITY_SW) { + server->crypto->devId = INVALID_DEVID; + resp.rc = WH_ERROR_OK; + } + else if (req.affinity == WH_CRYPTO_AFFINITY_HW) { + /* If the devId the server was configured with is valid then + * switch back to it. The devId is used to call the appropriate + * callback to utilize HW crypto */ + if (server->crypto->defaultDevId != INVALID_DEVID) { + server->crypto->devId = server->crypto->defaultDevId; resp.rc = WH_ERROR_OK; - break; - case WH_CRYPTO_AFFINITY_HW: -#ifdef WOLF_CRYPTO_CB - if (server->crypto->configDevId != INVALID_DEVID) { - server->crypto->devId = server->crypto->configDevId; - resp.rc = WH_ERROR_OK; - } - else { - resp.rc = WH_ERROR_BADCONFIG; - } - break; -#else + } + /* If the server was not configured with a valid devId + * Then we are unable to use HW crypto. Return error back + * back to the client */ + else { resp.rc = WH_ERROR_NOTIMPL; - break; -#endif - default: - resp.rc = WH_ERROR_BADARGS; - break; + } + } + else { + resp.rc = WH_ERROR_BADARGS; } resp.affinity = (server->crypto->devId == INVALID_DEVID) ? WH_CRYPTO_AFFINITY_SW @@ -295,11 +298,35 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, resp.affinity = WH_CRYPTO_AFFINITY_SW; #endif - wh_MessageComm_TranslateSetCryptoAffinityResponse( + (void)wh_MessageComm_TranslateSetCryptoAffinityResponse( magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet); *out_resp_size = sizeof(resp); }; break; + case WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY: { + whMessageCommGetCryptoAffinityResponse resp = {0}; + +#ifndef WOLFHSM_CFG_NO_CRYPTO + if (server->crypto == NULL) { + resp.rc = WH_ERROR_ABORTED; + resp.affinity = WH_CRYPTO_AFFINITY_SW; + } + else { + resp.rc = WH_ERROR_OK; + resp.affinity = (server->crypto->devId == INVALID_DEVID) + ? WH_CRYPTO_AFFINITY_SW + : WH_CRYPTO_AFFINITY_HW; + } +#else + resp.rc = WH_ERROR_NOTIMPL; + resp.affinity = WH_CRYPTO_AFFINITY_SW; +#endif + + wh_MessageComm_TranslateGetCryptoAffinityResponse( + magic, &resp, (whMessageCommGetCryptoAffinityResponse*)resp_packet); + *out_resp_size = sizeof(resp); + }; break; + case WH_MESSAGE_COMM_ACTION_CLOSE: { /* No message */ diff --git a/test/wh_test_check_struct_padding.c b/test/wh_test_check_struct_padding.c index 5db7ca0d8..65c584c4c 100644 --- a/test/wh_test_check_struct_padding.c +++ b/test/wh_test_check_struct_padding.c @@ -33,6 +33,7 @@ whMessageCommInitResponse whMessageCommInitResponse_test; whMessageCommInfoResponse whMessageCommInfoResponse_test; whMessageCommSetCryptoAffinityRequest whMessageCommSetCryptoAffinityRequest_test; whMessageCommSetCryptoAffinityResponse whMessageCommSetCryptoAffinityResponse_test; +whMessageCommGetCryptoAffinityResponse whMessageCommGetCryptoAffinityResponse_test; #include "wolfhsm/wh_message_customcb.h" whMessageCustomCb_Request whMessageCustomCb_Request_test; diff --git a/test/wh_test_crypto_affinity.c b/test/wh_test_crypto_affinity.c index 2e09640b3..5f7e61d48 100644 --- a/test/wh_test_crypto_affinity.c +++ b/test/wh_test_crypto_affinity.c @@ -206,7 +206,15 @@ static int whTest_CryptoAffinityWithCb(void) /* Verify initial state - should be HW since we configured with valid devId */ WH_TEST_ASSERT_RETURN(server->crypto->devId == TEST_DEV_ID); - WH_TEST_ASSERT_RETURN(server->crypto->configDevId == TEST_DEV_ID); + WH_TEST_ASSERT_RETURN(server->crypto->defaultDevId == TEST_DEV_ID); + + /* Test 1a: Get initial affinity - should be HW */ + WH_TEST_RETURN_ON_FAIL(wh_Client_GetCryptoAffinityRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_GetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_HW); /* Test 1: Set SW affinity */ WH_TEST_RETURN_ON_FAIL( @@ -218,7 +226,15 @@ static int whTest_CryptoAffinityWithCb(void) WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); - /* Test 2: Set HW affinity - should succeed since we have valid configDevId + /* Test 1b: Get affinity after setting SW - should be SW */ + WH_TEST_RETURN_ON_FAIL(wh_Client_GetCryptoAffinityRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_GetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); + + /* Test 2: Set HW affinity - should succeed since we have valid defaultDevId */ WH_TEST_RETURN_ON_FAIL( wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_HW)); @@ -309,7 +325,6 @@ static int whTest_CryptoAffinityWithCb(void) static int whTest_CryptoAffinityNoCb(void) { - int rc = 0; int32_t server_rc = 0; uint32_t affinity = 0; @@ -418,7 +433,16 @@ static int whTest_CryptoAffinityNoCb(void) /* Verify initial state - should be SW since we configured with INVALID_DEVID */ WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); - WH_TEST_ASSERT_RETURN(server->crypto->configDevId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->crypto->defaultDevId == INVALID_DEVID); + + /* Test 0: Get initial affinity - should be SW since configured with + * INVALID_DEVID */ + WH_TEST_RETURN_ON_FAIL(wh_Client_GetCryptoAffinityRequest(client)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_RETURN_ON_FAIL( + wh_Client_GetCryptoAffinityResponse(client, &server_rc, &affinity)); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); + WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); /* Test 1: Set SW affinity - should succeed */ WH_TEST_RETURN_ON_FAIL( @@ -437,7 +461,7 @@ static int whTest_CryptoAffinityNoCb(void) WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); WH_TEST_RETURN_ON_FAIL( wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); - WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADCONFIG); + WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_NOTIMPL); /* Affinity should remain SW after failed HW request */ WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); @@ -463,8 +487,6 @@ static int whTest_CryptoAffinityNoCb(void) wh_Nvm_Cleanup(nvm); wolfCrypt_Cleanup(); - (void)rc; - WH_TEST_PRINT("PASS\n"); return WH_ERROR_OK; } diff --git a/wolfhsm/wh_client.h b/wolfhsm/wh_client.h index df80d0312..a3682cf81 100644 --- a/wolfhsm/wh_client.h +++ b/wolfhsm/wh_client.h @@ -374,6 +374,41 @@ int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity, int32_t* out_rc, uint32_t* out_affinity); +/** + * @brief Sends a get crypto affinity request to the server. + * + * This function sends a request to query the current crypto affinity + * (software or hardware) on the server without changing it. + * + * @param[in] c Pointer to the client context. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_GetCryptoAffinityRequest(whClientContext* c); + +/** + * @brief Receives a get crypto affinity response from the server. + * + * This function waits for and processes the response message from the server. + * + * @param[in] c Pointer to the client context. + * @param[out] out_rc Pointer to store the server result code. + * @param[out] out_affinity Pointer to store the current crypto affinity. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_GetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity); + +/** + * @brief Gets the current crypto affinity with a blocking call. + * + * @param[in] c Pointer to the client context. + * @param[out] out_rc Pointer to store the server result code. + * @param[out] out_affinity Pointer to store the current crypto affinity. + * @return int Returns 0 on success, or a negative error code on failure. + */ +int wh_Client_GetCryptoAffinity(whClientContext* c, int32_t* out_rc, + uint32_t* out_affinity); + /** * @brief Sends a communication close request to the server. * diff --git a/wolfhsm/wh_common.h b/wolfhsm/wh_common.h index 6d60c5d2d..7093fff97 100644 --- a/wolfhsm/wh_common.h +++ b/wolfhsm/wh_common.h @@ -140,4 +140,9 @@ typedef uint16_t whCertFlags; #define WH_KEYWRAP_AES_GCM_HEADER_SIZE \ (WH_KEYWRAP_AES_GCM_IV_SIZE + WH_KEYWRAP_AES_GCM_TAG_SIZE) +enum WH_CRYPTO_AFFINITY_ENUM { + WH_CRYPTO_AFFINITY_SW = 0, + WH_CRYPTO_AFFINITY_HW = 1, +}; + #endif /* !WOLFHSM_WH_COMMON_H_ */ diff --git a/wolfhsm/wh_error.h b/wolfhsm/wh_error.h index c765c2313..5ce75cdde 100644 --- a/wolfhsm/wh_error.h +++ b/wolfhsm/wh_error.h @@ -45,7 +45,6 @@ enum WH_ERROR_ENUM { compile-time configuration */ WH_ERROR_USAGE = -2009, /* Operation not permitted based on object/key usage flags */ - WH_ERROR_BADCONFIG = -2010, /* Failed due to runtime configuration */ /* NVM and keystore specific status returns */ WH_ERROR_LOCKED = -2100, /* Unlock and retry if necessary */ diff --git a/wolfhsm/wh_message_comm.h b/wolfhsm/wh_message_comm.h index f645f44a8..51fe5ed40 100644 --- a/wolfhsm/wh_message_comm.h +++ b/wolfhsm/wh_message_comm.h @@ -42,11 +42,7 @@ enum WH_MESSAGE_COMM_ACTION_ENUM { WH_MESSAGE_COMM_ACTION_INFO = 0x04, WH_MESSAGE_COMM_ACTION_ECHO = 0x05, WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY = 0x06, -}; - -enum WH_CRYPTO_AFFINITY_ENUM { - WH_CRYPTO_AFFINITY_SW = 0, - WH_CRYPTO_AFFINITY_HW = 1, + WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY = 0x07, }; /* Info request/response data sizes*/ @@ -123,5 +119,14 @@ int wh_MessageComm_TranslateSetCryptoAffinityResponse( uint16_t magic, const whMessageCommSetCryptoAffinityResponse* src, whMessageCommSetCryptoAffinityResponse* dest); +typedef struct { + int32_t rc; + uint32_t affinity; +} whMessageCommGetCryptoAffinityResponse; + +int wh_MessageComm_TranslateGetCryptoAffinityResponse( + uint16_t magic, const whMessageCommGetCryptoAffinityResponse* src, + whMessageCommGetCryptoAffinityResponse* dest); + #endif /* !WOLFHSM_WH_MESSAGE_COMM_H_ */ diff --git a/wolfhsm/wh_server.h b/wolfhsm/wh_server.h index 23c63f570..19600ef8c 100644 --- a/wolfhsm/wh_server.h +++ b/wolfhsm/wh_server.h @@ -67,7 +67,7 @@ typedef struct whServerContext_t whServerContext; typedef struct whServerCryptoContext { int devId; - int configDevId; + int defaultDevId; #ifndef WC_NO_RNG WC_RNG rng[1]; #endif From b10905425a56fd08896742a8cebae547320843a4 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 10 Feb 2026 12:45:08 -0500 Subject: [PATCH 3/4] Move devId from crypto context into server context --- benchmark/wh_bench.c | 6 +- docs/draft/crypto_affinity.md | 4 +- docs/src-ja/chapter03.md | 7 +- docs/src/chapter03.md | 7 +- .../posix/wh_posix_server/wh_posix_server.c | 10 +-- src/wh_server.c | 16 ++-- src/wh_server_crypto.c | 90 +++++++++---------- src/wh_server_img_mgr.c | 12 +-- src/wh_server_keystore.c | 8 +- src/wh_server_she.c | 36 ++++---- test/wh_test_cert.c | 6 +- test/wh_test_clientserver.c | 18 ++-- test/wh_test_crypto.c | 6 +- test/wh_test_crypto_affinity.c | 32 +++---- test/wh_test_log.c | 6 +- test/wh_test_multiclient.c | 8 +- test/wh_test_posix_threadsafe_stress.c | 5 +- test/wh_test_server_img_mgr.c | 4 +- test/wh_test_she.c | 6 +- test/wh_test_wolfcrypt_test.c | 6 +- wolfhsm/wh_server.h | 4 +- 21 files changed, 132 insertions(+), 165 deletions(-) diff --git a/benchmark/wh_bench.c b/benchmark/wh_bench.c index 2be892e9c..3ce39ae06 100644 --- a/benchmark/wh_bench.c +++ b/benchmark/wh_bench.c @@ -1047,9 +1047,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex) #ifndef WOLFHSM_CFG_NO_CRYPTO /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif /* Set up server configuration with NVM and crypto */ @@ -1085,7 +1083,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex) } /* Initialize RNG */ - ret = wc_InitRng_ex(crypto->rng, NULL, crypto->devId); + ret = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID); if (ret != 0) { WH_BENCH_PRINTF("Failed to initialize RNG: %d\n", ret); wolfCrypt_Cleanup(); diff --git a/docs/draft/crypto_affinity.md b/docs/draft/crypto_affinity.md index 91afe9874..dfca6eadd 100644 --- a/docs/draft/crypto_affinity.md +++ b/docs/draft/crypto_affinity.md @@ -122,8 +122,8 @@ When affinity is set: | Affinity | Server Action | |----------|---------------| -| `WH_CRYPTO_AFFINITY_SW` | `server->crypto->devId = INVALID_DEVID` (wolfCrypt uses software) | -| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->defaultDevId` (wolfCrypt uses registered crypto callback) | +| `WH_CRYPTO_AFFINITY_SW` | `server->devId = INVALID_DEVID` (wolfCrypt uses software) | +| `WH_CRYPTO_AFFINITY_HW` | `server->devId = server->defaultDevId` (wolfCrypt uses registered crypto callback) | When affinity is queried (Get), the server reads the current `devId` and returns the corresponding affinity value without modifying any state. diff --git a/docs/src-ja/chapter03.md b/docs/src-ja/chapter03.md index 04daad7b8..a92b0f4b2 100644 --- a/docs/src-ja/chapter03.md +++ b/docs/src-ja/chapter03.md @@ -171,15 +171,14 @@ whNvmContext nvmCtx = {0}; wh_Nvm_Init(&nvmCtx, &whNvmConfig); /* 手順3: 暗号コンテキスト構造体の割り当てと初期化 */ -whServerCryptoContext cryptoCtx { - .devID = INVALID_DEVID; /* あるいは、カスタム暗号コールバックdevIDを設定 */ -}; +whServerCryptoContext cryptoCtx = {0}; /* サーバー設定の割り当てと初期化 */ whServerConfig serverCfg = { .comm = commServerCfg, .nvm = nvmCtx, - .crypto = cryptoCtx, + .crypto = &cryptoCtx, + .devId = INVALID_DEVID, /* あるいは、カスタム暗号コールバックdevIDを設定 */ }; /* 手順4: wolfCryptの初期化 */ diff --git a/docs/src/chapter03.md b/docs/src/chapter03.md index 38de120d3..c00dad25c 100644 --- a/docs/src/chapter03.md +++ b/docs/src/chapter03.md @@ -169,15 +169,14 @@ whNvmContext nvmCtx = {0}; wh_Nvm_Init(&nvmCtx, &whNvmConfig); /* Step 3: Allocate and initialize a crypto context structure */ -whServerCryptoContext cryptoCtx { - .devID = INVALID_DEVID; /* or set to custom crypto callback devID */ -}; +whServerCryptoContext cryptoCtx = {0}; /* Allocate and initialize the Server configuration*/ whServerConfig serverCfg = { .comm = commServerCfg, .nvm = nvmCtx, - .crypto = cryptoCtx, + .crypto = &cryptoCtx, + .devId = INVALID_DEVID, /* or set to custom crypto callback devID */ }; /* Step 4: Initialize wolfCrypt*/ diff --git a/examples/posix/wh_posix_server/wh_posix_server.c b/examples/posix/wh_posix_server/wh_posix_server.c index 0f0d9bca1..03c9b7a8a 100644 --- a/examples/posix/wh_posix_server/wh_posix_server.c +++ b/examples/posix/wh_posix_server/wh_posix_server.c @@ -416,9 +416,7 @@ int main(int argc, char** argv) } #if !defined(WOLFHSM_CFG_NO_CRYPTO) /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #if defined(WOLFHSM_CFG_SHE_EXTENSION) whServerSheContext she[1] = {{0}}; @@ -452,11 +450,11 @@ int main(int argc, char** argv) wh_Utils_Hexdump("Context 4: Server HW RNG:\n", buffer, sizeof(buffer)); /* Context 5: Set default server crypto to use cryptocb */ - crypto->devId = HW_DEV_ID; + s_conf->devId = HW_DEV_ID; WOLFHSM_CFG_PRINTF("Context 5: Setting up default server crypto with devId=%d\n", - crypto->devId); + s_conf->devId); - rc = wc_InitRng_ex(crypto->rng, NULL, crypto->devId); + rc = wc_InitRng_ex(crypto->rng, NULL, s_conf->devId); if (rc != 0) { WOLFHSM_CFG_PRINTF("Failed to wc_InitRng_ex: %d\n", rc); return rc; diff --git a/src/wh_server.c b/src/wh_server.c index ed8522888..aca6a4238 100644 --- a/src/wh_server.c +++ b/src/wh_server.c @@ -78,10 +78,8 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config) #ifndef WOLFHSM_CFG_NO_CRYPTO server->crypto = config->crypto; - if (server->crypto != NULL) { - server->crypto->devId = config->devId; - server->crypto->defaultDevId = config->devId; - } + server->devId = config->devId; + server->defaultDevId = config->devId; #ifdef WOLFHSM_CFG_SHE_EXTENSION server->she = config->she; #endif @@ -268,15 +266,15 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, } else { if (req.affinity == WH_CRYPTO_AFFINITY_SW) { - server->crypto->devId = INVALID_DEVID; + server->devId = INVALID_DEVID; resp.rc = WH_ERROR_OK; } else if (req.affinity == WH_CRYPTO_AFFINITY_HW) { /* If the devId the server was configured with is valid then * switch back to it. The devId is used to call the appropriate * callback to utilize HW crypto */ - if (server->crypto->defaultDevId != INVALID_DEVID) { - server->crypto->devId = server->crypto->defaultDevId; + if (server->defaultDevId != INVALID_DEVID) { + server->devId = server->defaultDevId; resp.rc = WH_ERROR_OK; } /* If the server was not configured with a valid devId @@ -289,7 +287,7 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, else { resp.rc = WH_ERROR_BADARGS; } - resp.affinity = (server->crypto->devId == INVALID_DEVID) + resp.affinity = (server->devId == INVALID_DEVID) ? WH_CRYPTO_AFFINITY_SW : WH_CRYPTO_AFFINITY_HW; } @@ -313,7 +311,7 @@ static int _wh_Server_HandleCommRequest(whServerContext* server, } else { resp.rc = WH_ERROR_OK; - resp.affinity = (server->crypto->devId == INVALID_DEVID) + resp.affinity = (server->devId == INVALID_DEVID) ? WH_CRYPTO_AFFINITY_SW : WH_CRYPTO_AFFINITY_HW; } diff --git a/src/wh_server_crypto.c b/src/wh_server_crypto.c index 282f0d0eb..9366217fc 100644 --- a/src/wh_server_crypto.c +++ b/src/wh_server_crypto.c @@ -305,7 +305,7 @@ static int _HandleRsaKeyGen(whServerContext* ctx, uint16_t magic, uint16_t der_size = 0; /* init the rsa key */ - ret = wc_InitRsaKey_ex(rsa, NULL, ctx->crypto->devId); + ret = wc_InitRsaKey_ex(rsa, NULL, ctx->devId); if (ret == 0) { /* make the rsa key with the given params */ ret = wc_MakeRsaKey(rsa, key_size, e, ctx->crypto->rng); @@ -457,7 +457,7 @@ static int _HandleRsaFunction( whServerContext* ctx, uint16_t magic, } /* init rsa key */ - ret = wc_InitRsaKey_ex(rsa, NULL, ctx->crypto->devId); + ret = wc_InitRsaKey_ex(rsa, NULL, ctx->devId); /* load the key from the keystore */ if (ret == 0) { ret = wh_Server_CacheExportRsaKey(ctx, key_id, rsa); @@ -516,7 +516,7 @@ static int _HandleRsaGetSize(whServerContext* ctx, uint16_t magic, int evict = !!(options & WH_MESSAGE_CRYPTO_RSA_GET_SIZE_OPTIONS_EVICT); /* init rsa key */ - ret = wc_InitRsaKey_ex(rsa, NULL, ctx->crypto->devId); + ret = wc_InitRsaKey_ex(rsa, NULL, ctx->devId); /* load the key from the keystore */ if (ret == 0) { ret = wh_Server_CacheExportRsaKey(ctx, key_id, rsa); @@ -837,7 +837,7 @@ static int _HandleEccKeyGen(whServerContext* ctx, uint16_t magic, uint16_t res_size = 0; /* init ecc key */ - ret = wc_ecc_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ecc_init_ex(key, NULL, ctx->devId); if (ret == 0) { /* generate the key */ ret = wc_ecc_make_key_ex(ctx->crypto->rng, key_size, key, curve_id); @@ -942,9 +942,9 @@ static int _HandleEccSharedSecret(whServerContext* ctx, uint16_t magic, word32 res_len = 0; /* init ecc keys */ - ret = wc_ecc_init_ex(pub_key, NULL, ctx->crypto->devId); + ret = wc_ecc_init_ex(pub_key, NULL, ctx->devId); if (ret == 0) { - ret = wc_ecc_init_ex(prv_key, NULL, ctx->crypto->devId); + ret = wc_ecc_init_ex(prv_key, NULL, ctx->devId); if (ret == 0) { /* set rng */ ret = wc_ecc_set_rng(prv_key, ctx->crypto->rng); @@ -1038,7 +1038,7 @@ static int _HandleEccSign(whServerContext* ctx, uint16_t magic, word32 res_len = max_len; /* init private key */ - ret = wc_ecc_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ecc_init_ex(key, NULL, ctx->devId); if (ret == 0) { /* load the private key */ ret = wh_Server_EccKeyCacheExport(ctx, key_id, key); @@ -1133,7 +1133,7 @@ static int _HandleEccVerify(whServerContext* ctx, uint16_t magic, int result = 0; /* init public key */ - ret = wc_ecc_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ecc_init_ex(key, NULL, ctx->devId); if (ret == 0) { /* load the public key */ ret = wh_Server_EccKeyCacheExport(ctx, key_id, key); @@ -1198,7 +1198,7 @@ static int _HandleEccCheckPrivKey(whServerContext* server, whPacket* packet, /* Response packet */ wh_Packet_pk_ecc_check_res* res = &packet->pkEccCheckRes; - ret = wc_ecc_init_ex(key, NULL, server->crypto->devId); + ret = wc_ecc_init_ex(key, NULL, server->devId); if (ret == 0) { /* load the private key */ ret = wh_Server_EccKeyCacheExport(server, key, key_id); @@ -1599,7 +1599,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic, ret = wc_KDA_KDF_twostep_cmac( salt, saltSz, z, zSz, (fixedInfoSz > 0) ? fixedInfo : NULL, fixedInfoSz, - out, outSz, NULL, ctx->crypto->devId); + out, outSz, NULL, ctx->devId); if (ret == 0) { if (flags & WH_NVM_FLAGS_EPHEMERAL) { keyIdOut = WH_KEYID_ERASED; @@ -1672,7 +1672,7 @@ static int _HandleCurve25519KeyGen(whServerContext* ctx, uint16_t magic, (word32)(WOLFHSM_CFG_COMM_DATA_LEN - (out - (uint8_t*)cryptoDataOut)); /* init key */ - ret = wc_curve25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_curve25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { /* make the key */ ret = wc_curve25519_make_key(ctx->crypto->rng, key_size, key); @@ -1771,10 +1771,10 @@ static int _HandleCurve25519SharedSecret(whServerContext* ctx, uint16_t magic, word32 res_len = max_len; /* init private key */ - ret = wc_curve25519_init_ex(priv, NULL, ctx->crypto->devId); + ret = wc_curve25519_init_ex(priv, NULL, ctx->devId); if (ret == 0) { /* init public key */ - ret = wc_curve25519_init_ex(pub, NULL, ctx->crypto->devId); + ret = wc_curve25519_init_ex(pub, NULL, ctx->devId); if (ret == 0) { #ifdef WOLFSSL_CURVE25519_BLINDING ret = wc_curve25519_set_rng(priv, ctx->crypto->rng); @@ -1847,7 +1847,7 @@ static int _HandleEd25519KeyGen(whServerContext* ctx, uint16_t magic, (res_out - (uint8_t*)cryptoDataOut)); uint16_t ser_size = 0; - ret = wc_ed25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ed25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { ret = wc_ed25519_make_key(ctx->crypto->rng, ED25519_KEY_SIZE, key); if (ret == 0) { @@ -1951,7 +1951,7 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic, (uint8_t*)cryptoDataOut + sizeof(whMessageCrypto_Ed25519SignResponse); word32 sig_len = sizeof(sig); - ret = wc_ed25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ed25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { ret = wh_Server_CacheExportEd25519Key(ctx, key_id, key); if (ret == WH_ERROR_OK) { @@ -2049,7 +2049,7 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic, int result = 0; - ret = wc_ed25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ed25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { ret = wh_Server_CacheExportEd25519Key(ctx, key_id, key); if (ret == WH_ERROR_OK) { @@ -2143,7 +2143,7 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic, } } if (ret == WH_ERROR_OK) { - ret = wc_ed25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ed25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { ret = wh_Server_CacheExportEd25519Key(ctx, key_id, key); if (ret == WH_ERROR_OK) { @@ -2249,7 +2249,7 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic, } if (ret == WH_ERROR_OK) { - ret = wc_ed25519_init_ex(key, NULL, ctx->crypto->devId); + ret = wc_ed25519_init_ex(key, NULL, ctx->devId); if (ret == 0) { ret = wh_Server_CacheExportEd25519Key(ctx, key_id, key); if (ret == WH_ERROR_OK) { @@ -2359,7 +2359,7 @@ static int _HandleAesCtr(whServerContext* ctx, uint16_t magic, } if (ret == 0) { /* init key with possible hardware */ - ret = wc_AesInit(aes, NULL, ctx->crypto->devId); + ret = wc_AesInit(aes, NULL, ctx->devId); } if (ret == 0) { /* load the key */ @@ -2471,7 +2471,7 @@ static int _HandleAesEcb(whServerContext* ctx, uint16_t magic, } if (ret == 0) { /* init key with possible hardware */ - ret = wc_AesInit(aes, NULL, ctx->crypto->devId); + ret = wc_AesInit(aes, NULL, ctx->devId); } if (ret == 0) { /* load the key */ @@ -2585,7 +2585,7 @@ static int _HandleAesCbc(whServerContext* ctx, uint16_t magic, const void* crypt } if (ret == 0) { /* init key with possible hardware */ - ret = wc_AesInit(aes, NULL, ctx->crypto->devId); + ret = wc_AesInit(aes, NULL, ctx->devId); } if (ret == 0) { /* load the key */ @@ -2728,7 +2728,7 @@ static int _HandleAesGcm(whServerContext* ctx, uint16_t magic, } if (ret == 0) { /* init key with possible hardware */ - ret = wc_AesInit(aes, NULL, ctx->crypto->devId); + ret = wc_AesInit(aes, NULL, ctx->devId); } if (ret == 0) { /* load the key */ @@ -2819,7 +2819,7 @@ static int _HandleAesGcmDma(whServerContext* ctx, uint16_t magic, uint16_t seq, } if (ret == WH_ERROR_OK) { - ret = wc_AesInit(aes, NULL, ctx->crypto->devId); + ret = wc_AesInit(aes, NULL, ctx->devId); } /* Handle key operations */ @@ -3077,7 +3077,7 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq, WH_DEBUG_SERVER_VERBOSE("cmac generate oneshot\n"); ret = wc_AesCmacGenerate_ex(cmac, out, &len, in, req.inSz, tmpKey, - tmpKeyLen, NULL, ctx->crypto->devId); + tmpKeyLen, NULL, ctx->devId); if (ret == 0) { res.outSz = len; @@ -3092,7 +3092,7 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq, /* Initialize CMAC context with key (re-derives k1/k2 subkeys) */ ret = wc_InitCmac_ex(cmac, tmpKey, tmpKeyLen, WC_CMAC_AES, NULL, NULL, - ctx->crypto->devId); + ctx->devId); WH_DEBUG_SERVER_VERBOSE("cmac init with keylen:%d ret:%d\n", tmpKeyLen, ret); @@ -3160,7 +3160,7 @@ static int _HandleSha256(whServerContext* ctx, uint16_t magic, return ret; } /* always init sha2 struct with the devid */ - ret = wc_InitSha256_ex(sha256, NULL, ctx->crypto->devId); + ret = wc_InitSha256_ex(sha256, NULL, ctx->devId); if (ret != 0) { return ret; } @@ -3233,7 +3233,7 @@ static int _HandleSha224(whServerContext* ctx, uint16_t magic, if (req.isLastBlock && req.lastBlockLen > WC_SHA224_BLOCK_SIZE) { return WH_ERROR_BADARGS; } - ret = wc_InitSha224_ex(sha224, NULL, ctx->crypto->devId); + ret = wc_InitSha224_ex(sha224, NULL, ctx->devId); if (ret != 0) { return ret; } @@ -3309,7 +3309,7 @@ static int _HandleSha384(whServerContext* ctx, uint16_t magic, } /* init sha2 struct with the devid */ - ret = wc_InitSha384_ex(sha384, NULL, ctx->crypto->devId); + ret = wc_InitSha384_ex(sha384, NULL, ctx->devId); if (ret != 0) { return ret; } @@ -3390,16 +3390,16 @@ static int _HandleSha512(whServerContext* ctx, uint16_t magic, switch (hashType) { #ifndef WOLFSSL_NOSHA512_224 case WC_HASH_TYPE_SHA512_224: - ret = wc_InitSha512_224_ex(sha512, NULL, ctx->crypto->devId); + ret = wc_InitSha512_224_ex(sha512, NULL, ctx->devId); break; #endif #ifndef WOLFSSL_NOSHA512_256 case WC_HASH_TYPE_SHA512_256: - ret = wc_InitSha512_256_ex(sha512, NULL, ctx->crypto->devId); + ret = wc_InitSha512_256_ex(sha512, NULL, ctx->devId); break; #endif default: - ret = wc_InitSha512_ex(sha512, NULL, ctx->crypto->devId); + ret = wc_InitSha512_ex(sha512, NULL, ctx->devId); break; } if (ret != 0) { @@ -3545,7 +3545,7 @@ static int _HandleMlDsaKeyGen(whServerContext* ctx, uint16_t magic, } else { /* init mldsa key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret == 0) { /* Set the ML-DSA security level */ ret = wc_MlDsaKey_SetParams(key, level); @@ -3664,7 +3664,7 @@ static int _HandleMlDsaSign(whServerContext* ctx, uint16_t magic, word32 res_len = max_len; /* init private key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret == 0) { /* load the private key */ ret = wh_Server_MlDsaKeyCacheExport(ctx, key_id, key); @@ -3753,7 +3753,7 @@ static int _HandleMlDsaVerify(whServerContext* ctx, uint16_t magic, int result = 0; /* init public key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret == 0) { /* load the public key */ ret = wh_Server_MlDsaKeyCacheExport(ctx, key_id, key); @@ -4201,7 +4201,7 @@ static int _HandleSha256Dma(whServerContext* ctx, uint16_t magic, uint16_t seq, * copied back into client memory. */ clientDevId = sha256->devId; /* overwrite the devId to that of the server for local crypto */ - sha256->devId = ctx->crypto->devId; + sha256->devId = ctx->devId; } } @@ -4322,7 +4322,7 @@ static int _HandleSha224Dma(whServerContext* ctx, uint16_t magic, uint16_t seq, * copied back into client memory. */ clientDevId = sha224->devId; /* overwrite the devId to that of the server for local crypto */ - sha224->devId = ctx->crypto->devId; + sha224->devId = ctx->devId; } } @@ -4443,7 +4443,7 @@ static int _HandleSha384Dma(whServerContext* ctx, uint16_t magic, uint16_t seq, * copied back into client memory. */ clientDevId = sha384->devId; /* overwrite the devId to that of the server for local crypto */ - sha384->devId = ctx->crypto->devId; + sha384->devId = ctx->devId; } } @@ -4564,7 +4564,7 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, uint16_t seq, * copied back into client memory. */ clientDevId = sha512->devId; /* overwrite the devId to that of the server for local crypto */ - sha512->devId = ctx->crypto->devId; + sha512->devId = ctx->devId; /* retrieve hash Type to handle 512, 512-224, or 512-256 */ hashType = sha512->hashType; } @@ -4697,7 +4697,7 @@ static int _HandleMlDsaKeyGenDma(whServerContext* ctx, uint16_t magic, } else { /* init mldsa key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret == 0) { /* Set the ML-DSA security level */ ret = wc_MlDsaKey_SetParams(key, req.level); @@ -4823,7 +4823,7 @@ static int _HandleMlDsaSignDma(whServerContext* ctx, uint16_t magic, evict = !!(req.options & WH_MESSAGE_CRYPTO_MLDSA_SIGN_OPTIONS_EVICT); /* Initialize key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret == 0) { /* Export key from cache */ /* TODO: sanity check security level against key pulled from cache? */ @@ -4934,7 +4934,7 @@ static int _HandleMlDsaVerifyDma(whServerContext* ctx, uint16_t magic, evict = !!(req.options & WH_MESSAGE_CRYPTO_MLDSA_VERIFY_OPTIONS_EVICT); /* Initialize key */ - ret = wc_MlDsaKey_Init(key, NULL, ctx->crypto->devId); + ret = wc_MlDsaKey_Init(key, NULL, ctx->devId); if (ret != 0) { return ret; } @@ -5138,7 +5138,7 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq, ret = wc_AesCmacGenerate_ex(cmac, out, &len, inAddr, req.input.sz, tmpKey, tmpKeyLen, NULL, - ctx->crypto->devId); + ctx->devId); } else if (ret == WH_ERROR_OK) { /* HSM-local key via keyId - init then generate */ @@ -5146,12 +5146,12 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq, req.keyId); ret = wc_InitCmac_ex(cmac, tmpKey, tmpKeyLen, WC_CMAC_AES, NULL, - NULL, ctx->crypto->devId); + NULL, ctx->devId); if (ret == WH_ERROR_OK) { ret = wc_AesCmacGenerate_ex(cmac, out, &len, inAddr, req.input.sz, - NULL, 0, NULL, ctx->crypto->devId); + NULL, 0, NULL, ctx->devId); } } @@ -5172,7 +5172,7 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq, /* Initialize CMAC context with key (re-derives k1/k2 subkeys) */ if (ret == 0) { ret = wc_InitCmac_ex(cmac, tmpKey, tmpKeyLen, WC_CMAC_AES, NULL, - NULL, ctx->crypto->devId); + NULL, ctx->devId); WH_DEBUG_SERVER_VERBOSE("dma cmac init with keylen:%d ret:%d\n", tmpKeyLen, ret); } diff --git a/src/wh_server_img_mgr.c b/src/wh_server_img_mgr.c index 9a32c1093..250ea79e7 100644 --- a/src/wh_server_img_mgr.c +++ b/src/wh_server_img_mgr.c @@ -239,11 +239,11 @@ int wh_Server_ImgMgrVerifyMethodEccWithSha256(whServerImgMgrContext* context, /* Hash the image data from server pointer using one-shot API */ ret = wc_Sha256Hash_ex((const uint8_t*)serverPtr, (word32)img->size, hash, - NULL, server->crypto->devId); + NULL, server->devId); #else /* Hash the image data using one-shot API */ ret = wc_Sha256Hash_ex((const uint8_t*)img->addr, (word32)img->size, hash, - NULL, context->server->crypto->devId); + NULL, context->server->devId); #endif if (ret != 0) { wc_ecc_free(&eccKey); @@ -319,11 +319,11 @@ int wh_Server_ImgMgrVerifyMethodAesCmac(whServerImgMgrContext* context, /* Compute CMAC of the image data from server pointer */ ret = wc_AesCmacVerify_ex(&cmac, sig, (word32)sigSz, (const byte*)serverPtr, (word32)img->size, key, (word32)keySz, NULL, - server->crypto->devId); + server->devId); #else ret = wc_AesCmacVerify_ex(&cmac, sig, (word32)sigSz, (const byte*)img->addr, (word32)img->size, key, (word32)keySz, NULL, - context->server->crypto->devId); + context->server->devId); #endif if (ret != 0) { return ret; @@ -389,11 +389,11 @@ int wh_Server_ImgMgrVerifyMethodRsaSslWithSha256( /* Hash the image data from server pointer using one-shot API */ ret = wc_Sha256Hash_ex((const uint8_t*)serverPtr, (word32)img->size, hash, - NULL, server->crypto->devId); + NULL, server->devId); #else /* Hash the image data using one-shot API */ ret = wc_Sha256Hash_ex((const uint8_t*)img->addr, (word32)img->size, hash, - NULL, context->server->crypto->devId); + NULL, context->server->devId); #endif if (ret != 0) { wc_FreeRsaKey(&rsaKey); diff --git a/src/wh_server_keystore.c b/src/wh_server_keystore.c index bbf0eca0c..79106fd07 100644 --- a/src/wh_server_keystore.c +++ b/src/wh_server_keystore.c @@ -942,7 +942,7 @@ static int _AesGcmKeyWrap(whServerContext* server, whKeyId serverKeyId, } /* Initialize AES context and set it to use the server side key */ - ret = wc_AesInit(aes, NULL, server->crypto->devId); + ret = wc_AesInit(aes, NULL, server->devId); if (ret != 0) { return ret; } @@ -1022,7 +1022,7 @@ static int _AesGcmKeyUnwrap(whServerContext* server, uint16_t serverKeyId, } /* Initialize AES context and set it to use the server side key */ - ret = wc_AesInit(aes, NULL, server->crypto->devId); + ret = wc_AesInit(aes, NULL, server->devId); if (ret != 0) { return ret; } @@ -1083,7 +1083,7 @@ static int _AesGcmDataWrap(whServerContext* server, whKeyId serverKeyId, serverKeySz = serverKeyMetadata->len; /* Initialize AES context and set it to use the server side key */ - ret = wc_AesInit(aes, NULL, server->crypto->devId); + ret = wc_AesInit(aes, NULL, server->devId); if (ret != 0) { return ret; } @@ -1149,7 +1149,7 @@ static int _AesGcmDataUnwrap(whServerContext* server, uint16_t serverKeyId, serverKeySz = serverKeyMetadata->len; /* Initialize AES context and set it to use the server side key */ - ret = wc_AesInit(aes, NULL, server->crypto->devId); + ret = wc_AesInit(aes, NULL, server->devId); if (ret != 0) { return ret; } diff --git a/src/wh_server_she.c b/src/wh_server_she.c index 633dfc4b2..490404a68 100644 --- a/src/wh_server_she.c +++ b/src/wh_server_she.c @@ -166,7 +166,7 @@ static int _AesMp16(whServerContext* server, uint8_t* in, word32 inSz, if (server == NULL || server->she == NULL) { return WH_ERROR_BADARGS; } - return wh_She_AesMp16_ex(server->she->sheAes, NULL, server->crypto->devId, + return wh_She_AesMp16_ex(server->she->sheAes, NULL, server->devId, in, inSz, out); } @@ -262,7 +262,7 @@ static int _SecureBootInit(whServerContext* server, uint16_t magic, * expected digest so meta->len will be too long */ if (ret == 0) { ret = wc_InitCmac_ex(server->she->sheCmac, macKey, WH_SHE_KEY_SZ, - WC_CMAC_AES, NULL, NULL, server->crypto->devId); + WC_CMAC_AES, NULL, NULL, server->devId); } /* hash 12 zeros */ if (ret == 0) { @@ -501,7 +501,7 @@ static int _LoadKey(whServerContext* server, uint16_t magic, uint16_t req_size, ret = wc_AesCmacGenerate_ex(server->she->sheCmac, cmacOutput, (word32*)&field, cmacInput, sizeof(cmacInput), tmpKey, WH_SHE_KEY_SZ, - NULL, server->crypto->devId); + NULL, server->devId); } /* compare digest to M3 */ if (ret == 0 && memcmp(req.messageThree, cmacOutput, field) != 0) { @@ -518,7 +518,7 @@ static int _LoadKey(whServerContext* server, uint16_t magic, uint16_t req_size, } /* decrypt messageTwo */ if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } if (ret == 0) { ret = wc_AesSetKey(server->she->sheAes, tmpKey, WH_SHE_KEY_SZ, NULL, @@ -611,7 +611,7 @@ static int _LoadKey(whServerContext* server, uint16_t magic, uint16_t req_size, meta->len + sizeof(_SHE_KEY_UPDATE_ENC_C), tmpKey); } if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } if (ret == 0) { ret = wc_AesSetKey(server->she->sheAes, tmpKey, WH_SHE_KEY_SZ, NULL, @@ -651,7 +651,7 @@ static int _LoadKey(whServerContext* server, uint16_t magic, uint16_t req_size, ret = wc_AesCmacGenerate_ex(server->she->sheCmac, resp.messageFive, (word32*)&field, resp.messageFour, sizeof(resp.messageFour), tmpKey, - WH_SHE_KEY_SZ, NULL, server->crypto->devId); + WH_SHE_KEY_SZ, NULL, server->devId); } if (ret == 0) { /* mark if the ram key was loaded */ @@ -764,7 +764,7 @@ static int _ExportRamKey(whServerContext* server, uint16_t magic, } /* encrypt M2 with K1 */ if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } if (ret == 0) { ret = wc_AesSetKey(server->she->sheAes, tmpKey, WH_SHE_KEY_SZ, NULL, @@ -798,7 +798,7 @@ static int _ExportRamKey(whServerContext* server, uint16_t magic, ret = wc_AesCmacGenerate_ex(server->she->sheCmac, resp.messageThree, (word32*)&field, cmacInput, sizeof(cmacInput), tmpKey, WH_SHE_KEY_SZ, - NULL, server->crypto->devId); + NULL, server->devId); } if (ret == 0) { /* copy the ram key to kdfInput */ @@ -812,7 +812,7 @@ static int _ExportRamKey(whServerContext* server, uint16_t magic, } /* set K3 as encryption key */ if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } if (ret == 0) { ret = wc_AesSetKey(server->she->sheAes, tmpKey, WH_SHE_KEY_SZ, NULL, @@ -849,7 +849,7 @@ static int _ExportRamKey(whServerContext* server, uint16_t magic, ret = wc_AesCmacGenerate_ex(server->she->sheCmac, resp.messageFive, (word32*)&field, resp.messageFour, sizeof(resp.messageFour), tmpKey, - WH_SHE_KEY_SZ, NULL, server->crypto->devId); + WH_SHE_KEY_SZ, NULL, server->devId); } resp.rc = _TranslateSheReturnCode(ret); @@ -913,7 +913,7 @@ static int _InitRnd(whServerContext* server, uint16_t magic, uint16_t req_size, } /* set up aes */ if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } if (ret == 0) { ret = wc_AesSetKey(server->she->sheAes, tmpKey, WH_SHE_KEY_SZ, NULL, @@ -978,7 +978,7 @@ static int _Rnd(whServerContext* server, uint16_t magic, uint16_t req_size, /* set up aes */ if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } /* use PRNG_KEY as the encryption key */ @@ -1104,7 +1104,7 @@ static int _EncEcb(whServerContext* server, uint16_t magic, uint16_t req_size, WH_MAKE_KEYID(WH_KEYTYPE_SHE, server->comm->client_id, req.keyId), NULL, tmpKey, &keySz); if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } else { ret = WH_SHE_ERC_KEY_NOT_AVAILABLE; @@ -1163,7 +1163,7 @@ static int _EncCbc(whServerContext* server, uint16_t magic, uint16_t req_size, tmpKey, &keySz); if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } else { ret = WH_SHE_ERC_KEY_NOT_AVAILABLE; @@ -1228,7 +1228,7 @@ static int _DecEcb(whServerContext* server, uint16_t magic, uint16_t req_size, WH_MAKE_KEYID(WH_KEYTYPE_SHE, server->comm->client_id, req.keyId), NULL, tmpKey, &keySz); if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } else { ret = WH_SHE_ERC_KEY_NOT_AVAILABLE; @@ -1292,7 +1292,7 @@ static int _DecCbc(whServerContext* server, uint16_t magic, uint16_t req_size, tmpKey, &keySz); if (ret == 0) { - ret = wc_AesInit(server->she->sheAes, NULL, server->crypto->devId); + ret = wc_AesInit(server->she->sheAes, NULL, server->devId); } else { ret = WH_SHE_ERC_KEY_NOT_AVAILABLE; @@ -1354,7 +1354,7 @@ static int _GenerateMac(whServerContext* server, uint16_t magic, if (ret == 0) { ret = wc_AesCmacGenerate_ex(server->she->sheCmac, resp.mac, (word32*)&field, in, req.sz, tmpKey, - WH_SHE_KEY_SZ, NULL, server->crypto->devId); + WH_SHE_KEY_SZ, NULL, server->devId); } else { ret = WH_SHE_ERC_KEY_NOT_AVAILABLE; @@ -1398,7 +1398,7 @@ static int _VerifyMac(whServerContext* server, uint16_t magic, if (ret == 0) { ret = wc_AesCmacVerify_ex(server->she->sheCmac, mac, req.macLen, message, req.messageLen, tmpKey, keySz, NULL, - server->crypto->devId); + server->devId); /* only evaluate if key was found */ if (ret == 0) { resp.status = 0; diff --git a/test/wh_test_cert.c b/test/wh_test_cert.c index 0da3f385f..c770a5f6d 100644 --- a/test/wh_test_cert.c +++ b/test/wh_test_cert.c @@ -625,9 +625,7 @@ int whTest_CertRamSim(whTestNvmBackendType nvmType) whTest_NvmCfgBackend(nvmType, &nvm_setup, n_conf, fc_conf, fc, fcb)); #ifndef WOLFHSM_CFG_NO_CRYPTO - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif whServerConfig s_conf[1] = {{ @@ -644,7 +642,7 @@ int whTest_CertRamSim(whTestNvmBackendType nvmType) WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); #ifndef WOLFHSM_CFG_NO_CRYPTO WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); #endif /* Run certificate configuration tests */ diff --git a/test/wh_test_clientserver.c b/test/wh_test_clientserver.c index 15f4ea67a..a23b1289e 100644 --- a/test/wh_test_clientserver.c +++ b/test/wh_test_clientserver.c @@ -626,9 +626,7 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType) whTest_NvmCfgBackend(nvmType, &nvm_setup, n_conf, fc_conf, fc, fcb)); #ifndef WOLFHSM_CFG_NO_CRYPTO - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif whServerConfig s_conf[1] = {{ @@ -647,7 +645,7 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType) #ifndef WOLFHSM_CFG_NO_CRYPTO WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); #endif WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); @@ -1662,9 +1660,7 @@ static int wh_ClientServer_MemThreadTest(whTestNvmBackendType nvmType) #ifndef WOLFHSM_CFG_NO_CRYPTO /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif @@ -1681,7 +1677,7 @@ static int wh_ClientServer_MemThreadTest(whTestNvmBackendType nvmType) #ifndef WOLFHSM_CFG_NO_CRYPTO WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); #endif _whClientServerThreadTest(c_conf, s_conf); @@ -1747,9 +1743,7 @@ static int wh_ClientServer_PosixMemMapThreadTest(whTestNvmBackendType nvmType) #ifndef WOLFHSM_CFG_NO_CRYPTO /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif whServerConfig s_conf[1] = {{ @@ -1764,7 +1758,7 @@ static int wh_ClientServer_PosixMemMapThreadTest(whTestNvmBackendType nvmType) #ifndef WOLFHSM_CFG_NO_CRYPTO WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); #endif _whClientServerThreadTest(c_conf, s_conf); diff --git a/test/wh_test_crypto.c b/test/wh_test_crypto.c index 1aaeb9dc4..df82ae12b 100644 --- a/test/wh_test_crypto.c +++ b/test/wh_test_crypto.c @@ -5704,9 +5704,7 @@ static int wh_ClientServer_MemThreadTest(whTestNvmBackendType nvmType) whTest_NvmCfgBackend(nvmType, &nvm_setup, n_conf, fc_conf, fc, fcb)); /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; whServerConfig s_conf[1] = {{ @@ -5720,7 +5718,7 @@ static int wh_ClientServer_MemThreadTest(whTestNvmBackendType nvmType) ret = wolfCrypt_Init(); if (ret == 0) { - ret = wc_InitRng_ex(crypto->rng, NULL, crypto->devId); + ret = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID); if (ret != 0) { WH_ERROR_PRINT("Failed to initialize wolfCrypt rng: %d\n", ret); } diff --git a/test/wh_test_crypto_affinity.c b/test/wh_test_crypto_affinity.c index 5f7e61d48..f4d770a4c 100644 --- a/test/wh_test_crypto_affinity.c +++ b/test/wh_test_crypto_affinity.c @@ -162,10 +162,8 @@ static int whTest_CryptoAffinityWithCb(void) }}; whNvmContext nvm[1] = {0}; - /* Crypto context - configure with our test device ID */ - whServerCryptoContext crypto[1] = {{ - .devId = TEST_DEV_ID, - }}; + /* Crypto context */ + whServerCryptoContext crypto[1] = {0}; whServerConfig s_conf[1] = {{ .comm_config = cs_conf, @@ -205,8 +203,8 @@ static int whTest_CryptoAffinityWithCb(void) /* Verify initial state - should be HW since we configured with valid devId */ - WH_TEST_ASSERT_RETURN(server->crypto->devId == TEST_DEV_ID); - WH_TEST_ASSERT_RETURN(server->crypto->defaultDevId == TEST_DEV_ID); + WH_TEST_ASSERT_RETURN(server->devId == TEST_DEV_ID); + WH_TEST_ASSERT_RETURN(server->defaultDevId == TEST_DEV_ID); /* Test 1a: Get initial affinity - should be HW */ WH_TEST_RETURN_ON_FAIL(wh_Client_GetCryptoAffinityRequest(client)); @@ -224,7 +222,7 @@ static int whTest_CryptoAffinityWithCb(void) wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); - WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->devId == INVALID_DEVID); /* Test 1b: Get affinity after setting SW - should be SW */ WH_TEST_RETURN_ON_FAIL(wh_Client_GetCryptoAffinityRequest(client)); @@ -243,7 +241,7 @@ static int whTest_CryptoAffinityWithCb(void) wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_HW); - WH_TEST_ASSERT_RETURN(server->crypto->devId == TEST_DEV_ID); + WH_TEST_ASSERT_RETURN(server->devId == TEST_DEV_ID); /* Test 3: Invalid affinity value - should return BADARGS */ WH_TEST_RETURN_ON_FAIL(wh_Client_SetCryptoAffinityRequest(client, 0xFF)); @@ -268,7 +266,7 @@ static int whTest_CryptoAffinityWithCb(void) { WC_RNG testRng[1]; uint8_t randomBytes[16]; - rc = wc_InitRng_ex(testRng, NULL, server->crypto->devId); + rc = wc_InitRng_ex(testRng, NULL, server->devId); if (rc == 0) { (void)wc_RNG_GenerateBlock(testRng, randomBytes, sizeof(randomBytes)); @@ -294,7 +292,7 @@ static int whTest_CryptoAffinityWithCb(void) { WC_RNG testRng[1]; uint8_t randomBytes[16]; - rc = wc_InitRng_ex(testRng, NULL, server->crypto->devId); + rc = wc_InitRng_ex(testRng, NULL, server->devId); if (rc == 0) { (void)wc_RNG_GenerateBlock(testRng, randomBytes, sizeof(randomBytes)); @@ -391,10 +389,8 @@ static int whTest_CryptoAffinityNoCb(void) }}; whNvmContext nvm[1] = {0}; - /* Crypto context - configure with INVALID_DEVID (no HW crypto) */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + /* Crypto context */ + whServerCryptoContext crypto[1] = {0}; whServerConfig s_conf[1] = {{ .comm_config = cs_conf, @@ -432,8 +428,8 @@ static int whTest_CryptoAffinityNoCb(void) /* Verify initial state - should be SW since we configured with INVALID_DEVID */ - WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); - WH_TEST_ASSERT_RETURN(server->crypto->defaultDevId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->devId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->defaultDevId == INVALID_DEVID); /* Test 0: Get initial affinity - should be SW since configured with * INVALID_DEVID */ @@ -452,7 +448,7 @@ static int whTest_CryptoAffinityNoCb(void) wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity)); WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK); WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); - WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->devId == INVALID_DEVID); /* Test 2: Set HW affinity - should fail with BADCONFIG since no HW * configured */ @@ -464,7 +460,7 @@ static int whTest_CryptoAffinityNoCb(void) WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_NOTIMPL); /* Affinity should remain SW after failed HW request */ WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW); - WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID); + WH_TEST_ASSERT_RETURN(server->devId == INVALID_DEVID); /* Test 3: Verify SW affinity still works after failed HW request */ WH_TEST_RETURN_ON_FAIL( diff --git a/test/wh_test_log.c b/test/wh_test_log.c index e58a4ea68..eeea1b843 100644 --- a/test/wh_test_log.c +++ b/test/wh_test_log.c @@ -1487,9 +1487,7 @@ static int whTest_LogClientServerMemTransport(void) WH_NVM_TEST_BACKEND_FLASH, &nvm_setup, n_conf, fc_conf, fc, fcb)); #ifndef WOLFHSM_CFG_NO_CRYPTO - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; #endif posixLogFileContext posixCtx[1] = {0}; @@ -1519,7 +1517,7 @@ static int whTest_LogClientServerMemTransport(void) #ifndef WOLFHSM_CFG_NO_CRYPTO WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); #endif _whLogClientServerThreadTest(c_conf, s_conf); diff --git a/test/wh_test_multiclient.c b/test/wh_test_multiclient.c index 54114cf66..8e54e9e64 100644 --- a/test/wh_test_multiclient.c +++ b/test/wh_test_multiclient.c @@ -1502,8 +1502,8 @@ static int whTest_MultiClientSequential(void) #if !defined(WOLFHSM_CFG_NO_CRYPTO) /* Crypto contexts for both servers */ - whServerCryptoContext crypto1[1] = {{.devId = INVALID_DEVID}}; - whServerCryptoContext crypto2[1] = {{.devId = INVALID_DEVID}}; + whServerCryptoContext crypto1[1] = {0}; + whServerCryptoContext crypto2[1] = {0}; #endif /* Server 1 configuration */ @@ -1561,11 +1561,11 @@ static int whTest_MultiClientSequential(void) #if !defined(WOLFHSM_CFG_NO_CRYPTO) /* Initialize RNGs */ - ret = wc_InitRng_ex(crypto1->rng, NULL, crypto1->devId); + ret = wc_InitRng_ex(crypto1->rng, NULL, INVALID_DEVID); if (ret != 0) return ret; - ret = wc_InitRng_ex(crypto2->rng, NULL, crypto2->devId); + ret = wc_InitRng_ex(crypto2->rng, NULL, INVALID_DEVID); if (ret != 0) return ret; #endif diff --git a/test/wh_test_posix_threadsafe_stress.c b/test/wh_test_posix_threadsafe_stress.c index fb4c6a0fc..e66db7e16 100644 --- a/test/wh_test_posix_threadsafe_stress.c +++ b/test/wh_test_posix_threadsafe_stress.c @@ -641,11 +641,8 @@ static int initClientServerPair(StressTestContext* ctx, int pairIndex) pair->serverCommConfig.transport_config = &pair->tmConfig; pair->serverCommConfig.server_id = (uint16_t)(200 + pairIndex); - /* Configure crypto context */ - pair->cryptoCtx.devId = INVALID_DEVID; - /* Initialize RNG for this server */ - rc = wc_InitRng_ex(pair->cryptoCtx.rng, NULL, pair->cryptoCtx.devId); + rc = wc_InitRng_ex(pair->cryptoCtx.rng, NULL, INVALID_DEVID); if (rc != 0) { WH_ERROR_PRINT("Failed to init RNG for pair %d: %d\n", pairIndex, rc); return rc; diff --git a/test/wh_test_server_img_mgr.c b/test/wh_test_server_img_mgr.c index dbd4e1484..986b2fe90 100644 --- a/test/wh_test_server_img_mgr.c +++ b/test/wh_test_server_img_mgr.c @@ -1236,9 +1236,7 @@ int whTest_ServerImgMgr(whTestNvmBackendType nvmType) WH_TEST_RETURN_ON_FAIL( whTest_NvmCfgBackend(nvmType, &nvm_setup, n_conf, fc_conf, fc, fcb)); - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; whServerConfig s_conf[1] = {{ .comm_config = cs_conf, diff --git a/test/wh_test_she.c b/test/wh_test_she.c index 3e710e935..0f2d73a0b 100644 --- a/test/wh_test_she.c +++ b/test/wh_test_she.c @@ -567,9 +567,7 @@ static int wh_ClientServer_MemThreadTest(void) whNvmContext nvm[1] = {{0}}; /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; whServerSheContext she[1]; memset(she, 0, sizeof(she)); @@ -585,7 +583,7 @@ static int wh_ClientServer_MemThreadTest(void) WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf)); WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init()); - WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId)); + WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID)); _whClientServerThreadTest(c_conf, s_conf); diff --git a/test/wh_test_wolfcrypt_test.c b/test/wh_test_wolfcrypt_test.c index fbe74d58c..f1db0ca7c 100644 --- a/test/wh_test_wolfcrypt_test.c +++ b/test/wh_test_wolfcrypt_test.c @@ -240,9 +240,7 @@ static int wh_ClientServer_MemThreadTest(void) whNvmContext nvm[1] = {{0}}; /* Crypto context */ - whServerCryptoContext crypto[1] = {{ - .devId = INVALID_DEVID, - }}; + whServerCryptoContext crypto[1] = {0}; whServerConfig s_conf[1] = {{ .comm_config = cs_conf, @@ -254,7 +252,7 @@ static int wh_ClientServer_MemThreadTest(void) ret = wolfCrypt_Init(); if (ret == 0) { - ret = wc_InitRng_ex(crypto->rng, NULL, crypto->devId); + ret = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID); if (ret != 0) { WH_ERROR_PRINT("Failed to initialize wolfCrypt rng: %d\n", ret); } diff --git a/wolfhsm/wh_server.h b/wolfhsm/wh_server.h index 19600ef8c..676dc3c65 100644 --- a/wolfhsm/wh_server.h +++ b/wolfhsm/wh_server.h @@ -66,8 +66,6 @@ typedef struct whServerContext_t whServerContext; #ifndef WOLFHSM_CFG_NO_CRYPTO typedef struct whServerCryptoContext { - int devId; - int defaultDevId; #ifndef WC_NO_RNG WC_RNG rng[1]; #endif @@ -162,6 +160,8 @@ struct whServerContext_t { whCommServer comm[1]; #ifndef WOLFHSM_CFG_NO_CRYPTO whServerCryptoContext* crypto; + int devId; + int defaultDevId; whKeyCacheContext localCache; /* Unified cache structure */ #ifdef WOLFHSM_CFG_SHE_EXTENSION whServerSheContext* she; From c72c40caa332182ee7a207f259efe4ac96ab0b04 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 10 Feb 2026 14:53:08 -0500 Subject: [PATCH 4/4] Update docs/draft/crypto_affinity.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- docs/draft/crypto_affinity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/draft/crypto_affinity.md b/docs/draft/crypto_affinity.md index dfca6eadd..0c3f407bb 100644 --- a/docs/draft/crypto_affinity.md +++ b/docs/draft/crypto_affinity.md @@ -7,7 +7,7 @@ The crypto affinity feature allows a client to control and query whether the ser ```c enum WH_CRYPTO_AFFINITY_ENUM { WH_CRYPTO_AFFINITY_SW = 0, // Use software crypto (devId = INVALID_DEVID) - WH_CRYPTO_AFFINITY_HW = 1, // Attmept to use hardware crypto (devId = configured value) + WH_CRYPTO_AFFINITY_HW = 1, // Attempt to use hardware crypto (devId = configured value) }; ```