Skip to content

Commit 08dfcea

Browse files
committed
cipher: fixed key ownership scope
Indicate key ownership earlier, so that it gets destroyed on faulty policy setting.
1 parent c750932 commit 08dfcea

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

library/cipher.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
312312
if( status != PSA_SUCCESS )
313313
return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
314314

315+
/* Indicate that we own the key slot and need to
316+
* destroy it in mbedtls_cipher_free(). */
317+
cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
318+
319+
/* From that point on, the responsibility for destroying the
320+
* key slot is on mbedtls_cipher_free(). This includes the case
321+
* where the policy setup or key import below fail, as
322+
* mbedtls_cipher_free() needs to be called in any case. */
323+
315324
/* Setup policy for the new key slot. */
316325
psa_key_policy_init( &key_policy );
317326

@@ -325,14 +334,6 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
325334
status = psa_set_key_policy( cipher_psa->slot, &key_policy );
326335
if( status != PSA_SUCCESS )
327336
return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
328-
/* Indicate that we own the key slot and need to
329-
* destroy it in mbedtls_cipher_free(). */
330-
cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
331-
332-
/* From that point on, the responsibility for destroying the
333-
* key slot is on mbedtls_cipher_free(). This includes the case
334-
* where the policy setup or key import below fail, as
335-
* mbedtls_cipher_free() needs to be called in any case. */
336337

337338
/* Populate new key slot. */
338339
status = psa_import_key( cipher_psa->slot,

0 commit comments

Comments
 (0)