-
Notifications
You must be signed in to change notification settings - Fork 96
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
Secure element key registration #183
Secure element key registration #183
Conversation
include/psa/crypto_extra.h
Outdated
@@ -154,6 +154,60 @@ typedef struct mbedtls_psa_stats_s | |||
*/ | |||
void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in commit c3d4baa - "pplications"
include/psa/crypto_extra.h
Outdated
* | ||
* \retval #PSA_SUCCESS | ||
* The key was successfully registered. | ||
* Note that depending on the driver's |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfinished sentence.
include/psa/crypto_extra.h
Outdated
* The key was successfully registered. | ||
* Note that depending on the driver's | ||
* \retval #PSA_ERROR_ALREADY_EXISTS | ||
* There is already a key with thw identifier specified in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - thw
-> the
include/psa/crypto_extra.h
Outdated
* \p attributes. | ||
* \retval #PSA_ERROR_INVALID_ARGUMENT | ||
* \p attributes specifies a lifetime which is not located | ||
* in a secure eleement. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - eleement
-> element
include/psa/crypto_se_driver.h
Outdated
* \param[in] key_slot Slot where the key is to be stored. | ||
* | ||
* \retval #PSA_SUCCESS | ||
* The given slot number is valid.for a key with the given |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - .
->
library/psa_crypto.c
Outdated
&slot->data.se.slot_number ); | ||
if( status != PSA_SUCCESS ) | ||
return( status ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a call to p_check_slot
could be made here if the method is PSA_KEY_CREATION_REGISTER
? Just to check that what the application is trying to register is sane.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but not as is: p_check_slot
might say “no, you can't create a key here because it's occupied”. I lean towards adding a psa_key_creation_method_t
to p_allocate
and p_check_slot
, which would allow the driver to enforce constraints like “this slot is locked against import and can only contain internally-generated, non-exportable keys”.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the newest rebase. On a related note, I renamed p_check_slot
to p_validate_slot_number
in #202.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This design looks promising. What part of the PSA depends on the key type being PSA_KEY_TYPE_RAW_DATA
to work properly with secure elements, if any? I'm asking because I'm considering a case where you pass the exact key type that can/will be handled in a given slot during registration.
I'm not sure I understand the question. A secure element doesn't have to support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the "unspecified/undefined" API behaviour if the key attributes has no valid key slot number? (see comments for further thoughts)
include/psa/crypto_extra.h
Outdated
attributes->has_slot_number = 1; | ||
} | ||
|
||
/** Retrieve the enrollment algorithm policy from key attributes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cut and paste bug - description copied from psa_get_key_policy
include/psa/crypto_extra.h
Outdated
* \return The slot containing the specified key. | ||
* If the key is not in a secure element, | ||
* or if the caller is not permitted to observe slot numbers, | ||
* the value is unspecified. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly 'euww!'
Can we define a symbolic name for PSA_KEY_SLOT_NUMBER_INVALID
(or similar), and have the implementation define it as a constant value of type psa_key_slot_number_t
? - this API will return that value if the key slot number is not set (or doesn't exist in this implementation).
That way we get implementation efficiency, precision in the spec, and still permit implementations to define the type of psa_key_slot_number_t
and use 0
as a valid slot number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, all 2^64 potential values for a slot number are considered valid. I'm not sure this is the best design. I'm sure every driver can afford one invalid value. For consistency with the rest of PSA crypto, 0 should be invalid. Making 0 invalid would also avoid the annoying extra has_slot_number
bit. But typical secure elements number their slots from 0 to N-1, so it would require most drivers to do some shifting. So declare all-bits-one invalid (with a symbolic constant of course) and mandate implementations to return it when not applicable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed psa_get_key_slot_number
to return an error if there is no recorded slot number. See #201.
include/psa/crypto_extra.h
Outdated
static inline psa_key_slot_number_t psa_get_key_slot_number( | ||
const psa_key_attributes_t *attributes) | ||
{ | ||
return( attributes->slot_number ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will compiler-error if MBEDTLS_PSA_CRYPTO_SE_C
is not defined. Yes I know you say "undefined" but this is a painful way to inform developers that they misunderstand the API and how to fix their mistake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix such errors in a batch when I add a build with MBEDTLS_PSA_CRYPTO_SE_C
not defined.
* \retval #PSA_ERROR_INVALID_ARGUMENT | ||
* \p attributes specifies a lifetime which is not located | ||
* in a secure eleement. | ||
* \retval #PSA_ERROR_INVALID_ARGUMENT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate error code - this might be confusing in the API documentation? (or is this pattern used elsewhere)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern is used elsewhere: each \retval
is an error reason, and some error reasons can map to the same error code. The alternative is something like
* \retval #PSA_ERROR_INVALID_ARGUMENT
* \p attributes specifies a lifetime which is not located
* in a secure eleement.
*
* Alternatively, …
I have no strong feelings either way, but I'd only change this if we change it elsewhere.
include/psa/crypto_se_driver.h
Outdated
@@ -114,10 +114,14 @@ typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context, | |||
void *persistent_data, | |||
psa_key_lifetime_t lifetime); | |||
|
|||
#ifdef __DOXYGEN_ONLY__ | |||
/* Mbed Crypto defines this type in crypto_extra.h because it is also | |||
* available to applications through implementation-specific extensions. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth a comment to indicate that this type is also defined in crypto_types.h
- and the definitions need to be identical?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to create a new header file. I made this hack to avoid having to mess with build scripts (including the API build scripts).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the time being, I'm not adding a header file. I think I'll just wait until the header files are no longer used to generate the documentation so this becomes moot. But I did improve the comment.
library/psa_crypto.c
Outdated
#if defined(MBEDTLS_PSA_CRYPTO_SE_C) | ||
if( psa_get_se_driver( slot->lifetime, &drv, NULL ) ) | ||
{ | ||
attributes->slot_number = slot->data.se.slot_number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also set has_slot_number
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Fixed.
Thanks for the reviews. I'll fix the documentation mistakes in a batch later, probably rebased against a new version of #157. |
06ca0b2
to
1dfa905
Compare
I've rebased this work on top of the latest version of #157 . I also made significant changes to the history, to address review comments, to avoid breaking tests, and also to make some design changes. @athoelke @AndrzejKurek As per our habit I marked the review comments that I've handled with 🚀. I'm still planning to make an improvement pass, but perhaps only after yet another rebase on top of #157 once I've addressed the review comments there. |
1dfa905
to
6ce6e65
Compare
Now that #157 is merged, I've rebased on top of that and switched the target branch to psa-api-1.0-beta. I'm not planning any more rebases, but I am planning further changes to address review feedback. |
On second thoughts, I am planning to rebase and even split this PR. It does
|
6ce6e65
to
828963c
Compare
828963c
to
afb289c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through the changes and ran the test suites, and didn't find issues. I've only found two suspicious documentation changes, noted below.
afb289c
to
0252f8d
Compare
PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */ | ||
|
||
#ifndef __DOXYGEN_ONLY__ | ||
/** A key is being registered with mbedtls_psa_register_se_key(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this in the doxygen ifndef block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's an Mbed Crypto extension, so it shouldn't be included in the PSA specification. We normally resolve this by using different files, but this is an enum so it can't be defined partly in one file and partly in another. __DOXYGEN_ONLY__
is not defined when generating the Mbed Crypto documentation.
Let psa_start_key_creation know what type of key creation this is. This will be used at least for key registration in a secure element, which is a peculiar kind of creation since it uses existing key material.
Pass the key creation method (import/generate/derive/copy) to the driver methods to allocate or validate a slot number. This allows drivers to enforce policies such as "this key slot can only be used for keys generated inside the secure element".
Register an existing key in a secure element. Minimal implementation that doesn't call any driver method and just lets the application declare whatever it wants.
When registering a key in a secure element, if the driver has a p_validate_slot_number method, call it.
49bd582
0252f8d
to
49bd582
Compare
I rebased again to get rid of the commits from preceding PRs, to resolve a trivial conflict in |
Add an API method that allows applications to register a key in a secure element.
Successor of #202. The first new commit is "psa_start_key_creation: take the method as a parameter".
This PR has undergone significant rework. See #183 (comment) . Previous iterations are available as 1, 2, 3, 4.