Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 232 additions & 12 deletions tests/api/test_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1997,12 +1997,21 @@ int test_wc_RsaDecisionCoverage(void)
!defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_NO_RSA_SW_PAD) && \
!defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256) && \
defined(USE_CERT_BUFFERS_2048) && !defined(HAVE_FIPS) && \
!defined(HAVE_SELFTEST)
defined(USE_CERT_BUFFERS_2048) && !defined(HAVE_SELFTEST) && \
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0)) && \
(!defined(WOLFSSL_ASYNC_CRYPT) || !(defined(WOLFSSL_ASYNC_CRYPT_SW) || \
defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)))
#define TEST_RSA_PENDING_CB

typedef struct RsaPendingCbCtx {
int error; /* returned instead of running the operation when non-zero */
int depth; /* WC_PENDING_E results before each operation runs */
int pends; /* WC_PENDING_E results given for the current operation */
} RsaPendingCbCtx;

static int rsa_pending_test_cb(int devId, wc_CryptoInfo* info, void* ctx)
{
RsaPendingCbCtx* dev = (RsaPendingCbCtx*)ctx;
RsaKey* key;
int ret;

Expand All @@ -2015,18 +2024,23 @@ static int rsa_pending_test_cb(int devId, wc_CryptoInfo* info, void* ctx)
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
}
#endif
if (*(const int*)ctx != 0) {
return *(const int*)ctx;
if (dev->error != 0) {
return dev->error;
}
/* Queue the job, and run it only when called again for it. */
if (dev->pends < dev->depth) {
dev->pends++;
return WC_NO_ERR_TRACE(WC_PENDING_E);
}
dev->pends = 0;

/* Finish the raw calculation before the caller polls for completion. */
key = info->pk.rsa.key;
key->devId = INVALID_DEVID;
ret = wc_RsaFunction(info->pk.rsa.in, info->pk.rsa.inLen,
info->pk.rsa.out, info->pk.rsa.outLen, info->pk.rsa.type,
key, info->pk.rsa.rng);
key->devId = devId;
return ret == 0 ? WC_NO_ERR_TRACE(WC_PENDING_E) : ret;
return ret;
}
#endif

Expand Down Expand Up @@ -2260,30 +2274,33 @@ int test_wc_RsaFeatureCoverage(void)
#ifdef TEST_RSA_PENDING_CB
if (EXPECT_SUCCESS()) {
int savedDevId = key.devId;
int deviceError = WC_NO_ERR_TRACE(WC_HW_E);
RsaPendingCbCtx dev;
const int pendingDevId = 0x52534150;
static const byte zeros[sizeof(cipher)] = {0};

XMEMSET(&dev, 0, sizeof(dev));
dev.error = WC_NO_ERR_TRACE(WC_HW_E);
ExpectIntEQ(wc_CryptoCb_RegisterDevice(pendingDevId,
rsa_pending_test_cb, &deviceError), 0);
rsa_pending_test_cb, &dev), 0);
if (EXPECT_SUCCESS()) {
key.devId = pendingDevId;
ExpectIntEQ(wc_RsaPublicEncrypt_ex(msg, sizeof(msg), cipher,
sizeof(cipher), &key, &rng, WC_RSA_OAEP_PAD,
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), deviceError);
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), dev.error);
ExpectBufEQ(cipher, zeros, sizeof(cipher));
ExpectIntEQ(wc_RsaPublicEncrypt(msg, sizeof(msg), cipher,
sizeof(cipher), &key, &rng), deviceError);
sizeof(cipher), &key, &rng), dev.error);

#ifdef WC_RSA_NONBLOCK
/* A would-block callback must preserve the padded input for retry. */
deviceError = FP_WOULDBLOCK;
dev.error = FP_WOULDBLOCK;
ExpectIntEQ(wc_RsaPublicEncrypt_ex(msg, sizeof(msg), cipher,
sizeof(cipher), &key, &rng, WC_RSA_OAEP_PAD,
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), FP_WOULDBLOCK);
ExpectIntNE(XMEMCMP(cipher, zeros, sizeof(cipher)), 0);
#endif
deviceError = 0;
dev.error = 0;
dev.depth = 1;
ExpectIntEQ(wc_RsaPublicEncrypt_ex(msg, sizeof(msg), cipher,
sizeof(cipher), &key, &rng, WC_RSA_OAEP_PAD,
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0),
Expand Down Expand Up @@ -2359,6 +2376,118 @@ int test_wc_RsaFeatureCoverage(void)
return EXPECT_RESULT();
} /* END test_wc_RsaFeatureCoverage */

#define TEST_RSA_PENDING_MAX_CALLS 8

#ifdef TEST_RSA_PENDING_CB
static int test_rsa_pending_verify(const byte* in, word32 inLen, byte* out,
word32 outLen, RsaKey* key)
{
int ret;
int calls = 0;

do {
ret = wc_RsaSSL_Verify(in, inLen, out, outLen, key);
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E) &&
++calls < TEST_RSA_PENDING_MAX_CALLS);

return ret;
}

static int test_rsa_pending_encrypt(const byte* in, word32 inLen, byte* out,
word32 outLen, RsaKey* key, WC_RNG* rng)
{
int ret;
int calls = 0;

do {
ret = wc_RsaPublicEncrypt(in, inLen, out, outLen, key, rng);
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E) &&
++calls < TEST_RSA_PENDING_MAX_CALLS);

return ret;
}
#endif

/* An RSA operation that a crypto callback pends must reach the callback again
* and return its result. */
int test_wc_RsaPending_CryptoCbReinvoke(void)
{
EXPECT_DECLS;
#ifdef TEST_RSA_PENDING_CB
RsaKey key;
WC_RNG rng;
RsaPendingCbCtx dev;
word32 idx = 0;
byte block[256];
byte sig[256];
byte cipher[256];
byte out[256];
int depth;
int initKey = 0;
int initRng = 0;
int registered = 0;
const int devId = 0x52535052;
static const byte digest[32] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f
};

XMEMSET(&key, 0, sizeof(key));
XMEMSET(&rng, 0, sizeof(rng));
XMEMSET(&dev, 0, sizeof(dev));

ExpectIntEQ(wc_InitRng(&rng), 0);
if (EXPECT_SUCCESS()) initRng = 1;
ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
if (EXPECT_SUCCESS()) initKey = 1;
ExpectIntEQ(wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &key,
sizeof_client_key_der_2048), 0);
#ifdef WC_RSA_BLINDING
ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
#endif

ExpectIntEQ(wc_RsaSSL_Sign(digest, sizeof(digest), sig, sizeof(sig), &key,
&rng), sizeof(sig));
ExpectIntEQ(wc_RsaPad_ex(digest, sizeof(digest), block, sizeof(block),
RSA_BLOCK_TYPE_1, &rng, WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE,
WC_MGF1NONE, NULL, 0, 0, 2048, HEAP_HINT), 0);

ExpectIntEQ(wc_CryptoCb_RegisterDevice(devId, rsa_pending_test_cb, &dev),
0);
if (EXPECT_SUCCESS()) registered = 1;

for (depth = 1; depth <= 2 && EXPECT_SUCCESS(); depth++) {
dev.depth = depth;
key.devId = devId;

ExpectIntLT(test_rsa_pending_verify(block, sizeof(block), out,
sizeof(out), &key), 0);

XMEMSET(out, 0, sizeof(out));
ExpectIntEQ(test_rsa_pending_verify(sig, sizeof(sig), out,
sizeof(out), &key), sizeof(digest));
ExpectBufEQ(out, digest, sizeof(digest));

ExpectIntEQ(test_rsa_pending_encrypt(digest, sizeof(digest), cipher,
sizeof(cipher), &key, &rng), sizeof(cipher));
/* Decrypt in software to check the ciphertext is real. */
key.devId = INVALID_DEVID;
XMEMSET(out, 0, sizeof(out));
ExpectIntEQ(wc_RsaPrivateDecrypt(cipher, sizeof(cipher), out,
sizeof(out), &key), sizeof(digest));
ExpectBufEQ(out, digest, sizeof(digest));
}

if (registered)
wc_CryptoCb_UnRegisterDevice(devId);
if (initKey) DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
if (initRng) DoExpectIntEQ(wc_FreeRng(&rng), 0);
#endif
return EXPECT_RESULT();
}

/* Test that wc_RsaPSS_VerifyCheck() routes RSA-PSS verification through a
* registered crypto callback (CryptoCb) device. */
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) && \
Expand Down Expand Up @@ -2912,6 +3041,97 @@ int test_wc_RsaOaep_EncryptFailureWipe(void)
return EXPECT_RESULT();
}

#if !defined(NO_RSA) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
defined(WC_ASYNC_ENABLE_RSA) && !defined(NO_RSA_BOUNDS_CHECK) && \
!defined(WC_NO_RSA_OAEP) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(NO_SHA256) && \
!defined(WC_NO_RNG) && defined(WC_RSA_BLINDING) && \
defined(USE_CERT_BUFFERS_2048) && \
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
#define TEST_RSA_OAEP_ASYNC_RESET

/* One OAEP-SHA256 operation, waiting out the async software device. */
static int test_rsa_oaep_async(int decrypt, const byte* in, word32 inLen,
byte* out, word32 outLen, RsaKey* key, WC_RNG* rng)
{
int ret = 0;
int calls = 0;

do {
ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
if (ret >= 0) {
if (decrypt) {
ret = wc_RsaPrivateDecrypt_ex(in, inLen, out, outLen, key,
WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
NULL, 0);
}
else {
ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outLen, key,
rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
NULL, 0);
}
}
} while (ret == WC_NO_ERR_TRACE(WC_PENDING_E) &&
++calls < TEST_RSA_PENDING_MAX_CALLS);

