Skip to content

Commit a944dfb

Browse files
committed
Add ECC support to PKCS7 crypto callback example.
1 parent 6a34cb5 commit a944dfb

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

pkcs7/signedData-cryptocb.c

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@
2525
#include <wolfssl/wolfcrypt/logging.h>
2626
#include <wolfssl/wolfcrypt/cryptocb.h>
2727

28+
#if 0
2829
#define CERT_FILE "../certs/client-cert.der"
2930
#define KEY_FILE "../certs/client-key.der"
3031
#define KEYPUB_FILE "../certs/client-keyPub.der"
32+
#else
33+
#define CERT_FILE "../certs/client-ecc-cert.der"
34+
#define KEY_FILE "../certs/ecc-client-key.der"
35+
#define KEYPUB_FILE "../certs/ecc-client-keyPub.der"
36+
#endif
37+
3138
#define encodedFileNoAttrs "signedData_cryptocb_noattrs.der"
3239
#define encodedFileAttrs "signedData_cryptocb_attrs.der"
3340

@@ -372,16 +379,13 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
372379
return ret;
373380
}
374381
ret = wc_RsaPrivateKeyDecode(der, &idx, &rsaPriv, derSz);
375-
if (ret != 0) {
376-
wc_FreeRsaKey(&rsaPriv);
377-
return ret;
382+
if (ret == 0) {
383+
/* perform software based RSA private op */
384+
ret = wc_RsaFunction(
385+
info->pk.rsa.in, info->pk.rsa.inLen,
386+
info->pk.rsa.out, info->pk.rsa.outLen,
387+
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
378388
}
379-
380-
/* perform software based RSA private op */
381-
ret = wc_RsaFunction(
382-
info->pk.rsa.in, info->pk.rsa.inLen,
383-
info->pk.rsa.out, info->pk.rsa.outLen,
384-
info->pk.rsa.type, &rsaPriv, info->pk.rsa.rng);
385389
wc_FreeRsaKey(&rsaPriv);
386390
if (der != NULL)
387391
free(der);
@@ -413,16 +417,31 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
413417
info->pk.eckg.key->devId = devIdArg;
414418
}
415419
else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
416-
/* set devId to invalid, so software is used */
417-
info->pk.eccsign.key->devId = INVALID_DEVID;
418-
419-
ret = wc_ecc_sign_hash(
420-
info->pk.eccsign.in, info->pk.eccsign.inlen,
421-
info->pk.eccsign.out, info->pk.eccsign.outlen,
422-
info->pk.eccsign.rng, info->pk.eccsign.key);
420+
ecc_key eccPriv;
421+
byte* der = NULL;
422+
size_t derSz = 0;
423+
word32 idx = 0;
424+
425+
ret = load_file(myCtx->keyFilePriv, &der, &derSz);
426+
if (ret != 0) {
427+
printf("Error %d loading %s\n", ret, myCtx->keyFilePriv);
428+
return ret;
429+
}
423430

424-
/* reset devId */
425-
info->pk.eccsign.key->devId = devIdArg;
431+
ret = wc_ecc_init_ex(&eccPriv, NULL, INVALID_DEVID);
432+
if (ret != 0) {
433+
return ret;
434+
}
435+
ret = wc_EccPrivateKeyDecode(der, &idx, &eccPriv, derSz);
436+
if (ret == 0) {
437+
ret = wc_ecc_sign_hash(
438+
info->pk.eccsign.in, info->pk.eccsign.inlen,
439+
info->pk.eccsign.out, info->pk.eccsign.outlen,
440+
info->pk.eccsign.rng, &eccPriv);
441+
}
442+
wc_ecc_free(&eccPriv);
443+
if (der != NULL)
444+
free(der);
426445
}
427446
else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
428447
/* set devId to invalid, so software is used */

0 commit comments

Comments
 (0)