Skip to content

Commit 2bcfff3

Browse files
committed
Expand testing to include SW implementation of RSA with padding callback, code cleanup to address review comments.
1 parent 50a3a37 commit 2bcfff3

2 files changed

Lines changed: 92 additions & 7 deletions

File tree

tests/api.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83662,7 +83662,71 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
8366283662
else if (info->pk.type == WC_PK_TYPE_RSA_PKCS ||
8366383663
info->pk.type == WC_PK_TYPE_RSA_PSS ||
8366483664
info->pk.type == WC_PK_TYPE_RSA_OAEP) {
83665-
ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
83665+
RsaKey key;
83666+
83667+
if (info->pk.rsa.type == RSA_PUBLIC_ENCRYPT ||
83668+
info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
83669+
/* Have all public key ops fall back to SW */
83670+
return CRYPTOCB_UNAVAILABLE;
83671+
}
83672+
83673+
if (info->pk.rsa.padding == NULL) {
83674+
return BAD_FUNC_ARG;
83675+
}
83676+
83677+
/* Initialize key */
83678+
ret = load_pem_key_file_as_der(privKeyFile, &pDer,
83679+
&keyFormat);
83680+
if (ret != 0) {
83681+
return ret;
83682+
}
83683+
83684+
ret = wc_InitRsaKey(&key, HEAP_HINT);
83685+
if (ret == 0) {
83686+
word32 keyIdx = 0;
83687+
/* load RSA private key and perform private transform */
83688+
ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
83689+
&key, pDer->length);
83690+
}
83691+
/* Perform RSA operation */
83692+
if ((ret == 0) && (info->pk.type == WC_PK_TYPE_RSA_PKCS)) {
83693+
#if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
83694+
ret = wc_RsaSSL_Sign(info->pk.rsa.in, info->pk.rsa.inLen,
83695+
info->pk.rsa.out, *info->pk.rsa.outLen, &key,
83696+
info->pk.rsa.rng);
83697+
#else
83698+
ret = CRYPTOCB_UNAVAILABLE;
83699+
#endif
83700+
}
83701+
if ((ret == 0) && (info->pk.type == WC_PK_TYPE_RSA_PSS)) {
83702+
#ifdef WC_RSA_PSS
83703+
ret = wc_RsaPSS_Sign_ex(info->pk.rsa.in, info->pk.rsa.inLen,
83704+
info->pk.rsa.out, *info->pk.rsa.outLen,
83705+
info->pk.rsa.padding->hash, info->pk.rsa.padding->mgf,
83706+
info->pk.rsa.padding->saltLen, &key, info->pk.rsa.rng);
83707+
#else
83708+
ret = CRYPTOCB_UNAVAILABLE;
83709+
#endif
83710+
}
83711+
if ((ret == 0) && (info->pk.type == WC_PK_TYPE_RSA_OAEP)) {
83712+
#if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_NO_PADDING)
83713+
ret = wc_RsaPrivateDecrypt_ex(
83714+
info->pk.rsa.in, info->pk.rsa.inLen,
83715+
info->pk.rsa.out, *info->pk.rsa.outLen,
83716+
&key, WC_RSA_OAEP_PAD, info->pk.rsa.padding->hash,
83717+
info->pk.rsa.padding->mgf, info->pk.rsa.padding->label,
83718+
info->pk.rsa.padding->labelSz);
83719+
#else
83720+
ret = CRYPTOCB_UNAVAILABLE;
83721+
#endif
83722+
}
83723+
83724+
if (ret > 0) {
83725+
*info->pk.rsa.outLen = ret;
83726+
}
83727+
83728+
wc_FreeRsaKey(&key);
83729+
wc_FreeDer(&pDer); pDer = NULL;
8366683730
}
8366783731
#endif /* ifdef WOLF_CRYPTO_CB_RSA_PAD */
8366883732
#endif /* !NO_RSA */

wolfcrypt/src/wc_pkcs11.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,57 +1809,78 @@ static int Pkcs11RsaPrivateKey(Pkcs11Session* session, RsaKey* rsaKey,
18091809
return ret;
18101810
}
18111811

1812+
/**
1813+
* Get the hash length associated with the WolfCrypt hash type.
1814+
*
1815+
* @param [in] hType Hash Type.
1816+
* @return -1 if hash type not recognized.
1817+
* @return hash length on success.
1818+
*/
18121819
int wc_hash2sz(int hType)
18131820
{
18141821
switch(hType) {
18151822
case WC_HASH_TYPE_SHA:
18161823
return 20;
1824+
case WC_HASH_TYPE_SHA224:
1825+
return 24;
18171826
case WC_HASH_TYPE_SHA256:
18181827
return 32;
18191828
case WC_HASH_TYPE_SHA384:
18201829
return 48;
18211830
case WC_HASH_TYPE_SHA512:
18221831
return 64;
1823-
case WC_HASH_TYPE_SHA224:
1824-
return 24;
18251832
default:
18261833
/* unsupported WC_HASH_TYPE_XXXX */
18271834
return -1;
18281835
}
18291836
}
18301837

1838+
/**
1839+
* Get PKCS11 hash mechanism associated with the WolfCrypt hash type.
1840+
*
1841+
* @param [in] hType Hash Type.
1842+
* @return 0 if hash type not recognized.
1843+
* @return PKCS11 mechanism on success.
1844+
*/
18311845
CK_MECHANISM_TYPE wc_hash2ckm(int hType)
18321846
{
18331847
switch(hType) {
18341848
case WC_HASH_TYPE_SHA:
18351849
return CKM_SHA_1;
1850+
case WC_HASH_TYPE_SHA224:
1851+
return CKM_SHA224;
18361852
case WC_HASH_TYPE_SHA256:
18371853
return CKM_SHA256;
18381854
case WC_HASH_TYPE_SHA384:
18391855
return CKM_SHA384;
18401856
case WC_HASH_TYPE_SHA512:
18411857
return CKM_SHA512;
1842-
case WC_HASH_TYPE_SHA224:
1843-
return CKM_SHA224;
18441858
default:
18451859
/* unsupported WC_HASH_TYPE_XXXX */
18461860
return 0UL;
18471861
}
18481862
}
18491863

1864+
/**
1865+
* Get PKCS11 MGF hash mechanism associated with the WolfCrypt MGF hash type.
1866+
*
1867+
* @param [in] mgf MGF Type.
1868+
* @return 0 if MGF type not recognized.
1869+
* @return PKCS11 MGF hash mechanism on success.
1870+
*/
18501871
CK_MECHANISM_TYPE wc_mgf2ckm(int mgf)
18511872
{
18521873
switch(mgf) {
18531874
case WC_MGF1SHA1:
18541875
return CKG_MGF1_SHA1;
1876+
case WC_MGF1SHA224:
1877+
return CKG_MGF1_SHA224;
18551878
case WC_MGF1SHA256:
18561879
return CKG_MGF1_SHA256;
18571880
case WC_MGF1SHA384:
18581881
return CKG_MGF1_SHA384;
18591882
case WC_MGF1SHA512:
18601883
return CKG_MGF1_SHA512;
1861-
case WC_MGF1SHA224:
1862-
return CKG_MGF1_SHA224;
18631884
default:
18641885
/* unsupported WC_MGF1XXXX */
18651886
return 0x0UL;

0 commit comments

Comments
 (0)