1583 Commits

Author SHA1 Message Date
Valerio Setti
a53e7a5cb5 psa: let mbedtls_psa_get_random() always use psa_generate_random()
It means that mbedtls_psa_get_random() goes through the PSA interface
all the times. Fallbacks to CTR_DRBG or HMAC_DRBG are still possible,
but that depends on how the crypto provider is built.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
2024-02-26 12:03:59 +01:00
Gilles Peskine
4c32b69f37 Ignore domain parameters in RSA key generation
Remove the ability to select a custom public exponent via domain parameters
in RSA key generation. The only way to select a custom public exponent is
now to pass custom production parameters to psa_generate_key_ext().

A subsequent commit will remove domain parameters altogether from the API,
thus this commit does not bother to update the documentation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-26 08:50:38 +01:00
Valerio Setti
bb91bcda0e psa: move mbedtls_psa_get_random() to psa_util.c
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
2024-02-26 08:41:33 +01:00
Gilles Peskine
0f63028809
Merge pull request #8815 from gilles-peskine-arm/psa_generate_key_ext-prototype
Introduce psa_generate_key_ext
2024-02-26 07:16:49 +00:00
tom-daubney-arm
5cd611d144
Merge branch 'development-restricted' into mac_buffer_protection
Signed-off-by: tom-daubney-arm <74920390+tom-daubney-arm@users.noreply.github.com>
2024-02-22 15:26:06 +00:00
Thomas Daubney
5390acada9 Decouple if statements in psa_raw_key_agreement exit.
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-22 11:06:04 +00:00
Ryan Everett
9af70e51c1 Make multi-part AEAD operations thread-safe
The setup calls are the only calls to use a key ID.
The key is then copied into the operation object,
all future API calls use the copy instead of the key in the slot.

Simultaneous API calls on the same operation object are not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
bbedfcec2e Make multi-part PAKE operations thread-safe
The only interaction with key IDs here is in the changed function.

Simultaneous API calls on the same operation object are not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
5ac6fa7aae Make multi-part key derivation operations thread-safe
One can input a key using a key identifier through the two changed functions.
Inputted keys are copied into the operation object.
Any material inputted in byte form is separate to the key slot system.
Outputting a key is threadsafe as per the key loading work.
The verification API is yet to be implemented.

Simultaneous API calls on the same operation object are not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
c0053cc499 Make multi-part cipher operations thread-safe
Within setup we create a copy of the key and put it in the operation field.
After setup, we only ever use the new copy,
and do not interact with any key slots.
Therefore we need only register as a reader of the key during setup,
then unregister after we stop accessing the key.

Simultaneous API calls on the same operation object are not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
291267f486 Make restartable signature verifications thread-safe
We copy the key from a slot to the operation object in _start.
_complete and _abort do not access any key slots, instead using
the local copy.

Concurrently using the same operation object is not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
dcc03d552d Make restartable signature operations thread-safe
We copy the key from a slot to the operation object in _start.
_complete and _abort do not access any key slots, instead using
the local copy.

Concurrently using the same operation object is not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Ryan Everett
fb9857ff9e Make multi-part MAC operations thread-safe
Within setup we create a copy of the key and put it in the operation field.
After setup, we only ever use the new copy - and do not interact with any key slots.
Therefore we need only register as a reader of the key during setup,
then unregister after we stop accessing the key.

Simultaneous API calls on the same operation object are not thread-safe.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-22 10:27:52 +00:00
Thomas Daubney
0576a6a174 Revise how output allocation is checked
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-21 15:15:00 +00:00
Paul Elliott
d237190f04
Merge pull request #8773 from Ryan-Everett-arm/threadsafe-key-locking
Make key locking and one-shot operations thread safe
2024-02-21 13:55:12 +00:00
Thomas Daubney
89d8c2a1b4 Rework check for failed output allocation
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-21 12:14:57 +00:00
Ryan Everett
93cea578b9 Clarify which unregister operation needs to be used
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-20 18:01:29 +00:00
David Horstmann
cf3457ef26
Merge pull request #1132 from davidhorstmann-arm/copying-aead
Copy buffers in AEAD
2024-02-20 16:07:30 +00:00
Gilles Peskine
092ce51c47 Rename "key generation method" to "key production parameters"
"Key generation method" was misleading since it also applies to key
derivation. Change "key generation" to "key production", which we aren't
using yet and has roughly the right intuition. Change "method" to
"parameters" which there seems to be a slight preference for. Discussion
thread: https://github.com/Mbed-TLS/mbedtls/pull/8815#discussion_r1486524295

Identifiers renamed:
psa_key_generation_method_t → psa_key_production_parameters_t
psa_key_generation_method_s → psa_key_production_parameters_s
PSA_KEY_GENERATION_METHOD_INIT → PSA_KEY_PRODUCTION_PARAMETERS_INIT
method → params
method_data_length → params_data_length
default_method → default_production_parameters
psa_key_generation_method_is_default → psa_key_production_parameters_are_default
setup_key_generation_method → setup_key_production_parameters
key_generation_method_init → key_production_parameters_init

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-20 16:18:13 +01:00
Thomas Daubney
d997e7ad9a Check output allocated before randomising
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-20 11:24:07 +00:00
Thomas Daubney
50f58fc3e4 Conditionally include exit label
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-15 14:24:03 +00:00
Thomas Daubney
9739ac047a Add buffer protection to psa_key_derivation_key_agreement
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-15 13:15:47 +00:00
Thomas Daubney
81899aba11 Add buffer protection to psa_raw_key_agreement
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-15 12:57:26 +00:00
David Horstmann
b539126670
Merge pull request #1156 from Ryan-Everett-arm/key-derivation-buffer-protection
Add buffer copying to the Key Derivation API
2024-02-15 11:54:20 +00:00
Gilles Peskine
69f11c8dfb generate key ext: skip driver invocation with non-default method
In the driver wrapper for psa_generate_key() and psa_generate_key_ext():

