Skip to content

Commit 40549bd

Browse files
authored
Merge pull request #281 from AlexLanzano/crypto-hw-sw-affinity
Implement HW/SW crypto affinity
2 parents 83cfd06 + dfbc8af commit 40549bd

32 files changed

+1408
-459
lines changed

benchmark/wh_bench.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex)
10711071

10721072
#ifndef WOLFHSM_CFG_NO_CRYPTO
10731073
/* Crypto context */
1074-
whServerCryptoContext crypto[1] = {{
1075-
.devId = INVALID_DEVID,
1076-
}};
1074+
whServerCryptoContext crypto[1] = {0};
10771075
#endif
10781076

10791077
/* Set up server configuration with NVM and crypto */
@@ -1109,7 +1107,7 @@ int wh_Bench_ClientServer_Posix(int transport, int moduleIndex)
11091107
}
11101108

11111109
/* Initialize RNG */
1112-
ret = wc_InitRng_ex(crypto->rng, NULL, crypto->devId);
1110+
ret = wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID);
11131111
if (ret != 0) {
11141112
WH_BENCH_PRINTF("Failed to initialize RNG: %d\n", ret);
11151113
wolfCrypt_Cleanup();

docs/draft/crypto_affinity.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Crypto Affinity Client API
2+
3+
The crypto affinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations on a per-request basis.
4+
5+
Affinity is stored as **client-local state** and is transmitted to the server in every crypto request message header. There is no dedicated round-trip required to change affinity -- setting it is instantaneous and takes effect on the next crypto operation. Affinity persists for all subsequent requests once changed.
6+
7+
## Affinity Values
8+
9+
```c
10+
enum WH_CRYPTO_AFFINITY_ENUM {
11+
WH_CRYPTO_AFFINITY_HW = 0, // Attempt to use hardware crypto (devId = configured value)
12+
WH_CRYPTO_AFFINITY_SW = 1, // Use software crypto (devId = INVALID_DEVID)
13+
};
14+
```
15+
16+
The default affinity after client initialization is `WH_CRYPTO_AFFINITY_HW`.
17+
18+
## API
19+
20+
### SetCryptoAffinity
21+
22+
```c
23+
int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity);
24+
```
25+
26+
Sets the client's crypto affinity. This is a **local operation** that does not communicate with the server. The new affinity value will be included in all subsequent crypto request messages.
27+
28+
**Parameters:**
29+
- `c` -- Client context
30+
- `affinity` -- `WH_CRYPTO_AFFINITY_SW` or `WH_CRYPTO_AFFINITY_HW`
31+
32+
**Returns:**
33+
- `WH_ERROR_OK` -- Affinity set successfully
34+
- `WH_ERROR_BADARGS` -- NULL context or invalid affinity value
35+
36+
### GetCryptoAffinity
37+
38+
```c
39+
int wh_Client_GetCryptoAffinity(whClientContext* c, uint32_t* out_affinity);
40+
```
41+
42+
Retrieves the client's current crypto affinity. This is a **local operation** that does not communicate with the server.
43+
44+
**Parameters:**
45+
- `c` -- Client context
46+
- `out_affinity` -- Pointer to receive the current affinity value
47+
48+
**Returns:**
49+
- `WH_ERROR_OK` -- Affinity retrieved successfully
50+
- `WH_ERROR_BADARGS` -- NULL context or NULL output pointer
51+
52+
## Usage Example
53+
54+
```c
55+
uint32_t affinity;
56+
57+
/* Default affinity is WH_CRYPTO_AFFINITY_HW after wh_Client_Init() */
58+
wh_Client_GetCryptoAffinity(client, &affinity);
59+
/* affinity == WH_CRYPTO_AFFINITY_HW */
60+
61+
/* Perform a crypto operation -- affinity is sent in the request header */
62+
wc_AesCbcEncrypt(&aes, out, in, len);
63+
/* If server has a valid devId, hardware crypto callback is used */
64+
65+
/* Switch to software crypto -- takes effect immediately, no round-trip */
66+
int rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW);
67+
if (rc == WH_ERROR_OK) {
68+
/* All subsequent crypto operations will use software implementation */
69+
}
70+
71+
/* Switch back to hardware crypto */
72+
wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_HW);
73+
/* Subsequent crypto operations request HW acceleration */
74+
```
75+
76+
## Server Behavior
77+
78+
When the server receives a crypto request, it reads the affinity field from the generic crypto request header and selects the appropriate `devId`:
79+
80+
| Affinity in Request | Server Action |
81+
|---------------------|---------------|
82+
| `WH_CRYPTO_AFFINITY_SW` | Uses `INVALID_DEVID` (wolfCrypt software implementation) |
83+
| `WH_CRYPTO_AFFINITY_HW` | Uses `server->devId` if valid, otherwise falls back to `INVALID_DEVID` |
84+
85+
The `devId` is configured at server initialization from `config->devId`. If the server was not configured with a valid hardware `devId`, hardware affinity requests will silently fall back to software crypto.
86+
87+
## Protocol Details
88+
89+
Affinity is transmitted in the `affinity` field of `whMessageCrypto_GenericRequestHeader`, which is included at the start of every crypto request message. This means:
90+
91+
- Each crypto operation independently specifies its desired affinity
92+
- Multiple clients can use different affinities concurrently without interference
93+
- No server-side affinity state is maintained per-client
94+
- Changing affinity has zero latency (no communication overhead)

docs/src-ja/chapter03.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ whNvmContext nvmCtx = {0};
171171
wh_Nvm_Init(&nvmCtx, &whNvmConfig);
172172

173173
/* 手順3: 暗号コンテキスト構造体の割り当てと初期化 */
174-
whServerCryptoContext cryptoCtx {
175-
.devID = INVALID_DEVID; /* あるいは、カスタム暗号コールバックdevIDを設定 */
176-
};
174+
whServerCryptoContext cryptoCtx = {0};
177175

178176
/* サーバー設定の割り当てと初期化 */
179177
whServerConfig serverCfg = {
180178
.comm = commServerCfg,
181179
.nvm = nvmCtx,
182-
.crypto = cryptoCtx,
180+
.crypto = &cryptoCtx,
181+
.devId = INVALID_DEVID, /* あるいは、カスタム暗号コールバックdevIDを設定 */
183182
};
184183

185184
/* 手順4: wolfCryptの初期化 */

docs/src/chapter03.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,14 @@ whNvmContext nvmCtx = {0};
169169
wh_Nvm_Init(&nvmCtx, &whNvmConfig);
170170

171171
/* Step 3: Allocate and initialize a crypto context structure */
172-
whServerCryptoContext cryptoCtx {
173-
.devID = INVALID_DEVID; /* or set to custom crypto callback devID */
174-
};
172+
whServerCryptoContext cryptoCtx = {0};
175173

176174
/* Allocate and initialize the Server configuration*/
177175
whServerConfig serverCfg = {
178176
.comm = commServerCfg,
179177
.nvm = nvmCtx,
180-
.crypto = cryptoCtx,
178+
.crypto = &cryptoCtx,
179+
.devId = INVALID_DEVID, /* or set to custom crypto callback devID */
181180
};
182181

183182
/* Step 4: Initialize wolfCrypt*/

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,7 @@ int main(int argc, char** argv)
416416
}
417417
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
418418
/* Crypto context */
419-
whServerCryptoContext crypto[1] = {{
420-
.devId = INVALID_DEVID,
421-
}};
419+
whServerCryptoContext crypto[1] = {0};
422420

