Various improvements to crypto callbacks.
parent
abb0380d6d
commit
b2c0ca21da
|
|
@ -9,9 +9,25 @@ CC=gcc
|
|||
#END EXAMPLE
|
||||
|
||||
WOLF_INSTALL_DIR=/usr/local
|
||||
CFLAGS=-I$(WOLF_INSTALL_DIR)/include -Wall
|
||||
LIBS=-L$(WOLF_INSTALL_DIR)/lib -lwolfssl
|
||||
|
||||
# ECC Examples Makefile
|
||||
CC = gcc
|
||||
LIB_PATH = /usr/local
|
||||
CFLAGS = -Wall -I$(LIB_PATH)/include
|
||||
LIBS = -L$(LIB_PATH)/lib -lm
|
||||
|
||||
# option variables
|
||||
DYN_LIB = -lwolfssl
|
||||
STATIC_LIB = $(LIB_PATH)/lib/libwolfssl.a
|
||||
DEBUG_FLAGS = -g -DDEBUG
|
||||
DEBUG_INC_PATHS = -MD
|
||||
OPTIMIZE = -Os
|
||||
|
||||
# Options
|
||||
#CFLAGS+=$(DEBUG_FLAGS)
|
||||
CFLAGS+=$(OPTIMIZE)
|
||||
#LIBS+=$(STATIC_LIB)
|
||||
LIBS+=$(DYN_LIB)
|
||||
|
||||
all:certgen_example csr_example csr_w_ed25519_example csr_sign csr_cryptocb custom_ext custom_ext_callback
|
||||
|
||||
|
|
|
|||
|
|
@ -35,8 +35,6 @@
|
|||
|
||||
#define LARGE_TEMP_SZ 4096
|
||||
|
||||
#define DEBUG_CRYPTOCB
|
||||
|
||||
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_CERT_REQ) && \
|
||||
defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
|
||||
(!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519))
|
||||
|
|
@ -72,14 +70,10 @@ typedef struct {
|
|||
/* Forward declarations */
|
||||
static int load_key_file(const char* fname, byte* derBuf, word32* derLen,
|
||||
int isPubKey);
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
static const char* GetAlgoTypeStr(int algo);
|
||||
static const char* GetPkTypeStr(int pk);
|
||||
#endif
|
||||
|
||||
/* Example crypto dev callback function that calls software version */
|
||||
/* This is where you would plug-in calls to your own hardware crypto */
|
||||
static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
{
|
||||
int ret = CRYPTOCB_UNAVAILABLE; /* return this to bypass HW and use SW */
|
||||
myCryptoCbCtx* myCtx = (myCryptoCbCtx*)ctx;
|
||||
|
|
@ -88,15 +82,14 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
if (info == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
wc_CryptoCb_InfoString(info);
|
||||
#endif
|
||||
|
||||
if (info->algo_type == WC_ALGO_TYPE_PK) {
|
||||
byte der[LARGE_TEMP_SZ];
|
||||
word32 derSz;
|
||||
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||
GetPkTypeStr(info->pk.type), info->pk.type);
|
||||
#endif
|
||||
|
||||
ret = load_key_file(myCtx->keyFilePriv, der, &derSz, 0);
|
||||
if (ret != 0) {
|
||||
printf("Error %d loading %s\n", ret, myCtx->keyFilePriv);
|
||||
|
|
@ -105,36 +98,42 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
|
||||
#ifndef NO_RSA
|
||||
if (info->pk.type == WC_PK_TYPE_RSA) {
|
||||
RsaKey rsaPriv;
|
||||
ret = wc_InitRsaKey_ex(&rsaPriv, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
|
||||
if (ret != 0) {
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (info->pk.rsa.type) {
|
||||
case RSA_PUBLIC_ENCRYPT:
|
||||
case RSA_PUBLIC_DECRYPT:
|
||||
/* set devId to invalid, so software is used */
|
||||
info->pk.rsa.key->devId = INVALID_DEVID;
|
||||
/* perform software based RSA public op */
|
||||
ret = wc_RsaFunction(
|
||||
info->pk.rsa.in, info->pk.rsa.inLen,
|
||||
info->pk.rsa.out, info->pk.rsa.outLen,
|
||||
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
|
||||
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
|
||||
info->pk.rsa.key->devId = devIdArg; /* reset devId */
|
||||
break;
|
||||
case RSA_PRIVATE_ENCRYPT:
|
||||
case RSA_PRIVATE_DECRYPT:
|
||||
{
|
||||
RsaKey rsaPriv;
|
||||
|
||||
ret = wc_InitRsaKey_ex(&rsaPriv, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
|
||||
if (ret != 0) {
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* perform software based RSA private op */
|
||||
ret = wc_RsaFunction(
|
||||
info->pk.rsa.in, info->pk.rsa.inLen,
|
||||
info->pk.rsa.out, info->pk.rsa.outLen,
|
||||
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
}
|
||||
#endif /* !NO_RSA */
|
||||
#ifdef HAVE_ECC
|
||||
|
|
@ -188,38 +187,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
static const char* GetAlgoTypeStr(int algo)
|
||||
{
|
||||
switch (algo) { /* enum wc_AlgoType */
|
||||
case WC_ALGO_TYPE_HASH: return "Hash";
|
||||
case WC_ALGO_TYPE_CIPHER: return "Cipher";
|
||||
case WC_ALGO_TYPE_PK: return "PK";
|
||||
case WC_ALGO_TYPE_RNG: return "RNG";
|
||||
case WC_ALGO_TYPE_SEED: return "Seed";
|
||||
case WC_ALGO_TYPE_HMAC: return "HMAC";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static const char* GetPkTypeStr(int pk)
|
||||
{
|
||||
switch (pk) {
|
||||
case WC_PK_TYPE_RSA: return "RSA";
|
||||
case WC_PK_TYPE_DH: return "DH";
|
||||
case WC_PK_TYPE_ECDH: return "ECDH";
|
||||
case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign";
|
||||
case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify";
|
||||
case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign";
|
||||
case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify";
|
||||
case WC_PK_TYPE_CURVE25519: return "CURVE25519";
|
||||
case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
|
||||
case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif /* DEBUG_CRYPTOCB */
|
||||
|
||||
/* reads file size, allocates buffer, reads into buffer, returns buffer */
|
||||
static int load_file(const char* fname, byte** buf, size_t* bufLen)
|
||||
{
|
||||
|
|
@ -364,7 +331,7 @@ static int gen_csr(const char* arg1)
|
|||
wolfCrypt_Init();
|
||||
|
||||
/* register a devID for crypto callbacks */
|
||||
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
|
||||
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
|
||||
if (ret != 0) {
|
||||
printf("Crypto callback register failed: %d\n", ret);
|
||||
goto exit;
|
||||
|
|
@ -481,7 +448,7 @@ static int gen_csr(const char* arg1)
|
|||
if (type == ED25519_TYPE)
|
||||
req.sigType = CTC_ED25519;
|
||||
#endif
|
||||
/* Because the key has devId set, it will call myCryptoDevCb for signing */
|
||||
/* Because the key has devId set, it will call myCryptoCb for signing */
|
||||
ret = wc_SignCert_ex(req.bodySz, req.sigType, der, sizeof(der), type,
|
||||
keyPtr, &rng);
|
||||
if (ret <= 0) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ LIB_PATH = /usr/local
|
|||
CFLAGS = -Wall -I$(LIB_PATH)/include
|
||||
ZLIB =
|
||||
#ZLIB += -lz
|
||||
LIBS = -L$(LIB_PATH)/lib -lwolfssl -lm ${ZLIB}
|
||||
LIBS = -L$(LIB_PATH)/lib -lm ${ZLIB}
|
||||
|
||||
# option variables
|
||||
DYN_LIB = -lwolfssl
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ Generated bundle files: `signedEncryptedFirmwarePkgData_noattrs.der`,
|
|||
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
|
||||
one without them. It uses RSA with SHA256 as the the signature algorithm,
|
||||
and specifies the signed content type as EncryptedData. The inner EncryptedData
|
||||
content type encpasulates a FirmwarePkgData type. After creating the
|
||||
content type encapsulates a FirmwarePkgData type. After creating the
|
||||
bundles, the app decodes them and verifies the operation was successful.
|
||||
|
||||
The generated SignedData bundles are written out to a file for analysis and
|
||||
|
|
@ -469,7 +469,7 @@ Generated bundle files: `signedCompressedFirmwarePkgData_noattrs.der`,
|
|||
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
|
||||
one without them. It uses RSA with SHA256 as the the signature algorithm,
|
||||
and specifies the signed content type as CompressedData. The inner
|
||||
CompressedData content type encpasulates a FirmwarePkgData type. After creating
|
||||
CompressedData content type encapsulates a FirmwarePkgData type. After creating
|
||||
the bundles, the app decodes them and verifies the operation was successful.
|
||||
|
||||
The generated SignedData bundles are written out to a file for analysis and
|
||||
|
|
@ -479,21 +479,23 @@ If wolfSSL has been configured and compiled with debug support, the bytes
|
|||
of the bundle will be printed out to the terminal window.
|
||||
|
||||
```
|
||||
./signedData-CommpressedFirmwarePkgData
|
||||
./signedData-CompressedFirmwarePkgData
|
||||
Successfully encoded Signed Compressed FirmwarePkgData (signedCompressedFPD_noattrs.der)
|
||||
Successfully extracted and verified bundle contents
|
||||
Successfully encoded Signed Compressed FirmwarePkgData (signedCompressedFPD_attrs.der)
|
||||
Successfully extracted and verified bundle contents
|
||||
```
|
||||
|
||||
### SignedData using CryptoDev Callback
|
||||
### SignedData using Crypto Callback
|
||||
|
||||
Example file: `signedData-cryptodev.c`
|
||||
Generated bundle files: `signedData_cryptodev_noattrs.der`,
|
||||
`signedData_cryptodev_attrs.der`
|
||||
Build wolfssl using: `./configure --enable-pkcs7 --enable-pwdbased --enable-cryptocb`.
|
||||
|
||||
Example file: `signedData-cryptocb.c`
|
||||
Generated bundle files: `signedData_cryptocb_noattrs.der`,
|
||||
`signedData_cryptocb_attrs.der`
|
||||
|
||||
This example creates a PKCS#7/CMS SignedData bundle using the wolfCrypt
|
||||
CryptoDev callback. CryptoDev allows a user to register a callback to do
|
||||
Crypto callback. This allows a user to register a callback to do
|
||||
cryptographic operations outside of wolfCrypt proper. This can be useful
|
||||
in order to take advantage of hardware-based cryptography instead of the
|
||||
default software implementation.
|
||||
|
|
@ -509,10 +511,10 @@ If wolfSSL has been configured and compiled with debug support, the bytes
|
|||
of the bundle will be printed out to the terminal window.
|
||||
|
||||
```
|
||||
./signedData-cryptodev
|
||||
Successfully encoded SignedData bundle (signedData_cryptodev_noattrs.der)
|
||||
./signedData-cryptocb
|
||||
Successfully encoded SignedData bundle (signedData_cryptocb_noattrs.der)
|
||||
Successfully verified SignedData bundle.
|
||||
Successfully encoded SignedData bundle (signedData_cryptodev_attrs.der)
|
||||
Successfully encoded SignedData bundle (signedData_cryptocb_attrs.der)
|
||||
Successfully verified SignedData bundle.
|
||||
```
|
||||
|
||||
|
|
@ -552,7 +554,7 @@ Generated bundle files: `signedEncryptedCompressedFirmwarePkgData_noattrs.der`,
|
|||
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
|
||||
one without them. It uses RSA with SHA256 as the the signature algorithm,
|
||||
and specifies the signed content type as CompressedData. The inner
|
||||
CompressedData content type encpasulates an EncryptedData type, which in turn
|
||||
CompressedData content type encapsulates an EncryptedData type, which in turn
|
||||
encapsulates a FirmwarePkgData type. After creating the bundles, the app
|
||||
decodes them and verifies the operation was successful.
|
||||
|
||||
|
|
@ -563,7 +565,7 @@ If wolfSSL has been configured and compiled with debug support, the bytes
|
|||
of the bundle will be printed out to the terminal window.
|
||||
|
||||
```
|
||||
./signedData-EncryptedCommpressedFirmwarePkgData
|
||||
./signedData-EncryptedCompressedFirmwarePkgData
|
||||
Successfully encoded Signed Encrypted Compressed FirmwarePkgData (signedEncryptedCompressedFPD_noattrs.der)
|
||||
Successfully extracted and verified bundle contents
|
||||
Successfully encoded Signed Encrypted Compressed FirmwarePkgData (signedEncryptedCompressedFPD_attrs.der)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/* signedData-cryptodev.c
|
||||
/* signedData-cryptocb.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
* Copyright (C) 2006-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||
*
|
||||
|
|
@ -25,10 +25,13 @@
|
|||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/cryptocb.h>
|
||||
|
||||
#define certFile "../certs/client-cert.der"
|
||||
#define keyFile "../certs/client-key.der"
|
||||
#define encodedFileNoAttrs "signedData_cryptodev_noattrs.der"
|
||||
#define encodedFileAttrs "signedData_cryptodev_attrs.der"
|
||||
#define CERT_FILE "../certs/client-cert.der"
|
||||
#define KEY_FILE "../certs/client-key.der"
|
||||
#define KEYPUB_FILE "../certs/client-keyPub.der"
|
||||
#define encodedFileNoAttrs "signedData_cryptocb_noattrs.der"
|
||||
#define encodedFileAttrs "signedData_cryptocb_attrs.der"
|
||||
|
||||
#define LARGE_TEMP_SZ 4096
|
||||
|
||||
#if defined(HAVE_PKCS7) && defined(WOLF_CRYPTO_CB)
|
||||
|
||||
|
|
@ -42,7 +45,7 @@ static int load_certs(byte* cert, word32* certSz, byte* key, word32* keySz)
|
|||
FILE* file;
|
||||
|
||||
/* certificate file */
|
||||
file = fopen(certFile, "rb");
|
||||
file = fopen(CERT_FILE, "rb");
|
||||
if (!file)
|
||||
return -1;
|
||||
|
||||
|
|
@ -50,7 +53,7 @@ static int load_certs(byte* cert, word32* certSz, byte* key, word32* keySz)
|
|||
fclose(file);
|
||||
|
||||
/* key file */
|
||||
file = fopen(keyFile, "rb");
|
||||
file = fopen(KEYPUB_FILE, "rb");
|
||||
if (!file)
|
||||
return -1;
|
||||
|
||||
|
|
@ -267,54 +270,124 @@ static int signedData_verify(byte* in, word32 inSz, byte* cert,
|
|||
return ret;
|
||||
}
|
||||
|
||||
/* reads file size, allocates buffer, reads into buffer, returns buffer */
|
||||
static int load_file(const char* fname, byte** buf, size_t* bufLen)
|
||||
{
|
||||
int ret;
|
||||
long int fileSz;
|
||||
XFILE lFile;
|
||||
|
||||
if (fname == NULL || buf == NULL || bufLen == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
/* set defaults */
|
||||
*buf = NULL;
|
||||
*bufLen = 0;
|
||||
|
||||
/* open file (read-only binary) */
|
||||
lFile = XFOPEN(fname, "rb");
|
||||
if (!lFile) {
|
||||
printf("Error loading %s\n", fname);
|
||||
return BAD_PATH_ERROR;
|
||||
}
|
||||
|
||||
fseek(lFile, 0, SEEK_END);
|
||||
fileSz = (int)ftell(lFile);
|
||||
rewind(lFile);
|
||||
if (fileSz > 0) {
|
||||
*bufLen = (size_t)fileSz;
|
||||
*buf = (byte*)malloc(*bufLen);
|
||||
if (*buf == NULL) {
|
||||
ret = MEMORY_E;
|
||||
printf("Error allocating %lu bytes\n", (unsigned long)*bufLen);
|
||||
}
|
||||
else {
|
||||
size_t readLen = fread(*buf, *bufLen, 1, lFile);
|
||||
|
||||
/* check response code */
|
||||
ret = (readLen > 0) ? 0 : -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = BUFFER_E;
|
||||
}
|
||||
fclose(lFile);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int exampleVar; /* example, not used */
|
||||
} myCryptoDevCtx;
|
||||
const char* keyFilePub;
|
||||
const char* keyFilePriv;
|
||||
} myCryptoCbCtx;
|
||||
|
||||
/* Example crypto dev callback function that calls software versions, could
|
||||
* be set up to call down to hardware module for crypto operations if
|
||||
* desired by user. If an algorithm is not supported by hardware, or user
|
||||
* callback, the cryptodev callback can return NOT_COMPILED_IN to default
|
||||
* callback, the crypto callback can return CRYPTOCB_UNAVAILABLE to default
|
||||
* back to using software crypto implementation. */
|
||||
static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
{
|
||||
int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
|
||||
myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
|
||||
int ret = CRYPTOCB_UNAVAILABLE; /* return this to bypass HW and use SW */
|
||||
myCryptoCbCtx* myCtx = (myCryptoCbCtx*)ctx;
|
||||
|
||||
if (info == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
if (info->algo_type == WC_ALGO_TYPE_PK) {
|
||||
#ifdef DEBUG_WOLFSSL
|
||||
printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
|
||||
#endif
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
wc_CryptoCb_InfoString(info);
|
||||
#endif
|
||||
|
||||
if (info->algo_type == WC_ALGO_TYPE_PK) {
|
||||
#ifndef NO_RSA
|
||||
if (info->pk.type == WC_PK_TYPE_RSA) {
|
||||
/* set devId to invalid, so software is used */
|
||||
info->pk.rsa.key->devId = INVALID_DEVID;
|
||||
|
||||
switch (info->pk.rsa.type) {
|
||||
case RSA_PUBLIC_ENCRYPT:
|
||||
case RSA_PUBLIC_DECRYPT:
|
||||
/* set devId to invalid, so software is used */
|
||||
info->pk.rsa.key->devId = INVALID_DEVID;
|
||||
/* perform software based RSA public op */
|
||||
ret = wc_RsaFunction(
|
||||
info->pk.rsa.in, info->pk.rsa.inLen,
|
||||
info->pk.rsa.out, info->pk.rsa.outLen,
|
||||
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
|
||||
info->pk.rsa.key->devId = devIdArg; /* reset devId */
|
||||
break;
|
||||
case RSA_PRIVATE_ENCRYPT:
|
||||
case RSA_PRIVATE_DECRYPT:
|
||||
{
|
||||
RsaKey rsaPriv;
|
||||
byte* der = NULL;
|
||||
size_t derSz = 0;
|
||||
word32 idx = 0;
|
||||
|
||||
ret = load_file(myCtx->keyFilePriv, &der, &derSz);
|
||||
if (ret != 0) {
|
||||
printf("Error %d loading %s\n", ret, myCtx->keyFilePriv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_InitRsaKey_ex(&rsaPriv, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
|
||||
if (ret != 0) {
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* perform software based RSA private op */
|
||||
ret = wc_RsaFunction(
|
||||
info->pk.rsa.in, info->pk.rsa.inLen,
|
||||
info->pk.rsa.out, info->pk.rsa.outLen,
|
||||
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
|
||||
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
|
||||
wc_FreeRsaKey(&rsaPriv);
|
||||
if (der != NULL)
|
||||
free(der);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* reset devId */
|
||||
info->pk.rsa.key->devId = devIdArg;
|
||||
}
|
||||
#ifdef WOLFSSL_KEY_GEN
|
||||
else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
|
||||
|
|
@ -433,7 +506,7 @@ int main(int argc, char** argv)
|
|||
int ret, devId;
|
||||
int encryptedSz, decryptedSz;
|
||||
word32 certSz, keySz;
|
||||
myCryptoDevCtx myCtx;
|
||||
myCryptoCbCtx myCtx;
|
||||
|
||||
byte cert[2048];
|
||||
byte key[2048];
|
||||
|
|
@ -450,13 +523,13 @@ int main(int argc, char** argv)
|
|||
return -1;
|
||||
}
|
||||
|
||||
/* example data for callback */
|
||||
myCtx.exampleVar = 1;
|
||||
/* provide private key to crypto callback */
|
||||
myCtx.keyFilePriv = KEY_FILE;
|
||||
|
||||
/* setting devId to something other than INVALID_DEVID, enables
|
||||
cryptodev callback to be used internally by wolfCrypt */
|
||||
* crypto callback to be used internally by wolfCrypt */
|
||||
devId = 1;
|
||||
ret = wc_CryptoDev_RegisterDevice(devId, myCryptoDevCb, &myCtx);
|
||||
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
|
||||
if (ret != 0) {
|
||||
printf("Failed to register crypto dev device, ret = %d\n", ret);
|
||||
return -1;
|
||||
|
|
@ -29,6 +29,8 @@
|
|||
* into a production application.
|
||||
*/
|
||||
#include <wolfssl/options.h>
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/pkcs7.h>
|
||||
#include <wolfssl/wolfcrypt/asn_public.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -39,14 +39,13 @@
|
|||
|
||||
#define DEFAULT_PORT 11111
|
||||
|
||||
#define CERT_FILE "../certs/ca-cert.pem"
|
||||
#define DEBUG_CRYPTOCB
|
||||
#define CA_FILE "../certs/ca-cert.pem"
|
||||
|
||||
#ifdef WOLF_CRYPTO_CB
|
||||
/* Example custom context for crypto callback */
|
||||
typedef struct {
|
||||
int exampleVar; /* example, not used */
|
||||
} myCryptoDevCtx;
|
||||
} myCryptoCbCtx;
|
||||
|
||||
static void error_out(char* msg, int err)
|
||||
{
|
||||
|
|
@ -54,88 +53,22 @@ static void error_out(char* msg, int err)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
static const char* GetAlgoTypeStr(int algo)
|
||||
{
|
||||
switch (algo) { /* enum wc_AlgoType */
|
||||
case WC_ALGO_TYPE_HASH: return "Hash";
|
||||
case WC_ALGO_TYPE_CIPHER: return "Cipher";
|
||||
case WC_ALGO_TYPE_PK: return "PK";
|
||||
case WC_ALGO_TYPE_RNG: return "RNG";
|
||||
case WC_ALGO_TYPE_SEED: return "Seed";
|
||||
case WC_ALGO_TYPE_HMAC: return "HMAC";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static const char* GetPkTypeStr(int pk)
|
||||
{
|
||||
switch (pk) {
|
||||
case WC_PK_TYPE_RSA: return "RSA";
|
||||
case WC_PK_TYPE_DH: return "DH";
|
||||
case WC_PK_TYPE_ECDH: return "ECDH";
|
||||
case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign";
|
||||
case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify";
|
||||
case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign";
|
||||
case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify";
|
||||
case WC_PK_TYPE_CURVE25519: return "CURVE25519";
|
||||
case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
|
||||
case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static const char* GetCipherTypeStr(int cipher)
|
||||
{
|
||||
switch (cipher) {
|
||||
case WC_CIPHER_AES: return "AES ECB";
|
||||
case WC_CIPHER_AES_CBC: return "AES CBC";
|
||||
case WC_CIPHER_AES_GCM: return "AES GCM";
|
||||
case WC_CIPHER_AES_CTR: return "AES CTR";
|
||||
case WC_CIPHER_AES_XTS: return "AES XTS";
|
||||
case WC_CIPHER_AES_CFB: return "AES CFB";
|
||||
case WC_CIPHER_DES3: return "DES3";
|
||||
case WC_CIPHER_DES: return "DES";
|
||||
case WC_CIPHER_CHACHA: return "ChaCha20";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static const char* GetHashTypeStr(int hash)
|
||||
{
|
||||
switch (hash) {
|
||||
case WC_HASH_TYPE_MD2: return "MD2";
|
||||
case WC_HASH_TYPE_MD4: return "MD4";
|
||||
case WC_HASH_TYPE_MD5: return "MD5";
|
||||
case WC_HASH_TYPE_SHA: return "SHA-1";
|
||||
case WC_HASH_TYPE_SHA224: return "SHA-224";
|
||||
case WC_HASH_TYPE_SHA256: return "SHA-256";
|
||||
case WC_HASH_TYPE_SHA384: return "SHA-384";
|
||||
case WC_HASH_TYPE_SHA512: return "SHA-512";
|
||||
case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1";
|
||||
case WC_HASH_TYPE_SHA3_224: return "SHA3-224";
|
||||
case WC_HASH_TYPE_SHA3_256: return "SHA3-256";
|
||||
case WC_HASH_TYPE_SHA3_384: return "SHA3-384";
|
||||
case WC_HASH_TYPE_SHA3_512: return "SHA3-512";
|
||||
case WC_HASH_TYPE_BLAKE2B: return "Blake2B";
|
||||
case WC_HASH_TYPE_BLAKE2S: return "Blake2S";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif /* DEBUG_CRYPTOCB */
|
||||
|
||||
|
||||
/* Example crypto dev callback function that calls software version */
|
||||
/* This is where you would plug-in calls to your own hardware crypto */
|
||||
static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
||||
{
|
||||
int ret = CRYPTOCB_UNAVAILABLE; /* return this to bypass HW and use SW */
|
||||
myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
|
||||
myCryptoCbCtx* myCtx = (myCryptoCbCtx*)ctx;
|
||||
|
||||
if (info == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
wc_CryptoCb_InfoString(info);
|
||||
#endif
|
||||
|
||||
if (info->algo_type == WC_ALGO_TYPE_RNG) {
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
|
||||
#endif
|
||||
#ifndef WC_NO_RNG
|
||||
/* set devId to invalid, so software is used */
|
||||
info->rng.rng->devId = INVALID_DEVID;
|
||||
|
|
@ -168,11 +101,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
#endif
|
||||
}
|
||||
else if (info->algo_type == WC_ALGO_TYPE_PK) {
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||
GetPkTypeStr(info->pk.type), info->pk.type);
|
||||
#endif
|
||||
|
||||
#ifndef NO_RSA
|
||||
if (info->pk.type == WC_PK_TYPE_RSA) {
|
||||
/* set devId to invalid, so software is used */
|
||||
|
|
@ -261,10 +189,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
#endif /* HAVE_ECC */
|
||||
}
|
||||
else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||
GetCipherTypeStr(info->cipher.type), info->cipher.type);
|
||||
#endif
|
||||
#if !defined(NO_AES) || !defined(NO_DES3)
|
||||
#ifdef HAVE_AESGCM
|
||||
if (info->cipher.type == WC_CIPHER_AES_GCM) {
|
||||
|
|
@ -372,10 +296,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
}
|
||||
else if (info->algo_type == WC_ALGO_TYPE_HASH) {
|
||||
#if !defined(NO_SHA) || !defined(NO_SHA256)
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||
GetHashTypeStr(info->hash.type), info->hash.type);
|
||||
#endif
|
||||
#if !defined(NO_SHA)
|
||||
if (info->hash.type == WC_HASH_TYPE_SHA) {
|
||||
if (info->hash.sha1 == NULL)
|
||||
|
|
@ -432,11 +352,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|||
}
|
||||
else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
|
||||
#ifndef NO_HMAC
|
||||
#ifdef DEBUG_CRYPTOCB
|
||||
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||
GetHashTypeStr(info->hmac.macType), info->hmac.macType);
|
||||
#endif
|
||||
|
||||
if (info->hmac.hmac == NULL)
|
||||
return CRYPTOCB_UNAVAILABLE;
|
||||
|
||||
|
|
@ -481,7 +396,7 @@ int main(int argc, char** argv)
|
|||
WOLFSSL* ssl;
|
||||
|
||||
int devId = 1; /* anything besides -2 (INVALID_DEVID) */
|
||||
myCryptoDevCtx myCtx;
|
||||
myCryptoCbCtx myCtx;
|
||||
|
||||
/* example data for callback */
|
||||
myCtx.exampleVar = 1;
|
||||
|
|
@ -539,7 +454,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
/* register a devID for crypto callbacks */
|
||||
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
|
||||
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
|
||||
if (ret != 0)
|
||||
error_out("wc_CryptoCb_RegisterDevice", ret);
|
||||
|
||||
|
|
@ -547,10 +462,10 @@ int main(int argc, char** argv)
|
|||
wolfSSL_CTX_SetDevId(ctx, devId);
|
||||
|
||||
/* Load client certificates into WOLFSSL_CTX */
|
||||
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CERT_FILE, NULL))
|
||||
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
|
||||
!= SSL_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
|
||||
CERT_FILE);
|
||||
CA_FILE);
|
||||
goto ctx_cleanup;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue