1594 Commits

Author SHA1 Message Date
Gilles Peskine
e8199f574c Dynamic key store: implementation
When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, key slots are now organized in
multiple slices. The slices are allocated on demand, which allows the key
store to grow. The size of slices grows exponentially, which allows reaching
a large number of slots with a small (static) number of slices without too
much overhead.

Maintain a linked list of free slots in each slice. This way, allocating a
slot takes O(1) time unless a slice needs to be allocated.

In this commit, slices are only ever freed when deinitializing the key
store. This should be improved in the future to free empty slices.

To avoid growing the persistent key cache without control, the persistent
key cache has a fixed size (reusing MBEDTLS_PSA_KEY_SLOT_COUNT to avoid
creating yet another option).

When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled. no semantic change and
minimal changes to the code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:52:56 +02:00
Gilles Peskine
47ad2f7484 psa_key_slot_t: different fields in free vs occupied slots
Place some fields of psa_key_slot_t in a union, to prepare for a new field
in free slots that should not require extra memory.

For occupied slots, place only the registered_readers field in the union,
not other fields, to minimize textual changes. All fields could move to the
union except state (also needed in free slots) and attr (which must stay
first to reduce the code size, because it is accessed at many call sites).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:52:54 +02:00
Gilles Peskine
3b41e1d2a5
Merge pull request #9403 from gilles-peskine-arm/psa-keystore-dynamic-prep-3.6
Backport 3.6: prepare for dynamic key store
2024-08-09 08:00:03 +00:00
Gilles Peskine
4a85ff3997 Rename one more deprecated identifier
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:13:05 +02:00
Gilles Peskine
52504f8568 Rename internal function psa_key_production_parameters_are_default
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:13:05 +02:00
Gilles Peskine
f36d785188 Implement psa_generate_key_custom
Implement `psa_generate_key_custom()` and
`psa_key_derivation_output_key_custom()`. These functions replace
`psa_generate_key_ext()` and `psa_key_derivation_output_key_ext()`.
They have the same functionality, but a slightly different interface:
the `ext` functions use a structure with a flexible array member to pass
variable-length data, while the `custom` functions use a separate parameter.

Keep the `ext` functions for backward compatibility with Mbed TLS 3.6.0.
But make them a thin wrapper around the new `custom` functions.

