The Wayback Machine - https://web.archive.org/web/20201109182219/https://github.com/ARMmbed/mbedtls/pull/3780
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

Add validate_key hooks and tests #3780

Conversation

@stevew817
Copy link
Contributor

@stevew817 stevew817 commented Oct 13, 2020

Description

This PR sets up the validate_key entry point as described in #3695 , and with the clearer distinction between 'import' and 'load' actions, cleans these up in the PSA Crypto Core to provide better performance on load-from-storage, as well as semantically cleaner code.

@torstenes for the bit size persistency issue
@gilles-peskine-arm who has defined validate_key.

Status

READY

Requires Backporting

NO

Migrations

NO

Todos

  • Tests
  • Documentation
  • Changelog updated

Steps to test or reproduce

Relevant tests added to regression suite.

@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 4db092b to 7988a81 Oct 13, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 14, 2020

@gilles-peskine-arm / @ronald-cron-arm / @danh-arm what's the CI complaining about?

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 14, 2020

One of the errors (looks the main one):

In file included from /var/lib/build/library/psa_crypto_driver_wrappers.c:22:
/var/lib/build/library/psa_crypto_core.h:147:15: error: parameter 'buffer_length' not found in the function declaration [-Werror,-Wdocumentation]

  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    /var/lib/build/library/psa_crypto_core.h:147:15: note: did you mean 'data_length'?
  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    data_length
    1 error generated.

