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

Fix psa_key_derivation_input_integer() not detecting bad state #186

Open
wants to merge 5 commits into
base: development
Choose a base branch
from

Conversation

waleed-elmelegy-arm
Copy link
Contributor

@waleed-elmelegy-arm waleed-elmelegy-arm commented Feb 27, 2025

Description

fixes #185
Fix psa_key_derivation_input_integer() not detecting bad state after an operation has been aborted.

changed some existing tests my justification is after they have returned PSA_ERROR_BAD_STATE they
were expected to return PSA_ERROR_INVALID_ARGUMENT but this shouldn't happen as the user is
expected to call abort() after a function has returned an error and we call abort() internally in case of an error
and without calling setup() first we should return PSA_ERROR_BAD_STATE and not any other error.

PR checklist

Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line.
If the provided content is part of the present PR remove the # symbol.

  • changelog provided
  • framework PR not required
  • mbedtls development PR not required
  • mbedtls 3.6 PR provided: 10025
  • mbedtls 2.28 PR provided: 10026
  • tests provided

@waleed-elmelegy-arm waleed-elmelegy-arm added needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review size-xs Estimated task size: extra small (a few hours at most) needs-ci Needs to pass CI tests labels Feb 27, 2025
@waleed-elmelegy-arm waleed-elmelegy-arm self-assigned this Feb 27, 2025
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
@waleed-elmelegy-arm waleed-elmelegy-arm force-pushed the fix-key-deriv-bad-state-error branch from 64f48d6 to 6b7da82 Compare February 27, 2025 17:16
@gilles-peskine-arm gilles-peskine-arm added priority-medium Medium priority - this can be reviewed as time permits needs-backports Backports are missing or are pending review and approval. labels Feb 27, 2025
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm 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 providing a fix and a test!

I'd just like to avoid making the key derivation testing function even more complicated.

@@ -8954,6 +8954,25 @@ void derive_input(int alg_arg,
}
TEST_EQUAL(actual_output_status, expected_output_status);

/* Test calling input functions after operation has been aborted
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding a test! But please don't add extra code to this function, which is already more complex than I'd like.

Proposal 1: keep this function, but skip the call to psa_key_derivation_setup() when alg == 0. Add test cases that expect that each input attempt returns BAD_STATE.

Proposal 2: make a new function that tries input_xxx without setup.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review labels Feb 27, 2025
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
@waleed-elmelegy-arm waleed-elmelegy-arm added needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review and removed needs-work labels Feb 28, 2025
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm 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 updating the tests. LGTM.

@@ -8900,7 +8900,9 @@ void derive_input(int alg_arg,
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
psa_set_key_algorithm(&attributes, alg);

PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
if (alg != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: the code has 0 and the test data has PSA_ALG_NONE. The value of PSA_ALG_NONE won't ever change, but it's a bit confusing for readers who aren't very familiar with the API.

Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@minosgalanakis minosgalanakis left a comment

Choose a reason for hiding this comment

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

Looks good overall, one minor suggestion for maintainability purposes only.

@@ -7612,6 +7612,12 @@ static psa_status_t psa_key_derivation_input_internal(
psa_status_t status;
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);

if (kdf_alg == 0) {
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
if (kdf_alg == 0) {
if (kdf_alg == PSA_ALG_NONE) {

3 occurences.

I think the intent of @gilles-peskine-arm's comment was to keep it consistent between codebase and tests?

It woudl be good for users revewing the code to understand that this exit's purpose is.

Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@minosgalanakis minosgalanakis left a comment

Choose a reason for hiding this comment

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

LGTM

@minosgalanakis minosgalanakis added approved Design and code approved - may be waiting for CI or backports needs-ci Needs to pass CI tests and removed needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review labels Mar 10, 2025
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 needs-ci Needs to pass CI tests priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most)
Projects
Status: Has Approval
Development

Successfully merging this pull request may close these issues.

psa_key_derivation_input_integer() does not detect bad state
3 participants