@@ -810,7 +810,7 @@ int se050_rsa_get_key_id(struct RsaKey* key, word32* keyId)
810810int se050_rsa_create_key (struct RsaKey * key , int size , long e )
811811{
812812 int ret = 0 ;
813- word32 keyId ;
813+ word32 keyId = 0 ;
814814 int keyCreated = 0 ;
815815 sss_status_t status = kStatus_SSS_Success ;
816816 sss_object_t keyPair ;
@@ -1483,7 +1483,7 @@ int se050_rsa_verify(const byte* in, word32 inLen, byte* out, word32 outLen,
14831483 keyId = se050_allocate_key (SE050_RSA_KEY );
14841484 status = sss_key_object_allocate_handle (& newKey , keyId ,
14851485 kSSS_KeyPart_Public , kSSS_CipherType_RSA , keySz ,
1486- kKeyObject_Mode_Persistent );
1486+ kKeyObject_Mode_Transient );
14871487 }
14881488 if (status == kStatus_SSS_Success ) {
14891489 /* Try to delete existing key first, ignore return since will
@@ -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 ) {
@@ -1696,8 +1710,17 @@ int se050_rsa_public_encrypt(const byte* in, word32 inLen, byte* out,
16961710 }
16971711
16981712 if (status == kStatus_SSS_Success ) {
1699- key -> keyId = keyId ;
1700- key -> keyIdSet = 1 ;
1713+ if (keyCreated ) {
1714+ /* Public-key encrypt imported a temporary public object only.
1715+ * Do not bind that SE050 object to the caller's RsaKey or later
1716+ * private-key operations will try to reuse a public handle. */
1717+ sss_key_store_erase_key (& host_keystore , & newKey );
1718+ sss_key_object_free (& newKey );
1719+ }
1720+ else {
1721+ key -> keyId = keyId ;
1722+ key -> keyIdSet = 1 ;
1723+ }
17011724 ret = encSz ;
17021725 }
17031726 else {
@@ -2123,11 +2146,8 @@ int se050_ecc_sign_hash_ex(const byte* in, word32 inLen, MATH_INT_T* r, MATH_INT
21232146
21242147 algorithm = se050_map_hash_alg (inLen );
21252148 if (algorithm == kAlgorithm_None ) {
2126- inLen = keySize ; /* try key size */
2127- algorithm = se050_map_hash_alg (inLen );
2128- }
2129- if (algorithm == kAlgorithm_None ) {
2130- return ECC_CURVE_OID_E ;
2149+ WOLFSSL_MSG ("SE050 ECDSA sign only supports SHA-1/224/256/384/512 digest sizes" );
2150+ return BAD_LENGTH_E ;
21312151 }
21322152
21332153 if (wolfSSL_CryptHwMutexLock () != 0 ) {
@@ -2294,11 +2314,8 @@ int se050_ecc_verify_hash_ex(const byte* hash, word32 hashLen, MATH_INT_T* r,
22942314
22952315 algorithm = se050_map_hash_alg (hashLen );
22962316 if (algorithm == kAlgorithm_None ) {
2297- hashLen = keySize ; /* try key size */
2298- algorithm = se050_map_hash_alg (hashLen );
2299- }
2300- if (algorithm == kAlgorithm_None ) {
2301- return ECC_CURVE_OID_E ;
2317+ WOLFSSL_MSG ("SE050 ECDSA verify only supports SHA-1/224/256/384/512 digest sizes" );
2318+ return BAD_LENGTH_E ;
23022319 }
23032320
23042321 if (wolfSSL_CryptHwMutexLock () != 0 ) {
@@ -2577,7 +2594,7 @@ int se050_ecc_create_key(struct ecc_key* key, int curve_id, int keySize)
25772594 sss_key_store_t host_keystore ;
25782595 uint8_t derBuf [SE050_ECC_DER_MAX ];
25792596 size_t derSz = sizeof (derBuf );
2580- word32 keyId ;
2597+ word32 keyId = 0 ;
25812598 int keySizeBits ;
25822599 sss_cipher_type_t curveType ;
25832600 int keyCreated = 0 ;
@@ -2671,7 +2688,7 @@ int se050_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key,
26712688 sss_object_t ref_public_key ;
26722689 sss_object_t deriveKey ;
26732690 sss_derive_key_t ctx_derive_key ;
2674- word32 keyId ;
2691+ word32 keyId = 0 ;
26752692 int keySize ;
26762693 int keySizeBits ;
26772694 sss_cipher_type_t curveType ;
@@ -3039,6 +3056,12 @@ int se050_ed25519_verify_msg(const byte* signature, word32 signatureLen,
30393056 key , signature , signatureLen , msg , msgLen );
30403057#endif
30413058
3059+ if (signature == NULL || msg == NULL || key == NULL || res == NULL ) {
3060+ return BAD_FUNC_ARG ;
3061+ }
3062+
3063+ * res = 0 ;
3064+
30423065 if (cfg_se050_i2c_pi == NULL ) {
30433066 return WC_HW_E ;
30443067 }
@@ -3099,8 +3122,21 @@ int se050_ed25519_verify_msg(const byte* signature, word32 signatureLen,
30993122 }
31003123
31013124 if (status == kStatus_SSS_Success ) {
3102- key -> keyId = keyId ;
3103- key -> keyIdSet = 1 ;
3125+ if (keyCreated ) {
3126+ /* We uploaded only the public part of the key for this verify.
3127+ * Don't persist keyIdSet=1 -- a later sign on the same ed25519_key
3128+ * would reuse this binding and fail because the SE050 object has
3129+ * no private material. Erase the transient object so the next
3130+ * SE050 op re-uploads. Mirrors the fix in se050_rsa_verify. */
3131+ sss_key_store_erase_key (& host_keystore , & newKey );
3132+ sss_key_object_free (& newKey );
3133+ }
3134+ else {
3135+ /* Pre-existing keyIdSet=1 binding (from prior sign that uploaded
3136+ * a keypair, or explicit caller setup). Preserve it. */
3137+ key -> keyId = keyId ;
3138+ key -> keyIdSet = 1 ;
3139+ }
31043140 * res = 1 ;
31053141 ret = 0 ;
31063142 }
0 commit comments