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 slot number attribute #201

Conversation

gilles-peskine-arm
Copy link
Collaborator

@gilles-peskine-arm gilles-peskine-arm commented Aug 2, 2019

Add a slot number attribute for the sake of keys in a secure element. Report the slot number when relevant.

The main point of having a slot number attribute is to allow applications to choose the slot number when creating a key, which is done in the successor PR #202.

Successor of #197. The first new commit is "Add infrastructure for key attribute flags".

@gilles-peskine-arm gilles-peskine-arm added needs: review The pull request is ready for review. This generally means that it has no known issues. api-spec Issue or PR about the PSA specifications labels Aug 2, 2019
@gilles-peskine-arm gilles-peskine-arm added needs: preceding PR Requires another PR to be merged first enhancement New feature or request labels Aug 2, 2019
@k-stachowiak k-stachowiak self-requested a review August 7, 2019 10:44
Copy link
Contributor

@k-stachowiak k-stachowiak left a comment

Choose a reason for hiding this comment

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

My brain may be short circuiting, but one of the bitwise operations looks off to me. Otherwise, looks good.

@@ -68,7 +68,7 @@ typedef struct

/* A mask of key attribute flags used only internally.
* Currently there aren't any. */
#define MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY ( \
#define PSA_KA_MASK_INTERNAL_ONLY ( \
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any policy for PSA macros similar to this one with MbedTLS ones?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. Mbed TLS had no internal headers (library/*.h), but Mbed Crypto does in its PSA parts (library/psa_*.h).

static inline void psa_clear_key_slot_number(
psa_key_attributes_t *attributes )
{
attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be good to reset the slot number to an invalid number here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but there's no invalid slot number at the moment. I'm considering introducing some though, if only to be future-proof. A slot number is 64 bits, maybe we should say that a slot number is 62 bits, reserve values 0xc000000000000000..0xfffffffffffffffe and declare 0xffffffffffffffff to be permanently invalid? This would not remove the need for the HAS_SLOT_NUMBER flag, because 0 is valid.


/* A mask of key attribute flags used externally only.
* Only meant for internal checks inside the library. */
#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
Copy link
Contributor

Choose a reason for hiding this comment

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

Why or it with zero?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So we can add/remove flags just by editing their line and not have to modify the surrounding punctuation.

static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
"One or more key attribute flag is listed as both external-only and dual-use" );
static_assert( ( MBEDTLS_PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
"One or more key attribute flag is listed as both external-only and dual-use" );
Copy link
Contributor

@AndrzejKurek AndrzejKurek Aug 7, 2019

Choose a reason for hiding this comment

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

external-only -> internal-only

@@ -714,20 +714,24 @@ static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
*
* \return The key size in bits, calculated from the key data.
*/
static size_t psa_calculate_key_bits( const psa_key_slot_t *slot )
static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for a key to have ( bits % 8 ) != 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, for some key types. Currently in Mbed Crypto this is only supported with two elliptic curves: Curve25519 (255) and SECP521R1 (521).

#define PSA_KEY_SLOT_FLAG_ALLOCATED ( (uint16_t) 0x0001 )
/** Test whether a key slot is occupied.
*
* A key slot is occupied iff the key type is nonzero. This works because
Copy link
Contributor

Choose a reason for hiding this comment

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

iff -> if

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. Iff = “if and only if”

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, right - Chris has reminded me of this acronym - if and only if. I really don't have it memorized.

{
psa_key_slot_set_bits_in_flags( *p_slot,
PSA_KEY_SLOT_FLAG_ALLOCATED );
if( ! psa_is_key_slot_occupied( *p_slot ) )
Copy link
Contributor

@AndrzejKurek AndrzejKurek Aug 7, 2019

Choose a reason for hiding this comment

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

This approach has one flaw: psa_internal_allocate_key_slot can be called twice on the same key slot and succeed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but does it matter? If the caller hasn't done anything with the slot yet, the slot is still free. Callers don't “release” the slot if they decide not to write anything after all. I do see how this could be error-prone though. How about renaming the function to psa_get_empty_key_slot (which used to exist)? I think that would make the new behavior of the function intuitive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this would make sense. Although this function is used internally, after seeing just its name, imagine the following situation:
I want to allocate a batch (let's say, 3) keys and then set the same attributes / perform operations with all of them - I would receive the same key, three times.

Add infrastructure for internal, external and dual-use flags, with a
compile-time check (if static_assert is available) to ensure that the
same numerical value doesn't get declared for two different purposes
in crypto_struct.h (external or dual-use) and
psa_crypto_core.h (internal).
Add a slot_number field to psa_key_attributes_t and getter/setter
functions. Since slot numbers can have the value 0, indicate the
presence of the field via a separate flag.

In psa_get_key_attributes(), report the slot number if the key is in a
secure element.

When creating a key, for now, applications cannot choose a slot
number. A subsequent commit will add this capability in the secure
element HAL.
Test the behavior of the getter/setter functions.

Test that psa_get_key_slot_number() reports a slot number for a key in
a secure element, and doesn't report a slot number for a key that is
not in a secure element.

Test that psa_get_key_slot_number() reports the correct slot number
for a key in a secure element.
check-names.sh rejects MBEDTLS_XXX identifiers that are not defined in
a public header.
This didn't break anything now, but would have broken things once we
start to add internal flags.
This function no longer modifies anything, so it doesn't actually
allocate the slot. Now, it just returns the empty key slot, and it's
up to the caller to cause the slot to be in use (or not).
@gilles-peskine-arm gilles-peskine-arm force-pushed the psa-se_driver-set_key_slot_number branch from da73da0 to edbed56 Compare August 8, 2019 08:58
@gilles-peskine-arm
Copy link
Collaborator Author

I've rebased on top of the target branch now that #197 is merged. The rebase shows a large diff because other things were also merged, including development There were no conflicts. The previous version is in https://github.com/gilles-peskine-arm/mbed-crypto/tree/psa-se_driver-set_key_slot_number-1

@gilles-peskine-arm gilles-peskine-arm removed the needs: preceding PR Requires another PR to be merged first label Aug 8, 2019
Copy link
Contributor

@k-stachowiak k-stachowiak 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 addressing my comments.

@gilles-peskine-arm
Copy link
Collaborator Author

Crypto CI ok except for API/ABI check.

@gilles-peskine-arm gilles-peskine-arm removed the needs: review The pull request is ready for review. This generally means that it has no known issues. label Aug 8, 2019
@gilles-peskine-arm gilles-peskine-arm merged commit 311809a into ARMmbed:psa-api-1.0-beta Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-spec Issue or PR about the PSA specifications enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants