Skip to content

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
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.


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.

@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
@danh-arm danh-arm requested a review from ronald-cron-arm June 8, 2020 10:13
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).

@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
stevew817 added 5 commits June 8, 2020 18:19
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
stevew817 added a commit to stevew817/mbedtls that referenced this pull request Jun 8, 2020
stevew817 added a commit to stevew817/mbedtls that referenced this pull request Jun 8, 2020
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.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.

I'm happy with the code and commit structure, thanks.

But CI is still unhappy.

@stevew817
Copy link
Contributor Author

@gilles-peskine-arm I've made the CI happy (at least the parts that are visible to me).

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.

There are a few minor things and a few things I am not sure about.

DOCKER="sudo docker"
fi

# Figure out the number of processors available
Copy link
Contributor

Choose a reason for hiding this comment

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

This commit is not related to the subject of this PR thus it would be better to put it in another PR. This is not a blocker though. If you are ok/have time to do it great, otherwise it's not a big deal.

Copy link
Contributor

Choose a reason for hiding this comment

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

A practical reason not to “sneak in” unrelated commits is when these unrelated commits need to be backported. But this script doesn't exist in the LTS branches, so it's ok.


Key registration: smoke test
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:PSA_SUCCESS
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:1:PSA_SUCCESS
Copy link
Contributor

Choose a reason for hiding this comment

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

This commit message is not rendered correctly by GitHub (too long), could you please rewrite it?

psa_se_drv_table_entry_t **p_drv )
{
psa_status_t status;
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto, the commit title is too long.

psa_key_file_id_t id,
psa_se_drv_table_entry_t **p_drv,
int creating )
psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
Copy link
Contributor

Choose a reason for hiding this comment

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

What about to pass only the lifetime and not all attributes? The idea would be to pass only what is needed. I can see that psa_validate_key_policy() is designed that way.

}

/** Test whether the given parameters are acceptable for a persistent key.
/** Validate that a key's attributes point to a known location.
Copy link
Contributor

Choose a reason for hiding this comment

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

What about just "Validate key's location" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because users will wonder what exactly the validation entails?

if( ! psa_is_key_id_valid( id, ! creating ) )
return( PSA_ERROR_INVALID_ARGUMENT );
return( PSA_SUCCESS );
psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes )
Copy link
Contributor

Choose a reason for hiding this comment

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

With such a name, I would expect the function to validate the persistence part of the lifetime (LSB byte) but in fact it rather validates the key identifier thus this is a bit confusing to me.
What about for this function to just test the validity of the key's persistence? It should probably be moved to psa_crypto.c as not related to SE.
Then, I would check the key identifier by calling psa_is_key_id_valid() directly from psa_validate_key_attributes(). I don't see why psa_is_key_id_valid() is in this C file and not in psa_crypto.c as it does not seem to be related to SE. It could probably be renamed to psa_validate_key_id() for naming alignement.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm Jun 16, 2020

Choose a reason for hiding this comment

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

That's an unfortunate clash of terminology, with “persistence” referring both to the general idea of persisting a key in storage (which involves both the id attribute and the lifetime attribute), and to the persistence level in particular which is encoded in the lifetime.

I prefer to have a single entry point for the persistence attributes (id and lifetime). We may add further constraints in the future (for example, that a certain id range is reserved to a certain lifetime). This belongs in slot management, which is not specific to SE support.

So I don't think this part of the code should change.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, yes in my mind, persistence here was for persistence level, thus I misunderstood the intent. Fine by me to keep it as it is then but probably that the documentation of the function should be improved along the line of @gilles-peskine-arm comments.

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 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
@mpg mpg requested a review from gilles-peskine-arm June 22, 2020 09:54
@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
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.

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