Skip to content

Commit c1e2cbd

Browse files
committed
Address comments
1 parent 153ba3a commit c1e2cbd

11 files changed

Lines changed: 264 additions & 55 deletions

docs/draft/crypto_affinity.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# SetCryptoAffinity Client API
1+
# Crypto Affinity Client API
22

3-
The SetCryptoAffinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations.
3+
The crypto affinity feature allows a client to control and query whether the server uses **software** or **hardware** cryptographic implementations.
44

55
## Affinity Values
66

77
```c
88
enum WH_CRYPTO_AFFINITY_ENUM {
99
WH_CRYPTO_AFFINITY_SW = 0, // Use software crypto (devId = INVALID_DEVID)
10-
WH_CRYPTO_AFFINITY_HW = 1, // Use hardware crypto (devId = configured value)
10+
WH_CRYPTO_AFFINITY_HW = 1, // Attmept to use hardware crypto (devId = configured value)
1111
};
1212
```
1313

14-
## Client API Functions
14+
## SetCryptoAffinity
1515

1616
### Blocking API (simplest)
1717

@@ -31,14 +31,43 @@ int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
3131
uint32_t* out_affinity);
3232
```
3333

34+
## GetCryptoAffinity
35+
36+
### Blocking API (simplest)
37+
38+
```c
39+
int wh_Client_GetCryptoAffinity(whClientContext* c,
40+
int32_t* out_rc, uint32_t* out_affinity);
41+
```
42+
43+
### Non-blocking (async) API
44+
45+
```c
46+
// Send request
47+
int wh_Client_GetCryptoAffinityRequest(whClientContext* c);
48+
49+
// Receive response
50+
int wh_Client_GetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
51+
uint32_t* out_affinity);
52+
```
53+
3454
## Usage Example
3555

3656
```c
3757
int32_t server_rc;
3858
uint32_t current_affinity;
3959

