-
Notifications
You must be signed in to change notification settings - Fork 30
Implement HW/SW crypto affinity #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
31452b2
9f9cce0
ed4a670
61fe117
d341955
fecb52f
78735dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Crypto Affinity Client API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The crypto affinity feature allows a client to control whether the server uses **software** or **hardware** cryptographic implementations on a per-request basis. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## Affinity Values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enum WH_CRYPTO_AFFINITY_ENUM { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WH_CRYPTO_AFFINITY_HW = 0, // Attempt to use hardware crypto (devId = configured value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WH_CRYPTO_AFFINITY_SW = 1, // Use software crypto (devId = INVALID_DEVID) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The default affinity after client initialization is `WH_CRYPTO_AFFINITY_HW`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### SetCryptoAffinity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int wh_Client_SetCryptoAffinity(whClientContext* c, uint32_t affinity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **Parameters:** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `c` -- Client context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `affinity` -- `WH_CRYPTO_AFFINITY_SW` or `WH_CRYPTO_AFFINITY_HW` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **Returns:** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `WH_ERROR_OK` -- Affinity set successfully | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `WH_ERROR_BADARGS` -- NULL context or invalid affinity value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### GetCryptoAffinity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int wh_Client_GetCryptoAffinity(whClientContext* c, uint32_t* out_affinity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Retrieves the client's current crypto affinity. This is a **local operation** that does not communicate with the server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **Parameters:** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `c` -- Client context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `out_affinity` -- Pointer to receive the current affinity value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **Returns:** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `WH_ERROR_OK` -- Affinity retrieved successfully | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - `WH_ERROR_BADARGS` -- NULL context or NULL output pointer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## Usage Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t affinity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Default affinity is WH_CRYPTO_AFFINITY_SW after wh_Client_Init() */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wh_Client_GetCryptoAffinity(client, &affinity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* affinity == WH_CRYPTO_AFFINITY_SW */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Switch to hardware crypto -- takes effect immediately, no round-trip */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_HW); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rc == WH_ERROR_OK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* All subsequent crypto operations will request HW acceleration */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Perform a crypto operation -- affinity is sent in the request header */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wc_AesCbcEncrypt(&aes, out, in, len); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* If server has a valid devId, hardware crypto callback is used */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Switch back to software crypto */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Subsequent crypto operations use software implementation */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+57
to
+73
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Default affinity is WH_CRYPTO_AFFINITY_SW after wh_Client_Init() */ | |
| wh_Client_GetCryptoAffinity(client, &affinity); | |
| /* affinity == WH_CRYPTO_AFFINITY_SW */ | |
| /* Switch to hardware crypto -- takes effect immediately, no round-trip */ | |
| int rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_HW); | |
| if (rc == WH_ERROR_OK) { | |
| /* All subsequent crypto operations will request HW acceleration */ | |
| } | |
| /* Perform a crypto operation -- affinity is sent in the request header */ | |
| wc_AesCbcEncrypt(&aes, out, in, len); | |
| /* If server has a valid devId, hardware crypto callback is used */ | |
| /* Switch back to software crypto */ | |
| wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW); | |
| /* Subsequent crypto operations use software implementation */ | |
| /* Default affinity is WH_CRYPTO_AFFINITY_HW after wh_Client_Init() */ | |
| wh_Client_GetCryptoAffinity(client, &affinity); | |
| /* affinity == WH_CRYPTO_AFFINITY_HW */ | |
| /* Switch to software crypto -- takes effect immediately, no round-trip */ | |
| int rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW); | |
| if (rc == WH_ERROR_OK) { | |
| /* All subsequent crypto operations will request software implementation */ | |
| } | |
| /* Perform a crypto operation -- affinity is sent in the request header */ | |
| wc_AesCbcEncrypt(&aes, out, in, len); | |
| /* If server has a valid devId, hardware crypto callback is used when affinity is HW */ | |
| /* Switch back to hardware crypto */ | |
| wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_HW); | |
| /* Subsequent crypto operations will again request HW acceleration */ |
Uh oh!
There was an error while loading. Please reload this page.