Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PK: don't use mbedtls_ecp_check_pub_priv() when USE_PSA is enabled #7391

Merged
merged 5 commits into from
Apr 7, 2023

Conversation

valeriosetti
Copy link
Contributor

@valeriosetti valeriosetti commented Apr 3, 2023

Instead of using the legacy mbedtls_ecp_check_pub_priv() function which was based on ECP math, we add a new option named eckey_check_pair_psa() which takes advantage of PSA.

Resolves #7387

Gatekeeper checklist

  • changelog not required because it's an internal improvement
  • backport not required because it's an improvement
  • tests not required: using already existing tests for the legacy function

Instead of using the legacy mbedtls_ecp_check_pub_priv() function which
was based on ECP math, we add a new option named eckey_check_pair_psa()
which takes advantage of PSA.
Of course, this is available when MBEDTLS_USE_PSA_CRYPTO in enabled.

Tests were also fixed accordingly.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
@valeriosetti valeriosetti added enhancement needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon labels Apr 3, 2023
@valeriosetti valeriosetti requested a review from mpg April 3, 2023 13:03
@valeriosetti valeriosetti self-assigned this Apr 3, 2023
@valeriosetti valeriosetti added the size-s Estimated task size: small (~2d) label Apr 3, 2023
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a misunderstanding about the scope of the task, and changes should be limited to PK. A few other points, and also the CI is unhappy.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
@valeriosetti valeriosetti requested a review from mpg April 4, 2023 08:23
mpg
mpg previously approved these changes Apr 4, 2023
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@mprse mprse self-requested a review April 5, 2023 07:23
@mpg mpg removed needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review labels Apr 5, 2023
Copy link
Contributor

@mprse mprse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left only few minor suggestions. Otherwise looks good.

Comment on lines 1145 to 1154
if (status != PSA_SUCCESS) {
ret = PSA_PK_TO_MBEDTLS_ERR(status);
status = psa_destroy_key(key_id);
return (status != PSA_SUCCESS) ? PSA_PK_TO_MBEDTLS_ERR(status) : ret;
}

status = psa_destroy_key(key_id);
if (status != PSA_SUCCESS) {
return PSA_PK_TO_MBEDTLS_ERR(status);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to psa_destroy_key(key_id) is duplicated. Key is destroyed regardless of status of psa_export_public_key. Seems that this can be optimized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key is destroyed regardless of status of psa_export_public_key

But if psa_export_public_key() fails then we need to destroy the key that was imported few lines above before returning from this function. Am I missing something?

Copy link
Contributor Author

@valeriosetti valeriosetti Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the possible optimization I thought something like this:

    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);
    }

Is that what you were looking for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done ;)

size_t pub_key_len;
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
size_t curve_bits;
psa_ecc_family_t curve =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
psa_ecc_family_t curve =
const psa_ecc_family_t curve =

size_t curve_bits;
psa_ecc_family_t curve =
mbedtls_ecc_group_to_psa(prv_ctx->grp.id, &curve_bits);
size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Copy link
Contributor

@mprse mprse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing my comments! LGTM

Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Apr 7, 2023
@mpg mpg added needs-ci Needs to pass CI tests and removed needs-ci Needs to pass CI tests labels Apr 7, 2023
@mpg mpg merged commit f740767 into Mbed-TLS:development Apr 7, 2023
@valeriosetti valeriosetti deleted the issue7387 branch December 6, 2024 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports enhancement priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PK: don't use mbedtls_ecp_check_pub_priv() when USE_PSA is enabled.
4 participants