60+
// Query current crypto affinity
61+
int rc = wh_Client_GetCryptoAffinity(client,
62+
&server_rc,
63+
&current_affinity);
64+
65+
if (rc == WH_ERROR_OK && server_rc == WH_ERROR_OK) {
66+
// current_affinity contains the active affinity
67+
}
68+
4069
// Switch to software crypto
41-
int rc = wh_Client_SetCryptoAffinity(client,
70+
rc = wh_Client_SetCryptoAffinity(client,
4271
WH_CRYPTO_AFFINITY_SW,
4372
&server_rc,
4473
&current_affinity);
@@ -57,7 +86,7 @@ rc = wh_Client_SetCryptoAffinity(client,
5786
if (rc == WH_ERROR_OK) {
5887
if (server_rc == WH_ERROR_OK) {
5988
// Server is now using hardware crypto
60-
} else if (server_rc == WH_ERROR_BADCONFIG) {
89+
} else if (server_rc == WH_ERROR_NOTIMPL) {
6190
// HW crypto not available (server wasn't configured with a valid devId)
6291
}
6392
}
@@ -70,15 +99,22 @@ if (rc == WH_ERROR_OK) {
7099
| `rc` (function return) | Transport/communication errors |
71100
| `server_rc` (output parameter) | Server-side result |
72101

73-
### Server Return Codes
102+
### Server Return Codes (SetCryptoAffinity)
74103

75104
| Code | Description |
76105
|------|-------------|
77106
| `WH_ERROR_OK` | Affinity changed successfully |
78-
| `WH_ERROR_BADCONFIG` | HW requested but no HW crypto configured |
107+
| `WH_ERROR_NOTIMPL` | HW requested but no HW crypto configured, `WOLF_CRYPTO_CB` is not defined, or `WOLFHSM_CFG_NO_CRYPTO` is defined |
79108
| `WH_ERROR_BADARGS` | Invalid affinity value |
80109
| `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) |
110+
111+
### Server Return Codes (GetCryptoAffinity)
112+
113+
| Code | Description |
114+
|------|-------------|
115+
| `WH_ERROR_OK` | Affinity queried successfully |
116+
| `WH_ERROR_ABORTED` | Server crypto context is NULL |
117+
| `WH_ERROR_NOTIMPL` | Not implemented (returned when `WOLFHSM_CFG_NO_CRYPTO` is defined) |
82118

83119
## Server Behavior
84120

@@ -87,6 +123,8 @@ When affinity is set:
87123
| Affinity | Server Action |
88124
|----------|---------------|
89125
| `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) |
126+
| `WH_CRYPTO_AFFINITY_HW` | `server->crypto->devId = server->crypto->defaultDevId` (wolfCrypt uses registered crypto callback) |
127+
128+
When affinity is queried (Get), the server reads the current `devId` and returns the corresponding affinity value without modifying any state.
91129

92-
The `configDevId` is stored at server init from `config->devId`.
130+
The `defaultDevId` is stored at server init from `config->devId`.

src/wh_client.c

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ int wh_Client_SetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
439439
}
440440

441441
rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg);
442-
if (rc == 0) {
442+
if (rc == WH_ERROR_OK) {
443443
/* Validate response */
444444
if ((resp_group != WH_MESSAGE_GROUP_COMM) ||
445445
(resp_action != WH_MESSAGE_COMM_ACTION_SET_CRYPTO_AFFINITY) ||
@@ -471,14 +471,79 @@ int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity,
471471
rc = wh_Client_SetCryptoAffinityRequest(c, affinity);
472472
} while (rc == WH_ERROR_NOTREADY);
473473

474-
if (rc == 0) {
474+
if (rc == WH_ERROR_OK) {
475475
do {
476476
rc = wh_Client_SetCryptoAffinityResponse(c, out_rc, out_affinity);
477477
} while (rc == WH_ERROR_NOTREADY);
478478
}
479479
return rc;
480480
}
481481

482+
int wh_Client_GetCryptoAffinityRequest(whClientContext* c)
483+
{
484+
if (c == NULL) {
485+
return WH_ERROR_BADARGS;
486+
}
487+
488+
return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_COMM,
489+
WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY,
490+
0, NULL);
491+
}
492+
493+
int wh_Client_GetCryptoAffinityResponse(whClientContext* c, int32_t* out_rc,
494+
uint32_t* out_affinity)
495+
{
496+
int rc = 0;
497+
whMessageCommGetCryptoAffinityResponse msg = {0};
498+
uint16_t resp_group = 0;
499+
uint16_t resp_action = 0;
500+
uint16_t resp_size = 0;
501+
502+
if (c == NULL) {
503+
return WH_ERROR_BADARGS;
504+
}
505+
506+
rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg);
507+
if (rc == 0) {
508+
/* Validate response */
509+
if ((resp_group != WH_MESSAGE_GROUP_COMM) ||
510+
(resp_action != WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY) ||
511+
(resp_size != sizeof(msg))) {
512+
/* Invalid message */
513+
rc = WH_ERROR_ABORTED;
514+
}
515+
else {
516+
/* Valid message */
517+
if (out_rc != NULL) {
518+
*out_rc = msg.rc;
519+
}
520+
if (out_affinity != NULL) {
521+
*out_affinity = msg.affinity;
522+
}
523+
}
524+
}
525+
return rc;
526+
}
527+
528+
int wh_Client_GetCryptoAffinity(whClientContext* c, int32_t* out_rc,
529+
uint32_t* out_affinity)
530+
{
531+
int rc = 0;
532+
if (c == NULL) {
533+
return WH_ERROR_BADARGS;
534+
}
535+
do {
536+
rc = wh_Client_GetCryptoAffinityRequest(c);
537+
} while (rc == WH_ERROR_NOTREADY);
538+
539+
if (rc == 0) {
540+
do {
541+
rc = wh_Client_GetCryptoAffinityResponse(c, out_rc, out_affinity);
542+
} while (rc == WH_ERROR_NOTREADY);
543+
}
544+
return rc;
545+
}
546+
482547

483548
int wh_Client_CommCloseRequest(whClientContext* c)
484549
{

src/wh_message_comm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ int wh_MessageComm_TranslateSetCryptoAffinityResponse(
105105
WH_T32(magic, dest, src, affinity);
106106
return 0;
107107
}
108+
109+
int wh_MessageComm_TranslateGetCryptoAffinityResponse(
110+
uint16_t magic, const whMessageCommGetCryptoAffinityResponse* src,
111+
whMessageCommGetCryptoAffinityResponse* dest)
112+
{
113+
if ((src == NULL) || (dest == NULL)) {
114+
return WH_ERROR_BADARGS;
115+
}
116+
WH_T32(magic, dest, src, rc);
117+
WH_T32(magic, dest, src, affinity);
118+
return 0;
119+
}

src/wh_server.c

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,8 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config)
7979
#ifndef WOLFHSM_CFG_NO_CRYPTO
8080
server->crypto = config->crypto;
8181
if (server->crypto != NULL) {
82-
#if defined(WOLF_CRYPTO_CB)
8382
server->crypto->devId = config->devId;
84-
server->crypto->configDevId = config->devId;
85-
#else
86-
server->crypto->devId = INVALID_DEVID;
87-
server->crypto->configDevId = INVALID_DEVID;
88-
#endif
83+
server->crypto->defaultDevId = config->devId;
8984
}
9085
#ifdef WOLFHSM_CFG_SHE_EXTENSION
9186
server->she = config->she;
@@ -253,7 +248,16 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
253248
whMessageCommSetCryptoAffinityRequest req = {0};
254249
whMessageCommSetCryptoAffinityResponse resp = {0};
255250

