Skip to content

Commit 89e7655

Browse files
committed
Merge remote-tracking branch 'crypto/pr/212' into development
* crypto/pr/212: (337 commits) Make TODO comments consistent Fix PSA tests Fix psa_generate_random for >1024 bytes Add tests to generate more random than MBEDTLS_CTR_DRBG_MAX_REQUEST Fix double free in psa_generate_key when psa_generate_random fails Fix copypasta in test data Avoid a lowercase letter in a macro name Correct some comments Fix PSA init/deinit in mbedtls_xxx tests when using PSA Make psa_calculate_key_bits return psa_key_bits_t Adjust secure element code to the new ITS interface More refactoring: consolidate attribute validation Fix policy validity check on key creation. Add test function for import with a bad policy Test key creation with an invalid type (0 and nonzero) Remove "allocated" flag from key slots Take advantage of psa_core_key_attributes_t internally #2 Store the key size in the slot in memory Take advantage of psa_core_key_attributes_t internally: key loading Switch storage functions over to psa_core_key_attributes_t ...
2 parents c26591a + 1d57a20 commit 89e7655

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+10569
-4398
lines changed

docs/architecture/mbed-crypto-storage-specification.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,92 @@ The layout of a key file is:
193193
* key material length (4 bytes)
194194
* key material: output of `psa_export_key`
195195
* Any trailing data is rejected on load.
196+
197+
Mbed Crypto TBD
198+
---------------
199+
200+
Tags: TBD
201+
202+
Released in TBD 2019. <br>
203+
Integrated in Mbed OS TBD.
204+
205+
### Changes introduced in TBD
206+
207+
* The layout of a key file now has a lifetime field before the type field.
208+
* Key files can store references to keys in a secure element. In such key files, the key material contains the slot number.
209+
210+
### File namespace on a PSA platform on TBD
211+
212+
Assumption: ITS provides a 64-bit file identifier namespace. The Crypto service can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
213+
214+
Assumption: the owner identifier is a nonzero value of type `int32_t`.
215+
216+
* Files 0 through 0xfffeffff: unused.
217+
* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-tbd).
218+
* Files 0x100000000 through 0xffffffffffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0). The upper 32 bits determine the owner.
219+
220+
### File namespace on ITS as a library on TBD
221+
222+
Assumption: ITS provides a 64-bit file identifier namespace. The entity using the crypto library can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
223+
224+
This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
225+
226+
* File 0: unused.
227+
* Files 1 through 0xfffeffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0).
228+
* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-tbd).
229+
* Files 0x100000000 through 0xffffffffffffffff: unused.
230+
231+
### Non-key files on TBD
232+
233+
File identifiers in the range 0xffff0000 through 0xffffffff are reserved for internal use in Mbed Crypto.
234+
235+
* Files 0xfffffe02 through 0xfffffeff (`PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + lifetime`): secure element driver storage. The content of the file is the secure element driver's persistent data.
236+
* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-1.0.0).
237+
* File 0xffffff54 (`PSA_CRYPTO_ITS_TRANSACTION_UID`): [transaction file](#transaction-file-format-for-tbd).
238+
* Other files are unused and reserved for future use.
239+
240+
### Key file format for TBD
241+
242+
All integers are encoded in little-endian order in 8-bit bytes except where otherwise indicated.
243+
244+
The layout of a key file is:
245+
246+
* magic (8 bytes): `"PSA\0KEY\0"`.
247+
* version (4 bytes): 0.
248+
* lifetime (4 bytes): `psa_key_lifetime_t` value.
249+
* type (4 bytes): `psa_key_type_t` value.
250+
* policy usage flags (4 bytes): `psa_key_usage_t` value.
251+
* policy usage algorithm (4 bytes): `psa_algorithm_t` value.
252+
* policy enrollment algorithm (4 bytes): `psa_algorithm_t` value.
253+
* key material length (4 bytes).
254+
* key material:
255+
* For a transparent key: output of `psa_export_key`.
256+
* For an opaque key (key in a secure element): slot number (8 bytes), in platform endianness.
257+
* Any trailing data is rejected on load.
258+
259+
### Transaction file format for TBD
260+
261+
The transaction file contains data about an ongoing action that cannot be completed atomically. It exists only if there is an ongoing transaction.
262+
263+
All integers are encoded in platform endianness.
264+
265+
All currently existing transactions concern a key in a secure element.
266+
267+
The layout of a transaction file is:
268+
269+
* type (2 bytes): the [transaction type](#transaction-types-on-tbd).
270+
* unused (2 bytes)
271+
* lifetime (4 bytes): `psa_key_lifetime_t` value that corresponds to a key in a secure element.
272+
* slot number (8 bytes): `psa_key_slot_number_t` value. This is the unique designation of the key for the secure element driver.
273+
* key identifier (4 bytes in a library integration, 8 bytes on a PSA platform): the internal representation of the key identifier. On a PSA platform, this encodes the key owner in the same way as [in file identifiers for key files](#file-namespace-on-a-psa-platform-on-tbd)).
274+
275+
#### Transaction types on TBD
276+
277+
* 0x0001: key creation. The following locations may or may not contain data about the key that is being created:
278+
* The slot in the secure element designated by the slot number.
279+
* The file containing the key metadata designated by the key identifier.
280+
* The driver persistent data.
281+
* 0x0002: key destruction. The following locations may or may not still contain data about the key that is being destroyed:
282+
* The slot in the secure element designated by the slot number.
283+
* The file containing the key metadata designated by the key identifier.
284+
* The driver persistent data.

docs/getting_started.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Importing a key and checking key information:
7373
1. Test the information stored in this slot:
7474
```C
7575
int key_slot = 1;
76-
uint8_t *data = "KEYPAIR_KEY_DATA";
76+
uint8_t *data = "KEY_PAIR_KEY_DATA";
7777
size_t data_size;
7878
psa_key_type_t type = PSA_KEY_TYPE_RSA_PUBLIC_KEY;
7979
size_t got_bits;
@@ -127,7 +127,7 @@ This allows the key in the key slot to be used for RSA signing.
127127
PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
128128
status = psa_set_key_policy(key_slot, &policy);
129129
130-
status = psa_import_key(key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
130+
status = psa_import_key(key_slot, PSA_KEY_TYPE_RSA_KEY_PAIR,
131131
key, sizeof(key));
132132
133133
/* Sing message using the key */
@@ -335,7 +335,7 @@ Deriving a new AES-CTR 128-bit encryption key into a given key slot using HKDF w
335335
1. Set up the generator using the `psa_key_derivation` function providing a key slot containing a key that can be used for key derivation and a salt and label (Note: salt and label are optional).
336336
1. Initiate a key policy to for the derived key by calling `psa_key_policy_set_usage()` with `PSA_KEY_USAGE_ENCRYPT` parameter and the algorithm `PSA_ALG_CTR`.
337337
1. Set the key policy to the derived key slot.
338-
1. Import a key from generator into the desired key slot using (`psa_generator_import_key`).
338+
1. Import a key from generator into the desired key slot using (`psa_key_derivation_output_key`).
339339
1. Clean up generator.
340340
341341
At this point the derived key slot holds a new 128-bit AES-CTR encryption key derived from the key, salt and label provided:
@@ -358,7 +358,7 @@ At this point the derived key slot holds a new 128-bit AES-CTR encryption key de
358358
359359
psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
360360
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
361-
psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361+
psa_key_derivation_operation_t generator = PSA_KEY_DERIVATION_OPERATION_INIT;
362362
size_t derived_bits = 128;
363363
size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
364364
@@ -378,10 +378,10 @@ At this point the derived key slot holds a new 128-bit AES-CTR encryption key de
378378
379379
psa_set_key_policy(derived_key, &policy);
380380
381-
psa_generator_import_key(derived_key, PSA_KEY_TYPE_AES, derived_bits, &generator);
381+
psa_key_derivation_output_key(derived_key, PSA_KEY_TYPE_AES, derived_bits, &generator);
382382
383383
/* Clean up generator and key */
384-
psa_generator_abort(&generator);
384+
psa_key_derivation_abort(&generator);
385385
/* as part of clean up you may want to clean up the keys used by calling:
386386
* psa_destroy_key( base_key ); or psa_destroy_key( derived_key ); */
387387
mbedtls_psa_crypto_free();
@@ -510,7 +510,7 @@ Generate a piece of random 128-bit AES data:
510510
psa_set_key_policy(slot, &policy);
511511

512512
/* Generate a key */
513-
psa_generate_key(slot, PSA_KEY_TYPE_AES, bits, NULL, 0);
513+
psa_generate_key(slot, PSA_KEY_TYPE_AES, bits);
514514

515515
psa_export_key(slot, exported, exported_size, &exported_length)
516516

include/mbedtls/check_config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@
458458
#error "MBEDTLS_PSA_CRYPTO_SPM defined, but not all prerequisites"
459459
#endif
460460

461+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C) && \
462+
! ( defined(MBEDTLS_PSA_CRYPTO_C) && \
463+
defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) )
464+
#error "MBEDTLS_PSA_CRYPTO_SE_C defined, but not all prerequisites"
465+
#endif
466+
461467
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) && \
462468
! defined(MBEDTLS_PSA_CRYPTO_C)
463469
#error "MBEDTLS_PSA_CRYPTO_STORAGE_C defined, but not all prerequisites"

include/mbedtls/cipher_internal.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,13 @@ typedef enum
124124
MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
125125
MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
126126
/* use raw key material internally imported */
127-
/* into a allocated key slot, and which */
128-
/* hence need to destroy that key slot */
129-
/* when they are no longer needed. */
127+
/* as a volatile key, and which hence need */
128+
/* to destroy that key when the context is */
129+
/* freed. */
130130
MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
131-
/* which use a key from a key slot */
132-
/* provided by the user, and which */
133-
/* hence should not be destroyed when */
134-
/* the context is no longer needed. */
131+
/* which use a key provided by the */
132+
/* user, and which hence will not be */
133+
/* destroyed when the context is freed. */
135134
} mbedtls_cipher_psa_key_ownership;
136135

137136
typedef struct

include/mbedtls/config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,22 @@
17461746
*/
17471747
#define MBEDTLS_PSA_CRYPTO_C
17481748

1749+
/**
1750+
* \def MBEDTLS_PSA_CRYPTO_SE_C
1751+
*
1752+
* Enable secure element support in the Platform Security Architecture
1753+
* cryptography API.
1754+
*
1755+
* \warning This feature is not yet suitable for production. It is provided
1756+
* for API evaluation and testing purposes only.
1757+
*
1758+
* Module: library/psa_crypto_se.c
1759+
*
1760+
* Requires: MBEDTLS_PSA_CRYPTO_C, MBEDTLS_PSA_CRYPTO_STORAGE_C
1761+
*
1762+
*/
1763+
//#define MBEDTLS_PSA_CRYPTO_SE_C
1764+
17491765
/**
17501766
* \def MBEDTLS_PSA_CRYPTO_STORAGE_C
17511767
*

include/mbedtls/pk.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void mbedtls_pk_init( mbedtls_pk_context *ctx );
217217
*
218218
* \note For contexts that have been set up with
219219
* mbedtls_pk_setup_opaque(), this does not free the underlying
220-
* key slot and you still need to call psa_destroy_key()
220+
* PSA key and you still need to call psa_destroy_key()
221221
* independently if you want to destroy that key.
222222
*/
223223
void mbedtls_pk_free( mbedtls_pk_context *ctx );
@@ -259,29 +259,29 @@ int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
259259

260260
#if defined(MBEDTLS_USE_PSA_CRYPTO)
261261
/**
262-
* \brief Initialize a PK context to wrap a PSA key slot.
262+
* \brief Initialize a PK context to wrap a PSA key.
263263
*
264264
* \note This function replaces mbedtls_pk_setup() for contexts
265-
* that wrap a (possibly opaque) PSA key slot instead of
265+
* that wrap a (possibly opaque) PSA key instead of
266266
* storing and manipulating the key material directly.
267267
*
268268
* \param ctx The context to initialize. It must be empty (type NONE).
269-
* \param key The PSA key slot to wrap, which must hold an ECC key pair
269+
* \param key The PSA key to wrap, which must hold an ECC key pair
270270
* (see notes below).
271271
*
272-
* \note The wrapped key slot must remain valid as long as the
272+
* \note The wrapped key must remain valid as long as the
273273
* wrapping PK context is in use, that is at least between
274274
* the point this function is called and the point
275275
* mbedtls_pk_free() is called on this context. The wrapped
276-
* key slot might then be independently used or destroyed.
276+
* key might then be independently used or destroyed.
277277
*
278278
* \note This function is currently only available for ECC key
279279
* pairs (that is, ECC keys containing private key material).
280280
* Support for other key types may be added later.
281281
*
282282
* \return \c 0 on success.
283283
* \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
284-
* (context already used, invalid key slot).
284+
* (context already used, invalid key handle).
285285
* \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
286286
* ECC key pair.
287287
* \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
@@ -788,26 +788,27 @@ int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
788788

789789
#if defined(MBEDTLS_USE_PSA_CRYPTO)
790790
/**
791-
* \brief Turn an EC key into an Opaque one
791+
* \brief Turn an EC key into an opaque one.
792792
*
793793
* \warning This is a temporary utility function for tests. It might
794794
* change or be removed at any time without notice.
795795
*
796796
* \note Only ECDSA keys are supported so far. Signing with the
797797
* specified hash is the only allowed use of that key.
798798
*
799-
* \param pk Input: the EC key to transfer to a PSA key slot.
800-
* Output: a PK context wrapping that PSA key slot.
801-
* \param slot Output: the chosen slot for storing the key.
802-
* It's the caller's responsibility to destroy that slot
803-
* after calling mbedtls_pk_free() on the PK context.
799+
* \param pk Input: the EC key to import to a PSA key.
800+
* Output: a PK context wrapping that PSA key.
801+
* \param handle Output: a PSA key handle.
802+
* It's the caller's responsibility to call
803+
* psa_destroy_key() on that handle after calling
804+
* mbedtls_pk_free() on the PK context.
804805
* \param hash_alg The hash algorithm to allow for use with that key.
805806
*
806807
* \return \c 0 if successful.
807808
* \return An Mbed TLS error code otherwise.
808809
*/
809810
int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
810-
psa_key_handle_t *slot,
811+
psa_key_handle_t *handle,
811812
psa_algorithm_t hash_alg );
812813
#endif /* MBEDTLS_USE_PSA_CRYPTO */
813814

include/mbedtls/psa_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
413413
/* All other failures */
414414
case PSA_ERROR_COMMUNICATION_FAILURE:
415415
case PSA_ERROR_HARDWARE_FAILURE:
416-
case PSA_ERROR_TAMPERING_DETECTED:
416+
case PSA_ERROR_CORRUPTION_DETECTED:
417417
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
418418
default: /* We return the same as for the 'other failures',
419419
* but list them separately nonetheless to indicate

0 commit comments

Comments
 (0)