Duplicate the test code and data. The test cases have to be duplicated
anyway, and the test functions are individually more readable this way.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:12:06 +02:00
Gilles Peskine
d72ad738bd Prevent mbedtls_psa_register_se_key with volatile keys
mbedtls_psa_register_se_key() is not usable with volatile keys, since there
is no way to return the implementation-chosen key identifier which would be
needed to use the key. Document this limitation. Reject an attempt to create
such an unusable key. Fixes #9253.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
b47c3b3111 psa_cipher_decrypt CCM*: fix rejection of messages shorter than 3 bytes
Credit to Cryptofuzz. Fixes #9314.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-03 09:42:35 +02:00
Gilles Peskine
51e96ad34c
Merge pull request #9074 from Ryan-Everett-arm/8357-fix-3.6
Backport 3.6: Fix error handling for secure element keys in `psa_start_key_creation`
2024-05-02 16:06:09 +00:00
Ryan Everett
b5a20d3bc5 Fix error handling for secure element keys in psa_start_key_creation
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-04-30 11:17:04 +01:00
Paul Elliott
304766ffa8 Add early exit if zero length AEAD AD passed in.
With multipart AEAD, if we attempt to add zero length additional data,
then with the buffer sharing fixes this can now lead to undefined
behaviour when using gcm. Fix this by returning early, as there is
nothing to do if the input length is zero.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-04-26 19:20:45 +01:00
Minos Galanakis
b70f0fd9a9 Merge branch 'development' into 'development-restricted'
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2024-03-19 22:24:40 +00:00
Paul Elliott
b24e36d07b Add explanatory comment for init flags
Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-15 16:25:48 +00:00
Paul Elliott
d35dce6e23 Add comments about RNG mutex requirements
Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-15 16:06:20 +00:00
Paul Elliott
0db6a9033a Start subsystem IDs at 1 instead of 0
Catch potential invalid calls to init.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-15 16:06:20 +00:00
Paul Elliott
78279962d6 Fix minor style issues
Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-15 13:34:01 +00:00
David Horstmann
4a48becdba Invert and rename config option
Replace MBEDTLS_PSA_COPY_CALLER_BUFFERS with inverse:
!MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS. This ensures that buffer
protection is enabled by default without any change to the Mbed TLS
config file.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-14 14:47:48 +00:00
Gilles Peskine
91f7e07c63
Merge pull request #1196 from davidhorstmann-arm/buffer-sharing-merge
Update development-restricted after buffer-sharing work
2024-03-14 13:28:35 +01:00
Paul Elliott
47cee8e2ee Add mbedtls_psa_crypto_init_subsystem()
Internal only for now, but can be made external with some more
work. Break up psa_crypto_init into chunks to prevent deadlocks when
initialising RNG, likewise break up mbedtls_crypto_free() to stop having
to hold more than one mutex at a time.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-13 12:39:02 +00:00
Thomas Daubney
dca796b966 Remove further instance of LOCAL_OUTPUT_WITH_COPY
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-03-13 10:59:19 +00:00
Thomas Daubney
4bc77c402d Remove LOCAL_OUTPUT_ALLOC_WITH_COPY
It is no longer needed and should be removed.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-03-12 17:01:11 +00:00
Thomas Daubney
692fb3c11c Fix missing semicolon
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-03-12 16:20:41 +00:00
David Horstmann
c2ac51e0c6 Fix removed space in merge resolution
This space was mysteriously removed during the merge, restore it here.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-12 16:09:58 +00:00
David Horstmann
db90914232 Change goto exit into direct return
Fix errors in merge conflict resolution - change
psa_generate_random_internal() to return directly rather than jumping to
an exit label and restore the variable psa_status_t status.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-12 16:07:08 +00:00
Paul Elliott
358165246b Protect PSA drivers_initialized with mutex
Writes to this in psa_crypto_init() were again already covered.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-12 15:36:57 +00:00
Paul Elliott
8e15153637 Protect PSA global rng data with mutex.
Reads and writes of rng_state in psa_crypto_init() and psa_crypto_free()
were already covered by mutex.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-12 15:36:57 +00:00
Paul Elliott
600472b443 Protect PSA global initialized flag with mutex.
Unfortunately this requires holding the mutex for the entire
psa_crypto_init() function, as calling psa_crypto_free() from another
thread should block until init has ended, then run.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2024-03-12 15:36:57 +00:00
David Horstmann
93fa4e1b87 Merge branch 'development' into buffer-sharing-merge 2024-03-12 15:05:06 +00:00
David Horstmann
c5064c83a1 Do not attempt to wipe output buffer if it is NULL
If the output buffer is NULL, it either:

* Does not need wiping because it is zero-length.
* Has failed allocation of a copy.
* Has not yet been written to as a copy hasn't been allocated.

In any of these circumstances, we should not try to write the buffer,
so perform a NULL check before wiping it.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-11 17:02:03 +00:00
David Horstmann
0fea6a52b4 Add buffer copying to psa_verify_hash_start()
Protect input buffers to psa_verify_hash_start(), namely the hash and
signature parameters.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-11 15:56:13 +00:00
David Horstmann
4a523a608e Add buffer copying to psa_sign_hash_start/complete
Add buffer protection to:
* psa_sign_hash_start(), which takes an input buffer for the hash.
* psa_sign_hash_complete(), which takes an output buffer for the
  calculated signature.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-11 15:56:13 +00:00
