Skip to content

Conversation

@michaelthomasj
Copy link

No description provided.

Copy link
Collaborator

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

Thank you for your contribution! I left some individual comments about specific issues.

Testing and documentation

Please note that any new feature should have adequate documentation and test coverage. In particular, any new compile-time option should be documented in include/mbedtls/config.h and tested in tests/scripts/all.sh. Once we (you and the Mbed Crypto team) have converged a bit more on architecture, we (the Mbed Crypto team) can provide more specific guidance.

For driver support code, testing would mean at least doing a build with the driver hooks enabled, and having unit tests with a mock driver or a test driver that wraps around the software implementation. We don't necessarily need full test coverage from the beginning, but at a minimum we need smoke tests to ensure that the new code builds and doesn't do anything that tools like ASan would flag.

Architecture

In general, this is not very well aligned with our medium-term architectural goals. We distinguish between two kinds of hooks for external hardware:

  • Accelerators work on plaintext keys. Mbed Crypto dispatches an operation to the accelerator driver if the driver implements that operation. Dispatch is based on a per-algorithm basis and is configured at compile time. psa/crypto_accel_driver.h declares types of functions that accelerator drivers can implement, and the documentation of each type explains the function names that the driver may define. We have not yet started to add support for PSA-style accelerators in Mbed Crypto. Mbed Crypto currently supports accelerators through the MBEDTLS_xxx_ALT interfaces.
  • Secure elements work on opaque keys. Mbed Crypto only stores a handle to the key, or a wrapped form of the key. psa/crypto_se_driver.h defines the type of structures containing function pointers that secure element drivers implement. Mbed Crypto currently contains proof-of-concept support for some secure element features (only with keys accessed through handles, not yet with keys where Mbed Crypto stores a wrapped form of the key).

We welcome contributions to improve support for cryptographic hardware in Mbed Crypto, but we strongly prefer to add features that are on the path towards our medium-term architectural goal, or at least that have a similar enough interface to facilitate later evolution.

Next steps

Since you're effectively contributing support for features that we intended to have, but earlier than we were planning to provide it, we should have some further private discussions about the architecture.

I think the plaintext-key case and the wrapped-key case should be treated separately. Even if there are many platforms where it's the same hardware under the hood, the key manipulations and the hook points in Mbed Crypto are rather different.

#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)

/** Whether a key type is AES. */
#define PSA_KEY_TYPE_IS_AES(type) (((type)&PSA_KEY_TYPE_AES) != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

PSA_KEY_TYPE_AES is a value, not a flag. The correct expansion of this macro would be (type) == PSA_KEY_TYPE_AES. The API generally doesn't define boolean macros with such a simple expansion.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed.

<li>Simplifying key expansion in the 256-bit
case by generating an extra round key.
</li></ul> */
void *vendor_ctx; /*!< Vendor defined context. */
Copy link
Collaborator

Choose a reason for hiding this comment

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

The official way to customize the AES context for an accelerator is to define MBEDTLS_AES_ALT and provide a custom version of the type mbedtls_aes_context as well as all the functions in aes.c (except selftests).

It's also possible to substitute the core AES mechanisms (key expansion, single-block encryption, single-block decryption) by defining MBEDTLS_AES_SETKEY_ENC_ALT, MBEDTLS_AES_SETKEY_DEC_ALT, MBEDTLS_AES_ENCRYPT_ALT and MBEDTLS_AES_DECRYPT_ALT and providing definitions for the corresponding functions (mbedtls_aes_setkey_enc, mbedtls_aes_setkey_dec, mbedtls_internal_aes_encrypt, mbedtls_internal_aes_decrypt). In this case, you have to use the default definition of mbedtls_aes_context. If you override all four functions, you can choose what you put in the rk field. So I believe that vendor_ctx is not necessary.

We would prefer not to change the layout of mbedtls_aes_context at this point.

* \note This function has to be defined by the vendor if MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C
* is defined.
* A weakly linked version is provided by default and returns
* PSA_ERROR_NOT_SUPPORTED. Do not use this function directly;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this function is not meant to be called by applications, it should not be declared in crypto_extra.h. The correct location would be crypto_accel_driver.h. That file is still in an early draft stage and not ready for production yet. I propose that we work together on getting at least part of it production-ready.

This applies to the other functions added here.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

{
attributes->core.lifetime = lifetime;
if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime))
Copy link
Collaborator

Choose a reason for hiding this comment

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

For Mbed Crypto specific files (as opposed to standard PSA headers), please use the Mbed TLS coding style, with its unusual whitespace placement: spaces inside parentheses, not outside.

if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)

#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
(((lifetime)&PSA_KEY_LIFETIME_VOLATILE) != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

PSA_KEY_LIFETIME_VOLATILE is a value, not a flag. There are currently two defined lifetime values: VOLATILE = 0 and PERSISTENT = 1. We can make 0x00000001 a PSA_KEY_LIFETIME_PERSISTENT_FLAG and thus

#define PSA_KEY_LIFETIME_PERSISTENT_FLAG ((psa_key_lifetime_t)0x00000001)
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) (((lifetime) & PSA_KEY_LIFETIME_PERSISTENT_FLAG) == 0)

#define PSA_KEY_LIFETIME_VOLATILE_VENDOR (PSA_KEY_LIFETIME_VENDOR_FLAG | PSA_KEY_LIFETIME_VOLATILE)

#define PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(lifetime) \
(((lifetime)&PSA_KEY_LIFETIME_VENDOR_FLAG) != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style: please put whitespace around binary operators including & ((t)&x would be the address of x cast to the type t).

Copy link
Author

Choose a reason for hiding this comment

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

This one will also change to match your proposal.

else
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
#if defined (MBEDTLS_PSA_CRYPTO_ACCEL_DRV_C)
if (PSA_KEY_LIFETIME_IS_VENDOR_DEFINED(slot->attr.lifetime))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If an accelerated implementation is available, it should be called for any transparent key regardless of its lifetime. Custom lifetimes may indicate keys in vendor-specific storage, or (to be clarified) keys with vendor-specific wrapping.

*/
#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)

#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
Copy link
Collaborator

@gilles-peskine-arm gilles-peskine-arm Feb 5, 2020

Choose a reason for hiding this comment

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

Please refer to the definition of PSA_KEY_LIFETIME_IS_VOLATILE and to the new structure for lifetime values in #358, which is slated to be included in the next release of Mbed Crypto and in an upcoming version of the PSA Cryptography specification.

Copy link
Author

Choose a reason for hiding this comment

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

I am fine with that new definition. Im not quite sure how to get it in here though.

moved accel definitions from other header files to accel header file
@gilles-peskine-arm gilles-peskine-arm changed the base branch from development to renesas-RA March 3, 2020 12:15
@danh-arm danh-arm merged commit 327730c into ARMmbed:renesas-RA Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants