@@ -30413,7 +30413,8 @@ static wc_test_ret_t rsa_certreq_test(RsaKey* key, RsaKey* keypub,
3041330413/* Exercise both routing paths under WOLFSSL_SE050_ONLY_KEY_ID for RSA: a
3041430414 * generated software key (keyIdSet == 0 -> wolfCrypt software) and the same key
3041530415 * promoted into the SE050 (keyIdSet == 1 -> hardware). PKCS#1 v1.5 sign/verify
30416- * are checked on each path and cross-checked for interoperability. */
30416+ * and public-encrypt/private-decrypt are checked on each path and cross-checked
30417+ * for interoperability. */
3041730418static wc_test_ret_t rsa_se050_onlykeyid_test(WC_RNG* rng)
3041830419{
3041930420 wc_test_ret_t ret;
@@ -30424,9 +30425,11 @@ static wc_test_ret_t rsa_se050_onlykeyid_test(WC_RNG* rng)
3042430425 byte in[32];
3042530426 byte sigSw[256];
3042630427 byte sigHw[256];
30428+ byte encSw[256];
30429+ byte encHw[256];
3042730430 byte plain[256];
3042830431 word32 inLen = (word32)sizeof(in);
30429- int sigSwSz, sigHwSz, plainSz;
30432+ int sigSwSz, sigHwSz, encSwSz, encHwSz, plainSz;
3043030433
3043130434 XMEMSET(in, 7, sizeof(in));
3043230435
@@ -30487,6 +30490,53 @@ static wc_test_ret_t rsa_se050_onlykeyid_test(WC_RNG* rng)
3048730490 if (sigSwSz != sigHwSz || XMEMCMP(sigSw, sigHw, (size_t)sigSwSz) != 0) {
3048830491 ret = WC_TEST_RET_ENC_NC; goto done;
3048930492 }
30493+
30494+ /* The software private-key path (wc_RsaPrivateDecrypt) takes no RNG
30495+ * argument, so with RSA blinding enabled the RNG must be attached to the
30496+ * key beforehand. The SE050 path does not need it. */
30497+ #ifdef WC_RSA_BLINDING
30498+ ret = wc_RsaSetRNG(&swKey, rng);
30499+ if (ret != 0) { ret = WC_TEST_RET_ENC_EC(ret); goto done; }
30500+ #endif
30501+
30502+ /* Software path: public-encrypt + private-decrypt round-trip. */
30503+ encSwSz = wc_RsaPublicEncrypt(in, inLen, encSw, (word32)sizeof(encSw),
30504+ &swKey, rng);
30505+ if (encSwSz < 0) { ret = WC_TEST_RET_ENC_EC(encSwSz); goto done; }
30506+ plainSz = wc_RsaPrivateDecrypt(encSw, (word32)encSwSz, plain,
30507+ (word32)sizeof(plain), &swKey);
30508+ if (plainSz < 0) { ret = WC_TEST_RET_ENC_EC(plainSz); goto done; }
30509+ if ((word32)plainSz != inLen || XMEMCMP(plain, in, inLen) != 0) {
30510+ ret = WC_TEST_RET_ENC_NC; goto done;
30511+ }
30512+
30513+ /* Hardware path: public-encrypt + private-decrypt round-trip. */
30514+ encHwSz = wc_RsaPublicEncrypt(in, inLen, encHw, (word32)sizeof(encHw),
30515+ &hwKey, rng);
30516+ if (encHwSz < 0) { ret = WC_TEST_RET_ENC_EC(encHwSz); goto done; }
30517+ plainSz = wc_RsaPrivateDecrypt(encHw, (word32)encHwSz, plain,
30518+ (word32)sizeof(plain), &hwKey);
30519+ if (plainSz < 0) { ret = WC_TEST_RET_ENC_EC(plainSz); goto done; }
30520+ if ((word32)plainSz != inLen || XMEMCMP(plain, in, inLen) != 0) {
30521+ ret = WC_TEST_RET_ENC_NC; goto done;
30522+ }
30523+
30524+ /* Cross-decrypt: PKCS#1 v1.5 encryption padding is randomized, so the
30525+ * ciphertexts cannot be compared directly like the signatures above.
30526+ * Instead confirm the software and hardware paths interoperate by having
30527+ * each key decrypt the ciphertext produced by the other. */
30528+ plainSz = wc_RsaPrivateDecrypt(encSw, (word32)encSwSz, plain,
30529+ (word32)sizeof(plain), &hwKey);
30530+ if (plainSz < 0) { ret = WC_TEST_RET_ENC_EC(plainSz); goto done; }
30531+ if ((word32)plainSz != inLen || XMEMCMP(plain, in, inLen) != 0) {
30532+ ret = WC_TEST_RET_ENC_NC; goto done;
30533+ }
30534+ plainSz = wc_RsaPrivateDecrypt(encHw, (word32)encHwSz, plain,
30535+ (word32)sizeof(plain), &swKey);
30536+ if (plainSz < 0) { ret = WC_TEST_RET_ENC_EC(plainSz); goto done; }
30537+ if ((word32)plainSz != inLen || XMEMCMP(plain, in, inLen) != 0) {
30538+ ret = WC_TEST_RET_ENC_NC; goto done;
30539+ }
3049030540 ret = 0;
3049130541
3049230542done:
0 commit comments