Skip to content

Commit 37c70c4

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 5facbf3 commit 37c70c4

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
@@ -25012,7 +25012,7 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2501225012
#else
2501325013
byte der[1280];
2501425014
#endif
25015-
#ifndef WOLFSSL_CRYPTOCELL
25015+
#if !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
2501625016
word32 idx = 0;
2501725017
#endif
2501825018
int derSz = 0;
@@ -25089,13 +25089,16 @@ static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
2508925089
if (ret != 0)
2509025090
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
2509125091

25092-
#ifndef WOLFSSL_CRYPTOCELL
25092+
#if !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
2509325093
idx = 0;
25094-
/* The private key part of the key gen pairs from cryptocell can't be exported */
25094+
/* The private key part of key pairs generated inside a secure element
25095+
* (CryptoCell, SE050) stays in hardware and isn't available to
25096+
* wc_RsaKeyToDer, so the exported DER can't be parsed back as a
25097+
* complete RSAPrivateKey. */
2509525098
ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, (word32)derSz);
2509625099
if (ret != 0)
2509725100
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
25098-
#endif /* WOLFSSL_CRYPTOCELL */
25101+
#endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */
2509925102

2510025103
exit_rsa:
2510125104

0 commit comments

Comments
 (0)