Skip to content

Commit 55fe3ab

Browse files
committed
Implement HW/SW crypto affinity
1 parent 062a005 commit 55fe3ab

13 files changed

Lines changed: 821 additions & 10 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tools/testcertgen/*.der
99
*.code-workspace
1010
.vscode
1111
compile_commands.json
12+
.cache
1213

1314
# Static analysis
1415
tools/static-analysis/reports/

docs/draft/crypto_affinity.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# SetCryptoAffinity Client API
2+
3+
The SetCryptoAffinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations.
4+
5+
## Affinity Values
6+
7+
```c
8+
enum WH_CRYPTO_AFFINITY_ENUM {
9+
WH_CRYPTO_AFFINITY_SW = 0, // Use software crypto (devId = INVALID_DEVID)
10+
WH_CRYPTO_AFFINITY_HW = 1, // Use hardware crypto (devId = configured value)
11+
};
12+
```
13+
14+
## Client API Functions
15+
16+
### Blocking API (simplest)
17+
18+
```c
19+
int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity,
20+
int32_t* out_rc, uint32_t* out_affinity);
21+
```
22+
23+
### Non-blocking (async) API
24+
25+
```c
26+
// Send request
27+
int wh_Client_SetCryptoAffinityRequest(whClientContext* c, uint32_t affinity);
28+
29+
// Receive response
30+
int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
31+
uint32_t* out_affinity);
32+
```
33+
34+
## Usage Example
35+
36+
```c
37+
int32_t server_rc;
38+
uint32_t current_affinity;
39+
40+
// Switch to software crypto
41+
int rc = wh_Client_SetCryptoAffinity(client,
42+
WH_CRYPTO_AFFINITY_SW,
43+
&server_rc,
44+
&current_affinity);
45+
46+
if (rc == WH_ERROR_OK && server_rc == WH_ERROR_OK) {
47+
// Server is now using software crypto
48+
// current_affinity == WH_CRYPTO_AFFINITY_SW
49+
}
50+
51+
// Switch to hardware crypto
52+
rc = wh_Client_SetCryptoAffinity(client,
53+
WH_CRYPTO_AFFINITY_HW,
54+
&server_rc,
55+
&current_affinity);
56+
57+
if (rc == WH_ERROR_OK) {
58+
if (server_rc == WH_ERROR_OK) {
59+
// Server is now using hardware crypto
60+
} else if (server_rc == WH_ERROR_BADCONFIG) {
61+
// HW crypto not available (server wasn't configured with a valid devId)
62+
}
63+
}
64+
```
65+
66+
## Return Values
67+
68+
| Value | Description |
69+
|-------|-------------|
70+
| `rc` (function return) | Transport/communication errors |
71+
| `server_rc` (output parameter) | Server-side result |
72+
73+
### Server Return Codes
74+
75+
| Code | Description |
76+
|------|-------------|
77+
| `WH_ERROR_OK` | Affinity changed successfully |
78+
| `WH_ERROR_BADCONFIG` | HW requested but no HW crypto configured |
79+
| `WH_ERROR_BADARGS` | Invalid affinity value |
80+
| `WH_ERROR_ABORTED` | Server crypto context is NULL |
81+
| `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) |
82+
83+
## Server Behavior
84+
85+
When affinity is set:
86+
87+
| Affinity | Server Action |
88+
|----------|---------------|
89+
| `WH_CRYPTO_AFFINITY_SW` | `server->crypto->devId = INVALID_DEVID` (wolfCrypt uses software) |
90+
| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->configDevId` (wolfCrypt uses registered crypto callback) |
91+
92+
The `configDevId` is stored at server init from `config->devId`.

src/wh_client.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,75 @@ int wh_Client_CommInfo(whClientContext* c,
414414
return rc;
415415
}
416416

417+
int wh_Client_SetCryptoAffinityRequest(whClientContext* c, uint32_t affinity)
418+
{
419+
whMessageCommSetCryptoAffinityRequest msg = {0};
420+
421+
if (c == NULL) {
422+
return WH_ERROR_BADARGS;
423+
}
424+
425+
msg.affinity = affinity;
426+
427+
return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_COMM,
428+
WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY,
429+
sizeof(msg), &msg);
430+
}
431+
432+
int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
433+
uint32_t* out_affinity)
434+
{
435+
int rc = 0;
436+
whMessageCommSetCryptoAffinityResponse msg = {0};
437+
uint16_t resp_group = 0;
438+
uint16_t resp_action = 0;
439+
uint16_t resp_size = 0;
440+
441+
if (c == NULL) {
442+
return WH_ERROR_BADARGS;
443+
}
444+
445+
rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg);
446+
if (rc == 0) {
447+
/* Validate response */
448+
if ((resp_group != WH_MESSAGE_GROUP_COMM) ||
449+
(resp_action != WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY) ||
450+
(resp_size != sizeof(msg))) {
451+
/* Invalid message */
452+
rc = WH_ERROR_ABORTED;
453+
}
454+
else {
455+
/* Valid message */
456+
if (out_rc != NULL) {
457+
*out_rc = msg.rc;
458+
}
459+
if (out_affinity != NULL) {
460+
*out_affinity = msg.affinity;
461+
}
462+
}
463+
}
464+
return rc;
465+
}
466+
467+
int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity,
468+
int32_t* out_rc, uint32_t* out_affinity)
469+
{
470+
int rc = 0;
471+
if (c == NULL) {
472+
return WH_ERROR_BADARGS;
473+
}
474+
do {
475+
rc = wh_Client_SetCryptoAffinityRequest(c, affinity);
476+
} while (rc == WH_ERROR_NOTREADY);
477+
478+
if (rc == 0) {
479+
do {
480+
rc = wh_Client_SetCryptoAffinityResponse(c, out_rc, out_affinity);
481+
} while (rc == WH_ERROR_NOTREADY);
482+
}
483+
return rc;
484+
}
485+
417486

