Skip to content

Commit edb28d0

Browse files
committed
docs: updates documentation with nvm enforcing and key revocation
1 parent b7b7193 commit edb28d0

File tree

3 files changed

+276
-1
lines changed

3 files changed

+276
-1
lines changed

docs/src/appendix01.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,178 @@
11
# wolfHSM API reference
2+
3+
## Key Revocation
4+
5+
### wh_Client_KeyRevokeRequest
6+
7+
Send a key revocation request to the server (non-blocking).
8+
9+
This function prepares and sends a revoke request for the specified key ID. It
10+
returns after the request is sent; use `wh_Client_KeyRevokeResponse()` to
11+
retrieve the result.
12+
13+
Parameters:
14+
15+
- `c`: Client context.
16+
- `keyId`: Key ID to revoke.
17+
18+
Return values:
19+
20+
- `WH_ERROR_OK` on successful request send.
21+
- A negative error code on failure.
22+
23+
Error codes:
24+
25+
- `WH_ERROR_BADARGS` if `c` is NULL or `keyId` is invalid.
26+
- Propagates comm layer errors on send failure.
27+
28+
### wh_Client_KeyRevokeResponse
29+
30+
Receive a key revocation response.
31+
32+
This function polls for the revoke response and returns `WH_ERROR_NOTREADY`
33+
until the server reply is available.
34+
35+
Parameters:
36+
37+
- `c`: Client context.
38+
39+
Return values:
40+
41+
- `WH_ERROR_OK` on success.
42+
- `WH_ERROR_NOTREADY` if the response has not arrived.
43+
- A negative error code on failure.
44+
45+
Error codes:
46+
47+
- `WH_ERROR_BADARGS` if `c` is NULL.
48+
- Server error codes such as `WH_ERROR_NOTFOUND`.
49+
50+
### wh_Client_KeyRevoke
51+
52+
Revoke a key using a blocking request/response.
53+
54+
This helper sends a revoke request and waits for the response.
55+
56+
Parameters:
57+
58+
- `c`: Client context.
59+
- `keyId`: Key ID to revoke.
60+
61+
Return values:
62+
63+
- `WH_ERROR_OK` on success.
64+
- A negative error code on failure.
65+
66+
Error codes:
67+
68+
- Any error code returned by `wh_Client_KeyRevokeRequest()` or
69+
`wh_Client_KeyRevokeResponse()`.
70+
71+
### wh_Server_KeystoreRevokeKey
72+
73+
Revoke a key by updating its metadata.
74+
75+
This server-side function marks a key as non-modifiable and clears all usage
76+
flags. If the key exists in NVM, the metadata update is committed so the revoke
77+
state persists.
78+
79+
Parameters:
80+
81+
- `server`: Server context.
82+
- `keyId`: Key ID to revoke.
83+
84+
Return values:
85+
86+
- `WH_ERROR_OK` on success.
87+
- A negative error code on failure.
88+
89+
Error codes:
90+
91+
- `WH_ERROR_BADARGS` if parameters are invalid.
92+
- `WH_ERROR_NOTFOUND` if the key is missing.
93+
- Propagates NVM/storage errors (for example `WH_ERROR_NOSPACE`).
94+
95+
## NVM Access and Flag Controls
96+
97+
### whNvmFlags
98+
99+
Policy flags for NVM objects and keys.
100+
101+
Flags include `WH_NVM_FLAGS_NONMODIFIABLE`, `WH_NVM_FLAGS_NONDESTROYABLE`,
102+
`WH_NVM_FLAGS_NONEXPORTABLE`, and the usage policy flags `WH_NVM_FLAGS_USAGE_*`.
103+
If no usage flags are set, the key is not permitted for cryptographic use.
104+
105+
### wh_Nvm_AddObjectChecked
106+
107+
Add an NVM object with policy enforcement.
108+
109+
This function applies NVM policy checks (for example non-modifiable objects)
110+
before writing the object.
111+
112+
Parameters:
113+
114+
- `context`: NVM context.
115+
- `meta`: Metadata describing the object.
116+
- `data_len`: Length of object data.
117+
- `data`: Object data buffer.
118+
119+
Return values:
120+
121+
- `WH_ERROR_OK` on success.
122+
- A negative error code on failure.
123+
124+
Error codes:
125+
126+
- `WH_ERROR_BADARGS` if parameters are invalid.
127+
- `WH_ERROR_ACCESS` if the object is non-modifiable.
128+
- Propagates backend errors (for example `WH_ERROR_NOSPACE`).
129+
130+
### wh_Nvm_ReadChecked
131+
132+
Read an NVM object with policy enforcement.
133+
134+
This function applies NVM policy checks (for example non-exportable objects)
135+
before reading the object data.
136+
137+
Parameters:
138+
139+
- `context`: NVM context.
140+
- `id`: Object ID to read.
141+
- `offset`: Byte offset into the object.
142+
- `data_len`: Length of data to read.
143+
- `data`: Output buffer.
144+
145+
Return values:
146+
147+
- `WH_ERROR_OK` on success.
148+
- A negative error code on failure.
149+
150+
Error codes:
151+
152+
- `WH_ERROR_BADARGS` if parameters are invalid.
153+
- `WH_ERROR_ACCESS` if the object is non-exportable.
154+
- `WH_ERROR_NOTFOUND` if the object does not exist.
155+
156+
### wh_Nvm_DestroyObjectsChecked
157+
158+
Destroy NVM objects with policy enforcement.
159+
160+
This function applies NVM policy checks (for example non-destroyable objects)
161+
before erasing the objects.
162+
163+
Parameters:
164+
165+
- `context`: NVM context.
166+
- `list_count`: Number of IDs in the list.
167+
- `id_list`: Array of object IDs to destroy.
168+
169+
Return values:
170+
171+
- `WH_ERROR_OK` on success.
172+
- A negative error code on failure.
173+
174+
Error codes:
175+
176+
- `WH_ERROR_BADARGS` if parameters are invalid.
177+
- `WH_ERROR_ACCESS` if any object is non-destroyable or non-modifiable.
178+
- `WH_ERROR_NOTFOUND` if a listed object is missing.

