|
| 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 | + ¤t_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 | + ¤t_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`. |
0 commit comments