418487
int wh_Client_CommCloseRequest(whClientContext* c)
419488
{

src/wh_message_comm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,25 @@ int wh_MessageComm_TranslateInfoResponse(uint16_t magic,
8383
return 0;
8484
}
8585

86+
int wh_MessageComm_TranslateSetCryptoAffinityRequest(
87+
uint16_t magic, const whMessageCommSetCryptoAffinityRequest* src,
88+
whMessageCommSetCryptoAffinityRequest* dest)
89+
{
90+
if ((src == NULL) || (dest == NULL)) {
91+
return WH_ERROR_BADARGS;
92+
}
93+
WH_T32(magic, dest, src, affinity);
94+
return 0;
95+
}
8696

97+
int wh_MessageComm_TranslateSetCryptoAffinityResponse(
98+
uint16_t magic, const whMessageCommSetCryptoAffinityResponse* src,
99+
whMessageCommSetCryptoAffinityResponse* dest)
100+
{
101+
if ((src == NULL) || (dest == NULL)) {
102+
return WH_ERROR_BADARGS;
103+
}
104+
WH_T32(magic, dest, src, rc);
105+
WH_T32(magic, dest, src, affinity);
106+
return 0;
107+
}

src/wh_server.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config)
8181
if (server->crypto != NULL) {
8282
#if defined(WOLF_CRYPTO_CB)
8383
server->crypto->devId = config->devId;
84+
server->crypto->configDevId = config->devId;
8485
#else
8586
server->crypto->devId = INVALID_DEVID;
87+
server->crypto->configDevId = INVALID_DEVID;
8688
#endif
8789
}
8890
#ifdef WOLFHSM_CFG_SHE_EXTENSION
@@ -266,6 +268,56 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
266268
*out_resp_size = sizeof(resp);
267269
}; break;
268270