docs/src/chapter04.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ wolfHSM is a modular and extensible library designed to provide a secure and eff
1111
- [Comms Layer](#comms-layer)
1212
- [Non Volatile Memory](#non-volatile-memory)
1313
- [NVM Metadata](#nvm-metadata)
14+
- [NVM Access and Flags](#nvm-access-and-flags)
1415
- [NVM Architecture](#nvm-architecture)
1516
- [NVM Back-Ends](#nvm-back-ends)
1617
- [Key Management](#key-management)
18+
- [Key Revocation](#key-revocation)
1719
- [Cryptographic Operations](#cryptographic-operations)
1820
- [Hardware Cryptography Support](#hardware-cryptography-support)
1921

@@ -222,6 +224,18 @@ typedef struct {
222224
Length (whNvmSize len): The length of the data associated with the object, in bytes.
223225
- Label (`uint8_t label[]`): A human-readable label or name for the object.
224226

227+
### NVM Access and Flags
228+
229+
NVM access controls are stored in the metadata for all objects and are returned by `wh_Nvm_GetMetadata()` and related client APIs.
230+
231+
NVM flags provide object and key policy hints that are enforced by the NVM library and keystore. Relevant flags include:
232+
233+
- `WH_NVM_FLAGS_NONMODIFIABLE`: Object cannot be modified and/or destroyed.
234+
- `WH_NVM_FLAGS_NONDESTROYABLE`: Object cannot be destroyed.
235+
- `WH_NVM_FLAGS_NONEXPORTABLE`: Object data cannot be read/exported.
236+
- `WH_NVM_FLAGS_USAGE_*`: Key usage policy flags for encrypt/decrypt/sign/verify/wrap/derive.
237+
- `WH_NVM_FLAGS_USAGE_ANY`: Allow all usage flags.
238+
225239
### NVM Architecture
226240

227241
The wolfHSM server follows the generic component architecture approach to handle Non-Volatile Memory (NVM) operations. The configuration is divided into generic and specific parts, allowing for flexibility and customization.
@@ -249,6 +263,21 @@ Currently, wolfHSM only supports one NVM back-end provider: the NVM flash module
249263

250264
The wolfHSM library provides comprehensive key management capabilities, including storing, loading, and exporting keys from non-volatile memory, caching of frequently used keys in RAM for fast access, and interacting with hardware-exclusive device keys. Keys are stored in non-volatile memory along side other NVM objects with corresponding access protections. wolfHSM will automatically load keys into the necessary cryptographic hardware when the key is selected for use with a specific consumer. More information on the key management API can be found in the [client library](./chapter05.md) and [API documentation](./appendix01.md) sections.
251265

266+
### Key Revocation
267+
268+
Key revocation provides a lightweight mechanism to invalidate a key without destroying its storage. When a key is revoked on the server, its metadata is updated by setting `WH_NVM_FLAGS_NONMODIFIABLE` and clearing all `WH_NVM_FLAGS_USAGE_*` bits. The key remains present in cache/NVM, but is no longer eligible for cryptographic operations that enforce usage flags.
269+
270+
Runtime behavior for revoked keys:
271+
272+
- Cryptographic operations that enforce usage flags return `WH_ERROR_USAGE` when the required usage bit is no longer set.
273+
- The `WH_NVM_FLAGS_NONMODIFIABLE` setting prevents further key metadata changes and blocks NVM modification or destruction checks.
274+
- Revocation does not automatically set `WH_NVM_FLAGS_NONEXPORTABLE`; export behavior remains controlled by those flags.
275+
276+
Persistence behavior:
277+
278+
- Revocation is committed to NVM for keys that already exist in NVM, so the revoked state persists across resets or power cycles.
279+
- If a key exists only in cache and has not been committed, the revoked state is limited to the cache lifetime.
280+
252281
## Cryptographic Operations
253282

254283
One of the defining features of wolfHSM is that it enables the client application to use the wolfCrypt API directly, but with the underlying cryptographic operations actually being executed on the HSM core. This is an incredibly powerful feature for a number of reasons:

docs/src/chapter05.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ The client library API is the primary mechanism through which users will interac
99
- [The Client Context](#the-client-context)
1010
- [Initializing the client context](#initializing-the-client-context)
1111
- [NVM Operations](#nvm-operations)
12+
- [NVM Access and Flags](#nvm-access-and-flags)
1213
- [Key Management](#key-management)
14+
- [Key Revocation](#key-revocation)
1315
- [Cryptography](#cryptography)
1416
- [AUTOSAR SHE API](#autosar-she-api)
1517

@@ -250,6 +252,10 @@ int wh_Client_NvmList(whClientContext* c,
250252
251253
For a full description of all the NVM API functions, please refer to the [API documentation](./appendix01.md).
252254
255+
## NVM Flags
256+
257+
NVM objects include flags in their metadata. Flags (such as `WH_NVM_FLAGS_NONMODIFIABLE`, `WH_NVM_FLAGS_NONDESTROYABLE`, and `WH_NVM_FLAGS_NONEXPORTABLE`) are enforced by the server NVM policy checks. Key usage flags (`WH_NVM_FLAGS_USAGE_*`) are enforced by the keystore during cryptographic operations.
258+
253259
## Key Management
254260
255261
Keys meant for use with wolfCrypt can be loaded into the HSM's keystore and optionally saved to NVM with the following APIs:
@@ -281,6 +287,70 @@ wh_Client_KeyErase(clientCtx, keyId);
281287
`wh_Client_KeyExport` will read the key contents out of the HSM back to the client.
282288
`wh_Client_KeyErase` will remove the indicated key from cache and erase it from NVM.
283289

290+
## Key Revocation
291+
292+
Key revocation updates key metadata to prevent further cryptographic use without destroying storage. Revocation clears all `WH_NVM_FLAGS_USAGE_*` bits and sets `WH_NVM_FLAGS_NONMODIFIABLE`. The revoked state is persisted when the key is already committed to NVM.
293+
294+
Creating a key with NVM usage flags:
295+
296+
```c
297+
int rc;
298+
uint16_t keyId = WH_KEYID_ERASED;
299+
byte label[WH_NVM_LABEL_LEN] = "app-signing";
300+
byte key[AES_128_KEY_SIZE] = { /* key bytes */ };
301+
whNvmFlags flags = WH_NVM_FLAGS_USAGE_SIGN | WH_NVM_FLAGS_NONEXPORTABLE;
302+
303+
rc = wh_Client_KeyCache(&clientCtx, flags, label, sizeof(label),
304+
key, sizeof(key), &keyId);
305+
if (rc == WH_ERROR_OK) {
306+
rc = wh_Client_KeyCommit(&clientCtx, keyId);
307+
}
308+
```
309+
310+
Revoking a key:
311+
312+
```c
313+
int rc;
314+
315+
rc = wh_Client_KeyRevoke(&clientCtx, keyId);
316+
if (rc != WH_ERROR_OK) {
317+
/* handle error */
318+
}
319+
```
320+
321+
Attempting to use a revoked key and handling failure:
322+
323+
```c
324+
int rc;
325+
Aes aes;
326+
byte iv[AES_BLOCK_SIZE] = {0};
327+
byte plain[AES_BLOCK_SIZE] = {0};
328+
byte cipher[AES_BLOCK_SIZE] = {0};
329+
330+
wc_AesInit(&aes, NULL, WOLFHSM_DEV_ID);
331+
wh_Client_AesSetKeyId(&aes, keyId);
332+
wc_AesSetIV(&aes, iv);
333+
334+
rc = wh_Client_AesCbc(&clientCtx, &aes, AES_ENCRYPTION,
335+
plain, sizeof(plain), cipher);
336+
if (rc == WH_ERROR_USAGE) {
337+
/* key revoked or usage not permitted */
338+
}
339+
wc_AesFree(&aes);
340+
```
341+
342+
Security notes:
343+
344+
- Set explicit usage flags for each key; avoid `WH_NVM_FLAGS_USAGE_ANY` unless required.
345+
- Use `WH_NVM_FLAGS_NONEXPORTABLE` for private keys and long-lived secrets.
346+
- Revoke keys on compromise and rotate to new key IDs rather than reusing revoked IDs.
347+
348+
Compatibility notes:
349+
350+
- Keys stored with `WH_NVM_FLAGS_NONE` (no usage flags) are treated as not permitted for cryptographic use and will return `WH_ERROR_USAGE`.
351+
- Keys committed to NVM retain revocation state across resets; cached-only keys do not persist after reset or eviction.
352+
- Keys cached via `wh_Client_KeyCache` are stored with `WH_NVM_ACCESS_ANY` on the server side.
353+
284354
## Cryptography
285355
286356
When using wolfCrypt in the client application, compatible crypto operations can be executed on the wolfHSM server by passing `WOLFHSM_DEV_ID` as the `devId` argument. The wolfHSM client must be initialized before using any wolfHSM remote crypto.
@@ -367,4 +437,3 @@ For CMAC operations that need to use cached keys, seperate wolfHSM specific func
367437

368438
## AUTOSAR SHE API
369439

370-

0 commit comments

Comments
 (0)