423421
#if defined(WOLFHSM_CFG_SHE_EXTENSION)
424422
whServerSheContext she[1] = {{0}};
@@ -452,11 +450,12 @@ int main(int argc, char** argv)
452450
wh_Utils_Hexdump("Context 4: Server HW RNG:\n", buffer, sizeof(buffer));
453451

454452
/* Context 5: Set default server crypto to use cryptocb */
455-
crypto->devId = HW_DEV_ID;
456-
WOLFHSM_CFG_PRINTF("Context 5: Setting up default server crypto with devId=%d\n",
457-
crypto->devId);
453+
s_conf->devId = HW_DEV_ID;
454+
WOLFHSM_CFG_PRINTF(
455+
"Context 5: Setting up default server crypto with devId=%d\n",
456+
s_conf->devId);
458457

459-
rc = wc_InitRng_ex(crypto->rng, NULL, crypto->devId);
458+
rc = wc_InitRng_ex(crypto->rng, NULL, s_conf->devId);
460459
if (rc != 0) {
461460
WOLFHSM_CFG_PRINTF("Failed to wc_InitRng_ex: %d\n", rc);
462461
return rc;

src/wh_client.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ int wh_Client_CommInfo(whClientContext* c,
410410
return rc;
411411
}
412412

413+
int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity)
414+
{
415+
if (c == NULL) {
416+
return WH_ERROR_BADARGS;
417+
}
418+
if (affinity != WH_CRYPTO_AFFINITY_SW &&
419+
affinity != WH_CRYPTO_AFFINITY_HW) {
420+
return WH_ERROR_BADARGS;
421+
}
422+
c->cryptoAffinity = affinity;
423+
return WH_ERROR_OK;
424+
}
425+
426+
int wh_Client_GetCryptoAffinity(whClientContext* c, uint32_t* out_affinity)
427+
{
428+
if (c == NULL || out_affinity == NULL) {
429+
return WH_ERROR_BADARGS;
430+
}
431+
*out_affinity = c->cryptoAffinity;
432+
return WH_ERROR_OK;
433+
}
434+
413435

414436
int wh_Client_CommCloseRequest(whClientContext* c)
415437
{

0 commit comments

Comments
 (0)