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

Support volatile keys in external SE #3382

Merged

Conversation

stevew817
Copy link
Contributor

@stevew817 stevew817 commented Jun 1, 2020

Description

Implemented changes requested in #3288 to support volatile keys located in Secure Element drivers, and to not have the PSA core store information about those volatile keys persistently.

Status

READY

Requires Backporting

NO

Migrations

NO

Additional comments

Todos

  • Tests
  • Documentation

Steps to test or reproduce

Tested through the updated SE HAL test suite.

@danh-arm danh-arm added this to To do in Mbed TLS PRs via automation Jun 2, 2020
@danh-arm
Copy link
Contributor

danh-arm commented Jun 2, 2020

Thanks @stevew817 for the PR. This doesn't build in some configs, e.g.


******************************************************************

* build_crypto_full: build: make, crypto only, full config 

* Mon Jun  1 16:51:26 UTC 2020

******************************************************************
  CC    test_suite_gcm.aes128_en.c

suites/test_suite_psa_crypto_se_driver_hal.function: In function 'test_key_creation_import_export':

suites/test_suite_psa_crypto_se_driver_hal.function:898:54: error: expected statement before ')' token

         if( ! check_no_persistent_data( location ) ) )

                                                      ^

suites/main_test.function: In function 'get_expression':

suites/main_test.function:136:30: error: 'TEST_SE_VOLATILE_LIFETIME' undeclared (first use in this function)

  *                  function parameters.

                              ^

suites/main_test.function:136:30: note: each undeclared identifier is reported only once for each function it appears in

@danh-arm danh-arm added the needs-ci Needs to pass CI tests label Jun 2, 2020
@stevew817
Copy link
Contributor Author

Interesting. I'm running with tests/scripts/basic-in-docker.sh, which does not throw this error.

Is this script somehow only testing a previous version, and not updating with the latest in the working directory? Is there some script that needs to be run before it to get it to regenerate the output of the .function and .data files?

@gilles-peskine-arm
Copy link
Contributor

basic-in-docker.sh only runs basic-build-test.sh which builds the library in one configuration and runs tests. You need all-in-docker.sh which runs tests/scripts/all.sh. Compared with all-in-docker.sh, the CI additionally runs some Windows and Arm Compiler jobs.

You can pass component names (tests/scripts/all.sh --list-components) to all.sh or all-in-docker.sh to run one or more component without spending the hours of the whole script.

@stevew817 stevew817 force-pushed the feature/volatile-keys-in-SE branch from b062248 to 68e9556 Compare June 2, 2020 15:28
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.

Overall this mostly looks good, but a few areas need work.

The CI results aren't very useful right now because builds without SE support break on p_drv being unused in psa_start_key_creation.

tests/suites/test_suite_psa_crypto_se_driver_hal.function Outdated Show resolved Hide resolved
tests/suites/test_suite_psa_crypto_se_driver_hal.function Outdated Show resolved Hide resolved
tests/suites/test_suite_psa_crypto_se_driver_hal.function Outdated Show resolved Hide resolved
library/psa_crypto.c Outdated Show resolved Hide resolved

