Skip to content

Commit 9d306f9

Browse files
committed
review comments
c syntax highlighting
1 parent 1b30073 commit 9d306f9

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

wolfSSL/src/chapter06.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Again, no dynamic memory is used for this structure since a maximum number of SS
5858
```c
5959
typedef struct packetInfo_st {
6060
char packetName[MAX_PACKETNAME_SZ + 1]; /* SSL name */
61-
Timeval timestamp; /* when it occured */
61+
Timeval timestamp; /* when it occurred */
6262
unsigned char value[MAX_VALUE_SZ]; /* if fits, it's here */
6363
unsigned char* bufferValue; /* otherwise here (non 0) */
6464
int valueSz; /* sz of value or buffer */
@@ -189,7 +189,7 @@ To use Atomic Record Layer callbacks, wolfSSL needs to be compiled using the [`-
189189

190190
## Crypto Callbacks (cryptocb)
191191

192-
The Crypto callback framework in wolfSSL/wolfCrypt enables users to override the default implementation of select cryptographic algorithms and provide their own custom implementations at runtime. The most common use case for crypto callbacks is to offload an algorithm to custom hardware acceleration, however it could also be used to add additional logging/introspection, retrieve keys from secure storage, to call crypto from another library for standards compliance reasons, or even to perform a remote procedure call.
192+
The Crypto callback framework in wolfSSL/wolfCrypt enables users to override the default implementation of select cryptographic algorithms and provide their own custom implementations at runtime. The most common use case for crypto callbacks is to offload an algorithm to custom hardware acceleration, however it could also be used to add additional logging/introspection, retrieve keys from secure storage, to call crypto from another library for standards compliance reasons, or even to perform a remote procedure call.
193193

194194
### Using Crypto callbacks
195195

@@ -210,18 +210,20 @@ To use crypto callbacks, the user must register a top-level cryptographic callba
210210

211211
To register a cryptographic callback function with wolfCrypt, use the `wc_CryptoCb_RegisterDevice` API. This takes a (`devId`), the callback function, and an optional user context that will be passed through to the callback when it is invoked.
212212

213-
```
213+
```c
214214
typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);
215215

216216
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId,
217217
CryptoDevCallbackFunc cb,
218218
void* ctx);
219219
```
220220
221+
Note: A registered `devId` can be any value other than `-2`, which is reserved for the special `INVALID_DEVID` enum value. Passing `devId == INVALID_DEVID` as an argument into a wolfCrypt API indicates that no callback should be invoked and to use the default internal implementation instead.
221222
222223
#### 3. Pass the `devId` as an argument to a wolfCrypt API function
223224
224-
For wolfCrypt API’s use the init functions that accept `devId` such as:
225+
For wolfCrypt API’s, use the init functions that accept `devId` such as:
226+
225227
```
226228
wc_InitRsaKey_ex
227229
wc_ecc_init_ex
@@ -231,11 +233,12 @@ wc_InitSha_ex
231233
wc_HmacInit
232234
wc_InitCmac_ex
233235
```
234-
This is not an exhaustive list. Please refer to the wolfCrypt API documentation to see if a particular algorithm supports crypto callbacks.
236+
237+
This will cause wolfCrypt to invoke the crypto callback in place of the default implementation. This is not an exhaustive API list. Please refer to the wolfCrypt API documentation to see if a particular algorithm supports crypto callbacks.
235238
236239
#### 4. (TLS only): associate the devId with a wolfSSL context
237240
To enable use of a crypto callback when using TLS, you must supply the `devId` arguments on initialization of a `WOLFSSL_CTX` or `WOLFSSL` struct.
238-
```
241+
```c
239242
wolfSSL_CTX_SetDevId(ctx, devId);
240243
wolfSSL_SetDevId(ssl, devId);
241244
```
@@ -258,7 +261,7 @@ The crypto callback should return zero on success, or a valid wolfCrypt error co
258261

259262
Here's a simplified example to illustrate this:
260263

261-
```
264+
```c
262265
int myCryptoCallback(int devId, wc_CryptoInfo* info, void* ctx)
263266
{
264267
int ret = CRYPTOCB_UNAVAILABLE;
@@ -290,7 +293,7 @@ Some of the simpler algo type unions, such as the RNG and seed unions, require n
290293
291294
Here is a simplified example for a complex algo type union with multiple levels of union decoding. The callback contains support for random number generation, as well as ECC key agreement, sign, and verify.
292295
293-
```
296+
```c
294297
int myCryptoCallback(int devId, wc_CryptoInfo* info, void* ctx)
295298
{
296299
int ret = CRYPTOCB_UNAVAILABLE;
@@ -328,15 +331,21 @@ int myCryptoCallback(int devId, wc_CryptoInfo* info, void* ctx)
328331

329332
### Handling the request
330333

331-
The data structure for each type of request generally contains a pointer and associated size for input and output data. Depending on the request it may include additional data including cryptographic keys, nonces, or further configuration. The crypto callback should operate on the input data and write relevant output data back to the appropriate variant of the algo type union. Writing data to a union variant that does not correspond to the algorithm type in question (e.g. using `info->cipher` when `info->algo_type == WC_ALGO_TYPE_RNG` is a memory error and can result in undefined behavior.
334+
The data structure for each type of request generally contains a pointer and associated size for input and output data. Depending on the request it may include additional data including cryptographic keys, nonces, or further configuration. The crypto callback should operate on the input data and write relevant output data back to the appropriate variant of the algo type union. Writing data to a union variant that does not correspond to the algorithm type in question (e.g. using `info->cipher`
335+
when `info->algo_type == WC_ALGO_TYPE_RNG` is a memory error and can result in undefined behavior.
336+
337+
### Troubleshooting
338+
339+
Some older compilers don't allow "anonymous inline aggregates", which wolfCrypt uses when defining the nested `wcCryptoInfo` anonymous unions in order to save space. To disable the use of anonymous inline aggregates, define the `HAVE_ANONYMOUS_INLINE_AGGREGATES` preprocessor macro as `0`. This will allow crypto callbacks to be used, but will dramatically increase the size of the `wcCryptoInfo` structure.
332340

333341

334342
### Examples
335343

336344
Full examples of crypto callbacks can be found in the following locations
337345

338-
VaultIC Crypto Callbacks: https://github.com/wolfSSL/wolfssl-examples/blob/master/ccb_vaultic/ccb_vaultic.c
339-
STSAFE-A100 ECC Crypto Callbacks: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/port/st/stsafe.c
340-
TPM 2.0 wolfTPM Crypto Callbacks: https://github.com/wolfSSL/wolfTPM/blob/master/src/tpm2_wrap.c
341-
Generic wolfCrypt tests: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/test/test.c
346+
* [VaultIC Crypto Callbacks](https://github.com/wolfSSL/wolfssl-examples/blob/master/ccb_vaultic/ccb_vaultic.c)
347+
* [STSAFE-A100 ECC Crypto Callbacks](https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/port/st/stsafe.c)
348+
* [TPM 2.0 wolfTPM Crypto Callbacks](https://github.com/wolfSSL/wolfTPM/blob/master/src/tpm2_cryptocb.c)
349+
* [Generic wolfCrypt tests](https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/test/test.c)
350+
342351

0 commit comments

Comments
 (0)