|
| 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) |
0 commit comments