if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
/* Check there is a proper handler for this lifetime */
if ( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
Copy link
Contributor

Choose a reason for hiding this comment

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

The code for persistent/volatile and local/external should be mostly orthogonal. As it stands, some validation is missing on persistent SE keys.

If MBEDTLS_PSA_CRYPTO_SE_C is enabled and MBEDTLS_PSA_CRYPTO_STORAGE_C is disabled, a persistent external key should be rejected. This configuration is currently rejected in check_config.h, but only because there is currently no support for volatile external keys, which this PR changes.

To keep this PR small, it's ok to keep the restriction in check_config.h and do the following later:

  • Remove the restriction in check_config.h.
  • In all.sh, no longer systematically disable MBEDTLS_PSA_CRYPTO_SE_C when MBEDTLS_PSA_CRYPTO_STORAGE_C is disabled.
  • Fix any failing test.

But please fix the existing bug that the key id isn't validated for keys in a secure element and add a non-regression test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any intent to support persistent SE-stored keys without having to require PSA Crypto Storage?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, because the metadata is always in internal storage. Due to the interface design (the application only remembers the key id, not its lifetime), it's difficult to have metadata spread over multiple storage areas.

library/psa_crypto.c Outdated Show resolved Hide resolved
library/psa_crypto.c Outdated Show resolved Hide resolved
Mbed TLS PRs automation moved this from To do to In progress Jun 2, 2020
@gilles-peskine-arm gilles-peskine-arm added component-crypto Crypto primitives and low-level interfaces needs-work labels Jun 2, 2020
@stevew817 stevew817 force-pushed the feature/volatile-keys-in-SE branch from 169de02 to 3561551 Compare June 3, 2020 11:16
@gilles-peskine-arm
Copy link
Contributor

Sorry I forgot to mention this earlier, but for future reference, please don't mark GitHub conversations as resolved when you're the author of the code: that's for the commenter to do when they've decided that the comment was resolved satisfactorily.

In Mbed TLS, we have a convention for the coder use the 🚀 emoji to indicate “I've handled this comment in my local copy”, if you want to keep track of which comments you've handled.

@stevew817
Copy link
Contributor Author

@gilles-peskine-arm Do you have insight into why those two CI jobs are still failing?

@gilles-peskine-arm
Copy link
Contributor

The remaining CI failure is from the ABI compatibility checker. It says that the symbol psa_validate_persistent_key_parameters has disappeared. Since this is an internal library function, this is not a change of the ABI and no update to the soversion is needed.

So that's a pass from CI. I'll review shortly.

@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, and removed needs-ci Needs to pass CI tests needs-work labels Jun 3, 2020
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.

I'm happy with the library code and with the tests. The documentation needs a little improvement.

The commit history is hard to follow, so I'm requesting a rewrite of the history. The commits “Update PSA core to not persist SE keys declared volatile” and “Fixups after review” do too many things. Please break them up. At this point, you might as well remove the back-and-forth between these two commits. In general, please don't do mix refactoring and adding new things in the same commit.

And please use more descriptive commit messages. For example, “Enable figuring out number of cores when running on OS X” is a good commit message: it explains exactly what's going on. “Fix error from #3302” is not so good; it's acceptable because the diff is very simple, but “Fix typo in a macro that isn't used yet” would be more informative. “Update PSA core to not persist SE keys declared volatile” is not bad in itself but the commit does too much which isn't described in the commit message. “Simplify key checking” lacks explanations and “Fixups after review” is not informative at all.

Please add a changelog entry in ChangeLog.d.

To facilitate review, please save a copy of the current state in your fork (I use the naming convention original-branch-name-1, original-branch-name-2, etc. for successive versions) before force-pushing. Please avoid combining new content with a force-push that rewrites the history. Also, please don't rebase on top of the current target branch if there's no strong reason for it (like a force push). The reason for these requests is to make the force-push diff easy to review (in particular, if you're only restructuring commits or rewriting commit messages, the force-push diff should be empty).

library/psa_crypto_slot_management.c Outdated Show resolved Hide resolved
library/psa_crypto_slot_management.h Outdated Show resolved Hide resolved
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Jun 8, 2020
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
@stevew817 stevew817 force-pushed the feature/volatile-keys-in-SE branch from 3561551 to 62ec1d4 Compare June 8, 2020 16:38
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
@stevew817 stevew817 force-pushed the feature/volatile-keys-in-SE branch from bd95312 to 14b8184 Compare June 17, 2020 11:54
@stevew817
Copy link
Contributor Author

Force-push updated commit message length as requested by @ronald-cron-arm

Inline with review comments.

Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
@stevew817
Copy link
Contributor Author

@gilles-peskine-arm again, all failing CI is inside the ARM network and I cannot see what would've cause failure.

@danh-arm
Copy link
Contributor

The CI failures seem mainly to be in our Mbed OS testing:

Compile [  0.1%]: mbed_tz_context.c

Compile [  0.2%]: rf_configuration.c

Compile [  0.3%]: fslittle_test.c

Compile [  0.4%]: MCR20Drv.c

Compile [  0.4%]: main.cpp

[Fatal Error] pk.h@49,10: psa/crypto.h: No such file or directory

[ERROR] In file included from ./mbed-os/features/mbedtls/inc/mbedtls/ssl_ciphersuites.h:33,

                 from ./mbed-os/features/mbedtls/inc/mbedtls/ssl.h:36,

                 from ./mbed-os/features/netsocket/TLSSocketWrapper.h:29,

                 from ./mbed-os/features/netsocket/nsapi.h:42,

                 from ./mbed-os/mbed.h:26,

                 from ./main.cpp:20:

./mbed-os/features/mbedtls/inc/mbedtls/pk.h:49:10: fatal error: psa/crypto.h: No such file or directory

   49 | #include "psa/crypto.h"

      |          ^~~~~~~~~~~~~~

compilation terminated.

However, I don't know enough about this change or that testing to suggest what the problem is.

There's also the ABI/API compatibility check failure, which Gilles said earlier can be ignored.

@ronald-cron-arm
Copy link
Contributor

Mbed OS Testing is currently broken thus just ignore it. Sorry for the inconvenience. Thus this PR does not have any CI issues currently.

Copy link
Contributor

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

Choose a reason for hiding this comment

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

Thank you for addressing my comments. LGTM.

@ronald-cron-arm ronald-cron-arm moved this from In progress to 1 Approval in Mbed TLS PRs Jun 19, 2020
@ronald-cron-arm ronald-cron-arm linked an issue Jun 19, 2020 that may be closed by this pull request
@ronald-cron-arm ronald-cron-arm added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Jun 19, 2020
Mbed TLS PRs automation moved this from 1 Approval to Approved Jun 26, 2020
@gilles-peskine-arm
Copy link
Contributor

CI passed except for:

  • Mbed OS which is currently failing for unrelated reasons.
  • ABI changes only in internal functions (false positives).

So this is as good as a pass.

@gilles-peskine-arm gilles-peskine-arm 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 Jun 26, 2020
@gilles-peskine-arm gilles-peskine-arm merged commit 961914d into Mbed-TLS:development Jun 26, 2020
Mbed TLS PRs automation moved this from Approved to Done Jun 26, 2020
return( PSA_SUCCESS );

if( psa_is_key_id_valid( key_id,
psa_key_lifetime_is_external( lifetime ) ) )
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for missing this at the time, but psa_key_lifetime_is_external is wrong here. This allows any application to create a key with a key id that's reserved for the implementation, as long as the key is in a secure element. This wasn't a big issue at the time of this pull request because Mbed TLS didn't use the implementation key id range. But with #3547 (comment) it's beginning to use the range and we're going to have to backtrack this change.

@daverodgman daverodgman removed this from Done in Mbed TLS PRs Mar 30, 2022
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 component-crypto Crypto primitives and low-level interfaces enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSA: Support volatile secure element keys
4 participants