David Horstmann
63dfb45e5e
Merge pull request #1181 from tom-daubney-arm/key_agreement_buffer_protection
Implement safe buffer copying in key agreement
2024-03-11 15:10:49 +00:00
Paul Elliott
a09b01b5a8
Merge pull request #8912 from Ryan-Everett-arm/double-destroy-key-bugfix
Fix threading bug when multiple destroy_key calls run on the same key
2024-03-11 12:04:04 +00:00
Manuel Pégourié-Gonnard
af3e574f5f
Merge pull request #8862 from valeriosetti/issue8825
Improve support of mbedtls_psa_get_random in client-only builds
2024-03-10 20:06:27 +00:00
Ryan Everett
d868b746a8 Fix potential bug in psa_destroy_key where multiple threads can return PSA_SUCCESS
Threads lose the mutex between locking the slot and changing the slot's state.
Make it so that threads check if another thread has completed a destruction during this period.
Also fix the issue with the incorrect status variable being used.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-03-08 18:39:07 +00:00
Janos Follath
a812e0fe14
Merge pull request #8883 from mfischer/fix_shared_secret
library: psa_crypto: Explicitly initialize shared_secret
2024-03-08 14:35:20 +00:00
tom-daubney-arm
d4c57c0ad2
Merge branch 'development-restricted' into key_agreement_buffer_protection
Signed-off-by: tom-daubney-arm <74920390+tom-daubney-arm@users.noreply.github.com>
2024-03-06 16:47:13 +00:00
David Horstmann
a5175634b0
Merge branch 'development-restricted' into copying-pake
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-03-06 11:18:28 +00:00
Moritz Fischer
967f8cde84 library: psa_crypto: Explicitly initialize shared_secret
When building with -Og (specifically Zephyr with
CONFIG_DEBUG_OPTIMIZATIONS=y) one observes the following warning:

'shared_secret' may be used uninitialized [-Werror=maybe-uninitialized]

Fix this by zero initializing 'shared_secret' similar to the issue
addressed in commit 2fab5c960 ("Work around for GCC bug").

Signed-off-by: Moritz Fischer <moritzf@google.com>
2024-03-05 22:32:32 +00:00
Gabor Mezei
1b5b58d4d9
Fix merge
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-03-04 17:15:08 +01:00
Gábor Mezei
716cf2d4e0
Merge branch 'development-restricted' into buffer_protection_for_cipher
Signed-off-by: Gábor Mezei <63054694+gabor-mezei-arm@users.noreply.github.com>
2024-03-04 15:38:05 +00:00
David Horstmann
c5688a2629
Merge branch 'development-restricted' into generate-random-buffer-protection
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-02-29 14:25:56 +00:00
Gabor Mezei
0b04116cc8
Do not copy the content to the local output buffer with allocation
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-29 10:08:16 +00:00
tom-daubney-arm
840dfe8b41
Merge branch 'development-restricted' into asymmetric_encrypt_buffer_protection
Signed-off-by: tom-daubney-arm <74920390+tom-daubney-arm@users.noreply.github.com>
2024-02-28 15:42:38 +00:00
Gabor Mezei
358eb218ab
Fix buffer protection handling for cipher_generate_iv
Use the `LOCAL_OUTPUT_` macros for buffer protection instead of the existing
local variable.

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-28 15:17:19 +00:00
Gabor Mezei
7abf8ee51b
Add buffer protection for cipher_generate_iv and cipher_set_iv
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-28 15:17:18 +00:00
Gabor Mezei
8b8e485961
Move local buffer allocation just before usage
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-28 15:17:18 +00:00
Gabor Mezei
4892d75e9b
Add LOCAL_OUTPUT_ALLOC_WITH_COPY macro if buffer protection is disabled
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-28 15:17:17 +00:00
Gabor Mezei
212eb08884
Add buffer protection for cipher functions
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-02-28 15:15:49 +00:00
David Horstmann
e097bbdcf3 Add missing guards around exit label
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-02-28 14:17:10 +00:00