Skip to content

Commit b7299a2

Browse files
committed
Add new crypto callback for RSA with padding.
1 parent d0475de commit b7299a2

8 files changed

Lines changed: 359 additions & 6 deletions

File tree

tests/api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83658,6 +83658,13 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
8365883658
info->pk.rsa.type, ret, *info->pk.rsa.outLen);
8365983659
#endif
8366083660
}
83661+
#ifdef WOLF_CRYPTO_CB_RSA_PAD
83662+
else if (info->pk.type == WC_PK_TYPE_RSA_PKCS ||
83663+
info->pk.type == WC_PK_TYPE_RSA_PSS ||
83664+
info->pk.type == WC_PK_TYPE_RSA_OAEP) {
83665+
ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
83666+
}
83667+
#endif /* ifdef WOLF_CRYPTO_CB_RSA_PAD */
8366183668
#endif /* !NO_RSA */
8366283669
#ifdef HAVE_ECC
8366383670
if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {

wolfcrypt/src/cryptocb.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,62 @@ int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
418418
return wc_CryptoCb_TranslateErrorCode(ret);
419419
}
420420

421+
#ifdef WOLF_CRYPTO_CB_RSA_PAD
422+
int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
423+
word32* outLen, int type, RsaKey* key, WC_RNG* rng,
424+
RsaPadding *padding)
425+
{
426+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
427+
CryptoCb* dev;
428+
int pk_type;
429+
430+
if (key == NULL)
431+
return ret;
432+
433+
/* locate registered callback */
434+
dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
435+
436+
if (padding) {
437+
switch(padding->pad_type) {
438+
#ifndef NO_PKCS11_RSA_PKCS
439+
case WC_RSA_PKCSV15_PAD:
440+
pk_type = WC_PK_TYPE_RSA_PKCS;
441+
break;
442+
case WC_RSA_PSS_PAD:
443+
pk_type = WC_PK_TYPE_RSA_PSS;
444+
break;
445+
case WC_RSA_OAEP_PAD:
446+
pk_type = WC_PK_TYPE_RSA_OAEP;
447+
break;
448+
#endif /* NO_PKCS11_RSA_PKCS */
449+
default:
450+
pk_type = WC_PK_TYPE_RSA;
451+
}
452+
} else {
453+
pk_type = WC_PK_TYPE_RSA;
454+
}
455+
456+
if (dev && dev->cb) {
457+
wc_CryptoInfo cryptoInfo;
458+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
459+
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
460+
cryptoInfo.pk.type = pk_type;
461+
cryptoInfo.pk.rsa.in = in;
462+
cryptoInfo.pk.rsa.inLen = inLen;
463+
cryptoInfo.pk.rsa.out = out;
464+
cryptoInfo.pk.rsa.outLen = outLen;
465+
cryptoInfo.pk.rsa.type = type;
466+
cryptoInfo.pk.rsa.key = key;
467+
cryptoInfo.pk.rsa.rng = rng;
468+
cryptoInfo.pk.rsa.padding = padding;
469+
470+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
471+
}
472+
473+
return wc_CryptoCb_TranslateErrorCode(ret);
474+
}
475+
#endif
476+
421477
#ifdef WOLFSSL_KEY_GEN
422478
int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
423479
{

wolfcrypt/src/rsa.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,9 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31123112
int ret = 0;
31133113
(void)rng;
31143114
(void)checkSmallCt;
3115+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3116+
RsaPadding padding;
3117+
#endif
31153118

31163119
if (key == NULL || in == NULL || inLen == 0 || out == NULL ||
31173120
outLen == NULL || *outLen == 0 || type == RSA_TYPE_UNKNOWN) {
@@ -3123,7 +3126,18 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31233126
if (key->devId != INVALID_DEVID)
31243127
#endif
31253128
{
3129+
#if defined(WOLF_CRYPTO_CB_RSA_PAD)
3130+
/* If we are here, either the RSA PAD callback was already called
3131+
* and returned that it could not implement for that padding scheme,
3132+
* or this is a public verify operation. Either way indicate to the
3133+
* callback that this should be a raw RSA operation with no padding.*/
3134+
XMEMSET(&padding, 0, sizeof(RsaPadding));
3135+
padding.pad_type = WC_RSA_NO_PAD;
3136+
ret = wc_CryptoCb_RsaPad(in, inLen, out,
3137+
outLen, type, key, rng, &padding);
3138+
#else
31263139
ret = wc_CryptoCb_Rsa(in, inLen, out, outLen, type, key, rng);
3140+
#endif
31273141
#ifndef WOLF_CRYPTO_CB_ONLY_RSA
31283142
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
31293143
return ret;
@@ -3231,6 +3245,9 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
32313245
int ret = 0;
32323246
int sz;
32333247
int state;
3248+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3249+
RsaPadding padding;
3250+
#endif
32343251

32353252
if (in == NULL || inLen == 0 || out == NULL || key == NULL) {
32363253
return BAD_FUNC_ARG;
@@ -3327,6 +3344,29 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33273344
#endif
33283345
#endif /* WOLFSSL_SE050 */
33293346

3347+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3348+
if (key->devId != INVALID_DEVID) {
3349+
XMEMSET(&padding, 0, sizeof(RsaPadding));
3350+
padding.pad_value = pad_value;
3351+
padding.pad_type = pad_type;
3352+
padding.hash = hash;
3353+
padding.mgf = mgf;
3354+
padding.label = label;
3355+
padding.labelSz = labelSz;
3356+
padding.saltLen = saltLen;
3357+
ret = wc_CryptoCb_RsaPad(in, inLen, out, &outLen, rsa_type, key, rng,
3358+
&padding);
3359+
3360+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
3361+
if (ret < 0) {
3362+
break;
3363+
}
3364+
3365+
ret = outLen;
3366+
break;
3367+
}
3368+
}
3369+
#endif
33303370
key->state = RSA_STATE_ENCRYPT_PAD;
33313371
ret = wc_RsaPad_ex(in, inLen, out, (word32)sz, pad_value, rng, pad_type,
33323372
hash, mgf, label, labelSz, saltLen,
@@ -3406,6 +3446,9 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
34063446
{
34073447
int ret = WC_NO_ERR_TRACE(RSA_WRONG_TYPE_E);
34083448
byte* pad = NULL;
3449+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3450+
RsaPadding padding;
3451+
#endif
34093452

34103453
if (in == NULL || inLen == 0 || out == NULL || key == NULL) {
34113454
return BAD_FUNC_ARG;
@@ -3516,6 +3559,25 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35163559
FALL_THROUGH;
35173560

35183561
case RSA_STATE_DECRYPT_EXPTMOD:
3562+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3563+
if ((key->devId != INVALID_DEVID) && (rsa_type != RSA_PUBLIC_DECRYPT)) {
3564+
/* Everything except verify goes to crypto cb if
3565+
* WOLF_CRYPTO_CB_RSA_PAD defined */
3566+
XMEMSET(&padding, 0, sizeof(RsaPadding));
3567+
padding.pad_value = pad_value;
3568+
padding.pad_type = pad_type;
3569+
padding.hash = hash;
3570+
padding.mgf = mgf;
3571+
padding.label = label;
3572+
padding.labelSz = labelSz;
3573+
padding.saltLen = saltLen;
3574+
ret = wc_CryptoCb_RsaPad(in, inLen, out,
3575+
&outLen, rsa_type, key, rng, &padding);
3576+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
3577+
break;
3578+
}
3579+
}
3580+
#endif
35193581
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
35203582
!defined(WOLFSSL_NO_MALLOC)
35213583
ret = wc_RsaFunction_ex(key->data, inLen, key->data, &key->dataLen,

0 commit comments

Comments
 (0)