return ret;
}
#endif

/* An OAEP ciphertext of 1 refused while the async device runs it must leave
* the key usable for the next decryption. */
int test_wc_RsaOaep_AsyncRangeFailReset(void)
{
EXPECT_DECLS;
#ifdef TEST_RSA_OAEP_ASYNC_RESET
RsaKey key;
WC_RNG rng;
word32 idx = 0;
byte cipher[256];
byte one[256];
byte plain[256];
int cipherLen = 0;
int initKey = 0;
int initRng = 0;
static const byte msg[16] = {
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f
};

XMEMSET(&key, 0, sizeof(key));
XMEMSET(&rng, 0, sizeof(rng));
XMEMSET(one, 0, sizeof(one));
one[sizeof(one) - 1] = 1;

ExpectIntEQ(wc_InitRng(&rng), 0);
if (EXPECT_SUCCESS()) initRng = 1;
/* devId 0 selects the async software device. */
ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, 0), 0);
if (EXPECT_SUCCESS()) initKey = 1;
ExpectIntEQ(wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &key,
sizeof_client_key_der_2048), 0);
ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);

if (EXPECT_SUCCESS()) {
cipherLen = test_rsa_oaep_async(0, msg, sizeof(msg), cipher,
sizeof(cipher), &key, &rng);
}
ExpectIntEQ(cipherLen, sizeof(cipher));

ExpectIntLT(test_rsa_oaep_async(1, one, sizeof(one), plain, sizeof(plain),
&key, &rng), 0);

XMEMSET(plain, 0, sizeof(plain));
ExpectIntEQ(test_rsa_oaep_async(1, cipher, sizeof(cipher), plain,
sizeof(plain), &key, &rng), sizeof(msg));
ExpectBufEQ(plain, msg, sizeof(msg));

if (initKey) DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
if (initRng) DoExpectIntEQ(wc_FreeRng(&rng), 0);
#endif
return EXPECT_RESULT();
}

#if !defined(NO_RSA) && defined(WOLF_CRYPTO_CB) && \
defined(WOLF_CRYPTO_CB_FREE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
(defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
Expand Down
4 changes: 4 additions & 0 deletions tests/api/test_rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int test_wc_RsaKeyToPublicDer(void);
int test_wc_RsaPublicEncryptDecrypt(void);
int test_wc_RsaPublicEncryptDecrypt_ex(void);
int test_wc_RsaOaep_EncryptFailureWipe(void);
int test_wc_RsaOaep_AsyncRangeFailReset(void);
int test_wc_RsaEncryptSize(void);
int test_wc_RsaSSL_SignVerify(void);
int test_wc_RsaFlattenPublicKey(void);
Expand All @@ -50,6 +51,7 @@ int test_wc_RsaFunctionCheckIn_OversizedModulus(void);
int test_wc_RsaKeyToDer_SizeOverflow(void);
int test_wc_RsaDecisionCoverage(void);
int test_wc_RsaFeatureCoverage(void);
int test_wc_RsaPending_CryptoCbReinvoke(void);
int test_wc_CryptoCb_RsaPssVerify(void);
int test_wc_CryptoCb_RsaPssVerifyRecover(void);
int test_wc_MakeRsaKey_id_dev(void);
Expand All @@ -74,6 +76,7 @@ int test_wc_FreeRsaKey_CryptoCb(void);
TEST_DECL_GROUP("rsa", test_wc_RsaPublicEncryptDecrypt), \
TEST_DECL_GROUP("rsa", test_wc_RsaPublicEncryptDecrypt_ex), \
TEST_DECL_GROUP("rsa", test_wc_RsaOaep_EncryptFailureWipe), \
TEST_DECL_GROUP("rsa", test_wc_RsaOaep_AsyncRangeFailReset), \
TEST_DECL_GROUP("rsa", test_wc_RsaEncryptSize), \
TEST_DECL_GROUP("rsa", test_wc_RsaSSL_SignVerify), \
TEST_DECL_GROUP("rsa", test_wc_RsaFlattenPublicKey), \
Expand All @@ -82,6 +85,7 @@ int test_wc_FreeRsaKey_CryptoCb(void);
TEST_DECL_GROUP("rsa", test_wc_RsaKeyToDer_SizeOverflow), \
TEST_DECL_GROUP("rsa", test_wc_RsaDecisionCoverage), \
TEST_DECL_GROUP("rsa", test_wc_RsaFeatureCoverage), \
TEST_DECL_GROUP("rsa", test_wc_RsaPending_CryptoCbReinvoke), \
TEST_DECL_GROUP("rsa", test_wc_CryptoCb_RsaPssVerify), \
TEST_DECL_GROUP("rsa", test_wc_CryptoCb_RsaPssVerifyRecover), \
TEST_DECL_GROUP("rsa", test_wc_MakeRsaKey_id_dev), \
Expand Down
Loading
Loading