Skip to content

Commit e7d48be

Browse files
Merge pull request #313 from dgarske/cryptocb
Various improvements to crypto callback examples
2 parents abb0380 + b2c0ca2 commit e7d48be

7 files changed

Lines changed: 177 additions & 202 deletions

File tree

certgen/Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,25 @@ CC=gcc
99
#END EXAMPLE
1010

1111
WOLF_INSTALL_DIR=/usr/local
12-
CFLAGS=-I$(WOLF_INSTALL_DIR)/include -Wall
13-
LIBS=-L$(WOLF_INSTALL_DIR)/lib -lwolfssl
1412

13+
# ECC Examples Makefile
14+
CC = gcc
15+
LIB_PATH = /usr/local
16+
CFLAGS = -Wall -I$(LIB_PATH)/include
17+
LIBS = -L$(LIB_PATH)/lib -lm
18+
19+
# option variables
20+
DYN_LIB = -lwolfssl
21+
STATIC_LIB = $(LIB_PATH)/lib/libwolfssl.a
22+
DEBUG_FLAGS = -g -DDEBUG
23+
DEBUG_INC_PATHS = -MD
24+
OPTIMIZE = -Os
25+
26+
# Options
27+
#CFLAGS+=$(DEBUG_FLAGS)
28+
CFLAGS+=$(OPTIMIZE)
29+
#LIBS+=$(STATIC_LIB)
30+
LIBS+=$(DYN_LIB)
1531

1632
all:certgen_example csr_example csr_w_ed25519_example csr_sign csr_cryptocb custom_ext custom_ext_callback
1733

certgen/csr_cryptocb.c

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
#define LARGE_TEMP_SZ 4096
3737

