Functions
cryptocb.h File Reference

Go to the source code of this file.

Functions

int wc_CryptoCb_RegisterDevice (int devId, CryptoDevCallbackFunc cb, void *ctx)
 This function registers a unique device identifier (devID) and callback function for offloading crypto operations to external hardware such as Key Store, Secure Element, HSM, PKCS11 or TPM. More...
 
void wc_CryptoCb_UnRegisterDevice (int devId)
 This function un-registers a unique device identifier (devID) callback function. More...
 

Function Documentation

int wc_CryptoCb_RegisterDevice ( int  devId,
CryptoDevCallbackFunc  cb,
void *  ctx 
)

This function registers a unique device identifier (devID) and callback function for offloading crypto operations to external hardware such as Key Store, Secure Element, HSM, PKCS11 or TPM.

For STSAFE with Crypto Callbacks example see wolfcrypt/src/port/st/stsafe.c and the wolfSSL_STSAFE_CryptoDevCb function.

For TPM based crypto callbacks example see the wolfTPM2_CryptoDevCb function in wolfTPM src/tpm2_wrap.c

Returns
CRYPTOCB_UNAVAILABLE to fallback to using software crypto
0 for success
negative value for failure
Parameters
devIdany unique value, not -2 (INVALID_DEVID)
cba callback function with prototype: typedef int (CryptoDevCallbackFunc)(int devId, wc_CryptoInfo info, void* ctx);

Example

1 #include <wolfssl/wolfcrypt/settings.h>
2 #include <wolfssl/wolfcrypt/cryptocb.h>
3 static int myCryptoCb_Func(int devId, wc_CryptoInfo* info, void* ctx)
4 {
5  int ret = CRYPTOCB_UNAVAILABLE;
6 
7  if (info->algo_type == WC_ALGO_TYPE_PK) {
8  #ifndef NO_RSA
9  if (info->pk.type == WC_PK_TYPE_RSA) {
10  switch (info->pk.rsa.type) {
11  case RSA_PUBLIC_ENCRYPT:
12  case RSA_PUBLIC_DECRYPT:
13  // RSA public op
14  ret = wc_RsaFunction(
15  info->pk.rsa.in, info->pk.rsa.inLen,
16  info->pk.rsa.out, info->pk.rsa.outLen,
17  info->pk.rsa.type, info->pk.rsa.key,
18  info->pk.rsa.rng);
19  break;
20  case RSA_PRIVATE_ENCRYPT:
21  case RSA_PRIVATE_DECRYPT:
22  // RSA private op
23  ret = wc_RsaFunction(
24  info->pk.rsa.in, info->pk.rsa.inLen,
25  info->pk.rsa.out, info->pk.rsa.outLen,
26  info->pk.rsa.type, info->pk.rsa.key,
27  info->pk.rsa.rng);
28  break;
29  }
30  }
31  #endif
32  #ifdef HAVE_ECC
33  if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
34  // ECDSA
35  ret = wc_ecc_sign_hash(
36  info->pk.eccsign.in, info->pk.eccsign.inlen,
37  info->pk.eccsign.out, info->pk.eccsign.outlen,
38  info->pk.eccsign.rng, info->pk.eccsign.key);
39  }
40  #endif
41  #ifdef HAVE_ED25519
42  if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
43  // ED25519 sign
44  ret = wc_ed25519_sign_msg_ex(
45  info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
46  info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
47  info->pk.ed25519sign.key, info->pk.ed25519sign.type,
48  info->pk.ed25519sign.context,
49  info->pk.ed25519sign.contextLen);
50  }
51  #endif
52  }
53  return ret;
54 }
55 
56 int devId = 1;
57 wc_CryptoCb_RegisterDevice(devId, myCryptoCb_Func, &myCtx);
58 wolfSSL_CTX_SetDevId(ctx, devId);
See also
wc_CryptoCb_UnRegisterDevice
wolfSSL_SetDevId
wolfSSL_CTX_SetDevId
void wc_CryptoCb_UnRegisterDevice ( int  devId)

This function un-registers a unique device identifier (devID) callback function.

Returns
none No returns.
Parameters
devIdany unique value, not -2 (INVALID_DEVID)

Example

1 wc_CryptoCb_UnRegisterDevice(devId);
2 devId = INVALID_DEVID;
3 wolfSSL_CTX_SetDevId(ctx, devId);
See also
wc_CryptoCb_RegisterDevice
wolfSSL_SetDevId
wolfSSL_CTX_SetDevId