make[2]: *** [library/CMakeFiles/mbedcrypto.dir/psa_crypto_driver_wrappers.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
library/CMakeFiles/mbedcrypto.dir/build.make:1262: recipe for target 'library/CMakeFiles/mbedcrypto.dir/psa_crypto_driver_wrappers.c.o' failed

In file included from /var/lib/build/library/psa_crypto.c:28:
/var/lib/build/library/psa_crypto_core.h:147:15: error: parameter 'buffer_length' not found in the function declaration [-Werror,-Wdocumentation]

  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    /var/lib/build/library/psa_crypto_core.h:147:15: note: did you mean 'data_length'?

  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    data_length

In file included from /var/lib/build/library/psa_crypto_slot_management.c:28:
/var/lib/build/library/psa_crypto_core.h:147:15: error: parameter 'buffer_length' not found in the function declaration [-Werror,-Wdocumentation]

  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    /var/lib/build/library/psa_crypto_core.h:147:15: note: did you mean 'data_length'?

  • \param[in] buffer_length Size of the key buffer.
    ^~~~~~~~~~~~~
    data_length
    1 error generated.

library/CMakeFiles/mbedcrypto.dir/build.make:1310: recipe for target 'library/CMakeFiles/mbedcrypto.dir/psa_crypto_slot_management.c.o' failed

make[2]: *** [library/CMakeFiles/mbedcrypto.dir/psa_crypto_slot_management.c.o] Error 1
1 error generated.

library/CMakeFiles/mbedcrypto.dir/build.make:1238: recipe for target 'library/CMakeFiles/mbedcrypto.dir/psa_crypto.c.o' failed
make[2]: *** [library/CMakeFiles/mbedcrypto.dir/psa_crypto.c.o] Error 1

CMakeFiles/Makefile2:400: recipe for target 'library/CMakeFiles/mbedcrypto.dir/all' failed
make[1]: *** [library/CMakeFiles/mbedcrypto.dir/all] Error 2

Makefile:138: recipe for target 'all' failed


  • test_valgrind: test: main suites valgrind (Release)
  • make: *** [all] Error 2

^^^^test_valgrind: build: Release (clang): command make -> 2^^^^
Wed Oct 14 11:57:51 UTC 2020


(skipped because the build failed)


  • Done, cleaning up
  • Wed Oct 14 11:57:53 UTC 2020

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FAILED: 1
test_valgrind: build: Release (clang): command make -> 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 14, 2020

Another one:
47>C:\builds\workspace-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\library\psa_crypto.c(1008): error C2220: warning treated as error - no 'object' file generated [C:\builds\workspace-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\cmake_solution\library\mbedcrypto.vcxproj]

47>C:\builds\workspace\-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\library\psa_crypto.c(1008): warning C4267: '=' : conversion from 'size_t' to 'psa_key_bits_t', possible loss of data [C:\builds\workspace\-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\cmake_solution\library\mbedcrypto.vcxproj]

47>C:\builds\workspace\-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\library\psa_crypto.c(1247): warning C4267: '=' : conversion from 'size_t' to 'psa_key_bits_t', possible loss of data [C:\builds\workspace\-3780-head-CAJUTT32RDVNSVZ4KUDDN\worktrees\tmp6gckeq3c\cmake_solution\library\mbedcrypto.vcxproj]
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 16, 2020

@ronald-cron-arm What are the errors now?

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 16, 2020

Hi, ABI-API checking script is complaining that psa_import_key_into_slot() has been removed but as it is an internal API it is ok I think. Thus false negative.
Otherwise it seems it is only CI glitches :-(. I am relaunching it to hopefully get rid of the glitches.

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 16, 2020

Still some CI glitches but otherwise the CI is happy with this PR. Nothing to change on your side regarding CI.

@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 16, 2020

Thanks. Who's going to review?

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 16, 2020

I will review it.

@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from fccab34 to 6916c40 Oct 19, 2020
@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 20, 2020

I have a question. Am I correct in thinking that with #3748 we can get rid of the psa_detect_bit_size_in_slot() function introduced by this PR?

@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 20, 2020

I have a question. Am I correct in thinking that with #3748 we can get rid of the psa_detect_bit_size_in_slot() function introduced by this PR?

Partially. #3748 leaves room for a backwards-compatible setup, where keys that are already stored will have their bits read from NVM as 0. So when a key is loaded from NVM, and 0 is loaded, we can fall back to psa_detect_bit_size_in_slot() to detect the actual bit size. But for keys that get stored in the updated format, the bit size will be part of the storage, and we'll thus get an optimized path that doesn't require spending time on figuring out the bit size.

@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 20, 2020

I have a question. Am I correct in thinking that with #3748 we can get rid of the psa_detect_bit_size_in_slot() function introduced by this PR?

Partially. #3748 leaves room for a backwards-compatible setup, where keys that are already stored will have their bits read from NVM as 0. So when a key is loaded from NVM, and 0 is loaded, we can fall back to psa_detect_bit_size_in_slot() to detect the actual bit size. But for keys that get stored in the updated format, the bit size will be part of the storage, and we'll thus get an optimized path that doesn't require spending time on figuring out the bit size.

I am on the side of not supporting "backwards-compatible setup" and thus removing psa_detect_bit_size_in_slot(). I don't think we have any commitment regarding the storage format (version 0) thus this seems ok to me. @gilles-peskine-arm, what's your opinion about this?

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Oct 20, 2020

We're about to change the format version from 0 (draft without backward compatibility) to 1. Once we do this, version-0 storage will not be accepted anymore. There is no upgrade path from version 0 to version 1.

@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 20, 2020

Sure, then either we merge #3748 first and I'll remove psa_detect_bit_size_in_slot(), or we merge this first and remove it again in #3748. Your call.

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

ok, thanks Gilles. Thus I would suggest to rebase this PR on top of #3748 and remove the psa_detect_bit_size_in_slot() function. By the way, could you please rewrite the history to reach a point where the CI passes (as far as it can unfortunately) and there is no "Fix" commit. That way we start the review from a clean state. I spotted already a few things thus publishing them now.

size_t *bits )
{
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
psa_status_t status = PSA_ERROR_BAD_STATE;

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor

I think we prefer PSA_ERROR_CORRUPTION_DETECTED.

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

Done

@@ -1,5 +1,5 @@
/*
* Test driver for generating keys.
* Test driver for generating and verifying keys.

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor

Maybe better to rename this file to key_management.h?

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

done

@@ -1,6 +1,6 @@
/*
* Test driver for generating keys.
* Currently only supports generating ECC keys.
* Test driver for generating and verifying keys.

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor

Ditto rename this file to key_management.c ?

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

done


psa_status_t psa_detect_bit_size_in_slot( psa_key_slot_t *slot )
{
if( slot->attr.bits == 0 )

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor
if( slot->attr.bits != 0 )
    return( PSA_SUCCESS );

This will reduce the indentation below.

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

done

* \param[in] data Buffer containing the key material to parse and import.
* \param data_length Size of \p data in bytes.
*
* \retval PSA_SUCCESS

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor

#PSA_SUCCESS and the same just below.

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

done

psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
const uint8_t *data,
size_t data_length )
psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,

This comment has been minimized.

@ronald-cron-arm

ronald-cron-arm Oct 20, 2020
Contributor

Please introduce this function in a separate commit.

This comment has been minimized.

@stevew817

stevew817 Oct 26, 2020
Author Contributor

done

Mbed TLS Unified Board automation moved this from To Do to In Progress Oct 20, 2020
@ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Oct 20, 2020

Sure, then either we merge #3748 first and I'll remove psa_detect_bit_size_in_slot(), or we merge this first and remove it again in #3748. Your call.

I prefer to merge #3748 first, that way no need to review psa_detect_bit_size_in_slot(). Thus I will focus on the review of #3748 and come back to this one after.

@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 6916c40 to 6e810fc Oct 23, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 23, 2020

@ronald-cron-arm applied your feedback and reworked the history. Not yet rebased on #3748.

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Oct 23, 2020

As we discussed yesterday, we'll move to an import_key entry point, so it might be best to pause the implementation work.

@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 23, 2020

I still think it makes sense to get this in, since there's a major performance gain in not doing the import-export dance on key copy and load-from-storage, and we'll need to branch off for our own release soon. If you can get validate_key redefined into import_key early next week, then I can rework this PR. If not, then I'd kindly ask to review it as-is, and then we'll update it later on to import_key.

As a side-note, import sounds a bit like a misnomer for what the function actually does. validate_and_convert (or something in that direction) would be better, no?

@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 6e810fc to 5b26ffd Oct 26, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 26, 2020

Rebased on top of #3748

There's no need for calling export-and-import when the key is
guaranteed to have been stored in export representation.

Signed-off-by: Steven Cooreman <[email protected]>
stevew817 added 2 commits Oct 13, 2020
Now that there's a validate_key entry point for drivers, it becomes
much more important to separate the import action (where a key needs
to be validated) from the load action (where a key has been
previously validated, and thus re-validating it would be a waste of
time).

This also exposes why not storing the 'bits' attribute persistently
was a bad idea. The only reason there's a rather large function to
detect bit size is because loading from persistent storage requires
it.

Signed-off-by: Steven Cooreman <[email protected]>
@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 5b26ffd to 3ca2657 Oct 26, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 26, 2020

Rebased on development now that #3748 is merged

@paul-elliott-arm paul-elliott-arm self-requested a review Oct 26, 2020
@gilles-peskine-arm gilles-peskine-arm self-requested a review Oct 27, 2020
library/psa_crypto_core.h Show resolved Hide resolved
library/psa_crypto_driver_wrappers.c Show resolved Hide resolved
*key_length = bytes;
}

mbedtls_ecp_keypair_free( &ecp );

This comment has been minimized.

@paul-elliott-arm

paul-elliott-arm Oct 27, 2020
Contributor

The original in psa_cyrpto.c had a call to psa_remove_key_data_from_memory() here on failure to copy / write. Is this no longer required here?

This comment has been minimized.

@stevew817

stevew817 Oct 29, 2020
Author Contributor

Not sure this is required for a test driver, but OK, I can add a buffer zeroisation on failure.

tests/src/drivers/key_management.c Outdated Show resolved Hide resolved
@paul-elliott-arm
Copy link
Contributor

@paul-elliott-arm paul-elliott-arm commented Oct 27, 2020

CI failures were ABI checker (expected due to change in ABI) and known CI failures. Kicking it again to see if we can get a better result.

Copy link
Contributor

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

Please omit the changelog entry until validate_key is replaced by import_key. There's one place where I think the code should be simplified. Other than that, LGTM.

* Implementation of the validate_key interface for PSA Crypto accelerator
drivers, as defined in #3695. Contributed in #3780.
Comment on lines 2 to 3

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 27, 2020
Contributor

Since validate_key won't ever make it into the driver interface specification, I'd prefer not to mention it in a changelog entry.

This comment has been minimized.

@stevew817

stevew817 Oct 29, 2020
Author Contributor

So I just drop the changelog entry? Or do you want to rephrase it?

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 29, 2020
Contributor

Please drop it for this PR.

library/psa_crypto.c Show resolved Hide resolved
library/psa_crypto_driver_wrappers.c Show resolved Hide resolved
stevew817 added 2 commits Oct 23, 2020
Storage format has been changed to always store the key's bit size

Signed-off-by: Steven Cooreman <[email protected]>
@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 702c649 to 11120d1 Oct 29, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 29, 2020

@gilles-peskine-arm and @paul-elliott-arm All your review feedback is now incorporated.

Copy link
Contributor

@paul-elliott-arm paul-elliott-arm left a comment

Thankyou for the changes - this looks good to me now 👍


/* Key format is not supported by any accelerator, try software fallback
* if present. */
if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 29, 2020
Contributor

Please move the if( … ) condition inside the corresponding #if defined(MBEDTLS_…). The code is correct either way, but this way is fragile (it depends on this if-chain running with status already set to PSA_ERROR_NOT_SUPPORTED) and wastes a few bytes of code (if you remove e.g. RSA support, there's no need to keep a call to PSA_KEY_TYPE_IS_RSA).

This comment has been minimized.

@stevew817

stevew817 Oct 30, 2020
Author Contributor

I don't agree with it wasting code bytes (no statement in the conditional is volatile or in a different CU, so if the clause body is empty the compiler will optimise out the check as well), but if this really throws you off I can change it.

This comment has been minimized.

@stevew817

stevew817 Oct 30, 2020
Author Contributor

@gilles-peskine-arm Force-pushed with your requested change.

* zero key buffer on failure
* readability improvements
* psa_finish_key_creation adjustment after removing import_key_into_slot

Signed-off-by: Steven Cooreman <[email protected]>
@stevew817 stevew817 force-pushed the stevew817:feature/validate_key_in_driver branch from 11120d1 to 40120f6 Oct 30, 2020
@stevew817
Copy link
Contributor Author

@stevew817 stevew817 commented Oct 30, 2020

@paul-elliott-arm I force-pushed the last change requested by @gilles-peskine-arm . Can you re-approve?

Copy link
Contributor

@paul-elliott-arm paul-elliott-arm left a comment

Looks fine to me now.

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Nov 2, 2020

pr-head didn't run due to an infrastructure problem but pr-merge passed except for API changes in library-internal functions so CI can be considered to have passed.

@gilles-peskine-arm gilles-peskine-arm dismissed ronald-cron-arm’s stale review Nov 2, 2020

Paul has taken over reviewing for Ronald and has validated that Ronald's change requests have been addressed in rework.

Mbed TLS Unified Board automation moved this from In Progress to Has Approval Nov 2, 2020
@gilles-peskine-arm gilles-peskine-arm merged commit a455e71 into ARMmbed:development Nov 2, 2020
5 of 10 checks passed
5 of 10 checks passed
continuous-integration/jenkins/pr-head This commit cannot be built
Details
continuous-integration/jenkins/pr-merge This commit cannot be built
Details
PR-3780-head Result analysis Analysis failed
Details
PR-3780-merge TLS Testing Failures: ABI-API-checking
Details
PR-3780-head TLS Testing In progress
Details
DCO DCO
Details
PR-3780-head Pre Test Checks OK
Details
PR-3780-merge Pre Test Checks OK
Details
PR-3780-merge Result analysis OK
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Mbed TLS Unified Board automation moved this from Has Approval to Done Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.