38-
#define DEBUG_CRYPTOCB
39-
4038
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_CERT_REQ) && \
4139
defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
4240
(!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519))
@@ -72,14 +70,10 @@ typedef struct {
7270
/* Forward declarations */
7371
static int load_key_file(const char* fname, byte* derBuf, word32* derLen,
7472
int isPubKey);
75-
#ifdef DEBUG_CRYPTOCB
76-
static const char* GetAlgoTypeStr(int algo);
77-
static const char* GetPkTypeStr(int pk);
78-
#endif
7973

8074
/* Example crypto dev callback function that calls software version */
8175
/* This is where you would plug-in calls to your own hardware crypto */
82-
static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
76+
static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
8377
{
8478
int ret = CRYPTOCB_UNAVAILABLE; /* return this to bypass HW and use SW */
8579
myCryptoCbCtx* myCtx = (myCryptoCbCtx*)ctx;
@@ -88,15 +82,14 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
8882
if (info == NULL)
8983
return BAD_FUNC_ARG;
9084

85+
#ifdef DEBUG_CRYPTOCB
86+
wc_CryptoCb_InfoString(info);
87+
#endif
88+
9189
if (info->algo_type == WC_ALGO_TYPE_PK) {
9290
byte der[LARGE_TEMP_SZ];
9391
word32 derSz;
9492

95-
#ifdef DEBUG_CRYPTOCB
96-
printf("CryptoCb: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
97-
GetPkTypeStr(info->pk.type), info->pk.type);
98-
#endif
99-
10093
ret = load_key_file(myCtx->keyFilePriv, der, &derSz, 0);
10194
if (ret != 0) {
10295
printf("Error %d loading %s\n", ret, myCtx->keyFilePriv);
@@ -105,36 +98,42 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
10598

10699
#ifndef NO_RSA
107100
if (info->pk.type == WC_PK_TYPE_RSA) {
108-
RsaKey rsaPriv;
109-
ret = wc_InitRsaKey_ex(&rsaPriv, NULL, INVALID_DEVID);
110-
if (ret != 0) {
111-
return ret;
112-
}
113-
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
114-
if (ret != 0) {
115-
wc_FreeRsaKey(&rsaPriv);
116-
return ret;
117-
}
118-
119101
switch (info->pk.rsa.type) {
120102
case RSA_PUBLIC_ENCRYPT:
121103
case RSA_PUBLIC_DECRYPT:
104+
/* set devId to invalid, so software is used */
105+
info->pk.rsa.key->devId = INVALID_DEVID;
122106
/* perform software based RSA public op */
123107
ret = wc_RsaFunction(
124108
info->pk.rsa.in, info->pk.rsa.inLen,
125109
info->pk.rsa.out, info->pk.rsa.outLen,
126-
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
110+
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
111+
info->pk.rsa.key->devId = devIdArg; /* reset devId */
127112
break;
128113
case RSA_PRIVATE_ENCRYPT:
129114
case RSA_PRIVATE_DECRYPT:
115+
{
116+
RsaKey rsaPriv;
117+
118+
ret = wc_InitRsaKey_ex(&rsaPriv, NULL, INVALID_DEVID);
119+
if (ret != 0) {
120+
return ret;
121+
}
122+
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
123+
if (ret != 0) {
124+
wc_FreeRsaKey(&rsaPriv);
125+
return ret;
126+
}
127+
130128
/* perform software based RSA private op */
131129
ret = wc_RsaFunction(
132130
info->pk.rsa.in, info->pk.rsa.inLen,
133131
info->pk.rsa.out, info->pk.rsa.outLen,
134132
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
133+
wc_FreeRsaKey(&rsaPriv);
135134
break;
135+
}
136136
}
137-
wc_FreeRsaKey(&rsaPriv);
138137
}
139138
#endif /* !NO_RSA */
140139
#ifdef HAVE_ECC
@@ -188,38 +187,6 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
188187
return ret;
189188
}
190189

191-
192-
#ifdef DEBUG_CRYPTOCB
193-
static const char* GetAlgoTypeStr(int algo)
194-
{
195-
switch (algo) { /* enum wc_AlgoType */
196-
case WC_ALGO_TYPE_HASH: return "Hash";
197-
case WC_ALGO_TYPE_CIPHER: return "Cipher";
198-
case WC_ALGO_TYPE_PK: return "PK";
199-
case WC_ALGO_TYPE_RNG: return "RNG";
200-
case WC_ALGO_TYPE_SEED: return "Seed";
201-
case WC_ALGO_TYPE_HMAC: return "HMAC";
202-
}
203-
return NULL;
204-
}
205-
static const char* GetPkTypeStr(int pk)
206-
{
207-
switch (pk) {
208-
case WC_PK_TYPE_RSA: return "RSA";
209-
case WC_PK_TYPE_DH: return "DH";
210-
case WC_PK_TYPE_ECDH: return "ECDH";
211-
case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign";
212-
case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify";
213-
case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign";
214-
case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify";
215-
case WC_PK_TYPE_CURVE25519: return "CURVE25519";
216-
case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
217-
case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
218-
}
219-
return NULL;
220-
}
221-
#endif /* DEBUG_CRYPTOCB */
222-
223190
/* reads file size, allocates buffer, reads into buffer, returns buffer */
224191
static int load_file(const char* fname, byte** buf, size_t* bufLen)
225192
{
@@ -364,7 +331,7 @@ static int gen_csr(const char* arg1)
364331
wolfCrypt_Init();
365332

366333
/* register a devID for crypto callbacks */
367-
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
334+
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
368335
if (ret != 0) {
369336
printf("Crypto callback register failed: %d\n", ret);
370337
goto exit;
@@ -481,7 +448,7 @@ static int gen_csr(const char* arg1)
481448
if (type == ED25519_TYPE)
482449
req.sigType = CTC_ED25519;
483450
#endif
484-
/* Because the key has devId set, it will call myCryptoDevCb for signing */
451+
/* Because the key has devId set, it will call myCryptoCb for signing */
485452
ret = wc_SignCert_ex(req.bodySz, req.sigType, der, sizeof(der), type,
486453
keyPtr, &rng);
487454
if (ret <= 0) {

pkcs7/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LIB_PATH = /usr/local
44
CFLAGS = -Wall -I$(LIB_PATH)/include
55
ZLIB =
66
#ZLIB += -lz
7-
LIBS = -L$(LIB_PATH)/lib -lwolfssl -lm ${ZLIB}
7+
LIBS = -L$(LIB_PATH)/lib -lm ${ZLIB}
88

99
# option variables
1010
DYN_LIB = -lwolfssl

pkcs7/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ Generated bundle files: `signedEncryptedFirmwarePkgData_noattrs.der`,
443443
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
444444
one without them. It uses RSA with SHA256 as the the signature algorithm,
445445
and specifies the signed content type as EncryptedData. The inner EncryptedData
446-
content type encpasulates a FirmwarePkgData type. After creating the
446+
content type encapsulates a FirmwarePkgData type. After creating the
447447
bundles, the app decodes them and verifies the operation was successful.
448448

449449
The generated SignedData bundles are written out to a file for analysis and
@@ -469,7 +469,7 @@ Generated bundle files: `signedCompressedFirmwarePkgData_noattrs.der`,
469469
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
470470
one without them. It uses RSA with SHA256 as the the signature algorithm,
471471
and specifies the signed content type as CompressedData. The inner
472-
CompressedData content type encpasulates a FirmwarePkgData type. After creating
472+
CompressedData content type encapsulates a FirmwarePkgData type. After creating
473473
the bundles, the app decodes them and verifies the operation was successful.
474474

475475
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
479479
of the bundle will be printed out to the terminal window.
480480

481481
```
482-
./signedData-CommpressedFirmwarePkgData
482+
./signedData-CompressedFirmwarePkgData
483483
Successfully encoded Signed Compressed FirmwarePkgData (signedCompressedFPD_noattrs.der)
484484
Successfully extracted and verified bundle contents
485485
Successfully encoded Signed Compressed FirmwarePkgData (signedCompressedFPD_attrs.der)
486486
Successfully extracted and verified bundle contents
487487
```
488488

489-
### SignedData using CryptoDev Callback
489+
### SignedData using Crypto Callback
490490

491-
Example file: `signedData-cryptodev.c`
492-
Generated bundle files: `signedData_cryptodev_noattrs.der`,
493-
`signedData_cryptodev_attrs.der`
491+
Build wolfssl using: `./configure --enable-pkcs7 --enable-pwdbased --enable-cryptocb`.
492+
493+
Example file: `signedData-cryptocb.c`
494+
Generated bundle files: `signedData_cryptocb_noattrs.der`,
495+
`signedData_cryptocb_attrs.der`
494496

495497
This example creates a PKCS#7/CMS SignedData bundle using the wolfCrypt
496-
CryptoDev callback. CryptoDev allows a user to register a callback to do
498+
Crypto callback. This allows a user to register a callback to do
497499
cryptographic operations outside of wolfCrypt proper. This can be useful
498500
in order to take advantage of hardware-based cryptography instead of the
499501
default software implementation.
@@ -509,10 +511,10 @@ If wolfSSL has been configured and compiled with debug support, the bytes
509511
of the bundle will be printed out to the terminal window.
510512

511513
```
512-
./signedData-cryptodev
513-
Successfully encoded SignedData bundle (signedData_cryptodev_noattrs.der)
514+
./signedData-cryptocb
515+
Successfully encoded SignedData bundle (signedData_cryptocb_noattrs.der)
514516
Successfully verified SignedData bundle.
515-
Successfully encoded SignedData bundle (signedData_cryptodev_attrs.der)
517+
Successfully encoded SignedData bundle (signedData_cryptocb_attrs.der)
516518
Successfully verified SignedData bundle.
517519
```
518520

@@ -552,7 +554,7 @@ Generated bundle files: `signedEncryptedCompressedFirmwarePkgData_noattrs.der`,
552554
This example creates two PKCS#7/CMS SignedData bundles, one with attributes and
553555
one without them. It uses RSA with SHA256 as the the signature algorithm,
554556
and specifies the signed content type as CompressedData. The inner
555-
CompressedData content type encpasulates an EncryptedData type, which in turn
557+
CompressedData content type encapsulates an EncryptedData type, which in turn
556558
encapsulates a FirmwarePkgData type. After creating the bundles, the app
557559
decodes them and verifies the operation was successful.
558560

@@ -563,7 +565,7 @@ If wolfSSL has been configured and compiled with debug support, the bytes
563565
of the bundle will be printed out to the terminal window.
564566

565567
```
566-
./signedData-EncryptedCommpressedFirmwarePkgData
568+
./signedData-EncryptedCompressedFirmwarePkgData
567569
Successfully encoded Signed Encrypted Compressed FirmwarePkgData (signedEncryptedCompressedFPD_noattrs.der)
568570
Successfully extracted and verified bundle contents
569571
Successfully encoded Signed Encrypted Compressed FirmwarePkgData (signedEncryptedCompressedFPD_attrs.der)

0 commit comments

Comments
 (0)