Skip to content

Commit f45a489

Browse files
committed
Fix SE050 RSA port bugs
- se050_rsa_verify: when the function uploads only the public part of the key (keyCreated == 1), erase the transient SE050 object and don't persist keyIdSet = 1. A subsequent sign on the same RsaKey was reusing the public-only SE050 object and failing. Pre-existing bindings (from wc_RsaUseKeyId or a prior sign that uploaded a keypair) are preserved untouched. - rsa_keygen_test: add WOLFSSL_SE050 to the existing WOLFSSL_CRYPTOCELL guard around the export-then-decode round-trip. SE050-generated keys keep their private components in the secure element, so wc_RsaKeyToDer + wc_RsaPrivateKeyDecode cannot complete. Matching guard on the idx declaration to avoid an unused-variable warning.
1 parent c3aeeab commit f45a489

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

wolfcrypt/src/port/nxp/se050_port.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,22 @@ int se050_rsa_verify(const byte* in, word32 inLen, byte* out, word32 outLen,
15381538
}
15391539

15401540
if (status == kStatus_SSS_Success) {
1541-
key->keyId = keyId;
1542-
key->keyIdSet = 1;
1541+
if (keyCreated) {
1542+
/* We uploaded only the public part of the key for this verify.
1543+
* Don't persist keyIdSet=1 — a later sign on the same RsaKey
1544+
* would reuse this binding and fail because the SE050 object has
1545+
* no private material. Erase the transient object so the next
1546+
* SE050 op (sign or verify) re-uploads from whatever the host
1547+
* RsaKey currently holds. */
1548+
sss_key_store_erase_key(&host_keystore, &newKey);
1549+
sss_key_object_free(&newKey);
1550+
}
1551+
else {
1552+
/* Pre-existing keyIdSet=1 binding (e.g. wc_RsaUseKeyId or prior
1553+
* sign that uploaded a keypair). Preserve it. */
1554+
key->keyId = keyId;
1555+
key->keyIdSet = 1;
1556+
}
15431557
}
15441558
else {
15451559
if (keyCreated) {

wolfcrypt/test/test.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25357,7 +25357,7 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2535725357
#else
2535825358
byte der[1280];
2535925359
#endif
25360-
#ifndef WOLFSSL_CRYPTOCELL
25360+
#if !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
2536125361
word32 idx = 0;
2536225362
#endif
2536325363
int derSz = 0;
@@ -25435,13 +25435,16 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2543525435
if (ret != 0)
2543625436
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
2543725437

25438-
#ifndef WOLFSSL_CRYPTOCELL
25438+
#if !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
2543925439
idx = 0;
25440-
/* The private key part of the key gen pairs from cryptocell can't be exported */
25440+
/* The private key part of key pairs generated inside a secure element
25441+
* (CryptoCell, SE050) stays in hardware and isn't available to
25442+
* wc_RsaKeyToDer, so the exported DER can't be parsed back as a
25443+
* complete RSAPrivateKey. */
2544125444
ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
2544225445
if (ret != 0)
2544325446
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
25444-
#endif /* WOLFSSL_CRYPTOCELL */
25447+
#endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */
2544525448
#endif /* !WC_TEST_SKIP_RSA_PRIVATE_EXPORT */
2544625449

2544725450
exit_rsa:

0 commit comments

Comments
 (0)