256-
wh_MessageComm_TranslateSetCryptoAffinityRequest(
251+
if (req_size != sizeof(whMessageCommSetCryptoAffinityRequest)) {
252+
resp.rc = WH_ERROR_ABORTED;
253+
254+
(void)wh_MessageComm_TranslateSetCryptoAffinityResponse(
255+
magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet);
256+
*out_resp_size = sizeof(resp);
257+
break;
258+
}
259+
260+
(void)wh_MessageComm_TranslateSetCryptoAffinityRequest(
257261
magic, (const whMessageCommSetCryptoAffinityRequest*)req_packet,
258262
&req);
259263

@@ -263,28 +267,27 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
263267
resp.affinity = WH_CRYPTO_AFFINITY_SW;
264268
}
265269
else {
266-
switch (req.affinity) {
267-
case WH_CRYPTO_AFFINITY_SW:
268-
server->crypto->devId = INVALID_DEVID;
270+
if (req.affinity == WH_CRYPTO_AFFINITY_SW) {
271+
server->crypto->devId = INVALID_DEVID;
272+
resp.rc = WH_ERROR_OK;
273+
}
274+
else if (req.affinity == WH_CRYPTO_AFFINITY_HW) {
275+
/* If the devId the server was configured with is valid then
276+
* switch back to it. The devId is used to call the appropriate
277+
* callback to utilize HW crypto */
278+
if (server->crypto->defaultDevId != INVALID_DEVID) {
279+
server->crypto->devId = server->crypto->defaultDevId;
269280
resp.rc = WH_ERROR_OK;
270-
break;
271-
case WH_CRYPTO_AFFINITY_HW:
272-
#ifdef WOLF_CRYPTO_CB
273-
if (server->crypto->configDevId != INVALID_DEVID) {
274-
server->crypto->devId = server->crypto->configDevId;
275-
resp.rc = WH_ERROR_OK;
276-
}
277-
else {
278-
resp.rc = WH_ERROR_BADCONFIG;
279-
}
280-
break;
281-
#else
281+
}
282+
/* If the server was not configured with a valid devId
283+
* Then we are unable to use HW crypto. Return error back
284+
* back to the client */
285+
else {
282286
resp.rc = WH_ERROR_NOTIMPL;
283-
break;
284-
#endif
285-
default:
286-
resp.rc = WH_ERROR_BADARGS;
287-
break;
287+
}
288+
}
289+
else {
290+
resp.rc = WH_ERROR_BADARGS;
288291
}
289292
resp.affinity = (server->crypto->devId == INVALID_DEVID)
290293
? WH_CRYPTO_AFFINITY_SW
@@ -295,11 +298,35 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
295298
resp.affinity = WH_CRYPTO_AFFINITY_SW;
296299
#endif
297300

298-
wh_MessageComm_TranslateSetCryptoAffinityResponse(
301+
(void)wh_MessageComm_TranslateSetCryptoAffinityResponse(
299302
magic, &resp, (whMessageCommSetCryptoAffinityResponse*)resp_packet);
300303
*out_resp_size = sizeof(resp);
301304
}; break;
302305

306+
case WH_MESSAGE_COMM_ACTION_GET_CRYPTO_AFFINITY: {
307+
whMessageCommGetCryptoAffinityResponse resp = {0};
308+
309+
#ifndef WOLFHSM_CFG_NO_CRYPTO
310+
if (server->crypto == NULL) {
311+
resp.rc = WH_ERROR_ABORTED;
312+
resp.affinity = WH_CRYPTO_AFFINITY_SW;
313+
}
314+
else {
315+
resp.rc = WH_ERROR_OK;
316+
resp.affinity = (server->crypto->devId == INVALID_DEVID)
317+
? WH_CRYPTO_AFFINITY_SW
318+
: WH_CRYPTO_AFFINITY_HW;
319+
}
320+
#else
321+
resp.rc = WH_ERROR_NOTIMPL;
322+
resp.affinity = WH_CRYPTO_AFFINITY_SW;
323+
#endif
324+
325+
wh_MessageComm_TranslateGetCryptoAffinityResponse(
326+
magic, &resp, (whMessageCommGetCryptoAffinityResponse*)resp_packet);
327+
*out_resp_size = sizeof(resp);
328+
}; break;
329+
303330
case WH_MESSAGE_COMM_ACTION_CLOSE:
304331
{
305332
/* No message */

test/wh_test_check_struct_padding.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ whMessageCommInitResponse whMessageCommInitResponse_test;
3333
whMessageCommInfoResponse whMessageCommInfoResponse_test;
3434
whMessageCommSetCryptoAffinityRequest whMessageCommSetCryptoAffinityRequest_test;
3535
whMessageCommSetCryptoAffinityResponse whMessageCommSetCryptoAffinityResponse_test;
36+
whMessageCommGetCryptoAffinityResponse whMessageCommGetCryptoAffinityResponse_test;
3637

3738
#include "wolfhsm/wh_message_customcb.h"
3839
whMessageCustomCb_Request whMessageCustomCb_Request_test;

0 commit comments

Comments
 (0)