271+
case WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY: {
272+
whMessageCommSetCryptoAffinityRequest req = {0};
273+
whMessageCommSetCryptoAffinityResponse resp = {0};
274+
275+
wh_MessageComm_TranslateSetCryptoAffinityRequest(
276+
magic, (const whMessageCommSetCryptoAffinityRequest*)req_packet,
277+
&req);
278+
279+
#ifndef WOLFHSM_CFG_NO_CRYPTO
280+
if (server->crypto == NULL) {
281+
resp.rc = WH_ERROR_ABORTED;
282+
resp.affinity = WH_CRYPTO_AFFINITY_SW;
283+
}
284+
else {
285+
switch (req.affinity) {
286+
case WH_CRYPTO_AFFINITY_SW:
287+
server->crypto->devId = INVALID_DEVID;
288+
resp.rc = WH_ERROR_OK;
289+
break;
290+
case WH_CRYPTO_AFFINITY_HW:
291+
#ifdef WOLF_CRYPTO_CB
292+
if (server->crypto->configDevId != INVALID_DEVID) {
293+
server->crypto->devId = server->crypto->configDevId;
294+
resp.rc = WH_ERROR_OK;
295+
}
296+
else {
297+
resp.rc = WH_ERROR_BADCONFIG;
298+
}
299+
break;
300+
#else
301+
resp.rc = WH_ERROR_NOTIMPL;
302+
break;
303+
#endif
304+
default:
305+
resp.rc = WH_ERROR_BADARGS;
306+
break;
307+
}
308+
resp.affinity = (server->crypto->devId == INVALID_DEVID)
309+
? WH_CRYPTO_AFFINITY_SW
310+
: WH_CRYPTO_AFFINITY_HW;
311+
}
312+
#else
313+
resp.rc = WH_ERROR_NOTIMPL;
314+
resp.affinity = WH_CRYPTO_AFFINITY_SW;
315+
#endif
316+
317+
wh_MessageComm_TranslateSetCryptoAffinityResponse(
318+
magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet);
319+
*out_resp_size = sizeof(resp);
320+
}; break;
269321

270322
case WH_MESSAGE_COMM_ACTION_CLOSE:
271323
{

test/wh_test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "wh_test_log.h"
4343
#include "wh_test_lock.h"
4444
#include "wh_test_posix_threadsafe_stress.h"
45+
#include "wh_test_crypto_affinity.h"
4546

4647
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER)
4748
#include "wh_test_cert.h"
@@ -93,6 +94,10 @@ int whTest_Unit(void)
9394
/* Crypto Tests */
9495
WH_TEST_ASSERT(0 == whTest_Crypto());
9596

97+
#ifdef WOLF_CRYPTO_CB
98+
WH_TEST_ASSERT(0 == whTest_CryptoAffinity());
99+
#endif
100+
96101
#if defined(WOLFHSM_CFG_SERVER_IMG_MGR) && !defined(WOLFHSM_CFG_NO_CRYPTO)
97102
/* Image Manager Tests */
98103
WH_TEST_ASSERT(0 == whTest_ServerImgMgr(WH_NVM_TEST_BACKEND_FLASH));

test/wh_test_check_struct_padding.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727

2828

2929
#include "wolfhsm/wh_message_comm.h"
30-
whMessageComm_ErrorResponse whMessageComm_ErrorResponse_test;
31-
whMessageCommInitRequest whMessageCommInitRequest_test;
32-
whMessageCommInitResponse whMessageCommInitResponse_test;
33-
whMessageCommInfoResponse whMessageCommInfoResponse_test;
30+
whMessageComm_ErrorResponse whMessageComm_ErrorResponse_test;
31+
whMessageCommInitRequest whMessageCommInitRequest_test;
32+
whMessageCommInitResponse whMessageCommInitResponse_test;
33+
whMessageCommInfoResponse whMessageCommInfoResponse_test;
34+
whMessageCommSetCryptoAffinityRequest whMessageCommSetCryptoAffinityRequest_test;
35+
whMessageCommSetCryptoAffinityResponse whMessageCommSetCryptoAffinityResponse_test;
3436

3537
#include "wolfhsm/wh_message_customcb.h"
3638
whMessageCustomCb_Request whMessageCustomCb_Request_test;

0 commit comments

Comments
 (0)