From 1df94f841b26e2b592ffb893afab6c95e311c5d5 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Fri, 7 Apr 2023 08:59:24 +0200 Subject: [PATCH] pk: fix return codes' precedence and code style Signed-off-by: Valerio Setti --- library/pk_wrap.c | 21 +++++++++++---------- library/pkparse.c | 22 ++++++++++------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/library/pk_wrap.c b/library/pk_wrap.c index a5eb46595..4e5293df5 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -1107,7 +1107,7 @@ cleanup: static int eckey_check_pair_psa(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv) { - psa_status_t status; + psa_status_t status, destruction_status; psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; mbedtls_ecp_keypair *prv_ctx = (mbedtls_ecp_keypair *) prv; mbedtls_ecp_keypair *pub_ctx = (mbedtls_ecp_keypair *) pub; @@ -1134,20 +1134,21 @@ static int eckey_check_pair_psa(const mbedtls_ecp_keypair *pub, } status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id); - if (status != PSA_SUCCESS) { - ret = PSA_PK_TO_MBEDTLS_ERR(status); + ret = PSA_PK_TO_MBEDTLS_ERR(status); + if (ret != 0) { return ret; } mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); - ret = PSA_PK_TO_MBEDTLS_ERR(psa_export_public_key(key_id, - prv_key_buf, - sizeof(prv_key_buf), - &prv_key_len)); - status = psa_destroy_key(key_id); - if (ret != 0 || status != PSA_SUCCESS) { - return (ret != 0) ? ret : PSA_PK_TO_MBEDTLS_ERR(status); + status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), + &prv_key_len); + ret = PSA_PK_TO_MBEDTLS_ERR(status); + destruction_status = psa_destroy_key(key_id); + if (ret != 0) { + return ret; + } else if (destruction_status != PSA_SUCCESS) { + return PSA_PK_TO_MBEDTLS_ERR(destruction_status); } ret = mbedtls_ecp_point_write_binary(&pub_ctx->grp, &pub_ctx->Q, diff --git a/library/pkparse.c b/library/pkparse.c index 73e7d8bf0..93f435d10 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -885,7 +885,7 @@ cleanup: static int pk_derive_public_key(mbedtls_ecp_group *grp, mbedtls_ecp_point *Q, const mbedtls_mpi *d) { - psa_status_t status; + psa_status_t status, destruction_status; psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; size_t curve_bits; psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(grp->id, &curve_bits); @@ -906,26 +906,24 @@ static int pk_derive_public_key(mbedtls_ecp_group *grp, mbedtls_ecp_point *Q, } status = psa_import_key(&key_attr, key_buf, key_len, &key_id); - if (status != PSA_SUCCESS) { - ret = psa_pk_status_to_mbedtls(status); + ret = psa_pk_status_to_mbedtls(status); + if (ret != 0) { return ret; } mbedtls_platform_zeroize(key_buf, sizeof(key_buf)); + status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len); - if (status != PSA_SUCCESS) { - ret = psa_pk_status_to_mbedtls(status); - status = psa_destroy_key(key_id); - return (status != PSA_SUCCESS) ? psa_pk_status_to_mbedtls(status) : ret; + ret = psa_pk_status_to_mbedtls(status); + destruction_status = psa_destroy_key(key_id); + if (ret != 0) { + return ret; + } else if (destruction_status != PSA_SUCCESS) { + return psa_pk_status_to_mbedtls(destruction_status); } ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len); - status = psa_destroy_key(key_id); - if (status != PSA_SUCCESS) { - return psa_pk_status_to_mbedtls(status); - } - return ret; } #endif /* MBEDTLS_USE_PSA_CRYPTO */