* Invoke the built-in code if using a non-default method, even if there
  might be an accelerator. This is ok because we only support non-default
  methods for RSA and we don't support driver-only RSA, therefore a
  non-default method will always have built-in code behind it.
* Return NOT_SUPPORTED if trying to use a non-default method with an opaque
  driver.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-15 11:57:48 +01:00
Gilles Peskine
c81393b2ed generate/derive key ext: pass method_data_length rather than method_length
Instead of passing the size of the whole structure, just pass the data
length and let the implementation worry about adding the size of the
structure. The intent with passing the structure size was to allow
the client code in a client-server implementation to know nothing
about the structure and just copy the bytes to the server. But that was not
really a useful consideration since the application has to know the
structure layout, so it has to be available in the client implementation's
headers. Passing the method data length makes life simpler for everyone by
not having to worry about possible padding at the end of the structure, and
removes a potential error condition
(method_length < sizeof(psa_key_generation_method_t)).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-15 11:57:48 +01:00
Gilles Peskine
7a18f9645c psa_generate_key_ext: RSA: support custom public exponent
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-15 11:57:46 +01:00
Gilles Peskine
f0765fa06a Implement psa_generate_key_ext, psa_key_derivation_output_key_ext
Implement and unit-test the new functions psa_generate_key_ext() and
psa_key_derivation_output_key_ext(), only for the default method.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-02-15 11:19:27 +01:00
Ryan Everett
73feaf2682 Comment on locking strategy in psa_fail_key_creation
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-14 14:19:54 +00:00
Ryan Everett
3d8118d9dc Revert psa_reserve_free_key_slot changes, lock in start_key_creation instead
This means we can hold the mutex around the call to reserve_free_key_slot
in get_and_lock_key_slot, avoiding inefficient rework.
(Changes to get_and_lock_key_slot are not in scope in this PR)

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-14 14:19:54 +00:00
Ryan Everett
b71014406c Make psa_fail_key_creation thread safe
Hold the mutex for the entirety of the call.
We need the mutex for the wipe, also hold it for aborting driver transactions as this
may have side effects.
We can't use the macros here as this function returns void.
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-14 14:19:54 +00:00
Ryan Everett
91ffe5b871 Make psa_finish_key_creation thread safe
Hold mutex for the entirety of the call.
We are writing to storage and writing to the slot state here.
If we didn't keep the mutex for the whole duration then we may end up with
another thread seeing that a persistent key is in storage before
our slot is set to FULL; this would be unlinearizable behaviour.

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-14 14:19:54 +00:00
Thomas Daubney
6adbb2a351 Implement safe buffer copying in asymm. encryption
Use local copy buffer macros to implement safe
copy mechanism in asymmetric encryption API.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:48:36 +00:00
Thomas Daubney
03f1ea3624 Change condition on wiping tag buffer
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:07:38 +00:00
Thomas Daubney
1ffc5cb4a5 Modify allocation and buffer wiping in sign_finish
Allocate immediately after declaration and only wipe
tag buffer if allocation didn't fail.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:07:38 +00:00
Thomas Daubney
7480a74cba Fix code style
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:07:38 +00:00
Thomas Daubney
c6705c6cb2 Conditionally include exit label
... on MAC functions where the label was only added
due to the modifications required by this PR.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:07:38 +00:00
Thomas Daubney
8db8d1a83e Implement safe buffer copying in MAC API
Use buffer local copy macros to implement safe
copy mechanism in MAC API.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 12:07:02 +00:00
Thomas Daubney
d2411565ce Fix code style
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 11:43:07 +00:00
Thomas Daubney
dedd1006b6 Conditionally include exit label
...on hash functions where the label was only added
due to the modifications required by this PR.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 11:43:07 +00:00
Thomas Daubney
51ffac9f40 Implement buffer copy code in psa_hash_compare
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 11:34:02 +00:00
Thomas Daubney
31d8c0bdb4 Make new internal function static
Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 11:34:02 +00:00
Thomas Daubney
1c5118e58c Implement safe buffer copying in hash API
Use local copy buffer macros to implement safe
copy mechanism in hash API.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
2024-02-12 11:34:02 +00:00
Ryan Everett
e02b63ac89
Merge branch 'Mbed-TLS:development' into threadsafe-key-locking 2024-02-12 10:04:07 +00:00
Janos Follath
f741db3d6e
Merge pull request #8764 from Ryan-Everett-arm/threadsafe-key-wiping
Make key destruction thread safe
2024-02-12 09:37:59 +00:00
David Horstmann
7175d71328 Remove unnecessary setting of status variable
The status is guaranteed to be PSA_SUCCESS at these points, so setting
them is redundant.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-02-09 18:20:05 +00:00
Ryan Everett
ee5920a7d5
Fix error path in psa_key_derivation_output_bytes
Co-authored-by: David Horstmann <david.horstmann@arm.com>
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-09 15:09:28 +00:00
Ryan Everett
7fee4f7318 Fix mutex unlock error handling in psa_destroy_key
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-09 14:11:27 +00:00
Ryan Everett
5d2e82f0ce Guard memcpy so that it won't fail on null input pointer
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-07 17:32:16 +00:00
Ryan Everett
b41c3c9582 Guard the exit to stop unused label warning
Signed-off-by: Ryan Everett <ryan.everett@arm.com>
2024-02-07 17:32:16 +00:00