-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
31452b2
Implement HW/SW crypto affinity
AlexLanzano 9f9cce0
Address comments
AlexLanzano ed4a670
Move devId from crypto context into server context
AlexLanzano 61fe117
Update docs/draft/crypto_affinity.md
AlexLanzano d341955
Refactor to move crypto affinity state to client and add affinity fie…
bigbrett fecb52f
Implement request/response functions for aes cbc. Update crypto affin…
AlexLanzano 78735dd
Address Brett's comments
AlexLanzano f45d111
rename defaultDevId back to devId. Address copilot comments
AlexLanzano dcbe3db
Fix SHE build
AlexLanzano a2e05f5
Address copilot comments
AlexLanzano dfbc8af
- Fix async AES CBC IV handling for streaming CBC
bigbrett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_HW after wh_Client_Init() */ | ||
| wh_Client_GetCryptoAffinity(client, &affinity); | ||
| /* affinity == WH_CRYPTO_AFFINITY_HW */ | ||
|
|
||
| /* 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 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 use software implementation */ | ||
| } | ||
|
|
||
| /* Switch back to hardware crypto */ | ||
| wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_HW); | ||
| /* Subsequent crypto operations request HW acceleration */ | ||
| ``` | ||
|
|
||
| ## Server Behavior | ||
|
|
||
| When the server receives a crypto request, it reads the affinity field from the generic crypto request header and selects the appropriate `devId`: | ||
|
|
||
| | Affinity in Request | Server Action | | ||
| |---------------------|---------------| | ||
| | `WH_CRYPTO_AFFINITY_SW` | Uses `INVALID_DEVID` (wolfCrypt software implementation) | | ||
| | `WH_CRYPTO_AFFINITY_HW` | Uses `server->devId` if valid, otherwise falls back to `INVALID_DEVID` | | ||
|
|
||
| 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. | ||
|
|
||
| ## Protocol Details | ||
|
|
||
| Affinity is transmitted in the `affinity` field of `whMessageCrypto_GenericRequestHeader`, which is included at the start of every crypto request message. This means: | ||
|
|
||
| - Each crypto operation independently specifies its desired affinity | ||
| - Multiple clients can use different affinities concurrently without interference | ||
| - No server-side affinity state is maintained per-client | ||
| - Changing affinity has zero latency (no communication overhead) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.