blob: 51ec72fa19fce56270a0384620b9200708a51009 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010073#include "mbedtls/psa_util.h"
Paul Elliott600472b2024-02-23 17:26:58 +000074#include "mbedtls/threading.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075
Gilles Peskine3b380da2025-08-07 21:59:07 +020076#include "constant_time_internal.h"
77
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020078#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
79 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
80 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020081#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020082#endif
83
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010084/****************************************************************/
85/* Global data, support functions and library management */
86/****************************************************************/
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020089{
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020091}
92
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010093/* Values for psa_global_data_t::rng_state */
94#define RNG_NOT_INITIALIZED 0
95#define RNG_INITIALIZED 1
96#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010097
Paul Elliott0db6a902024-03-15 13:48:20 +000098/* IDs for PSA crypto subsystems. Starts at 1 to catch potential uninitialized
99 * variables as arguments. */
Paul Elliott47cee8e2024-03-08 21:38:02 +0000100typedef enum {
Paul Elliott0db6a902024-03-15 13:48:20 +0000101 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 1,
Paul Elliott47cee8e2024-03-08 21:38:02 +0000102 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS,
103 PSA_CRYPTO_SUBSYSTEM_RNG,
104 PSA_CRYPTO_SUBSYSTEM_TRANSACTION,
105} mbedtls_psa_crypto_subsystem;
106
Paul Elliottb24e36d2024-03-15 16:25:48 +0000107/* Initialization flags for global_data::initialized */
Paul Elliott47cee8e2024-03-08 21:38:02 +0000108#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01
109#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02
110#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04
111
112#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \
113 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \
114 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \
115 PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100118 uint8_t initialized;
119 uint8_t rng_state;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121} psa_global_data_t;
122
123static psa_global_data_t global_data;
124
Paul Elliott600472b2024-02-23 17:26:58 +0000125static uint8_t psa_get_initialized(void)
126{
127 uint8_t initialized;
128
129#if defined(MBEDTLS_THREADING_C)
Paul Elliott47cee8e2024-03-08 21:38:02 +0000130 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
131#endif /* defined(MBEDTLS_THREADING_C) */
132
133 initialized = global_data.rng_state == RNG_SEEDED;
134
135#if defined(MBEDTLS_THREADING_C)
136 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
137#endif /* defined(MBEDTLS_THREADING_C) */
138
139#if defined(MBEDTLS_THREADING_C)
Paul Elliott600472b2024-02-23 17:26:58 +0000140 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
141#endif /* defined(MBEDTLS_THREADING_C) */
142
Paul Elliott47cee8e2024-03-08 21:38:02 +0000143 initialized =
144 (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED));
Paul Elliott600472b2024-02-23 17:26:58 +0000145
146#if defined(MBEDTLS_THREADING_C)
147 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
148#endif /* defined(MBEDTLS_THREADING_C) */
149
150 return initialized;
151}
152
Paul Elliott35816522024-02-23 17:47:42 +0000153static uint8_t psa_get_drivers_initialized(void)
154{
155 uint8_t initialized;
156
157#if defined(MBEDTLS_THREADING_C)
158 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
159#endif /* defined(MBEDTLS_THREADING_C) */
160
Paul Elliott47cee8e2024-03-08 21:38:02 +0000161 initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0;
Paul Elliott35816522024-02-23 17:47:42 +0000162
163#if defined(MBEDTLS_THREADING_C)
164 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
165#endif /* defined(MBEDTLS_THREADING_C) */
166
167 return initialized;
168}
169
itayzafrir0adf0fc2018-09-06 16:24:41 +0300170#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000171 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300173
David Horstmann4a48bec2024-03-13 15:42:31 +0000174#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann36df4b22023-12-13 15:55:25 +0000175
David Horstmann62a56d92023-12-14 18:12:47 +0000176/* Declare a local copy of an input buffer and a variable that will be used
177 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000178 *
179 * Note: This macro must be called before any operations which may jump to
180 * the exit label, so that the local input copy object is safe to be freed.
181 *
182 * Assumptions:
183 * - input is the name of a pointer to the buffer to be copied
184 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000185 * - input_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000186 */
David Horstmann62a56d92023-12-14 18:12:47 +0000187#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
188 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
189 const uint8_t *input_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000190
David Horstmann62a56d92023-12-14 18:12:47 +0000191/* Allocate a copy of the buffer input and set the pointer input_copy to
192 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000193 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000194 * Assumptions:
195 * - psa_status_t status exists
196 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000197 * - input is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000198 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000199 */
David Horstmann62a56d92023-12-14 18:12:47 +0000200#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000201 status = psa_crypto_local_input_alloc(input, length, \
202 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000203 if (status != PSA_SUCCESS) { \
204 goto exit; \
205 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000206 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000207
David Horstmann36df4b22023-12-13 15:55:25 +0000208/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
209 *
David Horstmann3e72db42023-12-08 14:08:18 +0000210 * Assumptions:
David Horstmann62a56d92023-12-14 18:12:47 +0000211 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann36df4b22023-12-13 15:55:25 +0000212 * - input is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000213 */
David Horstmann62a56d92023-12-14 18:12:47 +0000214#define LOCAL_INPUT_FREE(input, input_copy) \
215 input_copy = NULL; \
David Horstmann36df4b22023-12-13 15:55:25 +0000216 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmanne9a88ab2023-11-29 17:07:13 +0000217
David Horstmann62a56d92023-12-14 18:12:47 +0000218/* Declare a local copy of an output buffer and a variable that will be used
219 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000220 *
221 * Note: This macro must be called before any operations which may jump to
222 * the exit label, so that the local output copy object is safe to be freed.
223 *
224 * Assumptions:
225 * - output is the name of a pointer to the buffer to be copied
226 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000227 * - output_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000228 */
David Horstmann62a56d92023-12-14 18:12:47 +0000229#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
230 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
231 uint8_t *output_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000232
David Horstmann62a56d92023-12-14 18:12:47 +0000233/* Allocate a copy of the buffer output and set the pointer output_copy to
234 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000235 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000236 * Assumptions:
237 * - psa_status_t status exists
238 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000239 * - output is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000240 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000241 */
David Horstmann62a56d92023-12-14 18:12:47 +0000242#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000243 status = psa_crypto_local_output_alloc(output, length, \
244 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000245 if (status != PSA_SUCCESS) { \
246 goto exit; \
247 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000248 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000249
David Horstmann62a56d92023-12-14 18:12:47 +0000250/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann3e72db42023-12-08 14:08:18 +0000251 * after first copying back its contents to the original buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000252 *
David Horstmann3e72db42023-12-08 14:08:18 +0000253 * Assumptions:
David Horstmann3e72db42023-12-08 14:08:18 +0000254 * - psa_status_t status exists
David Horstmann62a56d92023-12-14 18:12:47 +0000255 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
256 * - output is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000257 */
David Horstmann62a56d92023-12-14 18:12:47 +0000258#define LOCAL_OUTPUT_FREE(output, output_copy) \
259 output_copy = NULL; \
David Horstmann5a945f52023-12-13 14:09:08 +0000260 do { \
261 psa_status_t local_output_status; \
David Horstmann36df4b22023-12-13 15:55:25 +0000262 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann5a945f52023-12-13 14:09:08 +0000263 if (local_output_status != PSA_SUCCESS) { \
264 /* Since this error case is an internal error, it's more serious than \
265 * any existing error code and so it's fine to overwrite the existing \
266 * status. */ \
267 status = local_output_status; \
268 } \
269 } while (0)
David Horstmann4a48bec2024-03-13 15:42:31 +0000270#else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmann62a56d92023-12-14 18:12:47 +0000271#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
272 const uint8_t *input_copy_name = NULL;
273#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
274 input_copy = input;
275#define LOCAL_INPUT_FREE(input, input_copy) \
276 input_copy = NULL;
277#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
278 uint8_t *output_copy_name = NULL;
279#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
280 output_copy = output;
281#define LOCAL_OUTPUT_FREE(output, output_copy) \
282 output_copy = NULL;
David Horstmann4a48bec2024-03-13 15:42:31 +0000283#endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000284
285
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100286int psa_can_do_hash(psa_algorithm_t hash_alg)
287{
288 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000289 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100290}
Valerio Settic6f004f2023-12-12 11:27:36 +0100291
Valerio Setti1fff4f22023-12-28 14:19:34 +0100292int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100293{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100294 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100295 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000296 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100297}
298
299
Valerio Settia55f0422023-07-10 15:34:41 +0200300#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200301 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200302 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200303static int psa_is_dh_key_size_valid(size_t bits)
304{
Valerio Setti504a1022024-01-17 15:19:30 +0100305 switch (bits) {
306#if defined(PSA_WANT_DH_RFC7919_2048)
307 case 2048:
308 return 1;
309#endif /* PSA_WANT_DH_RFC7919_2048 */
310#if defined(PSA_WANT_DH_RFC7919_3072)
311 case 3072:
312 return 1;
313#endif /* PSA_WANT_DH_RFC7919_3072 */
314#if defined(PSA_WANT_DH_RFC7919_4096)
315 case 4096:
316 return 1;
317#endif /* PSA_WANT_DH_RFC7919_4096 */
318#if defined(PSA_WANT_DH_RFC7919_6144)
319 case 6144:
320 return 1;
321#endif /* PSA_WANT_DH_RFC7919_6144 */
322#if defined(PSA_WANT_DH_RFC7919_8192)
323 case 8192:
324 return 1;
325#endif /* PSA_WANT_DH_RFC7919_8192 */
326 default:
327 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200328 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200329}
Valerio Settia55f0422023-07-10 15:34:41 +0200330#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200331 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200332 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100335{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100336 /* Mbed TLS error codes can combine a high-level error code and a
337 * low-level error code. The low-level error usually reflects the
338 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 int low_level_ret = -(-ret & 0x007f);
340 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100344#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
346 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100348 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
349 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100350#endif
351
352#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200353 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
354 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
355 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
356 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
357 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200359 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200361 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100363#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200364
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100365#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000366 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100367 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100369#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100370
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100371#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100372 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100374 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100376#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100377
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100378#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200379 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100381#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200382
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100383#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200384 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200386 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100388#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200389
Dave Rodgman509b5672023-08-16 19:26:23 +0100390#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100391 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100393 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100395 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100397 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100399 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100401 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100403 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100405#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
408 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100409 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
410 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100411 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100413 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
414 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100416 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100418#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100419
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100420#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100421 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100423#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100424
Gilles Peskinee59236f2018-01-27 23:32:46 +0100425 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
426 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
427 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100429
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100430#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100431 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200433 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100437#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100438
Gilles Peskine82e57d12020-11-13 21:31:17 +0100439#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100441 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
442 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100443 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100445 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
446 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100448 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100450#endif
451
Dave Rodgmandea266f2023-08-31 11:52:43 +0100452#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100453 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100455 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100457 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100459#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100460 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100462#endif
463#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100464
Dave Rodgman509b5672023-08-16 19:26:23 +0100465#if defined(MBEDTLS_BIGNUM_C)
466#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100467 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100469#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100470 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100472 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100474 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100476 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100478 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100480 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100482 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100484#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100485
Dave Rodgman509b5672023-08-16 19:26:23 +0100486#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100489 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
490 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100492#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
493 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100494 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100496#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
498 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100502 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
503 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 case MBEDTLS_ERR_PK_INVALID_ALG:
508 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
509 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200513 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100515#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100516
Gilles Peskineff2d2002019-05-06 15:26:23 +0200517 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200519 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200521
Dave Rodgman509b5672023-08-16 19:26:23 +0100522#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100523 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100525 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100527 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100529 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100531 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
532 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100534 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100536 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100538 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100540#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200541
Dave Rodgman1dab4452023-09-01 09:59:51 +0100542#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300543 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300544 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300546 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300548 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300550 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
551 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300553 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100555 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100557
Dave Rodgman509b5672023-08-16 19:26:23 +0100558#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000559 case MBEDTLS_ERR_ECP_IN_PROGRESS:
560 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100561#endif
562#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000563
Janos Follatha13b9052019-11-22 12:48:59 +0000564 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300566
Gilles Peskinee59236f2018-01-27 23:32:46 +0100567 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100569 }
570}
571
Paul Elliottdc42ca82023-02-24 18:11:59 +0000572/**
573 * \brief For output buffers which contain "tags"
574 * (outputs that may be checked for validity like
575 * hashes, MACs and signatures), fill the unused
576 * part of the output buffer (the whole buffer on
577 * error, the trailing part on success) with
578 * something that isn't a valid tag (barring an
579 * attack on the tag and deliberately-crafted
580 * input), in case the caller doesn't check the
581 * return status properly.
582 *
583 * \param output_buffer Pointer to buffer to wipe. May not be NULL
584 * unless \p output_buffer_size is zero.
585 * \param status Status of function called to generate
586 * output_buffer originally
587 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
588 * could be NULL.
589 * \param output_buffer_length Length of data written to output_buffer, must be
590 * less than \p output_buffer_size
591 */
592static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000593 size_t output_buffer_size, size_t output_buffer_length)
594{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000595 size_t offset = 0;
596
597 if (output_buffer_size == 0) {
598 /* If output_buffer_size is 0 then we have nothing to do. We must not
599 call memset because output_buffer may be NULL in this case */
600 return;
601 }
602
603 if (status == PSA_SUCCESS) {
604 offset = output_buffer_length;
605 }
606
607 memset(output_buffer + offset, '!', output_buffer_size - offset);
608}
609
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
612 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613{
614 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200617 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200618 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200619 case PSA_KEY_TYPE_PASSWORD:
620 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200621 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100622#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (bits != 128 && bits != 192 && bits != 256) {
625 return PSA_ERROR_INVALID_ARGUMENT;
626 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 break;
628#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200629#if defined(PSA_WANT_KEY_TYPE_ARIA)
630 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (bits != 128 && bits != 192 && bits != 256) {
632 return PSA_ERROR_INVALID_ARGUMENT;
633 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200634 break;
635#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100636#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (bits != 128 && bits != 192 && bits != 256) {
639 return PSA_ERROR_INVALID_ARGUMENT;
640 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641 break;
642#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100643#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200644 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (bits != 64 && bits != 128 && bits != 192) {
646 return PSA_ERROR_INVALID_ARGUMENT;
647 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200648 break;
649#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100650#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200651 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (bits != 256) {
653 return PSA_ERROR_INVALID_ARGUMENT;
654 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200655 break;
656#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200657 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200659 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (bits % 8 != 0) {
661 return PSA_ERROR_INVALID_ARGUMENT;
662 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200665}
666
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100667/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668 *
Steven Cooremancd640932021-03-08 12:00:27 +0100669 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
670 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100671 *
672 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673 * \param[in] key_type The key type of the key to be used with the
674 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100675 *
676 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100677 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100678 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100679 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100680 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100681MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100682 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (PSA_ALG_IS_HMAC(algorithm)) {
686 if (key_type == PSA_KEY_TYPE_HMAC) {
687 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100688 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100689 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
692 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
693 * key. */
694 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
695 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
696 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
697 * the block length (larger than 1) for block ciphers. */
698 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
699 return PSA_SUCCESS;
700 }
701 }
702 }
703
704 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100705}
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
708 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200709{
Valerio Setti8d4f1502024-06-14 07:49:05 +0200710#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
Valerio Setti8d4f1502024-06-14 07:49:05 +0200711 if (buffer_length > ((size_t) MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)) {
712 return PSA_ERROR_NOT_SUPPORTED;
713 }
Valerio Setti8d4f1502024-06-14 07:49:05 +0200714#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (slot->key.data != NULL) {
716 return PSA_ERROR_ALREADY_EXISTS;
717 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 slot->key.data = mbedtls_calloc(1, buffer_length);
720 if (slot->key.data == NULL) {
721 return PSA_ERROR_INSUFFICIENT_MEMORY;
722 }
Valerio Setti8d4f1502024-06-14 07:49:05 +0200723#endif
Steven Cooreman75b74362020-07-28 14:30:13 +0200724
Ronald Cronea0f8a62020-11-25 17:52:23 +0100725 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200727}
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
730 const uint8_t *data,
731 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200732{
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 psa_status_t status = psa_allocate_buffer_to_slot(slot,
734 data_length);
735 if (status != PSA_SUCCESS) {
736 return status;
737 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 memcpy(slot->key.data, data, data_length);
740 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200741}
742
Ronald Crona0fe59f2020-11-29 11:14:53 +0100743psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100744 const psa_key_attributes_t *attributes,
745 const uint8_t *data, size_t data_length,
746 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100750 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200752 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (data_length == 0) {
754 return PSA_ERROR_NOT_SUPPORTED;
755 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (key_type_is_raw_bytes(type)) {
758 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200759
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100760 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 *bits);
762 if (status != PSA_SUCCESS) {
763 return status;
764 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200765
Ronald Crondd04d422020-11-28 15:14:42 +0100766 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100768 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 return PSA_SUCCESS;
772 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200773#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200774 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100775 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200776 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100777 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100778 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200779 return mbedtls_psa_ffdh_import_key(attributes,
780 data, data_length,
781 key_buffer, key_buffer_size,
782 key_buffer_length,
783 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100784 }
Valerio Settia55f0422023-07-10 15:34:41 +0200785#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200786 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200787#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
789 if (PSA_KEY_TYPE_IS_ECC(type)) {
790 return mbedtls_psa_ecp_import_key(attributes,
791 data, data_length,
792 key_buffer, key_buffer_size,
793 key_buffer_length,
794 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100795 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200796#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800797 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200798#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
799 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
801 if (PSA_KEY_TYPE_IS_RSA(type)) {
802 return mbedtls_psa_rsa_import_key(attributes,
803 data, data_length,
804 key_buffer, key_buffer_size,
805 key_buffer_length,
806 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200807 }
Valerio Settife478902023-07-25 12:27:19 +0200808#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
809 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800810 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100811 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100814}
815
Gilles Peskinef603c712019-01-19 13:40:11 +0100816/** Calculate the intersection of two algorithm usage policies.
817 *
818 * Return 0 (which allows no operation) on incompatibility.
819 */
820static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100821 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100824{
Gilles Peskine549ea862019-05-22 11:45:59 +0200825 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (alg1 == alg2) {
827 return alg1;
828 }
Steven Cooreman03488022021-02-18 12:07:20 +0100829 /* If the policies are from the same hash-and-sign family, check
830 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
832 PSA_ALG_IS_SIGN_HASH(alg2) &&
833 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
834 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
835 return alg2;
836 }
837 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
838 return alg1;
839 }
Steven Cooreman03488022021-02-18 12:07:20 +0100840 }
841 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100842 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100843 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
845 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
846 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
847 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
848 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100849 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100850
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100851 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
853 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
854 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
855 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100856 }
857 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
859 (alg1_len <= alg2_len)) {
860 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100861 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
863 (alg2_len <= alg1_len)) {
864 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100865 }
866 }
867 /* If the policies are from the same MAC family, check whether one
868 * of them is a minimum-MAC-length policy. Calculate the most
869 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
871 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
872 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100873 /* Validate the combination of key type and algorithm. Since the base
874 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
876 return 0;
877 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100878
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879 /* Get the (exact or at-least) output lengths for both sides of the
880 * requested intersection. None of the currently supported algorithms
881 * have an output length dependent on the actual key size, so setting it
882 * to a bogus value of 0 is currently OK.
883 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100884 * Note that for at-least-this-length wildcard algorithms, the output
885 * length is set to the shortest allowed length, which allows us to
886 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
888 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100889 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100890
Steven Cooremana1d83222021-02-25 10:20:29 +0100891 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
893 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
894 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100895 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100896
897 /* If only one is an at-least-this-length policy, the intersection would
898 * be the other (fixed-length) policy as long as said fixed length is
899 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
901 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100902 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
904 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100905 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100906
Steven Cooremancd640932021-03-08 12:00:27 +0100907 /* If none of them are wildcards, check whether they define the same tag
908 * length. This is still possible here when one is default-length and
909 * the other specific-length. Ensure to always return the
910 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (alg1_len == alg2_len) {
912 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
913 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100914 }
915 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100917}
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919static int psa_key_algorithm_permits(psa_key_type_t key_type,
920 psa_algorithm_t policy_alg,
921 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200922{
Gilles Peskine549ea862019-05-22 11:45:59 +0200923 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (requested_alg == policy_alg) {
925 return 1;
926 }
Steven Cooreman03488022021-02-18 12:07:20 +0100927 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
928 * and requested_alg is the same hash-and-sign family with any hash,
929 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
931 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
932 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
933 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100934 }
935 /* If policy_alg is a wildcard AEAD algorithm of the same base as
936 * the requested algorithm, check the requested tag length to be
937 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (PSA_ALG_IS_AEAD(policy_alg) &&
939 PSA_ALG_IS_AEAD(requested_alg) &&
940 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
941 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
942 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
943 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
944 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100945 }
Steven Cooremancd640932021-03-08 12:00:27 +0100946 /* If policy_alg is a MAC algorithm of the same base as the requested
947 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (PSA_ALG_IS_MAC(policy_alg) &&
949 PSA_ALG_IS_MAC(requested_alg) &&
950 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
951 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100952 /* Validate the combination of key type and algorithm. Since the policy
953 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
955 return 0;
956 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100957
Steven Cooreman31a876d2021-03-03 20:47:40 +0100958 /* Get both the requested output length for the algorithm which is to be
959 * verified, and the default output length for the base algorithm.
960 * Note that none of the currently supported algorithms have an output
961 * length dependent on actual key size, so setting it to a bogus value
962 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100963 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100965 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 key_type, 0,
967 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100968
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100969 /* If the policy is default-length, only allow an algorithm with
970 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
972 return requested_output_length == default_output_length;
973 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100974
975 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100976 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
978 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
979 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100980 }
981
Steven Cooremancd640932021-03-08 12:00:27 +0100982 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
983 * check for the requested MAC length to be equal to or longer than the
984 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
986 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
987 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100988 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200989 }
Steven Cooremance48e852020-10-05 16:02:45 +0200990 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200991 * a key derivation with that key agreement should also be allowed. This
992 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
994 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
995 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
996 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200997 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100998 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001000}
1001
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001002/** Test whether a policy permits an algorithm.
1003 *
1004 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +01001005 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001006 * \note This function requires providing the key type for which the policy is
1007 * being validated, since some algorithm policy definitions (e.g. MAC)
1008 * have different properties depending on what kind of cipher it is
1009 * combined with.
1010 *
Steven Cooreman7e39f052021-02-23 14:18:32 +01001011 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1012 * allowed by the \p policy.
1013 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1014 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1015 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1018 psa_key_type_t key_type,
1019 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020{
Steven Cooremand788fab2021-02-25 11:29:17 +01001021 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (alg == 0) {
1023 return PSA_ERROR_INVALID_ARGUMENT;
1024 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001025
Steven Cooreman7e39f052021-02-23 14:18:32 +01001026 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (PSA_ALG_IS_WILDCARD(alg)) {
1028 return PSA_ERROR_INVALID_ARGUMENT;
1029 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1032 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1033 return PSA_SUCCESS;
1034 } else {
1035 return PSA_ERROR_NOT_PERMITTED;
1036 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001037}
1038
Gilles Peskinef603c712019-01-19 13:40:11 +01001039/** Restrict a key policy based on a constraint.
1040 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001041 * \note This function requires providing the key type for which the policy is
1042 * being restricted, since some algorithm policy definitions (e.g. MAC)
1043 * have different properties depending on what kind of cipher it is
1044 * combined with.
1045 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001046 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001047 * \param[in,out] policy The policy to restrict.
1048 * \param[in] constraint The policy constraint to apply.
1049 *
1050 * \retval #PSA_SUCCESS
1051 * \c *policy contains the intersection of the original value of
1052 * \c *policy and \c *constraint.
1053 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001054 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 * \c *policy is unchanged.
1056 */
1057static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001058 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001059 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001061{
1062 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1064 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001065 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1067 constraint->alg2);
1068 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1069 return PSA_ERROR_INVALID_ARGUMENT;
1070 }
1071 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1072 return PSA_ERROR_INVALID_ARGUMENT;
1073 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001074 policy->usage &= constraint->usage;
1075 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001078}
1079
Przemek Stekiel061f6942022-11-30 10:51:35 +01001080/** Get the description of a key given its identifier and policy constraints
1081 * and lock it.
1082 *
1083 * The key must have allow all the usage flags set in \p usage. If \p alg is
1084 * nonzero, the key must allow operations with this algorithm. If \p alg is
1085 * zero, the algorithm is not checked.
1086 *
1087 * In case of a persistent key, the function loads the description of the key
1088 * into a key slot if not already done.
1089 *
Ryan Everett098c6652024-01-03 13:03:36 +00001090 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001091 * It is the responsibility of the caller to then unregister
1092 * once they have finished reading the contents of the slot.
1093 * The caller unregisters by calling psa_unregister_read() or
1094 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1095 * if and only if the caller already holds the global key slot mutex
1096 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1097 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +01001098 */
1099static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001100 mbedtls_svc_key_id_t key,
1101 psa_key_slot_t **p_slot,
1102 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001104{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001106 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 status = psa_get_and_lock_key_slot(key, p_slot);
1109 if (status != PSA_SUCCESS) {
1110 return status;
1111 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001112 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001113
1114 /* Enforce that usage policy for the key slot contains all the flags
1115 * required by the usage parameter. There is one exception: public
1116 * keys can always be exported, so we treat public key objects as
1117 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001119 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001123 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001124 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001125 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001126
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001127 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (alg != 0) {
1129 status = psa_key_policy_permits(&slot->attr.policy,
1130 slot->attr.type,
1131 alg);
1132 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001133 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001135 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001138
1139error:
1140 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001141 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001144}
Darryl Green940d72c2018-07-13 13:18:51 +01001145
Ronald Cron5c522922020-11-14 16:35:34 +01001146/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001147 *
1148 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001149 * available, as opposed to a key in a secure element and/or to be used
1150 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001151 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001152 * This is a temporary function that may be used instead of
1153 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1154 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001155 *
Ryan Everett098c6652024-01-03 13:03:36 +00001156 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001157 * It is the responsibility of the caller to then unregister
1158 * once they have finished reading the contents of the slot.
1159 * The caller unregisters by calling psa_unregister_read() or
1160 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1161 * if and only if the caller already holds the global key slot mutex
1162 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1163 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001164 */
Ronald Cron5c522922020-11-14 16:35:34 +01001165static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1166 mbedtls_svc_key_id_t key,
1167 psa_key_slot_t **p_slot,
1168 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001170{
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1172 usage, alg);
1173 if (status != PSA_SUCCESS) {
1174 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001175 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001178 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 *p_slot = NULL;
1180 return PSA_ERROR_NOT_SUPPORTED;
1181 }
1182
1183 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001184}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001187{
Valerio Settidbfb2ab2024-08-16 12:52:19 +02001188#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
1189 if (slot->key.bytes > 0) {
1190 mbedtls_platform_zeroize(slot->key.data, MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE);
1191 }
1192#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001194 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001196
Ronald Cronea0f8a62020-11-25 17:52:23 +01001197 slot->key.data = NULL;
Valerio Setti8d4f1502024-06-14 07:49:05 +02001198#endif /* MBEDTLS_PSA_STATIC_KEY_SLOTS */
1199
Ronald Cronea0f8a62020-11-25 17:52:23 +01001200 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001203}
1204
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001205/** Completely wipe a slot in memory, including its policy.
1206 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001208{
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 /*
1212 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001213 * do our best to report an unexpected amount of registered readers or
1214 * an unexpected state.
1215 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1216 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1218 * function is called as part of the execution of a test suite, the
1219 * execution of the test suite is stopped in error if the assertion fails.
1220 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001221 switch (slot->state) {
1222 case PSA_SLOT_FULL:
1223 /* In this state psa_wipe_key_slot() must only be called if the
1224 * caller is the last reader. */
1225 case PSA_SLOT_PENDING_DELETION:
1226 /* In this state psa_wipe_key_slot() must only be called if the
1227 * caller is the last reader. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001228 if (slot->var.occupied.registered_readers != 1) {
1229 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 1);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001230 status = PSA_ERROR_CORRUPTION_DETECTED;
1231 }
1232 break;
1233 case PSA_SLOT_FILLING:
1234 /* In this state registered_readers must be 0. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001235 if (slot->var.occupied.registered_readers != 0) {
1236 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 0);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001237 status = PSA_ERROR_CORRUPTION_DETECTED;
1238 }
1239 break;
1240 case PSA_SLOT_EMPTY:
1241 /* The slot is already empty, it cannot be wiped. */
1242 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1243 status = PSA_ERROR_CORRUPTION_DETECTED;
1244 break;
1245 default:
1246 /* The slot's state is invalid. */
1247 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001248 }
1249
Gilles Peskinee8199f52024-06-10 11:53:33 +02001250#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1251 size_t slice_index = slot->slice_index;
1252#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1253
1254
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001255 /* Multipart operations may still be using the key. This is safe
1256 * because all multipart operation objects are independent from
1257 * the key slot: if they need to access the key after the setup
1258 * phase, they have a copy of the key. Note that this means that
1259 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001260 /* At this point, key material and other type-specific content has
1261 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001262 * zeroize because the metadata is not particularly sensitive.
1263 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 memset(slot, 0, sizeof(*slot));
Gilles Peskinee8199f52024-06-10 11:53:33 +02001265
1266#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1267 /* If the slot is already corrupted, something went deeply wrong,
1268 * like a thread still using the slot or a stray pointer leading
1269 * to the slot's memory being used for another object. Let the slot
1270 * leak rather than make the corruption worse. */
1271 if (status == PSA_SUCCESS) {
1272 status = psa_free_key_slot(slice_index, slot);
1273 }
1274#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001277}
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001281 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282 psa_status_t status; /* status of the last operation */
1283 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1285 psa_se_drv_table_entry_t *driver;
1286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (mbedtls_svc_key_id_is_null(key)) {
1289 return PSA_SUCCESS;
1290 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001291
Ronald Cronf2911112020-10-29 17:51:10 +01001292 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001293 * Get the description of the key in a key slot, and register to read it.
1294 * In the case of a persistent key, this will load the key description
1295 * from persistent memory if not done yet.
1296 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001297 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001298 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 status = psa_get_and_lock_key_slot(key, &slot);
1300 if (status != PSA_SUCCESS) {
1301 return status;
1302 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001303
Ryan Everettc053d962024-01-25 17:56:32 +00001304#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001305 /* We cannot unlock between setting the state to PENDING_DELETION
1306 * and destroying the key in storage, as otherwise another thread
1307 * could load the key into a new slot and the key will not be
1308 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001309 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1310 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001311
1312 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1313 /* Another thread has destroyed the key between us locking the slot
1314 * and us gaining the mutex. Unregister from the slot,
1315 * and report that the key does not exist. */
1316 status = psa_unregister_read(slot);
1317
1318 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1319 &mbedtls_threading_key_slot_mutex));
1320 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1321 }
Ryan Everettc053d962024-01-25 17:56:32 +00001322#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001323 /* Set the key slot containing the key description's state to
1324 * PENDING_DELETION. This stops new operations from registering
1325 * to read the slot. Current readers can safely continue to access
1326 * the key within the slot; the last registered reader will
1327 * automatically wipe the slot when they call psa_unregister_read().
1328 * If the key is persistent, we can now delete the copy of the key
1329 * from memory. If the key is opaque, we require the driver to
1330 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001331 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1332 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001333
Ryan Everettd868b742024-03-08 18:35:09 +00001334 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001335 goto exit;
1336 }
Ronald Cronf2911112020-10-29 17:51:10 +01001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001339 /* Refuse the destruction of a read-only key (which may or may not work
1340 * if we attempt it, depending on whether the key is merely read-only
1341 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001342 * Just do the best we can, which is to wipe the copy in memory
1343 * (done in this function's cleanup code). */
1344 overall_status = PSA_ERROR_NOT_PERMITTED;
1345 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001346 }
1347
Gilles Peskine354f7672019-07-12 23:46:38 +02001348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1350 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001351 /* For a key in a secure element, we need to do three things:
1352 * remove the key file in internal storage, destroy the
1353 * key inside the secure element, and update the driver's
1354 * persistent data. Start a transaction that will encompass these
1355 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001357 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001359 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 status = psa_crypto_save_transaction();
1361 if (status != PSA_SUCCESS) {
1362 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001363 /* We should still try to destroy the key in the secure
1364 * element and the key metadata in storage. This is especially
1365 * important if the error is that the storage is full.
1366 * But how to do it exactly without risking an inconsistent
1367 * state after a reset?
1368 * https://github.com/ARMmbed/mbed-crypto/issues/215
1369 */
1370 overall_status = status;
1371 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001372 }
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 status = psa_destroy_se_key(driver,
1375 psa_key_slot_get_slot_number(slot));
1376 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001377 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001379 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001380#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1381
Darryl Greend49a4992018-06-18 17:27:26 +01001382#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001384 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001385 * The slot will still hold a copy of the key until the last reader
1386 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 status = psa_destroy_persistent_key(slot->attr.id);
1388 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001389 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 }
Darryl Greend49a4992018-06-18 17:27:26 +01001391 }
1392#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001393
Gilles Peskinefc762652019-07-22 19:30:34 +02001394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (driver != NULL) {
1396 status = psa_save_se_persistent_data(driver);
1397 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001398 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 }
1400 status = psa_crypto_stop_transaction();
1401 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001402 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001404 }
1405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1406
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001407exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001408 /* Unregister from reading the slot. If we are the last active reader
1409 * then this will wipe the slot. */
1410 status = psa_unregister_read(slot);
1411 /* Prioritize CORRUPTION_DETECTED from unregistering over
1412 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001414 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 }
Ryan Everettc053d962024-01-25 17:56:32 +00001416
1417#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001418 /* Don't overwrite existing errors if the unlock fails. */
1419 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001420 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1421 &mbedtls_threading_key_slot_mutex));
1422#endif
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425}
1426
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001427/** Retrieve all the publicly-accessible attributes of a key.
1428 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001429psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1430 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001433 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1438 if (status != PSA_SUCCESS) {
1439 return status;
1440 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001441
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001442 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001443
1444#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1446 psa_set_key_slot_number(attributes,
1447 psa_key_slot_get_slot_number(slot));
1448 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001449#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001450
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001451 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452}
1453
Gilles Peskinec8000c02019-08-02 20:15:51 +02001454#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1455psa_status_t psa_get_key_slot_number(
1456 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001458{
Gilles Peskine972539c2024-02-28 01:49:45 +01001459 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001460 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return PSA_SUCCESS;
1462 } else {
1463 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001464 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001465}
1466#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1469 size_t key_buffer_size,
1470 uint8_t *data,
1471 size_t data_size,
1472 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001473{
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (key_buffer_size > data_size) {
1475 return PSA_ERROR_BUFFER_TOO_SMALL;
1476 }
1477 memcpy(data, key_buffer, key_buffer_size);
1478 memset(data + key_buffer_size, 0,
1479 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001480 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001482}
1483
Ronald Cron7285cda2020-11-26 14:46:37 +01001484psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001485 const psa_key_attributes_t *attributes,
1486 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001489 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (key_type_is_raw_bytes(type) ||
1492 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001493 PSA_KEY_TYPE_IS_ECC(type) ||
1494 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 return psa_export_key_buffer_internal(
1496 key_buffer, key_buffer_size,
1497 data, data_size, data_length);
1498 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001499 /* This shouldn't happen in the reference implementation, but
1500 it is valid for a special-purpose implementation to omit
1501 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001503 }
1504}
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001507 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 size_t data_size,
1509 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001510{
1511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1512 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1513 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001514 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001515
Ronald Crone907e552021-01-18 13:22:38 +01001516 /* Reject a zero-length output buffer now, since this can never be a
1517 * valid key representation. This way we know that data must be a valid
1518 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (data_size == 0) {
1520 return PSA_ERROR_BUFFER_TOO_SMALL;
1521 }
Ronald Crone907e552021-01-18 13:22:38 +01001522
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523 /* Set the key to empty now, so that even when there are errors, we always
1524 * set data_length to a value between 0 and data_size. On error, setting
1525 * the key to empty is a good choice because an empty key representation is
1526 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001527 *data_length = 0;
1528
Ronald Cron9486f9d2020-11-26 13:36:23 +01001529 /* Export requires the EXPORT flag. There is an exception for public keys,
1530 * which don't require any flag, but
1531 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1534 PSA_KEY_USAGE_EXPORT, 0);
1535 if (status != PSA_SUCCESS) {
1536 return status;
1537 }
mohammad160306e79202018-03-28 13:17:44 +03001538
Ryan Everett45ac5262024-01-08 17:15:19 +00001539 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1540
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001541 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 slot->key.data, slot->key.bytes,
1543 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001544
David Horstmann4a48bec2024-03-13 15:42:31 +00001545#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001546exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001547#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001548 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001549
Ryan Everett45ac5262024-01-08 17:15:19 +00001550 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001552}
1553
Ronald Cron7285cda2020-11-26 14:46:37 +01001554psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001555 const psa_key_attributes_t *attributes,
1556 const uint8_t *key_buffer,
1557 size_t key_buffer_size,
1558 uint8_t *data,
1559 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001561{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001562 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001563
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001564 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001565 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1566 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001567 /* Exporting public -> public */
1568 return psa_export_key_buffer_internal(
1569 key_buffer, key_buffer_size,
1570 data, data_size, data_length);
1571 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001572#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001573 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1574 return mbedtls_psa_rsa_export_public_key(attributes,
1575 key_buffer,
1576 key_buffer_size,
1577 data,
1578 data_size,
1579 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001580#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001581 /* We don't know how to convert a private RSA key to public. */
1582 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001583#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001584 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001585 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001586#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001587 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1588 return mbedtls_psa_ecp_export_public_key(attributes,
1589 key_buffer,
1590 key_buffer_size,
1591 data,
1592 data_size,
1593 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001595 /* We don't know how to convert a private ECC key to public */
1596 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001597#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001598 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001599 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001600#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001601 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001602 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001603 key_buffer,
1604 key_buffer_size,
1605 data, data_size,
1606 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001607#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001608 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001609#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001610 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001612 (void) key_buffer;
1613 (void) key_buffer_size;
1614 (void) data;
1615 (void) data_size;
1616 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001618 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619}
Ronald Crond18b5f82020-11-25 16:46:05 +01001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001622 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 size_t data_size,
1624 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001625{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001627 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001628 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001629
Ryan Everettb1d2c672024-01-08 17:19:30 +00001630 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001631
Ronald Crone907e552021-01-18 13:22:38 +01001632 /* Reject a zero-length output buffer now, since this can never be a
1633 * valid key representation. This way we know that data must be a valid
1634 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (data_size == 0) {
1636 return PSA_ERROR_BUFFER_TOO_SMALL;
1637 }
Ronald Crone907e552021-01-18 13:22:38 +01001638
Darryl Greendd8fb772018-11-07 16:00:44 +00001639 /* Set the key to empty now, so that even when there are errors, we always
1640 * set data_length to a value between 0 and data_size. On error, setting
1641 * the key to empty is a good choice because an empty key representation is
1642 * unlikely to be accepted anywhere. */
1643 *data_length = 0;
1644
1645 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1647 if (status != PSA_SUCCESS) {
1648 return status;
1649 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001650
Ryan Everettb1d2c672024-01-08 17:19:30 +00001651 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1654 status = PSA_ERROR_INVALID_ARGUMENT;
1655 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001656 }
1657
Ronald Cron67227982020-11-26 15:16:05 +01001658 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001659 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001661
1662exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001663 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001664
Ryan Everettb1d2c672024-01-08 17:19:30 +00001665 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001667}
1668
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001669/** Validate that a key policy is internally well-formed.
1670 *
1671 * This function only rejects invalid policies. It does not validate the
1672 * consistency of the policy with respect to other attributes of the key
1673 * such as the key type.
1674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001676{
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1678 PSA_KEY_USAGE_COPY |
1679 PSA_KEY_USAGE_ENCRYPT |
1680 PSA_KEY_USAGE_DECRYPT |
1681 PSA_KEY_USAGE_SIGN_MESSAGE |
1682 PSA_KEY_USAGE_VERIFY_MESSAGE |
1683 PSA_KEY_USAGE_SIGN_HASH |
1684 PSA_KEY_USAGE_VERIFY_HASH |
1685 PSA_KEY_USAGE_VERIFY_DERIVATION |
1686 PSA_KEY_USAGE_DERIVE)) != 0) {
1687 return PSA_ERROR_INVALID_ARGUMENT;
1688 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001691}
1692
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001693/** Validate the internal consistency of key attributes.
1694 *
1695 * This function only rejects invalid attribute values. If does not
1696 * validate the consistency of the attributes with any key data that may
1697 * be involved in the creation of the key.
1698 *
1699 * Call this function early in the key creation process.
1700 *
1701 * \param[in] attributes Key attributes for the new key.
1702 * \param[out] p_drv On any return, the driver for the key, if any.
1703 * NULL for a transparent key.
1704 *
1705 */
1706static psa_status_t psa_validate_key_attributes(
1707 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001709{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001710 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1712 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 status = psa_validate_key_location(lifetime, p_drv);
1715 if (status != PSA_SUCCESS) {
1716 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001717 }
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_validate_key_persistence(lifetime);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
1723
1724 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1725 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1726 return PSA_ERROR_INVALID_ARGUMENT;
1727 }
1728 } else {
1729 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1730 return PSA_ERROR_INVALID_ARGUMENT;
1731 }
1732 }
1733
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001734 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (status != PSA_SUCCESS) {
1736 return status;
1737 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001738
1739 /* Refuse to create overly large keys.
1740 * Note that this doesn't trigger on import if the attributes don't
1741 * explicitly specify a size (so psa_get_key_bits returns 0), so
1742 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1744 return PSA_ERROR_NOT_SUPPORTED;
1745 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748}
1749
Gilles Peskine4747d192019-04-17 15:05:45 +02001750/** Prepare a key slot to receive key material.
1751 *
1752 * This function allocates a key slot and sets its metadata.
1753 *
1754 * If this function fails, call psa_fail_key_creation().
1755 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * This function is intended to be used as follows:
1757 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 * it with the specified attributes, and in case of a volatile key assign it
1759 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001760 * -# Populate the slot with the key material.
1761 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1762 * In case of failure at any step, stop the sequence and call
1763 * psa_fail_key_creation().
1764 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001765 * On success, the key slot's state is PSA_SLOT_FILLING.
1766 * It is the responsibility of the caller to change the slot's state to
1767 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001768 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001769 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001770 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001771 * \param[out] p_slot On success, a pointer to the prepared slot.
1772 * \param[out] p_drv On any return, the driver for the key, if any.
1773 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001774 *
1775 * \retval #PSA_SUCCESS
1776 * The key slot is ready to receive key material.
1777 * \return If this function fails, the key slot is an invalid state.
1778 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 */
1780static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001781 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001783 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
1786 psa_status_t status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001787
Gilles Peskinedf179142019-07-15 22:02:14 +02001788 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 *p_drv = NULL;
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 status = psa_validate_key_attributes(attributes, p_drv);
1792 if (status != PSA_SUCCESS) {
1793 return status;
1794 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001795
Gilles Peskinee8199f52024-06-10 11:53:33 +02001796 int key_is_volatile = PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime);
1797 psa_key_id_t volatile_key_id;
1798
Ryan Everett3d8118d2024-01-30 16:58:47 +00001799#if defined(MBEDTLS_THREADING_C)
1800 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1801 &mbedtls_threading_key_slot_mutex));
1802#endif
Gilles Peskinee8199f52024-06-10 11:53:33 +02001803 status = psa_reserve_free_key_slot(
1804 key_is_volatile ? &volatile_key_id : NULL,
1805 p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001806#if defined(MBEDTLS_THREADING_C)
1807 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1808 &mbedtls_threading_key_slot_mutex));
1809#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (status != PSA_SUCCESS) {
1811 return status;
1812 }
Gilles Peskinee8199f52024-06-10 11:53:33 +02001813 psa_key_slot_t *slot = *p_slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001814
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001815 /* We're storing the declared bit-size of the key. It's up to each
1816 * creation mechanism to verify that this information is correct.
1817 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001818 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001819 * is optional (import, copy). In case of a volatile key, assign it the
1820 * volatile key identifier associated to the slot returned to contain its
1821 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001822
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001823 slot->attr = *attributes;
Gilles Peskinee8199f52024-06-10 11:53:33 +02001824 if (key_is_volatile) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001825#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1826 slot->attr.id = volatile_key_id;
1827#else
1828 slot->attr.id.key_id = volatile_key_id;
1829#endif
1830 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001831
Gilles Peskinecbaff462019-07-12 23:46:04 +02001832#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001833 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001834 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001835 * create the key file in internal storage, create the
1836 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001837 * persistent data. This is done by starting a transaction that will
1838 * encompass these three actions.
1839 * For registering a volatile key, we just need to find an appropriate
1840 * slot number inside the SE. Since the key is designated volatile, creating
1841 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001842 /* The first thing to do is to find a slot number for the new key.
1843 * We save the slot number in persistent storage as part of the
1844 * transaction data. It will be needed to recover if the power
1845 * fails during the key creation process, to clean up on the secure
1846 * element side after restarting. Obtaining a slot number from the
1847 * secure element driver updates its persistent state, but we do not yet
1848 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001849 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001851 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1853 &slot_number);
1854 if (status != PSA_SUCCESS) {
1855 return status;
1856 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001857
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001858 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001860 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001861 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001862 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 status = psa_crypto_save_transaction();
1864 if (status != PSA_SUCCESS) {
1865 (void) psa_crypto_stop_transaction();
1866 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001867 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001868 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001869
1870 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everettb5a20d32023-11-15 16:26:01 +00001872 if (status != PSA_SUCCESS) {
1873 return status;
1874 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001875 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001878 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001880 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001881#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001884}
Gilles Peskine4747d192019-04-17 15:05:45 +02001885
1886/** Finalize the creation of a key once its key material has been set.
1887 *
1888 * This entails writing the key to persistent storage.
1889 *
1890 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001891 * See the documentation of psa_start_key_creation() for the intended use
1892 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001894 * If the finalization succeeds, the function sets the key slot's state to
1895 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1896 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001897 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001898 * \param[in,out] slot Pointer to the slot with key material.
1899 * \param[in] driver The secure element driver for the key,
1900 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001901 * \param[out] key On success, identifier of the key. Note that the
1902 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001903 *
1904 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001905 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001906 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1907 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1908 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1909 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1910 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1911 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001912 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001913 * \return If this function fails, the key slot is an invalid state.
1914 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001915 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001916static psa_status_t psa_finish_key_creation(
1917 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001918 psa_se_drv_table_entry_t *driver,
1919 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001920{
1921 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001922 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001923 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001924
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001925#if defined(MBEDTLS_THREADING_C)
1926 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1927 &mbedtls_threading_key_slot_mutex));
1928#endif
1929
Gilles Peskine4747d192019-04-17 15:05:45 +02001930#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001932#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001934 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001935 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001937
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001938 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1939 sizeof(data.slot_number),
1940 "Slot number size does not match psa_se_key_data_storage_t");
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1943 status = psa_save_persistent_key(&slot->attr,
1944 (uint8_t *) &data,
1945 sizeof(data));
1946 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001947#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1948 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001949 /* Key material is saved in export representation in the slot, so
1950 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 status = psa_save_persistent_key(&slot->attr,
1952 slot->key.data,
1953 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001954 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001955 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001956#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1957
Gilles Peskinecbaff462019-07-12 23:46:04 +02001958#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001959 /* Finish the transaction for a key creation. This does not
1960 * happen when registering an existing key. Detect this case
1961 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001962 * creation of a persistent key in a secure element requires a transaction,
1963 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if (driver != NULL &&
1965 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1966 status = psa_save_se_persistent_data(driver);
1967 if (status != PSA_SUCCESS) {
1968 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001969
1970#if defined(MBEDTLS_THREADING_C)
1971 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1972 &mbedtls_threading_key_slot_mutex));
1973#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001975 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001977 }
1978#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001981 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001982 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1983 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001985 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 }
Ronald Cron50972942020-11-14 11:28:25 +01001988
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001989#if defined(MBEDTLS_THREADING_C)
1990 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1991 &mbedtls_threading_key_slot_mutex));
1992#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001994}
1995
1996/** Abort the creation of a key.
1997 *
1998 * You may call this function after calling psa_start_key_creation(),
1999 * or after psa_finish_key_creation() fails. In other circumstances, this
2000 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002001 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00002002 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02002003 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 * \param[in,out] slot Pointer to the slot with key material.
2005 * \param[in] driver The secure element driver for the key,
2006 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008static void psa_fail_key_creation(psa_key_slot_t *slot,
2009 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002010{
Gilles Peskine011e4282019-06-26 18:34:38 +02002011 (void) driver;
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002016
Ryan Everettb7101442024-01-23 20:09:49 +00002017#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00002018 /* If the lock operation fails we still wipe the slot.
2019 * Operations will no longer work after a failed lock,
2020 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00002021 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
2022#endif
2023
Gilles Peskine011e4282019-06-26 18:34:38 +02002024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002025 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002026 * element, and the failure happened later (when saving metadata
2027 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002028 * element.
2029 * https://github.com/ARMmbed/mbed-crypto/issues/217
2030 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002031
Gilles Peskined7729582019-08-05 15:55:54 +02002032 /* Abort the ongoing transaction if any (there may not be one if
2033 * the creation process failed before starting one, or if the
2034 * key creation is a registration of a key in a secure element).
2035 * Earlier functions must already have done what it takes to undo any
2036 * partial creation. All that's left is to update the transaction data
2037 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002039#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00002042
2043#if defined(MBEDTLS_THREADING_C)
2044 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
2045#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02002046}
2047
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002048/** Validate optional attributes during key creation.
2049 *
2050 * Some key attributes are optional during key creation. If they are
2051 * specified in the attributes structure, check that they are consistent
2052 * with the data in the slot.
2053 *
2054 * This function should be called near the end of key creation, after
2055 * the slot in memory is fully populated but before saving persistent data.
2056 */
2057static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002061 if (attributes->type != 0) {
2062 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 return PSA_ERROR_INVALID_ARGUMENT;
2064 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065 }
2066
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002067 if (attributes->bits != 0) {
2068 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return PSA_ERROR_INVALID_ARGUMENT;
2070 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 }
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074}
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00002077 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 size_t data_length,
2079 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002080{
2081 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00002082 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002083 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002084 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002085 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302086 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087
Ronald Cron81709fc2020-11-14 12:10:32 +01002088 *key = MBEDTLS_SVC_KEY_ID_INIT;
2089
Gilles Peskine0f84d622019-09-12 19:03:13 +02002090 /* Reject zero-length symmetric keys (including raw data key objects).
2091 * This also rejects any key which might be encoded as an empty string,
2092 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (data_length == 0) {
2094 return PSA_ERROR_INVALID_ARGUMENT;
2095 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002096
Archana449608b2021-09-08 15:36:05 +05302097 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (data_length > SIZE_MAX / 8) {
2099 return PSA_ERROR_NOT_SUPPORTED;
2100 }
Archana6ed4bda2021-08-04 10:47:15 +05302101
Ryan Everettf028fe12024-01-08 17:14:44 +00002102 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2105 &slot, &driver);
2106 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002110 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302111 * storage ( thus not in the case of importing a key in a secure element
2112 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2113 * buffer to hold the imported key material. */
Valerio Setti70fa89c2024-08-14 05:12:45 +02002114 if (slot->key.bytes == 0) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002115 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302116 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 attributes, data, data_length, &storage_size);
2118 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 }
Archanad8a83dc2021-06-14 10:04:16 +05302121 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_allocate_buffer_to_slot(slot, storage_size);
2123 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002126 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002127
2128 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 status = psa_driver_wrapper_import_key(attributes,
2130 data, data_length,
2131 slot->key.data,
2132 slot->key.bytes,
2133 &slot->key.bytes, &bits);
2134 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002139 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002141 status = PSA_ERROR_INVALID_ARGUMENT;
2142 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002143 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002144
Archana6ed4bda2021-08-04 10:47:15 +05302145 /* Enforce a size limit, and in particular ensure that the bit
2146 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302148 status = PSA_ERROR_NOT_SUPPORTED;
2149 goto exit;
2150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 status = psa_validate_optional_attributes(slot, attributes);
2152 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002157exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002158 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (status != PSA_SUCCESS) {
2160 psa_fail_key_creation(slot, driver);
2161 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002164}
2165
Gilles Peskined7729582019-08-05 15:55:54 +02002166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2167psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002169{
2170 psa_status_t status;
2171 psa_key_slot_t *slot = NULL;
2172 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002174
2175 /* Leaving attributes unspecified is not currently supported.
2176 * It could make sense to query the key type and size from the
2177 * secure element, but not all secure elements support this
2178 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2180 return PSA_ERROR_NOT_SUPPORTED;
2181 }
2182 if (psa_get_key_bits(attributes) == 0) {
2183 return PSA_ERROR_NOT_SUPPORTED;
2184 }
Gilles Peskined7729582019-08-05 15:55:54 +02002185
Gilles Peskined72ad732024-06-13 16:06:45 +02002186 /* Not usable with volatile keys, even with an appropriate location,
2187 * due to the API design.
2188 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2189 */
2190 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2191 return PSA_ERROR_INVALID_ARGUMENT;
2192 }
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2195 &slot, &driver);
2196 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 }
Gilles Peskined7729582019-08-05 15:55:54 +02002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002201
2202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if (status != PSA_SUCCESS) {
2204 psa_fail_key_creation(slot, driver);
2205 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002206
Gilles Peskined7729582019-08-05 15:55:54 +02002207 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 psa_close_key(key);
2209 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002210}
2211#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2214 const psa_key_attributes_t *specified_attributes,
2215 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002216{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002218 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002219 psa_key_slot_t *source_slot = NULL;
2220 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002222 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302223 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002224
Ronald Cron81709fc2020-11-14 12:10:32 +01002225 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2226
Archana8a180362021-07-05 02:18:48 +05302227 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2229 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_validate_optional_attributes(source_slot,
2234 specified_attributes);
2235 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002238
Archana9d17bf42021-09-10 06:22:44 +05302239 /* The target key type and number of bits have been validated by
2240 * psa_validate_optional_attributes() to be either equal to zero or
2241 * equal to the ones of the source key. So it is safe to inherit
2242 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302243 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002244 actual_attributes.bits = source_slot->attr.bits;
2245 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302246
2247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002249 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 &source_slot->attr.policy);
2251 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002252 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2256 &target_slot, &driver);
2257 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 }
2260 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2261 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302262 /*
Archana9d17bf42021-09-10 06:22:44 +05302263 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302264 * the source key would need to be exported as plaintext and re-imported
2265 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302266 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302267 * appropriate API invocations from the application, if needed.
2268 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002269 status = PSA_ERROR_NOT_SUPPORTED;
2270 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002271 }
Archana8a180362021-07-05 02:18:48 +05302272 /*
2273 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302274 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302275 * - For opaque keys this translates to an invocation of the drivers'
2276 * copy_key entry point through the dispatch layer.
2277 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002278 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2280 &storage_size);
2281 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
Archana374fe5b2021-09-08 15:50:28 +05302284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2286 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 }
Archana374fe5b2021-09-08 15:50:28 +05302289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_driver_wrapper_copy_key(&actual_attributes,
2291 source_slot->key.data,
2292 source_slot->key.bytes,
2293 target_slot->key.data,
2294 target_slot->key.bytes,
2295 &target_slot->key.bytes);
2296 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
2299 } else {
2300 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302301 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 source_slot->key.bytes);
2303 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 }
Archana8a180362021-07-05 02:18:48 +05302306 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (status != PSA_SUCCESS) {
2310 psa_fail_key_creation(target_slot, driver);
2311 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312
Ryan Everetta103ec92024-01-31 13:59:57 +00002313 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002316}
2317
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002318
2319
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002320/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002321/* Message digests */
2322/****************************************************************/
2323
Gilles Peskine808b5412024-04-10 20:05:06 +02002324static int is_hash_supported(psa_algorithm_t alg)
2325{
2326 switch (alg) {
2327#if defined(PSA_WANT_ALG_MD5)
2328 case PSA_ALG_MD5:
2329 return 1;
2330#endif
2331#if defined(PSA_WANT_ALG_RIPEMD160)
2332 case PSA_ALG_RIPEMD160:
2333 return 1;
2334#endif
2335#if defined(PSA_WANT_ALG_SHA_1)
2336 case PSA_ALG_SHA_1:
2337 return 1;
2338#endif
2339#if defined(PSA_WANT_ALG_SHA_224)
2340 case PSA_ALG_SHA_224:
2341 return 1;
2342#endif
2343#if defined(PSA_WANT_ALG_SHA_256)
2344 case PSA_ALG_SHA_256:
2345 return 1;
2346#endif
2347#if defined(PSA_WANT_ALG_SHA_384)
2348 case PSA_ALG_SHA_384:
2349 return 1;
2350#endif
2351#if defined(PSA_WANT_ALG_SHA_512)
2352 case PSA_ALG_SHA_512:
2353 return 1;
2354#endif
2355#if defined(PSA_WANT_ALG_SHA3_224)
2356 case PSA_ALG_SHA3_224:
2357 return 1;
2358#endif
2359#if defined(PSA_WANT_ALG_SHA3_256)
2360 case PSA_ALG_SHA3_256:
2361 return 1;
2362#endif
2363#if defined(PSA_WANT_ALG_SHA3_384)
2364 case PSA_ALG_SHA3_384:
2365 return 1;
2366#endif
2367#if defined(PSA_WANT_ALG_SHA3_512)
2368 case PSA_ALG_SHA3_512:
2369 return 1;
2370#endif
2371 default:
2372 return 0;
2373 }
2374}
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002378 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (operation->id == 0) {
2380 return PSA_SUCCESS;
2381 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002384 operation->id = 0;
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387}
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2390 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002393
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002394 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002396 status = PSA_ERROR_BAD_STATE;
2397 goto exit;
2398 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002401 status = PSA_ERROR_INVALID_ARGUMENT;
2402 goto exit;
2403 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002404
Gilles Peskinea7a480b2025-02-05 19:41:52 +01002405 /* Make sure the driver-dependent part of the operation is zeroed.
2406 * This is a guarantee we make to drivers. Initializing the operation
2407 * does not necessarily take care of it, since the context is a
2408 * union and initializing a union does not necessarily initialize
2409 * all of its members. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413
2414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (status != PSA_SUCCESS) {
2416 psa_hash_abort(operation);
2417 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418
2419 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420}
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002423 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002427 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002430 status = PSA_ERROR_BAD_STATE;
2431 goto exit;
2432 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002433
Steven Cooremanc8288352021-03-04 14:02:19 +01002434 /* Don't require hash implementations to behave correctly on a
2435 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (input_length == 0) {
2437 return PSA_SUCCESS;
2438 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002439
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002440 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002442
2443exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (status != PSA_SUCCESS) {
2445 psa_hash_abort(operation);
2446 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002447
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002448 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450}
2451
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002452static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002453 uint8_t *hash,
2454 size_t hash_size,
2455 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2458
Steven Cooremanfbe09282021-03-08 18:41:12 +01002459 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (operation->id == 0) {
2461 return PSA_ERROR_BAD_STATE;
2462 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002464 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 operation, hash, hash_size, hash_length);
2466 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002467
2468 return status;
2469}
2470
2471psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2472 uint8_t *hash_external,
2473 size_t hash_size,
2474 size_t *hash_length)
2475{
2476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2477 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2478
2479 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2480 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2481
David Horstmann4a48bec2024-03-13 15:42:31 +00002482#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002483exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002484#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002485 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487}
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002490 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492{
Ronald Cron69a63422021-10-18 09:47:58 +02002493 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2496 LOCAL_INPUT_DECLARE(hash_external, hash);
2497
2498 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 operation,
2500 actual_hash, sizeof(actual_hash),
2501 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002504 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002508 status = PSA_ERROR_INVALID_SIGNATURE;
2509 goto exit;
2510 }
2511
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002512 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002513 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002514 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002516
2517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2519 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002520 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002522 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002524}
2525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002527 const uint8_t *input_external, size_t input_length,
2528 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002530{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2532 LOCAL_INPUT_DECLARE(input_external, input);
2533 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2534
Steven Cooremanfbe09282021-03-08 18:41:12 +01002535 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (!PSA_ALG_IS_HASH(alg)) {
2537 return PSA_ERROR_INVALID_ARGUMENT;
2538 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002539
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002540 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2541 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2542 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002543 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002544
David Horstmann4a48bec2024-03-13 15:42:31 +00002545#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002546exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002547#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002548 LOCAL_INPUT_FREE(input_external, input);
2549 LOCAL_OUTPUT_FREE(hash_external, hash);
2550 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551}
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002554 const uint8_t *input_external, size_t input_length,
2555 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002556{
Ronald Cron69a63422021-10-18 09:47:58 +02002557 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002558 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2560
2561 LOCAL_INPUT_DECLARE(input_external, input);
2562 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002565 status = PSA_ERROR_INVALID_ARGUMENT;
2566 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002568
Thomas Daubney51ffac92024-01-18 15:46:26 +00002569 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2570 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 alg, input, input_length,
2572 actual_hash, sizeof(actual_hash),
2573 &actual_hash_length);
2574 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 }
2577 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002578 status = PSA_ERROR_INVALID_SIGNATURE;
2579 goto exit;
2580 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002581
2582 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002583 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002584 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002586
2587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002589
2590 LOCAL_INPUT_FREE(input_external, input);
2591 LOCAL_INPUT_FREE(hash_external, hash);
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002594}
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2597 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002598{
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (source_operation->id == 0 ||
2600 target_operation->id != 0) {
2601 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002602 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002603
Gilles Peskinea7a480b2025-02-05 19:41:52 +01002604 /* Make sure the driver-dependent part of the operation is zeroed.
2605 * This is a guarantee we make to drivers. Initializing the operation
2606 * does not necessarily take care of it, since the context is a
2607 * union and initializing a union does not necessarily initialize
2608 * all of its members. */
2609 memset(&target_operation->ctx, 0, sizeof(target_operation->ctx));
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2612 target_operation);
2613 if (status != PSA_SUCCESS) {
2614 psa_hash_abort(target_operation);
2615 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002618}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619
2620
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002621/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622/* MAC */
2623/****************************************************************/
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626{
Steven Cooremane6804192021-03-19 18:28:56 +01002627 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (operation->id == 0) {
2629 return PSA_SUCCESS;
2630 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002633 operation->mac_size = 0;
2634 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002635 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638}
2639
Ronald Cron2dff3b22021-06-17 16:33:22 +02002640static psa_status_t psa_mac_finalize_alg_and_key_validation(
2641 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002642 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 psa_key_type_t key_type = psa_get_key_type(attributes);
2647 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (!PSA_ALG_IS_MAC(alg)) {
2650 return PSA_ERROR_INVALID_ARGUMENT;
2651 }
Steven Cooremane6804192021-03-19 18:28:56 +01002652
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002653 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 status = psa_mac_key_can_do(alg, key_type);
2655 if (status != PSA_SUCCESS) {
2656 return status;
2657 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002658
Steven Cooremand1a68f12021-05-10 11:13:41 +02002659 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002663 /* A very short MAC is too short for security since it can be
2664 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2665 * so we make this our minimum, even though 32 bits is still
2666 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002668 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2671 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002672 /* It's impossible to "truncate" to a larger length than the full length
2673 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002675 }
2676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002678 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2679 * that is disabled in the compile-time configuration. The result can
2680 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2681 * configuration into account. In this case, force a return of
2682 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2683 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2684 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2685 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2686 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002688 }
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002691}
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2694 mbedtls_svc_key_id_t key,
2695 psa_algorithm_t alg,
2696 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002697{
2698 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2699 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002700 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701
2702 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002704 status = PSA_ERROR_BAD_STATE;
2705 goto exit;
2706 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707
Gilles Peskinea7a480b2025-02-05 19:41:52 +01002708 /* Make sure the driver-dependent part of the operation is zeroed.
2709 * This is a guarantee we make to drivers. Initializing the operation
2710 * does not necessarily take care of it, since the context is a
2711 * union and initializing a union does not necessarily initialize
2712 * all of its members. */
2713 memset(&operation->ctx, 0, sizeof(operation->ctx));
2714
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 key,
2717 &slot,
2718 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2719 alg);
2720 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002724 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 &operation->mac_size);
2726 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729
2730 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002731 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (is_sign) {
2733 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002734 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 slot->key.data,
2736 slot->key.bytes,
2737 alg);
2738 } else {
2739 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002740 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 slot->key.data,
2742 slot->key.bytes,
2743 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002745
Gilles Peskinefbfac682018-07-08 20:51:54 +02002746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if (status != PSA_SUCCESS) {
2748 psa_mac_abort(operation);
2749 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002750
Ryan Everettfb9857f2024-02-14 12:16:41 +00002751 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754}
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2757 mbedtls_svc_key_id_t key,
2758 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002759{
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002761}
2762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2764 mbedtls_svc_key_id_t key,
2765 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002766{
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002768}
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002771 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2775 LOCAL_INPUT_DECLARE(input_external, input);
2776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002778 status = PSA_ERROR_BAD_STATE;
2779 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002782 /* Don't require hash implementations to behave correctly on a
2783 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002785 status = PSA_SUCCESS;
2786 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002788
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002789 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2790 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (status != PSA_SUCCESS) {
2793 psa_mac_abort(operation);
2794 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002795
David Horstmann4a48bec2024-03-13 15:42:31 +00002796#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002797exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002798#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002799 LOCAL_INPUT_FREE(input_external, input);
2800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802}
2803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002805 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 size_t mac_size,
2807 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002808{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002809 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2810 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002811 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002812 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002815 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002816 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002817 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002820 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002821 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002822 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002823
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002824 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2825 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002827 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002828 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002829 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002832 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002833 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002834 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002835
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 status = psa_driver_wrapper_mac_sign_finish(operation,
2838 mac, operation->mac_size,
2839 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002840
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002841exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002842 /* In case of success, set the potential excess room in the output buffer
2843 * to an invalid value, to avoid potentially leaking a longer MAC.
2844 * In case of error, set the output length and content to a safe default,
2845 * such that in case the caller misses an error check, the output would be
2846 * an unachievable MAC.
2847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002849 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002850 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002851 }
mohammad16036df908f2018-04-02 08:34:15 -07002852
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002853 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002854 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2855 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002858 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002861}
2862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002864 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2868 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002869 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002872 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002873 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002874 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002877 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002878 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002879 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002882 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002883 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002884 }
2885
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002886 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 status = psa_driver_wrapper_mac_verify_finish(operation,
2888 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002889
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002890exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002892 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895}
2896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2898 psa_algorithm_t alg,
2899 const uint8_t *input,
2900 size_t input_length,
2901 uint8_t *mac,
2902 size_t mac_size,
2903 size_t *mac_length,
2904 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002907 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2908 psa_key_slot_t *slot;
2909 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910
Ronald Cron51131b52021-06-17 17:17:20 +02002911 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 key,
2913 &slot,
2914 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2915 alg);
2916 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002919
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002920 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 &operation_mac_size);
2922 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002923 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002927 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002928 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002929 }
2930
2931 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002932 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 slot->key.data, slot->key.bytes,
2934 alg,
2935 input, input_length,
2936 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002937
2938exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002939 /* In case of success, set the potential excess room in the output buffer
2940 * to an invalid value, to avoid potentially leaking a longer MAC.
2941 * In case of error, set the output length and content to a safe default,
2942 * such that in case the caller misses an error check, the output would be
2943 * an unachievable MAC.
2944 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002946 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002947 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002948 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002949
2950 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002951
Ryan Everetta103ec92024-01-31 13:59:57 +00002952 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002955}
2956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002958 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002959 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002960 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002961 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 size_t mac_size,
2963 size_t *mac_length)
2964{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2966 LOCAL_INPUT_DECLARE(input_external, input);
2967 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2968
2969 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2970 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2971 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002972 input, input_length,
2973 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002974
David Horstmann4a48bec2024-03-13 15:42:31 +00002975#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002976exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002977#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002978 LOCAL_INPUT_FREE(input_external, input);
2979 LOCAL_OUTPUT_FREE(mac_external, mac);
2980
2981 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982}
2983
2984psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2985 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002986 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002988 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002990{
2991 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002992 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2993 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002994 LOCAL_INPUT_DECLARE(input_external, input);
2995 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002996
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002997 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 status = psa_mac_compute_internal(key, alg,
2999 input, input_length,
3000 actual_mac, sizeof(actual_mac),
3001 &actual_mac_length, 0);
3002 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003003 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02003007 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003008 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02003009 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00003010
3011 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01003012 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02003013 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003014 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02003015 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003016
3017exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00003019 LOCAL_INPUT_FREE(input_external, input);
3020 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003023}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003024
Gilles Peskinee59236f2018-01-27 23:32:46 +01003025/****************************************************************/
3026/* Asymmetric cryptography */
3027/****************************************************************/
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029static psa_status_t psa_sign_verify_check_alg(int input_is_message,
3030 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003031{
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (input_is_message) {
3033 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
3034 return PSA_ERROR_INVALID_ARGUMENT;
3035 }
Gilles Peskine808b5412024-04-10 20:05:06 +02003036 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003037
Gilles Peskine808b5412024-04-10 20:05:06 +02003038 psa_algorithm_t hash_alg = 0;
3039 if (PSA_ALG_IS_SIGN_HASH(alg)) {
3040 hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3041 }
3042
3043 /* Now hash_alg==0 if alg by itself doesn't need a hash.
3044 * This is good enough for sign-hash, but a guaranteed failure for
3045 * sign-message which needs to hash first for all algorithms
3046 * supported at the moment. */
3047
3048 if (hash_alg == 0 && input_is_message) {
3049 return PSA_ERROR_INVALID_ARGUMENT;
3050 }
3051 if (hash_alg == PSA_ALG_ANY_HASH) {
3052 return PSA_ERROR_INVALID_ARGUMENT;
3053 }
3054 /* Give up immediately if the hash is not supported. This has
3055 * several advantages:
3056 * - For mechanisms that don't use the hash at all (e.g.
3057 * ECDSA verification, randomized ECDSA signature), without
3058 * this check, the operation would succeed even though it has
3059 * been given an invalid argument. This would not be insecure
3060 * since the hash was not necessary, but it would be weird.
3061 * - For mechanisms that do use the hash, we avoid an error
3062 * deep inside the execution. In principle this doesn't matter,
3063 * but there is a little more risk of a bug in error handling
3064 * deep inside than in this preliminary check.
3065 * - When calling a driver, the driver might be capable of using
3066 * a hash that the core doesn't support. This could potentially
3067 * result in a buffer overflow if the hash is larger than the
3068 * maximum hash size assumed by the core.
3069 * - Returning a consistent error makes it possible to test
3070 * not-supported hashes in a consistent way.
3071 */
3072 if (hash_alg != 0 && !is_hash_supported(hash_alg)) {
3073 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003074 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003077}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
3080 int input_is_message,
3081 psa_algorithm_t alg,
3082 const uint8_t *input,
3083 size_t input_length,
3084 uint8_t *signature,
3085 size_t signature_size,
3086 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003087{
3088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3090 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003091
3092 *signature_length = 0;
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 status = psa_sign_verify_check_alg(input_is_message, alg);
3095 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003096 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003098
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003099 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02003100 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003101 * buffer can in principle be empty since it doesn't actually have
3102 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (signature_size == 0) {
3104 return PSA_ERROR_BUFFER_TOO_SMALL;
3105 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003106
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003107 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 key, &slot,
3109 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3110 PSA_KEY_USAGE_SIGN_HASH,
3111 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003118 status = PSA_ERROR_INVALID_ARGUMENT;
3119 goto exit;
3120 }
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003123 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003124 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003125 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 signature, signature_size, signature_length);
3127 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003128
3129 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003130 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003131 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003133 }
3134
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003135
3136exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003137 psa_wipe_tag_output_buffer(signature, status, signature_size,
3138 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003139
Ryan Everetta103ec92024-01-31 13:59:57 +00003140 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003143}
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3146 int input_is_message,
3147 psa_algorithm_t alg,
3148 const uint8_t *input,
3149 size_t input_length,
3150 const uint8_t *signature,
3151 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003152{
3153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3154 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3155 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 status = psa_sign_verify_check_alg(input_is_message, alg);
3158 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003159 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003161
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003162 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 key, &slot,
3164 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3165 PSA_KEY_USAGE_VERIFY_HASH,
3166 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 if (status != PSA_SUCCESS) {
3169 return status;
3170 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003173 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003174 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003175 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 signature, signature_length);
3177 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003178 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003179 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003180 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003182 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003183
Ryan Everetta103ec92024-01-31 13:59:57 +00003184 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003187
3188}
3189
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003190psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003191 const psa_key_attributes_t *attributes,
3192 const uint8_t *key_buffer,
3193 size_t key_buffer_size,
3194 psa_algorithm_t alg,
3195 const uint8_t *input,
3196 size_t input_length,
3197 uint8_t *signature,
3198 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003200{
3201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003204 size_t hash_length;
3205 uint8_t hash[PSA_HASH_MAX_SIZE];
3206
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003207 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 PSA_ALG_SIGN_GET_HASH(alg),
3209 input, input_length,
3210 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003213 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003215
3216 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 attributes, key_buffer, key_buffer_size,
3218 alg, hash, hash_length,
3219 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003220 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003223}
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3226 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003227 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003229 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 size_t signature_size,
3231 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003232{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3234 LOCAL_INPUT_DECLARE(input_external, input);
3235 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3236
3237 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3238 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3239 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3240 signature_size, signature_length);
3241
David Horstmann4a48bec2024-03-13 15:42:31 +00003242#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003243exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003244#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003245 LOCAL_INPUT_FREE(input_external, input);
3246 LOCAL_OUTPUT_FREE(signature_external, signature);
3247 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003248}
3249
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003250psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003251 const psa_key_attributes_t *attributes,
3252 const uint8_t *key_buffer,
3253 size_t key_buffer_size,
3254 psa_algorithm_t alg,
3255 const uint8_t *input,
3256 size_t input_length,
3257 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003259{
3260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003263 size_t hash_length;
3264 uint8_t hash[PSA_HASH_MAX_SIZE];
3265
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003266 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 PSA_ALG_SIGN_GET_HASH(alg),
3268 input, input_length,
3269 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003272 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003274
3275 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 attributes, key_buffer, key_buffer_size,
3277 alg, hash, hash_length,
3278 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003279 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003282}
3283
Gilles Peskine449bd832023-01-11 14:50:10 +01003284psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3285 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003286 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003288 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003290{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3292 LOCAL_INPUT_DECLARE(input_external, input);
3293 LOCAL_INPUT_DECLARE(signature_external, signature);
3294
3295 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3296 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3297 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3298 signature_length);
3299
David Horstmann4a48bec2024-03-13 15:42:31 +00003300#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003301exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003302#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003303 LOCAL_INPUT_FREE(input_external, input);
3304 LOCAL_INPUT_FREE(signature_external, signature);
3305
3306 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003307}
3308
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003309psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003310 const psa_key_attributes_t *attributes,
3311 const uint8_t *key_buffer, size_t key_buffer_size,
3312 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003314{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003315 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3317 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003318#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3320 return mbedtls_psa_rsa_sign_hash(
3321 attributes,
3322 key_buffer, key_buffer_size,
3323 alg, hash, hash_length,
3324 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003325#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3326 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 } else {
3328 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003329 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003330 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003332#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3334 return mbedtls_psa_ecdsa_sign_hash(
3335 attributes,
3336 key_buffer, key_buffer_size,
3337 alg, hash, hash_length,
3338 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003339#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3340 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 } else {
3342 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003343 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003344 }
Ronald Cron4501c982021-03-19 20:09:32 +01003345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 (void) key_buffer;
3347 (void) key_buffer_size;
3348 (void) hash;
3349 (void) hash_length;
3350 (void) signature;
3351 (void) signature_size;
3352 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003355}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3358 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003359 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003361 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 size_t signature_size,
3363 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003364{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3366 LOCAL_INPUT_DECLARE(hash_external, hash);
3367 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3368
3369 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3370 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3371 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3372 signature_size, signature_length);
3373
David Horstmann4a48bec2024-03-13 15:42:31 +00003374#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003375exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003376#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003377 LOCAL_INPUT_FREE(hash_external, hash);
3378 LOCAL_OUTPUT_FREE(signature_external, signature);
3379
3380 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003381}
3382
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003383psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003384 const psa_key_attributes_t *attributes,
3385 const uint8_t *key_buffer, size_t key_buffer_size,
3386 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003388{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003389 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3391 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003392#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3394 return mbedtls_psa_rsa_verify_hash(
3395 attributes,
3396 key_buffer, key_buffer_size,
3397 alg, hash, hash_length,
3398 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003399#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3400 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 } else {
3402 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003403 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003404 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003406#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3408 return mbedtls_psa_ecdsa_verify_hash(
3409 attributes,
3410 key_buffer, key_buffer_size,
3411 alg, hash, hash_length,
3412 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003413#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3414 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 } else {
3416 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003417 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003418 }
Ronald Cron4501c982021-03-19 20:09:32 +01003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 (void) key_buffer;
3421 (void) key_buffer_size;
3422 (void) hash;
3423 (void) hash_length;
3424 (void) signature;
3425 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003428}
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3431 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003432 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003434 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003436{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3438 LOCAL_INPUT_DECLARE(hash_external, hash);
3439 LOCAL_INPUT_DECLARE(signature_external, signature);
3440
3441 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3442 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3443 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3444 signature_length);
3445
David Horstmann4a48bec2024-03-13 15:42:31 +00003446#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003447exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003448#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003449 LOCAL_INPUT_FREE(hash_external, hash);
3450 LOCAL_INPUT_FREE(signature_external, signature);
3451
3452 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003453}
3454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3456 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003457 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003459 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003461 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 size_t output_size,
3463 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003464{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003466 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003467 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003468
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003469 LOCAL_INPUT_DECLARE(input_external, input);
3470 LOCAL_INPUT_DECLARE(salt_external, salt);
3471 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003472
Darryl Green5cc689a2018-07-24 15:34:10 +01003473 (void) input;
3474 (void) input_length;
3475 (void) salt;
3476 (void) output;
3477 (void) output_size;
3478
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003479 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003480
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3482 return PSA_ERROR_INVALID_ARGUMENT;
3483 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003484
Valerio Setti5bb454a2024-01-15 10:43:16 +01003485 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3487 if (status != PSA_SUCCESS) {
3488 return status;
3489 }
3490 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3491 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003492 status = PSA_ERROR_INVALID_ARGUMENT;
3493 goto exit;
3494 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003495
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003496 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3497 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3498 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003499
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003500 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003501 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003502 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003504exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003505 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003506
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003507 LOCAL_INPUT_FREE(input_external, input);
3508 LOCAL_INPUT_FREE(salt_external, salt);
3509 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003510
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003512}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003513
Gilles Peskine449bd832023-01-11 14:50:10 +01003514psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3515 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003516 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003518 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003520 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 size_t output_size,
3522 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003525 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003526 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003527
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003528 LOCAL_INPUT_DECLARE(input_external, input);
3529 LOCAL_INPUT_DECLARE(salt_external, salt);
3530 LOCAL_OUTPUT_DECLARE(output_external, output);
3531
Darryl Green5cc689a2018-07-24 15:34:10 +01003532 (void) input;
3533 (void) input_length;
3534 (void) salt;
3535 (void) output;
3536 (void) output_size;
3537
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003538 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3541 return PSA_ERROR_INVALID_ARGUMENT;
3542 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003543
Valerio Setti5bb454a2024-01-15 10:43:16 +01003544 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3546 if (status != PSA_SUCCESS) {
3547 return status;
3548 }
3549 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003550 status = PSA_ERROR_INVALID_ARGUMENT;
3551 goto exit;
3552 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003554 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3555 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3556 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003557
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003558 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003559 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003560 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003562
3563exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003564 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003565
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003566 LOCAL_INPUT_FREE(input_external, input);
3567 LOCAL_INPUT_FREE(salt_external, salt);
3568 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003571}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003572
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573/****************************************************************/
3574/* Asymmetric interruptible cryptography */
3575/****************************************************************/
3576
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003577static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3578
Paul Elliott9fe12f62022-11-30 19:16:02 +00003579void psa_interruptible_set_max_ops(uint32_t max_ops)
3580{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003581 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003582}
3583
3584uint32_t psa_interruptible_get_max_ops(void)
3585{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003586 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587}
3588
Paul Elliott9fe12f62022-11-30 19:16:02 +00003589uint32_t psa_sign_hash_get_num_ops(
3590 const psa_sign_hash_interruptible_operation_t *operation)
3591{
Paul Elliott296ede92022-12-15 17:00:30 +00003592 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003593}
3594
3595uint32_t psa_verify_hash_get_num_ops(
3596 const psa_verify_hash_interruptible_operation_t *operation)
3597{
Paul Elliott296ede92022-12-15 17:00:30 +00003598 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003599}
3600
Paul Elliott59ad9452022-12-18 15:09:02 +00003601static psa_status_t psa_sign_hash_abort_internal(
3602 psa_sign_hash_interruptible_operation_t *operation)
3603{
3604 if (operation->id == 0) {
3605 /* The object has (apparently) been initialized but it is not (yet)
3606 * in use. It's ok to call abort on such an object, and there's
3607 * nothing to do. */
3608 return PSA_SUCCESS;
3609 }
3610
3611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3612
3613 status = psa_driver_wrapper_sign_hash_abort(operation);
3614
3615 operation->id = 0;
3616
Paul Elliotte6145dc2023-02-07 12:51:21 +00003617 /* Do not clear either the error_occurred or num_ops elements here as they
3618 * only want to be cleared by the application calling abort, not by abort
3619 * being called at completion of an operation. */
3620
Paul Elliott59ad9452022-12-18 15:09:02 +00003621 return status;
3622}
3623
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624psa_status_t psa_sign_hash_start(
3625 psa_sign_hash_interruptible_operation_t *operation,
3626 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003627 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003628{
3629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3630 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3631 psa_key_slot_t *slot;
3632
David Horstmann4a523a62024-03-11 13:32:16 +00003633 LOCAL_INPUT_DECLARE(hash_external, hash);
3634
Paul Elliottc9774412023-02-06 15:14:07 +00003635 /* Check that start has not been previously called, or operation has not
3636 * previously errored. */
3637 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003638 return PSA_ERROR_BAD_STATE;
3639 }
3640
Gilles Peskinea7a480b2025-02-05 19:41:52 +01003641 /* Make sure the driver-dependent part of the operation is zeroed.
3642 * This is a guarantee we make to drivers. Initializing the operation
3643 * does not necessarily take care of it, since the context is a
3644 * union and initializing a union does not necessarily initialize
3645 * all of its members. */
3646 memset(&operation->ctx, 0, sizeof(operation->ctx));
3647
Paul Elliott9fe12f62022-11-30 19:16:02 +00003648 status = psa_sign_verify_check_alg(0, alg);
3649 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003650 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003651 return status;
3652 }
3653
3654 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3655 PSA_KEY_USAGE_SIGN_HASH,
3656 alg);
3657
3658 if (status != PSA_SUCCESS) {
3659 goto exit;
3660 }
3661
3662 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3663 status = PSA_ERROR_INVALID_ARGUMENT;
3664 goto exit;
3665 }
3666
David Horstmann4a523a62024-03-11 13:32:16 +00003667 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3668
Paul Elliott296ede92022-12-15 17:00:30 +00003669 /* Ensure ops count gets reset, in case of operation re-use. */
3670 operation->num_ops = 0;
3671
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003672 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003673 slot->key.data,
3674 slot->key.bytes, alg,
3675 hash, hash_length);
3676exit:
3677
3678 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003679 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003680 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003681 }
3682
Ryan Everettdcc03d52024-02-14 15:44:13 +00003683 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003684
Paul Elliottc9774412023-02-06 15:14:07 +00003685 if (unlock_status != PSA_SUCCESS) {
3686 operation->error_occurred = 1;
3687 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003688
David Horstmann4a523a62024-03-11 13:32:16 +00003689 LOCAL_INPUT_FREE(hash_external, hash);
3690
Paul Elliottc9774412023-02-06 15:14:07 +00003691 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003692}
3693
3694
3695psa_status_t psa_sign_hash_complete(
3696 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003697 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003698 size_t *signature_length)
3699{
3700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3701
David Horstmann4a523a62024-03-11 13:32:16 +00003702 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3703
Paul Elliott9fe12f62022-11-30 19:16:02 +00003704 *signature_length = 0;
3705
Paul Elliottc9774412023-02-06 15:14:07 +00003706 /* Check that start has been called first, and that operation has not
3707 * previously errored. */
3708 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003709 status = PSA_ERROR_BAD_STATE;
3710 goto exit;
3711 }
3712
Paul Elliott096abc42023-02-03 18:33:23 +00003713 /* Immediately reject a zero-length signature buffer. This guarantees that
3714 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003715 if (signature_size == 0) {
3716 status = PSA_ERROR_BUFFER_TOO_SMALL;
3717 goto exit;
3718 }
3719
David Horstmann4a523a62024-03-11 13:32:16 +00003720 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3721
Paul Elliott9fe12f62022-11-30 19:16:02 +00003722 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3723 signature_size,
3724 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003725
Paul Elliott296ede92022-12-15 17:00:30 +00003726 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003727 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003728
Paul Elliottebe225c2023-02-10 14:32:53 +00003729exit:
3730
David Horstmannc5064c82024-03-11 17:02:03 +00003731 if (signature != NULL) {
3732 psa_wipe_tag_output_buffer(signature, status, signature_size,
3733 *signature_length);
3734 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003735
Paul Elliott53bb3122023-02-10 14:22:22 +00003736 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003737 if (status != PSA_SUCCESS) {
3738 operation->error_occurred = 1;
3739 }
3740
Paul Elliott59ad9452022-12-18 15:09:02 +00003741 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003742 }
3743
David Horstmann4a523a62024-03-11 13:32:16 +00003744 LOCAL_OUTPUT_FREE(signature_external, signature);
3745
Paul Elliott9fe12f62022-11-30 19:16:02 +00003746 return status;
3747}
3748
3749psa_status_t psa_sign_hash_abort(
3750 psa_sign_hash_interruptible_operation_t *operation)
3751{
Paul Elliott59ad9452022-12-18 15:09:02 +00003752 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3753
3754 status = psa_sign_hash_abort_internal(operation);
3755
3756 /* We clear the number of ops done here, so that it is not cleared when
3757 * the operation fails or succeeds, only on manual abort. */
3758 operation->num_ops = 0;
3759
Paul Elliottc9774412023-02-06 15:14:07 +00003760 /* Likewise, failure state. */
3761 operation->error_occurred = 0;
3762
Paul Elliott59ad9452022-12-18 15:09:02 +00003763 return status;
3764}
3765
3766static psa_status_t psa_verify_hash_abort_internal(
3767 psa_verify_hash_interruptible_operation_t *operation)
3768{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003769 if (operation->id == 0) {
3770 /* The object has (apparently) been initialized but it is not (yet)
3771 * in use. It's ok to call abort on such an object, and there's
3772 * nothing to do. */
3773 return PSA_SUCCESS;
3774 }
3775
Paul Elliott59ad9452022-12-18 15:09:02 +00003776 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3777
3778 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003779
3780 operation->id = 0;
3781
Paul Elliotte6145dc2023-02-07 12:51:21 +00003782 /* Do not clear either the error_occurred or num_ops elements here as they
3783 * only want to be cleared by the application calling abort, not by abort
3784 * being called at completion of an operation. */
3785
Paul Elliott59ad9452022-12-18 15:09:02 +00003786 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003787}
3788
3789psa_status_t psa_verify_hash_start(
3790 psa_verify_hash_interruptible_operation_t *operation,
3791 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003792 const uint8_t *hash_external, size_t hash_length,
3793 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003794{
3795 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3796 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3797 psa_key_slot_t *slot;
3798
David Horstmann0fea6a52024-03-11 13:41:05 +00003799 LOCAL_INPUT_DECLARE(hash_external, hash);
3800 LOCAL_INPUT_DECLARE(signature_external, signature);
3801
Paul Elliottc9774412023-02-06 15:14:07 +00003802 /* Check that start has not been previously called, or operation has not
3803 * previously errored. */
3804 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003805 return PSA_ERROR_BAD_STATE;
3806 }
3807
Gilles Peskinea7a480b2025-02-05 19:41:52 +01003808 /* Make sure the driver-dependent part of the operation is zeroed.
3809 * This is a guarantee we make to drivers. Initializing the operation
3810 * does not necessarily take care of it, since the context is a
3811 * union and initializing a union does not necessarily initialize
3812 * all of its members. */
3813 memset(&operation->ctx, 0, sizeof(operation->ctx));
3814
Paul Elliott9fe12f62022-11-30 19:16:02 +00003815 status = psa_sign_verify_check_alg(0, alg);
3816 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003817 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003818 return status;
3819 }
3820
3821 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3822 PSA_KEY_USAGE_VERIFY_HASH,
3823 alg);
3824
3825 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003826 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003827 return status;
3828 }
3829
David Horstmann0fea6a52024-03-11 13:41:05 +00003830 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3831 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3832
Paul Elliott296ede92022-12-15 17:00:30 +00003833 /* Ensure ops count gets reset, in case of operation re-use. */
3834 operation->num_ops = 0;
3835
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003836 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003837 slot->key.data,
3838 slot->key.bytes,
3839 alg, hash, hash_length,
3840 signature, signature_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00003841#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann0fea6a52024-03-11 13:41:05 +00003842exit:
3843#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003844
3845 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003846 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003847 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003848 }
3849
Ryan Everett291267f2024-02-14 15:59:15 +00003850 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003851
Paul Elliottc9774412023-02-06 15:14:07 +00003852 if (unlock_status != PSA_SUCCESS) {
3853 operation->error_occurred = 1;
3854 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003855
David Horstmann0fea6a52024-03-11 13:41:05 +00003856 LOCAL_INPUT_FREE(hash_external, hash);
3857 LOCAL_INPUT_FREE(signature_external, signature);
3858
Paul Elliottc9774412023-02-06 15:14:07 +00003859 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003860}
3861
3862psa_status_t psa_verify_hash_complete(
3863 psa_verify_hash_interruptible_operation_t *operation)
3864{
3865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3866
Paul Elliottc9774412023-02-06 15:14:07 +00003867 /* Check that start has been called first, and that operation has not
3868 * previously errored. */
3869 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003870 status = PSA_ERROR_BAD_STATE;
3871 goto exit;
3872 }
3873
3874 status = psa_driver_wrapper_verify_hash_complete(operation);
3875
Paul Elliott296ede92022-12-15 17:00:30 +00003876 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003877 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003878 operation);
3879
Paul Elliottebe225c2023-02-10 14:32:53 +00003880exit:
3881
Paul Elliott9fe12f62022-11-30 19:16:02 +00003882 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003883 if (status != PSA_SUCCESS) {
3884 operation->error_occurred = 1;
3885 }
3886
Paul Elliott59ad9452022-12-18 15:09:02 +00003887 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003888 }
3889
3890 return status;
3891}
3892
3893psa_status_t psa_verify_hash_abort(
3894 psa_verify_hash_interruptible_operation_t *operation)
3895{
Paul Elliott59ad9452022-12-18 15:09:02 +00003896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003897
Paul Elliott59ad9452022-12-18 15:09:02 +00003898 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003899
Paul Elliott59ad9452022-12-18 15:09:02 +00003900 /* We clear the number of ops done here, so that it is not cleared when
3901 * the operation fails or succeeds, only on manual abort. */
3902 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003903
Paul Elliottc9774412023-02-06 15:14:07 +00003904 /* Likewise, failure state. */
3905 operation->error_occurred = 0;
3906
Paul Elliott59ad9452022-12-18 15:09:02 +00003907 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003908}
3909
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910/****************************************************************/
3911/* Asymmetric interruptible cryptography internal */
3912/* implementations */
3913/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003914
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3916{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003917
Paul Elliott588f8ed2022-12-02 18:10:26 +00003918#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3919 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3920 defined(MBEDTLS_ECP_RESTARTABLE)
3921
3922 /* Internal implementation uses zero to indicate infinite number max ops,
3923 * therefore avoid this value, and set to minimum possible. */
3924 if (max_ops == 0) {
3925 max_ops = 1;
3926 }
3927
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003929#else
3930 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3932 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3933 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3934}
3935
Paul Elliott588f8ed2022-12-02 18:10:26 +00003936uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003937 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938{
3939#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3940 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3941 defined(MBEDTLS_ECP_RESTARTABLE)
3942
Paul Elliott93d9ca82023-02-15 18:14:21 +00003943 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944#else
3945 (void) operation;
3946 return 0;
3947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3948 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3949 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3950}
3951
3952uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003953 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954{
3955 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3956 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3957 defined(MBEDTLS_ECP_RESTARTABLE)
3958
Paul Elliott93d9ca82023-02-15 18:14:21 +00003959 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003960#else
3961 (void) operation;
3962 return 0;
3963#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3964 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3965 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3966}
3967
Gilles Peskine8a8aa592024-11-20 16:13:10 +01003968/* Detect supported interruptible sign/verify mechanisms precisely.
3969 * This is not strictly needed: we could accept everything, and let the
3970 * code fail later during complete() if the mechanism is unsupported
3971 * (e.g. attempting deterministic ECDSA when only the randomized variant
3972 * is available). But it's easier for applications and especially for our
3973 * test code to detect all not-supported errors during start().
3974 *
3975 * Note that this function ignores the hash component. The core code
3976 * is supposed to check the hash part by calling is_hash_supported().
3977 */
3978static inline int can_do_interruptible_sign_verify(psa_algorithm_t alg)
3979{
3980#if defined(MBEDTLS_ECP_RESTARTABLE)
3981#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3982 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
3983 return 1;
3984 }
3985#endif
3986#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
3987 if (PSA_ALG_IS_RANDOMIZED_ECDSA(alg)) {
3988 return 1;
3989 }
3990#endif
3991#endif /* defined(MBEDTLS_ECP_RESTARTABLE) */
3992 (void) alg;
3993 return 0;
3994}
3995
Paul Elliott588f8ed2022-12-02 18:10:26 +00003996psa_status_t mbedtls_psa_sign_hash_start(
3997 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3998 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3999 size_t key_buffer_size, psa_algorithm_t alg,
4000 const uint8_t *hash, size_t hash_length)
4001{
4002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00004003 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004004
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01004005 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00004006 return PSA_ERROR_NOT_SUPPORTED;
4007 }
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01004008 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type);
4009 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
4010 return PSA_ERROR_INVALID_ARGUMENT;
4011 }
Paul Elliott068fe072023-01-16 13:59:15 +00004012
Gilles Peskine8a8aa592024-11-20 16:13:10 +01004013 if (!can_do_interruptible_sign_verify(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004014 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004015 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004016
4017#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004018 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4019 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004020
Paul Elliotta1c94092023-02-15 16:38:04 +00004021 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4022
Paul Elliott93d9ca82023-02-15 18:14:21 +00004023 /* Ensure num_ops is zero'ed in case of context re-use. */
4024 operation->num_ops = 0;
4025
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004026 status = mbedtls_psa_ecp_load_representation(attributes->type,
4027 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004028 key_buffer,
4029 key_buffer_size,
4030 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004031
Paul Elliott068fe072023-01-16 13:59:15 +00004032 if (status != PSA_SUCCESS) {
4033 return status;
4034 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004035
Paul Elliott1bc59df2023-02-05 13:41:57 +00004036 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00004037 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004038
Paul Elliott068fe072023-01-16 13:59:15 +00004039 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02004040 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00004041 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004042
Paul Elliott0290a762023-02-09 14:30:24 +00004043 /* We only need to store the same length of hash as the private key size
4044 * here, it would be truncated by the internal implementation anyway. */
4045 required_hash_length = (hash_length < operation->coordinate_bytes ?
4046 hash_length : operation->coordinate_bytes);
4047
Paul Elliottba70ad42023-02-15 18:23:53 +00004048 if (required_hash_length > sizeof(operation->hash)) {
4049 /* Shouldn't happen, but better safe than sorry. */
4050 return PSA_ERROR_CORRUPTION_DETECTED;
4051 }
4052
Paul Elliott0290a762023-02-09 14:30:24 +00004053 memcpy(operation->hash, hash, required_hash_length);
4054 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004055
4056 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004057
4058#else
Paul Elliott068fe072023-01-16 13:59:15 +00004059 (void) operation;
4060 (void) key_buffer;
4061 (void) key_buffer_size;
4062 (void) alg;
4063 (void) hash;
4064 (void) hash_length;
4065 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00004066 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004067
Paul Elliott068fe072023-01-16 13:59:15 +00004068 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004069#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4070 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4071 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004072}
4073
4074psa_status_t mbedtls_psa_sign_hash_complete(
4075 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
4076 uint8_t *signature, size_t signature_size,
4077 size_t *signature_length)
4078{
Paul Elliott588f8ed2022-12-02 18:10:26 +00004079#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4080 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4081 defined(MBEDTLS_ECP_RESTARTABLE)
4082
Paul Elliotta1c94092023-02-15 16:38:04 +00004083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00004084 mbedtls_mpi r;
4085 mbedtls_mpi s;
4086
Paul Elliott46845252023-02-03 14:59:11 +00004087 mbedtls_mpi_init(&r);
4088 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004089
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004090 /* Ensure max_ops is set to the current value (or default). */
4091 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4092
Paul Elliotta1c94092023-02-15 16:38:04 +00004093 if (signature_size < 2 * operation->coordinate_bytes) {
4094 status = PSA_ERROR_BUFFER_TOO_SMALL;
4095 goto exit;
4096 }
4097
Paul Elliott588f8ed2022-12-02 18:10:26 +00004098 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00004099
Paul Elliott588f8ed2022-12-02 18:10:26 +00004100#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
4101 status = mbedtls_to_psa_error(
4102 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00004103 &r,
4104 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004105 &operation->ctx->d,
4106 operation->hash,
4107 operation->hash_length,
4108 operation->md_alg,
4109 mbedtls_psa_get_random,
4110 MBEDTLS_PSA_RANDOM_STATE,
4111 &operation->restart_ctx));
4112#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00004113 status = PSA_ERROR_NOT_SUPPORTED;
4114 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004115#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
4116 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00004117 status = mbedtls_to_psa_error(
4118 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00004119 &r,
4120 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004121 &operation->ctx->d,
4122 operation->hash,
4123 operation->hash_length,
4124 mbedtls_psa_get_random,
4125 MBEDTLS_PSA_RANDOM_STATE,
4126 mbedtls_psa_get_random,
4127 MBEDTLS_PSA_RANDOM_STATE,
4128 &operation->restart_ctx));
4129 }
4130
Paul Elliottf8e5b562023-02-19 18:43:45 +00004131 /* Hide the fact that the restart context only holds a delta of number of
4132 * ops done during the last operation, not an absolute value. */
4133 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4134
Paul Elliott724bd252023-02-08 12:35:08 +00004135 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00004136 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00004137 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004138 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004139 operation->coordinate_bytes)
4140 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00004141
4142 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00004143 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004144 }
4145
4146 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00004147 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004148 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004149 operation->coordinate_bytes,
4150 operation->coordinate_bytes)
4151 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00004152
4153 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00004154 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004155 }
4156
Paul Elliott1bc59df2023-02-05 13:41:57 +00004157 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004158
Paul Elliott724bd252023-02-08 12:35:08 +00004159 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004160 }
Paul Elliott724bd252023-02-08 12:35:08 +00004161
4162exit:
4163
4164 mbedtls_mpi_free(&r);
4165 mbedtls_mpi_free(&s);
4166 return status;
4167
Paul Elliott588f8ed2022-12-02 18:10:26 +00004168 #else
4169
4170 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004171 (void) signature;
4172 (void) signature_size;
4173 (void) signature_length;
4174
4175 return PSA_ERROR_NOT_SUPPORTED;
4176
4177#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4178 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4179 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4180}
4181
4182psa_status_t mbedtls_psa_sign_hash_abort(
4183 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
4184{
4185
4186#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4187 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4188 defined(MBEDTLS_ECP_RESTARTABLE)
4189
4190 if (operation->ctx) {
4191 mbedtls_ecdsa_free(operation->ctx);
4192 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004193 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004194 }
4195
4196 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4197
Paul Elliott93d9ca82023-02-15 18:14:21 +00004198 operation->num_ops = 0;
4199
Paul Elliott588f8ed2022-12-02 18:10:26 +00004200 return PSA_SUCCESS;
4201
4202#else
4203
4204 (void) operation;
4205
4206 return PSA_ERROR_NOT_SUPPORTED;
4207
4208#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4209 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4210 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4211}
4212
4213psa_status_t mbedtls_psa_verify_hash_start(
4214 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
4215 const psa_key_attributes_t *attributes,
4216 const uint8_t *key_buffer, size_t key_buffer_size,
4217 psa_algorithm_t alg,
4218 const uint8_t *hash, size_t hash_length,
4219 const uint8_t *signature, size_t signature_length)
4220{
4221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00004222 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00004223 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004224
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004225 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00004226 return PSA_ERROR_NOT_SUPPORTED;
4227 }
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01004228 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type);
4229 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
4230 return PSA_ERROR_INVALID_ARGUMENT;
4231 }
Paul Elliott068fe072023-01-16 13:59:15 +00004232
Gilles Peskine8a8aa592024-11-20 16:13:10 +01004233 if (!can_do_interruptible_sign_verify(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004234 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004235 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004236
4237#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004238 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4239 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004240
Paul Elliotta1c94092023-02-15 16:38:04 +00004241 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4242 mbedtls_mpi_init(&operation->r);
4243 mbedtls_mpi_init(&operation->s);
4244
Paul Elliott93d9ca82023-02-15 18:14:21 +00004245 /* Ensure num_ops is zero'ed in case of context re-use. */
4246 operation->num_ops = 0;
4247
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004248 status = mbedtls_psa_ecp_load_representation(attributes->type,
4249 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004250 key_buffer,
4251 key_buffer_size,
4252 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004253
Paul Elliott068fe072023-01-16 13:59:15 +00004254 if (status != PSA_SUCCESS) {
4255 return status;
4256 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004257
Paul Elliottc569fc22023-02-10 13:02:54 +00004258 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004259
Paul Elliott1bc59df2023-02-05 13:41:57 +00004260 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004261 return PSA_ERROR_INVALID_SIGNATURE;
4262 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004263
Paul Elliott068fe072023-01-16 13:59:15 +00004264 status = mbedtls_to_psa_error(
4265 mbedtls_mpi_read_binary(&operation->r,
4266 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004267 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004268
Paul Elliott068fe072023-01-16 13:59:15 +00004269 if (status != PSA_SUCCESS) {
4270 return status;
4271 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004272
Paul Elliott068fe072023-01-16 13:59:15 +00004273 status = mbedtls_to_psa_error(
4274 mbedtls_mpi_read_binary(&operation->s,
4275 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004276 coordinate_bytes,
4277 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004278
Paul Elliott068fe072023-01-16 13:59:15 +00004279 if (status != PSA_SUCCESS) {
4280 return status;
4281 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004282
Paul Elliott2c9843f2023-02-15 17:32:42 +00004283 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004284
Paul Elliott2c9843f2023-02-15 17:32:42 +00004285 if (status != PSA_SUCCESS) {
4286 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004287 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004288
Paul Elliott0290a762023-02-09 14:30:24 +00004289 /* We only need to store the same length of hash as the private key size
4290 * here, it would be truncated by the internal implementation anyway. */
4291 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4292 coordinate_bytes);
4293
Paul Elliottba70ad42023-02-15 18:23:53 +00004294 if (required_hash_length > sizeof(operation->hash)) {
4295 /* Shouldn't happen, but better safe than sorry. */
4296 return PSA_ERROR_CORRUPTION_DETECTED;
4297 }
4298
Paul Elliott0290a762023-02-09 14:30:24 +00004299 memcpy(operation->hash, hash, required_hash_length);
4300 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004301
4302 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004303#else
Paul Elliott068fe072023-01-16 13:59:15 +00004304 (void) operation;
4305 (void) key_buffer;
4306 (void) key_buffer_size;
4307 (void) alg;
4308 (void) hash;
4309 (void) hash_length;
4310 (void) signature;
4311 (void) signature_length;
4312 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004313 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004314 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004315
Paul Elliott068fe072023-01-16 13:59:15 +00004316 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004317#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4318 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4319 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004320}
4321
4322psa_status_t mbedtls_psa_verify_hash_complete(
4323 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4324{
4325
4326#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4327 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4328 defined(MBEDTLS_ECP_RESTARTABLE)
4329
Paul Elliottf8e5b562023-02-19 18:43:45 +00004330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4331
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004332 /* Ensure max_ops is set to the current value (or default). */
4333 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4334
Paul Elliottf8e5b562023-02-19 18:43:45 +00004335 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004336 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4337 operation->hash,
4338 operation->hash_length,
4339 &operation->ctx->Q,
4340 &operation->r,
4341 &operation->s,
4342 &operation->restart_ctx));
4343
Paul Elliottf8e5b562023-02-19 18:43:45 +00004344 /* Hide the fact that the restart context only holds a delta of number of
4345 * ops done during the last operation, not an absolute value. */
4346 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4347
4348 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004349#else
4350 (void) operation;
4351
4352 return PSA_ERROR_NOT_SUPPORTED;
4353
4354#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4355 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4356 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4357}
4358
4359psa_status_t mbedtls_psa_verify_hash_abort(
4360 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4361{
4362
4363#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4364 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4365 defined(MBEDTLS_ECP_RESTARTABLE)
4366
4367 if (operation->ctx) {
4368 mbedtls_ecdsa_free(operation->ctx);
4369 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004370 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004371 }
4372
4373 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4374
Paul Elliott93d9ca82023-02-15 18:14:21 +00004375 operation->num_ops = 0;
4376
Paul Elliott588f8ed2022-12-02 18:10:26 +00004377 mbedtls_mpi_free(&operation->r);
4378 mbedtls_mpi_free(&operation->s);
4379
4380 return PSA_SUCCESS;
4381
4382#else
4383 (void) operation;
4384
4385 return PSA_ERROR_NOT_SUPPORTED;
4386
4387#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4388 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4389 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4390}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004391
David Horstmann6e99bb22024-02-06 15:27:49 +00004392static psa_status_t psa_generate_random_internal(uint8_t *output,
4393 size_t output_size)
4394{
4395 GUARD_MODULE_INITIALIZED;
4396
David Horstmann6e99bb22024-02-06 15:27:49 +00004397#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4398
David Horstmanndb909142024-03-12 16:07:08 +00004399 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004400 size_t output_length = 0;
4401 status = mbedtls_psa_external_get_random(&global_data.rng,
4402 output, output_size,
4403 &output_length);
4404 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004405 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004406 }
4407 /* Breaking up a request into smaller chunks is currently not supported
4408 * for the external RNG interface. */
4409 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004410 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004411 }
David Horstmanndb909142024-03-12 16:07:08 +00004412 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004413
4414#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4415
4416 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004417 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004418 size_t request_size =
4419 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4420 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4421 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004422#if defined(MBEDTLS_CTR_DRBG_C)
4423 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4424#elif defined(MBEDTLS_HMAC_DRBG_C)
4425 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4426#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004427 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004428 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004429 }
4430 output_size -= request_size;
4431 output += request_size;
4432 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004433 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004434#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004435}
4436
4437
mohammad1603503973b2018-03-12 15:59:30 +02004438/****************************************************************/
4439/* Symmetric cryptography */
4440/****************************************************************/
4441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4443 mbedtls_svc_key_id_t key,
4444 psa_algorithm_t alg,
4445 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004446{
4447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4448 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004449 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4451 PSA_KEY_USAGE_ENCRYPT :
4452 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004453
4454 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004456 status = PSA_ERROR_BAD_STATE;
4457 goto exit;
4458 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004461 status = PSA_ERROR_INVALID_ARGUMENT;
4462 goto exit;
4463 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4466 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004469
Ronald Cron49fafa92021-03-10 08:34:23 +01004470 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004471 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004472 * so we only set it (in the driver wrapper) after resources have been
4473 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004474 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004476 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004478 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 }
4480 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004481
Gilles Peskinea7a480b2025-02-05 19:41:52 +01004482
4483 /* Make sure the driver-dependent part of the operation is zeroed.
4484 * This is a guarantee we make to drivers. Initializing the operation
4485 * does not necessarily take care of it, since the context is a
4486 * union and initializing a union does not necessarily initialize
4487 * all of its members. */
4488 memset(&operation->ctx, 0, sizeof(operation->ctx));
4489
Ronald Cronab99ac22020-10-01 16:38:28 +02004490 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 if (cipher_operation == MBEDTLS_ENCRYPT) {
4492 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004493 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 slot->key.data,
4495 slot->key.bytes,
4496 alg);
4497 } else {
4498 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004499 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 slot->key.data,
4501 slot->key.bytes,
4502 alg);
4503 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004504
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (status != PSA_SUCCESS) {
4507 psa_cipher_abort(operation);
4508 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004509
Ryan Everettc0053cc2024-02-14 16:27:13 +00004510 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004513}
4514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4516 mbedtls_svc_key_id_t key,
4517 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004518{
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004520}
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4523 mbedtls_svc_key_id_t key,
4524 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004525{
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004527}
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004530 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 size_t iv_size,
4532 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004533{
Ronald Cron6d051732020-10-01 14:10:20 +02004534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004535 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004536
Gabor Mezei358eb212024-02-07 18:10:13 +01004537 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004540 status = PSA_ERROR_BAD_STATE;
4541 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004542 }
4543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004545 status = PSA_ERROR_BAD_STATE;
4546 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004547 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004548
Ronald Cron2fb90522021-07-05 12:31:44 +02004549 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004551 status = PSA_ERROR_BUFFER_TOO_SMALL;
4552 goto exit;
4553 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004556 status = PSA_ERROR_GENERIC_ERROR;
4557 goto exit;
4558 }
4559
Gabor Mezei358eb212024-02-07 18:10:13 +01004560 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4561
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004562 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 }
Ronald Cron5618a392021-03-26 09:52:26 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004568 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004569
4570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004572 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004573 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004575 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004577 if (iv != NULL) {
4578 mbedtls_platform_zeroize(iv, default_iv_length);
4579 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004580 }
Ronald Cron6d051732020-10-01 14:10:20 +02004581
Gabor Mezei358eb212024-02-07 18:10:13 +01004582 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004584}
4585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004587 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004589{
Ronald Cron6d051732020-10-01 14:10:20 +02004590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004591
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004592 LOCAL_INPUT_DECLARE(iv_external, iv);
4593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004595 status = PSA_ERROR_BAD_STATE;
4596 goto exit;
4597 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004600 status = PSA_ERROR_BAD_STATE;
4601 goto exit;
4602 }
Ronald Crona0d68172021-03-26 10:15:08 +01004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004605 status = PSA_ERROR_INVALID_ARGUMENT;
4606 goto exit;
4607 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004608
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004609 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 status = psa_driver_wrapper_cipher_set_iv(operation,
4612 iv,
4613 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004614
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004617 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 } else {
4619 psa_cipher_abort(operation);
4620 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004621
4622 LOCAL_INPUT_FREE(iv_external, iv);
4623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004625}
4626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004628 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004630 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 size_t output_size,
4632 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004633{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004635
Gabor Mezei212eb082024-01-24 16:56:00 +01004636 LOCAL_INPUT_DECLARE(input_external, input);
4637 LOCAL_OUTPUT_DECLARE(output_external, output);
4638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004640 status = PSA_ERROR_BAD_STATE;
4641 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004642 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004645 status = PSA_ERROR_BAD_STATE;
4646 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004647 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004648
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004649 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004650 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 status = psa_driver_wrapper_cipher_update(operation,
4653 input,
4654 input_length,
4655 output,
4656 output_size,
4657 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004658
4659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (status != PSA_SUCCESS) {
4661 psa_cipher_abort(operation);
4662 }
Ronald Cron6d051732020-10-01 14:10:20 +02004663
Gabor Mezei212eb082024-01-24 16:56:00 +01004664 LOCAL_INPUT_FREE(input_external, input);
4665 LOCAL_OUTPUT_FREE(output_external, output);
4666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004668}
4669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004671 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 size_t output_size,
4673 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004674{
David Saadab4ecc272019-02-14 13:48:10 +02004675 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004676
Gabor Mezei212eb082024-01-24 16:56:00 +01004677 LOCAL_OUTPUT_DECLARE(output_external, output);
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004680 status = PSA_ERROR_BAD_STATE;
4681 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004682 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004685 status = PSA_ERROR_BAD_STATE;
4686 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004687 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004688
Gabor Mezei0b041162024-02-29 10:08:16 +00004689 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 status = psa_driver_wrapper_cipher_finish(operation,
4692 output,
4693 output_size,
4694 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004695
4696exit:
Gilles Peskine3b380da2025-08-07 21:59:07 +02004697 /* C99 doesn't allow a declaration to follow a label */;
4698 psa_status_t abort_status = psa_cipher_abort(operation);
4699 /* Normally abort shouldn't fail unless the operation is in a bad
4700 * state, in which case we'd expect finish to fail with the same error.
4701 * So it doesn't matter much which call's error code we pick when both
4702 * fail. However, in unauthenticated decryption specifically, the
4703 * distinction between PSA_SUCCESS and PSA_ERROR_INVALID_PADDING is
4704 * security-sensitive (risk of a padding oracle attack), so here we
4705 * must not have a code path that depends on the value of status. */
4706 if (abort_status != PSA_SUCCESS) {
4707 status = abort_status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004708 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004709
Gilles Peskine3b380da2025-08-07 21:59:07 +02004710 /* Set *output_length to 0 if status != PSA_SUCCESS, without
4711 * leaking the value of status through a timing side channel
4712 * (status == PSA_ERROR_INVALID_PADDING is sensitive when doing
4713 * unpadded decryption, due to the risk of padding oracle attack). */
4714 mbedtls_ct_condition_t success =
4715 mbedtls_ct_bool_not(mbedtls_ct_bool(status));
4716 *output_length = mbedtls_ct_size_if_else_0(success, *output_length);
4717
Gabor Mezei212eb082024-01-24 16:56:00 +01004718 LOCAL_OUTPUT_FREE(output_external, output);
4719
4720 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004721}
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004724{
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004726 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004727 * in use. It's ok to call abort on such an object, and there's
4728 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004730 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004733
Ronald Cron49fafa92021-03-10 08:34:23 +01004734 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004735 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004736 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004739}
4740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4742 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004743 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004745 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 size_t output_size,
4747 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004748{
4749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004750 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004751 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004752 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4753 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004754
David Horstmann62a56d92023-12-14 18:12:47 +00004755 LOCAL_INPUT_DECLARE(input_external, input);
4756 LOCAL_OUTPUT_DECLARE(output_external, output);
4757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004759 status = PSA_ERROR_INVALID_ARGUMENT;
4760 goto exit;
4761 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4764 PSA_KEY_USAGE_ENCRYPT,
4765 alg);
4766 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004767 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4771 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004772 status = PSA_ERROR_GENERIC_ERROR;
4773 goto exit;
4774 }
4775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (default_iv_length > 0) {
4777 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004778 status = PSA_ERROR_BUFFER_TOO_SMALL;
4779 goto exit;
4780 }
4781
David Horstmann6e99bb22024-02-06 15:27:49 +00004782 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004786 }
4787
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004788 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4789 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4790
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004791 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004792 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004793 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004794 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004796
4797exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004798 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004800 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004801 }
Ronald Cron23919522021-07-08 19:00:07 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (status == PSA_SUCCESS) {
4804 if (default_iv_length > 0) {
4805 memcpy(output, local_iv, default_iv_length);
4806 }
4807 *output_length += default_iv_length;
4808 } else {
4809 *output_length = 0;
4810 }
4811
David Horstmann62a56d92023-12-14 18:12:47 +00004812 LOCAL_INPUT_FREE(input_external, input);
4813 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004816}
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4819 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004820 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004822 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 size_t output_size,
4824 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004825{
4826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004827 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004828 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004829
Gabor Mezei212eb082024-01-24 16:56:00 +01004830 LOCAL_INPUT_DECLARE(input_external, input);
4831 LOCAL_OUTPUT_DECLARE(output_external, output);
4832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto exit;
4836 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4839 PSA_KEY_USAGE_DECRYPT,
4840 alg);
4841 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004844
Gilles Peskineb47c3b32024-06-26 13:16:33 +02004845 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004846 status = PSA_ERROR_INVALID_ARGUMENT;
4847 goto exit;
4848 }
4849
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004850 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4851 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4852
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004853 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004854 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004855 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004857
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004858exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004859 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004861 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004865 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 }
Ronald Cron23919522021-07-08 19:00:07 +02004867
Gabor Mezei212eb082024-01-24 16:56:00 +01004868 LOCAL_INPUT_FREE(input_external, input);
4869 LOCAL_OUTPUT_FREE(output_external, output);
4870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004872}
4873
4874
mohammad16035955c982018-04-26 00:53:03 +03004875/****************************************************************/
4876/* AEAD */
4877/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004878
Paul Elliottbaff51c2021-09-28 17:44:45 +01004879/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004881{
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004883}
4884
4885/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004886static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4887 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004888{
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004892#if defined(PSA_WANT_ALG_GCM)
4893 case PSA_ALG_GCM:
4894 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 * arbitrarily large nonces. Please note that we do not generally
4896 * recommend the usage of nonces of greater length than
4897 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4898 * size, which can then lead to collisions if you encrypt a very
4899 * large number of messages.*/
4900 if (nonce_length != 0) {
4901 return PSA_SUCCESS;
4902 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004903 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004904#endif /* PSA_WANT_ALG_GCM */
4905#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004906 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (nonce_length >= 7 && nonce_length <= 13) {
4908 return PSA_SUCCESS;
4909 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004911#endif /* PSA_WANT_ALG_CCM */
4912#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004913 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (nonce_length == 12) {
4915 return PSA_SUCCESS;
4916 } else if (nonce_length == 8) {
4917 return PSA_ERROR_NOT_SUPPORTED;
4918 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004919 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004920#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004921 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004922 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004924 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004927}
4928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004930{
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4932 return PSA_ERROR_INVALID_ARGUMENT;
4933 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004936}
4937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4939 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004940 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004942 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004944 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004946 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 size_t ciphertext_size,
4948 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004949{
Ronald Cron215633c2021-03-16 17:15:37 +01004950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4951 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004952
David Horstmann9d09a022023-11-28 19:25:00 +00004953 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4954 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4955 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4956 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4957
mohammad1603f08a5502018-06-03 15:05:47 +03004958 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 status = psa_aead_check_algorithm(alg);
4961 if (status != PSA_SUCCESS) {
4962 return status;
4963 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004964
Ronald Cron9a986162021-03-26 12:40:07 +01004965 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4967 if (status != PSA_SUCCESS) {
4968 return status;
4969 }
mohammad16035955c982018-04-26 00:53:03 +03004970
David Horstmann9d09a022023-11-28 19:25:00 +00004971 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4972 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4973 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4974 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 status = psa_aead_check_nonce_length(alg, nonce_length);
4977 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004978 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004980
Ronald Cronde822812021-03-17 16:08:20 +01004981 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004982 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004983 alg,
4984 nonce, nonce_length,
4985 additional_data, additional_data_length,
4986 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4990 memset(ciphertext, 0, ciphertext_size);
4991 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004992
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004993exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004994 LOCAL_INPUT_FREE(nonce_external, nonce);
4995 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4996 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4997 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4998
Ryan Everetta103ec92024-01-31 13:59:57 +00004999 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03005000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02005002}
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
5005 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00005006 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005008 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005010 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005012 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 size_t plaintext_size,
5014 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03005015{
Ronald Cron215633c2021-03-16 17:15:37 +01005016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5017 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02005018
David Horstmann7f2e0402023-11-28 19:43:53 +00005019 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5020 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
5021 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
5022 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5023
Gilles Peskineee652a32018-06-01 19:23:52 +02005024 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 status = psa_aead_check_algorithm(alg);
5027 if (status != PSA_SUCCESS) {
5028 return status;
5029 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01005030
Ronald Cron9a986162021-03-26 12:40:07 +01005031 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
5033 if (status != PSA_SUCCESS) {
5034 return status;
5035 }
mohammad16035955c982018-04-26 00:53:03 +03005036
David Horstmann7f2e0402023-11-28 19:43:53 +00005037 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
5038 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
5039 additional_data);
5040 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
5041 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 status = psa_aead_check_nonce_length(alg, nonce_length);
5044 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005045 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005047
Ronald Cronde822812021-03-17 16:08:20 +01005048 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005049 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01005050 alg,
5051 nonce, nonce_length,
5052 additional_data, additional_data_length,
5053 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (status != PSA_SUCCESS && plaintext_size != 0) {
5057 memset(plaintext, 0, plaintext_size);
5058 }
mohammad160360a64d02018-06-03 17:20:42 +03005059
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005060exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00005061 LOCAL_INPUT_FREE(nonce_external, nonce);
5062 LOCAL_INPUT_FREE(additional_data_external, additional_data);
5063 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
5064 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5065
Ryan Everetta103ec92024-01-31 13:59:57 +00005066 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01005067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 return status;
mohammad16035955c982018-04-26 00:53:03 +03005069}
5070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
5072{
5073 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02005076#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005078 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
5080 return PSA_ERROR_INVALID_ARGUMENT;
5081 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005082 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005083#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005084
Przemek Stekiel86679c72022-10-06 17:06:56 +02005085#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005087 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
5089 return PSA_ERROR_INVALID_ARGUMENT;
5090 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005091 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005092#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005093
Przemek Stekiel86679c72022-10-06 17:06:56 +02005094#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005096 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (tag_len != 16) {
5098 return PSA_ERROR_INVALID_ARGUMENT;
5099 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005100 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005101#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005102
5103 default:
5104 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005106 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005108}
5109
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005110/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005111static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
5112 int is_encrypt,
5113 mbedtls_svc_key_id_t key,
5114 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01005115{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
5118 psa_key_slot_t *slot = NULL;
5119 psa_key_usage_t key_usage = 0;
5120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 status = psa_aead_check_algorithm(alg);
5122 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 }
Paul Elliottc2b71442021-06-24 18:17:52 +01005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005127 status = PSA_ERROR_BAD_STATE;
5128 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005129 }
5130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 if (operation->nonce_set || operation->lengths_set ||
5132 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005133 status = PSA_ERROR_BAD_STATE;
5134 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005135 }
5136
Gilles Peskinea7a480b2025-02-05 19:41:52 +01005137 /* Make sure the driver-dependent part of the operation is zeroed.
5138 * This is a guarantee we make to drivers. Initializing the operation
5139 * does not necessarily take care of it, since the context is a
5140 * union and initializing a union does not necessarily initialize
5141 * all of its members. */
5142 memset(&operation->ctx, 0, sizeof(operation->ctx));
5143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005145 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005147 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
5151 alg);
5152 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (is_encrypt) {
5161 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005162 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 slot->key.data,
5164 slot->key.bytes,
5165 alg);
5166 } else {
5167 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005168 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 slot->key.data,
5170 slot->key.bytes,
5171 alg);
5172 }
5173 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005176
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005177 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005178
5179exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00005180 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005183 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01005185 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 } else {
5187 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005188 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01005191}
5192
Paul Elliott302ff6b2021-04-20 18:10:30 +01005193/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
5195 mbedtls_svc_key_id_t key,
5196 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005197{
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005199}
5200
5201/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
5203 mbedtls_svc_key_id_t key,
5204 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005205{
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005207}
5208
David Horstmannfed23772023-12-19 17:49:20 +00005209static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
5210 const uint8_t *nonce,
5211 size_t nonce_length)
5212{
5213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5214
5215 if (operation->id == 0) {
5216 status = PSA_ERROR_BAD_STATE;
5217 goto exit;
5218 }
5219
5220 if (operation->nonce_set) {
5221 status = PSA_ERROR_BAD_STATE;
5222 goto exit;
5223 }
5224
5225 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
5226 if (status != PSA_SUCCESS) {
5227 status = PSA_ERROR_INVALID_ARGUMENT;
5228 goto exit;
5229 }
5230
5231 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
5232 nonce_length);
5233
5234exit:
5235 if (status == PSA_SUCCESS) {
5236 operation->nonce_set = 1;
5237 } else {
5238 psa_aead_abort(operation);
5239 }
5240
5241 return status;
5242}
5243
Paul Elliott302ff6b2021-04-20 18:10:30 +01005244/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01005245psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00005246 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 size_t nonce_size,
5248 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005249{
5250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01005251 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07005252 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005253
David Horstmannd3cad8b2023-12-11 14:46:04 +00005254 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
5255 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
5256
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005257 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005260 status = PSA_ERROR_BAD_STATE;
5261 goto exit;
5262 }
5263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005265 status = PSA_ERROR_BAD_STATE;
5266 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005267 }
5268
Paul Elliotte193ea82021-10-01 13:00:16 +01005269 /* For CCM, this size may not be correct according to the PSA
5270 * specification. The PSA Crypto 1.0.1 specification states:
5271 *
5272 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5273 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5274 *
5275 * However this restriction that L has to be the smallest integer is not
5276 * applied in practice, and it is not implementable here since the
5277 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5279 operation->alg);
5280 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005281 status = PSA_ERROR_BUFFER_TOO_SMALL;
5282 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005283 }
5284
David Horstmann6e99bb22024-02-06 15:27:49 +00005285 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005289
David Horstmannfed23772023-12-19 17:49:20 +00005290 status = psa_aead_set_nonce_internal(operation, local_nonce,
5291 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005292
Paul Elliott39dc6b82021-05-11 19:16:09 +01005293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (status == PSA_SUCCESS) {
5295 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005296 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 } else {
5298 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005299 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005300
David Horstmannd3cad8b2023-12-11 14:46:04 +00005301 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005304}
5305
5306/* Set the nonce for a multipart authenticated encryption or decryption
5307 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005308psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005309 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005311{
David Horstmannfed23772023-12-19 17:49:20 +00005312 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005313
David Horstmann8f0ef512023-12-11 15:17:11 +00005314 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5315 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005316
David Horstmannfed23772023-12-19 17:49:20 +00005317 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005318
David Horstmann18dc0322023-12-20 16:16:43 +00005319/* Exit label is only needed for buffer copying, prevent unused warnings. */
David Horstmann4a48bec2024-03-13 15:42:31 +00005320#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005321exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005322#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005323
David Horstmann8f0ef512023-12-11 15:17:11 +00005324 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005327}
5328
5329/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5331 size_t ad_length,
5332 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005333{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005337 status = PSA_ERROR_BAD_STATE;
5338 goto exit;
5339 }
5340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (operation->lengths_set || operation->ad_started ||
5342 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005343 status = PSA_ERROR_BAD_STATE;
5344 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005345 }
5346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005348#if defined(PSA_WANT_ALG_GCM)
5349 case PSA_ALG_GCM:
5350 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 * bits. Without the guard this code will generate warnings on 32bit
5352 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005353#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 if (((uint64_t) ad_length) >> 61 != 0 ||
5355 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005356 status = PSA_ERROR_INVALID_ARGUMENT;
5357 goto exit;
5358 }
Paul Elliott325d3742021-09-27 17:56:28 +01005359#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005360 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005361#endif /* PSA_WANT_ALG_GCM */
5362#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005363 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005365 status = PSA_ERROR_INVALID_ARGUMENT;
5366 goto exit;
5367 }
5368 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005369#endif /* PSA_WANT_ALG_CCM */
5370#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005371 case PSA_ALG_CHACHA20_POLY1305:
5372 /* No length restrictions for ChaChaPoly. */
5373 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005374#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005375 default:
5376 break;
5377 }
Paul Elliott325d3742021-09-27 17:56:28 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5380 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005381
Paul Elliott39dc6b82021-05-11 19:16:09 +01005382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005384 operation->ad_remaining = ad_length;
5385 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005386 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 } else {
5388 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005389 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005392}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005393
5394/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005395psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005396 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005398{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5400
David Horstmann25dac6e2023-12-11 15:23:13 +00005401 LOCAL_INPUT_DECLARE(input_external, input);
5402 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005405 status = PSA_ERROR_BAD_STATE;
5406 goto exit;
5407 }
5408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005410 status = PSA_ERROR_BAD_STATE;
5411 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005412 }
5413
Paul Elliott304766f2024-04-26 18:30:11 +01005414 /* No input to add (zero length), nothing to do. */
5415 if (input_length == 0) {
5416 status = PSA_SUCCESS;
5417 goto exit;
5418 }
5419
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (operation->lengths_set) {
5421 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005422 status = PSA_ERROR_INVALID_ARGUMENT;
5423 goto exit;
5424 }
5425
5426 operation->ad_remaining -= input_length;
5427 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005428#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005430 status = PSA_ERROR_BAD_STATE;
5431 goto exit;
5432 }
5433#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 status = psa_driver_wrapper_aead_update_ad(operation, input,
5436 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005437
Paul Elliott39dc6b82021-05-11 19:16:09 +01005438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005440 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 } else {
5442 psa_aead_abort(operation);
5443 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005444
David Horstmann25dac6e2023-12-11 15:23:13 +00005445 LOCAL_INPUT_FREE(input_external, input);
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005448}
5449
5450/* Encrypt or decrypt a message fragment in an active multipart AEAD
5451 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005452psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005453 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005455 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 size_t output_size,
5457 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005458{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005460
David Horstmann2914fac2023-12-11 15:28:37 +00005461
5462 LOCAL_INPUT_DECLARE(input_external, input);
5463 LOCAL_OUTPUT_DECLARE(output_external, output);
5464
5465 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5466 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5467
Paul Elliott302ff6b2021-04-20 18:10:30 +01005468 *output_length = 0;
5469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005471 status = PSA_ERROR_BAD_STATE;
5472 goto exit;
5473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005476 status = PSA_ERROR_BAD_STATE;
5477 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005478 }
5479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005481 /* Additional data length was supplied, but not all the additional
5482 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005484 status = PSA_ERROR_INVALID_ARGUMENT;
5485 goto exit;
5486 }
5487
5488 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005490 status = PSA_ERROR_INVALID_ARGUMENT;
5491 goto exit;
5492 }
5493
5494 operation->body_remaining -= input_length;
5495 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005496#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005498 status = PSA_ERROR_BAD_STATE;
5499 goto exit;
5500 }
5501#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5504 output, output_size,
5505 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005506
Paul Elliott39dc6b82021-05-11 19:16:09 +01005507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005509 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 } else {
5511 psa_aead_abort(operation);
5512 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005513
David Horstmann2914fac2023-12-11 15:28:37 +00005514 LOCAL_INPUT_FREE(input_external, input);
5515 LOCAL_OUTPUT_FREE(output_external, output);
5516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005518}
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005521{
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (operation->id == 0 || !operation->nonce_set) {
5523 return PSA_ERROR_BAD_STATE;
5524 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5527 operation->body_remaining != 0)) {
5528 return PSA_ERROR_INVALID_ARGUMENT;
5529 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005532}
5533
Paul Elliott302ff6b2021-04-20 18:10:30 +01005534/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005535psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005536 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 size_t ciphertext_size,
5538 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005539 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 size_t tag_size,
5541 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005542{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005543 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5544
David Horstmann6db0e732023-12-11 15:35:59 +00005545 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5546 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5547
5548 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5549 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5550
Paul Elliott302ff6b2021-04-20 18:10:30 +01005551 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005552 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 status = psa_aead_final_checks(operation);
5555 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005560 status = PSA_ERROR_BAD_STATE;
5561 goto exit;
5562 }
5563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5565 ciphertext_size,
5566 ciphertext_length,
5567 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005568
Paul Elliott39dc6b82021-05-11 19:16:09 +01005569exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005570
5571
Paul Elliottf47b0952021-05-21 18:02:33 +01005572 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005573 * the zero tag size, make sure the tag is set to something implausible.
5574 * Even if the operation succeeds, make sure we clear the rest of the
5575 * buffer to prevent potential leakage of anything previously placed in
5576 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005577 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005580
David Horstmann6db0e732023-12-11 15:35:59 +00005581 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5582 LOCAL_OUTPUT_FREE(tag_external, tag);
5583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005585}
5586
5587/* Finish authenticating and decrypting a message in a multipart AEAD
5588 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005589psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005590 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 size_t plaintext_size,
5592 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005593 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005595{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5597
David Horstmanne000a0a2023-12-11 16:37:04 +00005598 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5599 LOCAL_INPUT_DECLARE(tag_external, tag);
5600
5601 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5602 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5603
Paul Elliott302ff6b2021-04-20 18:10:30 +01005604 *plaintext_length = 0;
5605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 status = psa_aead_final_checks(operation);
5607 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005612 status = PSA_ERROR_BAD_STATE;
5613 goto exit;
5614 }
5615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5617 plaintext_size,
5618 plaintext_length,
5619 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005620
5621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005623
David Horstmanne000a0a2023-12-11 16:37:04 +00005624 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5625 LOCAL_INPUT_FREE(tag_external, tag);
5626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005628}
5629
5630/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005631psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005632{
5633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005636 /* The object has (apparently) been initialized but it is not (yet)
5637 * in use. It's ok to call abort on such an object, and there's
5638 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005640 }
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005647}
5648
Gilles Peskinee59236f2018-01-27 23:32:46 +01005649/****************************************************************/
Gilles Peskinecdb37812025-02-05 18:02:49 +01005650/* Key derivation: output generation */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651/****************************************************************/
5652
Przemek Stekiel69c46792022-06-10 12:59:51 +02005653#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005654 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005655 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305656 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305657 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005658#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005659#endif /* At least one builtin KDF */
5660
Przemek Stekiel69c46792022-06-10 12:59:51 +02005661#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005662 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5663 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005664
5665/** Internal helper to set up an HMAC operation with a key passed directly.
5666 *
5667 * \param[in,out] operation A MAC operation object. It does not need to
5668 * be initialized.
5669 * \param hash_alg The hash algorithm used for HMAC.
5670 * \param hmac_key The HMAC key.
5671 * \param hmac_key_length Length of \p hmac_key in bytes.
5672 *
5673 * \return A PSA status code.
5674 */
Steven Cooreman094a77e2021-05-06 17:58:36 +02005675static psa_status_t psa_key_derivation_start_hmac(
5676 psa_mac_operation_t *operation,
5677 psa_algorithm_t hash_alg,
5678 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005680{
5681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5684 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5685 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005686
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005687 /* Make sure the whole the operation is zeroed.
5688 * It isn't enough to require the caller to initialize operation to
5689 * PSA_MAC_OPERATION_INIT, since one field is a union and initializing
5690 * a union does not necessarily initialize all of its members.
Gilles Peskine91b29a02025-05-13 11:53:31 +02005691 * psa_mac_setup() would handle PSA_MAC_OPERATION_INIT, but here we
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005692 * bypass it and call lower-level functions directly. */
5693 memset(operation, 0, sizeof(*operation));
5694
Steven Cooreman72f736a2021-05-07 14:14:37 +02005695 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 status = psa_driver_wrapper_mac_sign_setup(operation,
5699 &attributes,
5700 hmac_key, hmac_key_length,
5701 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 psa_reset_key_attributes(&attributes);
5704 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005705}
5706#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005707
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005708#define HKDF_STATE_INIT 0 /* no input yet */
5709#define HKDF_STATE_STARTED 1 /* got salt */
5710#define HKDF_STATE_KEYED 2 /* got key */
5711#define HKDF_STATE_OUTPUT 3 /* output started */
5712
Gilles Peskinecbe66502019-05-16 16:59:18 +02005713static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005715{
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5717 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5718 } else {
5719 return operation->alg;
5720 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721}
5722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005724{
5725 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5727 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005728 /* The object has (apparently) been initialized but it is not
5729 * in use. It's ok to call abort on such an object, and there's
5730 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005732#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5734 mbedtls_free(operation->ctx.hkdf.info);
5735 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5736 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005737#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005738#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5739 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5741 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5742 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5743 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005744 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005746 }
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005749 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005751 }
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005754 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005756 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005757#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005759 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005761 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005762#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005763 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005764
5765 /* We leave the fields Ai and output_block to be erased safely by the
5766 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005768#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5769 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005770#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5772 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5773 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5774 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005775#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305776#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305777 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305778 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005779 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305780 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305781 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305782
5783 status = PSA_SUCCESS;
5784 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305785#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005786 {
5787 status = PSA_ERROR_BAD_STATE;
5788 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 mbedtls_platform_zeroize(operation, sizeof(*operation));
5790 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005791}
5792
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005795{
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005797 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005799 }
5800
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005803}
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5806 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807{
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 if (operation->alg == 0) {
5809 return PSA_ERROR_BAD_STATE;
5810 }
5811 if (capacity > operation->capacity) {
5812 return PSA_ERROR_INVALID_ARGUMENT;
5813 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005814 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005816}
5817
Przemek Stekiel69c46792022-06-10 12:59:51 +02005818#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005819/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005820static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5821 psa_algorithm_t kdf_alg,
5822 uint8_t *output,
5823 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005824{
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5826 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005827 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005828 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005829#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005831#else
5832 const uint8_t last_block = 0xff;
5833#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005834
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 if (hkdf->state < HKDF_STATE_KEYED ||
5836 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005837#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005839#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 )) {
5841 return PSA_ERROR_BAD_STATE;
5842 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005843 hkdf->state = HKDF_STATE_OUTPUT;
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005846 /* Copy what remains of the current block */
5847 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005849 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 }
5851 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005852 output += n;
5853 output_length -= n;
5854 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005856 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005858 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005859 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005860 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005861 * object was corrupted or if this function is called directly
5862 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 if (hkdf->block_number == last_block) {
5864 return PSA_ERROR_BAD_STATE;
5865 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005866
5867 /* We need a new block */
5868 ++hkdf->block_number;
5869 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5872 hash_alg,
5873 hkdf->prk,
5874 hash_length);
5875 if (status != PSA_SUCCESS) {
5876 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005877 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005878
5879 if (hkdf->block_number != 1) {
5880 status = psa_mac_update(&hkdf->hmac,
5881 hkdf->output_block,
5882 hash_length);
5883 if (status != PSA_SUCCESS) {
5884 return status;
5885 }
5886 }
5887 status = psa_mac_update(&hkdf->hmac,
5888 hkdf->info,
5889 hkdf->info_length);
5890 if (status != PSA_SUCCESS) {
5891 return status;
5892 }
5893 status = psa_mac_update(&hkdf->hmac,
5894 &hkdf->block_number, 1);
5895 if (status != PSA_SUCCESS) {
5896 return status;
5897 }
5898 status = psa_mac_sign_finish(&hkdf->hmac,
5899 hkdf->output_block,
5900 sizeof(hkdf->output_block),
5901 &hmac_output_length);
5902 if (status != PSA_SUCCESS) {
5903 return status;
5904 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005905 }
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005908}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005909#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005910
John Durkop07cc04a2020-11-16 22:08:34 -08005911#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5912 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005913static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5914 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005916{
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5918 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005919 psa_mac_operation_t hmac;
Steven Cooremana6df6042021-04-29 19:32:25 +02005920 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005921 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005922
Janos Follath7742fee2019-06-17 12:58:10 +01005923 /* We can't be wanting more output after block 0xff, otherwise
5924 * the capacity check in psa_key_derivation_output_bytes() would have
5925 * prevented this call. It could happen only if the operation
5926 * object was corrupted or if this function is called directly
5927 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (tls12_prf->block_number == 0xff) {
5929 return PSA_ERROR_CORRUPTION_DETECTED;
5930 }
Janos Follath7742fee2019-06-17 12:58:10 +01005931
5932 /* We need a new block */
5933 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005934 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005935
5936 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5937 *
5938 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5939 *
5940 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5941 * HMAC_hash(secret, A(2) + seed) +
5942 * HMAC_hash(secret, A(3) + seed) + ...
5943 *
5944 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005945 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005946 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005947 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005948 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005949 * is currently extracted as `output_block` and where i is
5950 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005951 */
5952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 status = psa_key_derivation_start_hmac(&hmac,
5954 hash_alg,
5955 tls12_prf->secret,
5956 tls12_prf->secret_length);
5957 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005958 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005960
5961 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005963 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5964 * the variable seed and in this instance means it in the context of the
5965 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 status = psa_mac_update(&hmac,
5967 tls12_prf->label,
5968 tls12_prf->label_length);
5969 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005970 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 }
5972 status = psa_mac_update(&hmac,
5973 tls12_prf->seed,
5974 tls12_prf->seed_length);
5975 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005976 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 }
5978 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005979 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5981 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005982 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005984 }
5985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 status = psa_mac_sign_finish(&hmac,
5987 tls12_prf->Ai, hash_length,
5988 &hmac_output_length);
5989 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005990 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 }
5992 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005993 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005995
5996 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 status = psa_key_derivation_start_hmac(&hmac,
5998 hash_alg,
5999 tls12_prf->secret,
6000 tls12_prf->secret_length);
6001 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006002 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 }
6004 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
6005 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006006 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
6008 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
6009 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006010 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 }
6012 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
6013 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006014 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 }
6016 status = psa_mac_sign_finish(&hmac,
6017 tls12_prf->output_block, hash_length,
6018 &hmac_output_length);
6019 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006020 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 }
Janos Follathea29bfb2019-06-19 12:21:20 +01006022
Janos Follath7742fee2019-06-17 12:58:10 +01006023
6024cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 cleanup_status = psa_mac_abort(&hmac);
6026 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006027 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 }
Janos Follathea29bfb2019-06-19 12:21:20 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01006031}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01006032
Janos Follath844eb0e2019-06-19 12:10:49 +01006033static psa_status_t psa_key_derivation_tls12_prf_read(
6034 psa_tls12_prf_key_derivation_t *tls12_prf,
6035 psa_algorithm_t alg,
6036 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01006038{
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
6040 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01006041 psa_status_t status;
6042 uint8_t offset, length;
6043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02006045 case PSA_TLS12_PRF_STATE_LABEL_SET:
6046 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
6047 break;
6048 case PSA_TLS12_PRF_STATE_OUTPUT:
6049 break;
6050 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02006052 }
6053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01006055 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (tls12_prf->left_in_block == 0) {
6057 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
6058 alg);
6059 if (status != PSA_SUCCESS) {
6060 return status;
6061 }
Janos Follath844eb0e2019-06-19 12:10:49 +01006062
6063 continue;
6064 }
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01006067 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01006069 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 }
Janos Follath844eb0e2019-06-19 12:10:49 +01006071
6072 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01006074 output += length;
6075 output_length -= length;
6076 tls12_prf->left_in_block -= length;
6077 }
6078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01006080}
John Durkop07cc04a2020-11-16 22:08:34 -08006081#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6082 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02006083
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006084#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6085static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
6086 psa_tls12_ecjpake_to_pms_t *ecjpake,
6087 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006089{
6090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006091 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (output_length != 32) {
6094 return PSA_ERROR_INVALID_ARGUMENT;
6095 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
6098 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
6099 &output_size);
6100 if (status != PSA_SUCCESS) {
6101 return status;
6102 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (output_size != output_length) {
6105 return PSA_ERROR_GENERIC_ERROR;
6106 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006109}
6110#endif
6111
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306112#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306113static psa_status_t psa_key_derivation_pbkdf2_generate_block(
6114 psa_pbkdf2_key_derivation_t *pbkdf2,
6115 psa_algorithm_t prf_alg,
6116 uint8_t prf_output_length,
6117 psa_key_attributes_t *attributes)
6118{
6119 psa_status_t status;
Gilles Peskinebbec1c12025-02-05 19:53:28 +01006120 psa_mac_operation_t mac_operation;
6121 /* Make sure the whole the operation is zeroed.
6122 * PSA_MAC_OPERATION_INIT does not necessarily do it fully,
6123 * since one field is a union and initializing a union does not
6124 * necessarily initialize all of its members.
6125 * psa_mac_setup() would do it, but here we bypass it and call
6126 * lower-level functions directly. */
6127 memset(&mac_operation, 0, sizeof(mac_operation));
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306128 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306129 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306130 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306131 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306132 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306133
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306134 mac_operation.is_sign = 1;
6135 mac_operation.mac_size = prf_output_length;
6136 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306137
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306138 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
6139 attributes,
6140 pbkdf2->password,
6141 pbkdf2->password_length,
6142 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306143 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306144 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306145 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306146 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
6147 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306148 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306149 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306150 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306151 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306152 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306153 }
6154 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
6155 &mac_output_length);
6156 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306157 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306158 }
6159
6160 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306161 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306162 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306163 }
6164
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306165 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306166
6167 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306168 /* We are passing prf_output_length as mac_size because the driver
6169 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306170 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306171 status = psa_driver_wrapper_mac_compute(attributes,
6172 pbkdf2->password,
6173 pbkdf2->password_length,
6174 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306175 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306176 &mac_output_length);
6177 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306178 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306179 }
6180
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05306181 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306182 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306183
6184cleanup:
6185 /* Zeroise buffers to clear sensitive data from memory. */
6186 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
6187 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306188}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306189
6190static psa_status_t psa_key_derivation_pbkdf2_read(
6191 psa_pbkdf2_key_derivation_t *pbkdf2,
6192 psa_algorithm_t kdf_alg,
6193 uint8_t *output,
6194 size_t output_length)
6195{
6196 psa_status_t status;
6197 psa_algorithm_t prf_alg;
6198 uint8_t prf_output_length;
6199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6200 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
6201 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
6202
6203 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6204 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
6205 prf_output_length = PSA_HASH_LENGTH(prf_alg);
6206 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306207 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6208 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306209 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306210 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05306211 } else {
6212 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306213 }
6214
6215 switch (pbkdf2->state) {
6216 case PSA_PBKDF2_STATE_PASSWORD_SET:
6217 /* Initially we need a new block so bytes_used is equal to block size*/
6218 pbkdf2->bytes_used = prf_output_length;
6219 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
6220 break;
6221 case PSA_PBKDF2_STATE_OUTPUT:
6222 break;
6223 default:
6224 return PSA_ERROR_BAD_STATE;
6225 }
6226
6227 while (output_length != 0) {
6228 uint8_t n = prf_output_length - pbkdf2->bytes_used;
6229 if (n > output_length) {
6230 n = (uint8_t) output_length;
6231 }
6232 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
6233 output += n;
6234 output_length -= n;
6235 pbkdf2->bytes_used += n;
6236
6237 if (output_length == 0) {
6238 break;
6239 }
6240
6241 /* We need a new block */
6242 pbkdf2->bytes_used = 0;
6243 pbkdf2->block_number++;
6244
6245 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
6246 prf_output_length,
6247 &attributes);
6248 if (status != PSA_SUCCESS) {
6249 return status;
6250 }
6251 }
6252
6253 return PSA_SUCCESS;
6254}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306255#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306256
Janos Follath6c6c8fc2019-06-17 12:38:20 +01006257psa_status_t psa_key_derivation_output_bytes(
6258 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00006259 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006261{
6262 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00006263 LOCAL_OUTPUT_DECLARE(output_external, output);
6264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006268 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006270 }
6271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006273 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006274 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02006275 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
6276 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006277 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02006278 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006280 }
Ryan Everettf943e222024-01-19 14:46:39 +00006281
6282 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00006283 if (output_length > operation->capacity) {
6284 operation->capacity = 0;
6285 /* Go through the error path to wipe all confidential data now
6286 * that the operation object is useless. */
6287 status = PSA_ERROR_INSUFFICIENT_DATA;
6288 goto exit;
6289 }
6290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006291 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006292
Przemek Stekiel69c46792022-06-10 12:59:51 +02006293#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6295 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6296 output, output_length);
6297 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006298#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006299#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6300 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6302 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6303 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6304 kdf_alg, output,
6305 output_length);
6306 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006307#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6308 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006309#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006311 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6313 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006314#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306315#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306316 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306317 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6318 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306319 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306320#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006321
Gilles Peskineeab56e42018-07-12 17:12:33 +02006322 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006323 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006324 status = PSA_ERROR_BAD_STATE;
6325 LOCAL_OUTPUT_FREE(output_external, output);
6326
6327 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006328 }
6329
6330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006332 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006333 * This allows us to differentiate between exhausted operations and
6334 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6335 * operations. */
6336 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006338 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006339 if (output != NULL) {
6340 memset(output, '!', output_length);
6341 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006342 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006343
6344 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006346}
6347
David Brown7807bf72021-02-09 16:28:23 -07006348#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006349static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006350{
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 if (data_size >= 8) {
6352 mbedtls_des_key_set_parity(data);
6353 }
6354 if (data_size >= 16) {
6355 mbedtls_des_key_set_parity(data + 8);
6356 }
6357 if (data_size >= 24) {
6358 mbedtls_des_key_set_parity(data + 16);
6359 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006360}
David Brown7807bf72021-02-09 16:28:23 -07006361#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006362
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006363/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 * ECC keys on a Weierstrass elliptic curve require the generation
6365 * of a private key which is an integer
6366 * in the range [1, N - 1], where N is the boundary of the private key domain:
6367 * N is the prime p for Diffie-Hellman, or the order of the
6368 * curve’s base point for ECC.
6369 *
6370 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6371 * This function generates the private key using the following process:
6372 *
6373 * 1. Draw a byte string of length ceiling(m/8) bytes.
6374 * 2. If m is not a multiple of 8, set the most significant
6375 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6376 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6377 * 4. If k > N - 2, discard the result and return to step 1.
6378 * 5. Output k + 1 as the private key.
6379 *
6380 * This method allows compliance to NIST standards, specifically the methods titled
6381 * Key-Pair Generation by Testing Candidates in the following publications:
6382 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6383 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6384 * Diffie-Hellman keys.
6385 *
6386 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6387 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6388 *
6389 * Note: Function allocates memory for *data buffer, so given *data should be
6390 * always NULL.
6391 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006392#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6393#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006394static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006395 psa_key_slot_t *slot,
6396 size_t bits,
6397 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006398 uint8_t **data
6399 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006400{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006401 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006402 mbedtls_mpi k;
6403 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006406 size_t m;
Gilles Peskinece726b22025-03-06 19:27:50 +01006407 size_t m_bytes = 0;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 mbedtls_mpi_init(&k);
6410 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006411
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006412 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006414 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006415 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006418 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006419 goto cleanup;
6420 }
6421
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006422 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006426
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006427 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006428 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006429 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006430
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006431 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006432
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006433 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006435
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006436 /* Note: This function is always called with *data == NULL and it
6437 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 *data = mbedtls_calloc(1, m_bytes);
6439 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006440 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006441 goto cleanup;
6442 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006445 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006447 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006449
6450 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006452 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006453 * (8 * ceiling(m/8) - m) bits of the first byte in
6454 * the string to zero.
6455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006457 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006458 }
6459
6460 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 * big-endian byte string.
6462 */
6463 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006464
6465 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 * Result of comparison is returned. When it indicates error
6467 * then this function is called again.
6468 */
6469 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006470 }
6471
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006472 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6474 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006475cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 if (ret != 0) {
6477 status = mbedtls_to_psa_error(ret);
6478 }
6479 if (status != PSA_SUCCESS) {
Gilles Peskine184cac12025-03-06 12:42:30 +01006480 mbedtls_zeroize_and_free(*data, m_bytes);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006481 *data = NULL;
6482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 mbedtls_mpi_free(&k);
6484 mbedtls_mpi_free(&diff_N_2);
6485 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006486}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006487
6488/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6489 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6490 *
6491 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6492 * draw a 32-byte string and process it as specified in
6493 * Elliptic Curves for Security [RFC7748] §5.
6494 *
6495 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6496 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6497 *
6498 * Note: Function allocates memory for *data buffer, so given *data should be
6499 * always NULL.
6500 */
6501
6502static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6503 size_t bits,
6504 psa_key_derivation_operation_t *operation,
6505 uint8_t **data
6506 )
6507{
6508 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006512 case 255:
6513 output_length = 32;
6514 break;
6515 case 448:
6516 output_length = 56;
6517 break;
6518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006520 break;
6521 }
6522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 if (*data == NULL) {
6526 return PSA_ERROR_INSUFFICIENT_MEMORY;
6527 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006532 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006536 case 255:
6537 (*data)[0] &= 248;
6538 (*data)[31] &= 127;
6539 (*data)[31] |= 64;
6540 break;
6541 case 448:
6542 (*data)[0] &= 252;
6543 (*data)[55] |= 128;
6544 break;
6545 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006547 break;
6548 }
6549
6550 return status;
6551}
Valerio Setti86587ab2023-06-27 13:09:30 +02006552#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6553static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6554 psa_key_slot_t *slot, size_t bits,
6555 psa_key_derivation_operation_t *operation, uint8_t **data)
6556{
6557 (void) slot;
6558 (void) bits;
6559 (void) operation;
6560 (void) data;
6561 return PSA_ERROR_NOT_SUPPORTED;
6562}
6563
6564static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6565 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6566{
6567 (void) bits;
6568 (void) operation;
6569 (void) data;
6570 return PSA_ERROR_NOT_SUPPORTED;
6571}
6572#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6573#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006574
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006575static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006576 psa_key_slot_t *slot,
6577 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006579{
6580 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306582 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006584
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6586 return PSA_ERROR_INVALID_ARGUMENT;
6587 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006588
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006589#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006590 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6592 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6593 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006594 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6596 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 }
6599 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006600 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6602 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006605 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006606 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006607#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006608 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 if (key_type_is_raw_bytes(slot->attr.type)) {
6610 if (bits % 8 != 0) {
6611 return PSA_ERROR_INVALID_ARGUMENT;
6612 }
6613 data = mbedtls_calloc(1, bytes);
6614 if (data == NULL) {
6615 return PSA_ERROR_INSUFFICIENT_MEMORY;
6616 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006617
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 status = psa_key_derivation_output_bytes(operation, data, bytes);
6619 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 }
David Brown12ca5032021-02-11 11:02:00 -07006622#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6624 psa_des_set_key_parity(data, bytes);
6625 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006626#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 } else {
6628 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006629 }
Ronald Crondd04d422020-11-28 15:14:42 +01006630
Ronald Cronbf33c932020-11-28 18:06:53 +01006631 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006632
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006633 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006634 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 &storage_size);
6636 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 }
Archanad8a83dc2021-06-14 10:04:16 +05306639 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 status = psa_allocate_buffer_to_slot(slot, storage_size);
6641 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 }
Archanad8a83dc2021-06-14 10:04:16 +05306644
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006645 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 data, bytes,
6647 slot->key.data,
6648 slot->key.bytes,
6649 &slot->key.bytes, &bits);
6650 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006651 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006653
6654exit:
Gilles Peskine184cac12025-03-06 12:42:30 +01006655 mbedtls_zeroize_and_free(data, bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006657}
6658
Gilles Peskinef36d7852024-06-06 21:11:44 +02006659static const psa_custom_key_parameters_t default_custom_production =
Gilles Peskine4a85ff32024-07-18 18:52:59 +02006660 PSA_CUSTOM_KEY_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006661
Gilles Peskine52504f82024-06-20 17:19:58 +02006662int psa_custom_key_parameters_are_default(
Gilles Peskinef36d7852024-06-06 21:11:44 +02006663 const psa_custom_key_parameters_t *custom,
6664 size_t custom_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006665{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006666 if (custom->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006667 return 0;
6668 }
Gilles Peskinef36d7852024-06-06 21:11:44 +02006669 if (custom_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006670 return 0;
6671 }
6672 return 1;
6673}
6674
Gilles Peskinef36d7852024-06-06 21:11:44 +02006675psa_status_t psa_key_derivation_output_key_custom(
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006676 const psa_key_attributes_t *attributes,
6677 psa_key_derivation_operation_t *operation,
Gilles Peskinef36d7852024-06-06 21:11:44 +02006678 const psa_custom_key_parameters_t *custom,
6679 const uint8_t *custom_data,
6680 size_t custom_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006681 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006682{
6683 psa_status_t status;
6684 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006685 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006686
Ronald Cron81709fc2020-11-14 12:10:32 +01006687 *key = MBEDTLS_SVC_KEY_ID_INIT;
6688
Gilles Peskine0f84d622019-09-12 19:03:13 +02006689 /* Reject any attempt to create a zero-length key so that we don't
6690 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (psa_get_key_bits(attributes) == 0) {
6692 return PSA_ERROR_INVALID_ARGUMENT;
6693 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006694
Gilles Peskinef36d7852024-06-06 21:11:44 +02006695 (void) custom_data; /* We only accept 0-length data */
Gilles Peskine52504f82024-06-20 17:19:58 +02006696 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006697 return PSA_ERROR_INVALID_ARGUMENT;
6698 }
6699
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 if (operation->alg == PSA_ALG_NONE) {
6701 return PSA_ERROR_BAD_STATE;
6702 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (!operation->can_output_key) {
6705 return PSA_ERROR_NOT_PERMITTED;
6706 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006707
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6709 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006710#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006712 /* Deriving a key in a secure element is not implemented yet. */
6713 status = PSA_ERROR_NOT_SUPPORTED;
6714 }
6715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 if (status == PSA_SUCCESS) {
6717 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006718 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006720 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 if (status == PSA_SUCCESS) {
6722 status = psa_finish_key_creation(slot, driver, key);
6723 }
6724 if (status != PSA_SUCCESS) {
6725 psa_fail_key_creation(slot, driver);
6726 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006729}
6730
Gilles Peskinef36d7852024-06-06 21:11:44 +02006731psa_status_t psa_key_derivation_output_key_ext(
6732 const psa_key_attributes_t *attributes,
6733 psa_key_derivation_operation_t *operation,
6734 const psa_key_production_parameters_t *params,
6735 size_t params_data_length,
6736 mbedtls_svc_key_id_t *key)
6737{
6738 return psa_key_derivation_output_key_custom(
6739 attributes, operation,
6740 (const psa_custom_key_parameters_t *) params,
6741 params->data, params_data_length,
6742 key);
6743}
6744
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006745psa_status_t psa_key_derivation_output_key(
6746 const psa_key_attributes_t *attributes,
6747 psa_key_derivation_operation_t *operation,
6748 mbedtls_svc_key_id_t *key)
6749{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006750 return psa_key_derivation_output_key_custom(attributes, operation,
6751 &default_custom_production,
6752 NULL, 0,
6753 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006754}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006755
6756
6757/****************************************************************/
Gilles Peskinecdb37812025-02-05 18:02:49 +01006758/* Key derivation: operation management */
Gilles Peskineea0fb492018-07-12 17:17:20 +02006759/****************************************************************/
6760
Steven Cooreman932ffb72021-02-15 12:14:32 +01006761#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006762static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006763{
6764#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6766 return 1;
6767 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006768#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006769#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6771 return 1;
6772 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006773#endif
6774#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6776 return 1;
6777 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006778#endif
6779#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6781 return 1;
6782 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006783#endif
6784#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6786 return 1;
6787 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006788#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006789#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6791 return 1;
6792 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006793#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306794#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6795 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6796 return 1;
6797 }
6798#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306799#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6800 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6801 return 1;
6802 }
6803#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006805}
6806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006808{
6809 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 psa_status_t status = psa_hash_setup(&operation, alg);
6811 psa_hash_abort(&operation);
6812 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006813}
6814
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306815static psa_status_t psa_key_derivation_set_maximum_capacity(
6816 psa_key_derivation_operation_t *operation,
6817 psa_algorithm_t kdf_alg)
6818{
6819#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6820 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6821 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6822 return PSA_SUCCESS;
6823 }
6824#endif
6825#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6826 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6827#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306828 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306829 PSA_KEY_TYPE_AES,
6830 128U,
6831 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306832#else
6833 operation->capacity = SIZE_MAX;
6834#endif
6835 return PSA_SUCCESS;
6836 }
6837#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6838
6839 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6840 * invalid or meaningless but it does not affect this function */
6841 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6842 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306843 if (hash_size == 0) {
6844 return PSA_ERROR_NOT_SUPPORTED;
6845 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306846
6847 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6848 * we might fail later, which is somewhat unfriendly and potentially
6849 * risk-prone. */
6850 psa_status_t status = psa_hash_try_support(hash_alg);
6851 if (status != PSA_SUCCESS) {
6852 return status;
6853 }
6854
6855#if defined(PSA_WANT_ALG_HKDF)
6856 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6857 operation->capacity = 255 * hash_size;
6858 } else
6859#endif
6860#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6861 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6862 operation->capacity = hash_size;
6863 } else
6864#endif
6865#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6866 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6867 operation->capacity = 255 * hash_size;
6868 } else
6869#endif
6870#if defined(PSA_WANT_ALG_TLS12_PRF)
6871 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6872 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6873 operation->capacity = SIZE_MAX;
6874 } else
6875#endif
6876#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6877 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6878 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6879 /* Master Secret is always 48 bytes
6880 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6881 operation->capacity = 48U;
6882 } else
6883#endif
6884#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6885 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6886#if (SIZE_MAX > UINT32_MAX)
6887 operation->capacity = UINT32_MAX * hash_size;
6888#else
6889 operation->capacity = SIZE_MAX;
6890#endif
6891 } else
6892#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6893 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306894 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306895 status = PSA_ERROR_NOT_SUPPORTED;
6896 }
6897 return status;
6898}
6899
Gilles Peskine969c5d62019-01-16 15:53:06 +01006900static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006901 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006903{
Janos Follath5fe19732019-06-20 15:09:30 +01006904 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6905 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006907
Gilles Peskine969c5d62019-01-16 15:53:06 +01006908 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (!is_kdf_alg_supported(kdf_alg)) {
6910 return PSA_ERROR_NOT_SUPPORTED;
6911 }
John Durkop07cc04a2020-11-16 22:08:34 -08006912
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306913 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6914 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306915 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006916}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006917
Gilles Peskine449bd832023-01-11 14:50:10 +01006918static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006919{
6920#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (alg == PSA_ALG_ECDH) {
6922 return PSA_SUCCESS;
6923 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006924#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006925#if defined(PSA_WANT_ALG_FFDH)
6926 if (alg == PSA_ALG_FFDH) {
6927 return PSA_SUCCESS;
6928 }
6929#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006930 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006932}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006933
6934static int psa_key_derivation_allows_free_form_secret_input(
6935 psa_algorithm_t kdf_alg)
6936{
6937#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6938 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6939 return 0;
6940 }
6941#endif
6942 (void) kdf_alg;
6943 return 1;
6944}
John Durkop07cc04a2020-11-16 22:08:34 -08006945#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6948 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006949{
6950 psa_status_t status;
6951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (operation->alg != 0) {
6953 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006954 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6957 return PSA_ERROR_INVALID_ARGUMENT;
6958 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6959#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6960 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6961 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6962 status = psa_key_agreement_try_support(ka_alg);
6963 if (status != PSA_SUCCESS) {
6964 return status;
6965 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006966 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6967 return PSA_ERROR_INVALID_ARGUMENT;
6968 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6970#else
6971 return PSA_ERROR_NOT_SUPPORTED;
6972#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6973 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6974#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6975 status = psa_key_derivation_setup_kdf(operation, alg);
6976#else
6977 return PSA_ERROR_NOT_SUPPORTED;
6978#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6979 } else {
6980 return PSA_ERROR_INVALID_ARGUMENT;
6981 }
6982
6983 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006984 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 }
6986 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006987}
6988
Przemek Stekiel69c46792022-06-10 12:59:51 +02006989#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006990static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6991 psa_algorithm_t kdf_alg,
6992 psa_key_derivation_step_t step,
6993 const uint8_t *data,
6994 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006995{
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006997 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006999 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007000#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
7002 return PSA_ERROR_INVALID_ARGUMENT;
7003 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007004#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (hkdf->state != HKDF_STATE_INIT) {
7006 return PSA_ERROR_BAD_STATE;
7007 } else {
7008 status = psa_key_derivation_start_hmac(&hkdf->hmac,
7009 hash_alg,
7010 data, data_length);
7011 if (status != PSA_SUCCESS) {
7012 return status;
7013 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02007014 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02007016 }
Gilles Peskine03410b52019-05-16 16:05:19 +02007017 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007018#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02007020 /* We shouldn't be in different state as HKDF_EXPAND only allows
7021 * two inputs: SECRET (this case) and INFO which does not modify
7022 * the state. It could happen only if the hkdf
7023 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (hkdf->state != HKDF_STATE_INIT) {
7025 return PSA_ERROR_BAD_STATE;
7026 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007027
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02007028 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
7030 return PSA_ERROR_INVALID_ARGUMENT;
7031 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007032
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 memcpy(hkdf->prk, data, data_length);
7034 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007035#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007036 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007037 /* HKDF: If no salt was provided, use an empty salt.
7038 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007040#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
7042 return PSA_ERROR_BAD_STATE;
7043 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007044#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 status = psa_key_derivation_start_hmac(&hkdf->hmac,
7046 hash_alg,
7047 NULL, 0);
7048 if (status != PSA_SUCCESS) {
7049 return status;
7050 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007051 hkdf->state = HKDF_STATE_STARTED;
7052 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 if (hkdf->state != HKDF_STATE_STARTED) {
7054 return PSA_ERROR_BAD_STATE;
7055 }
7056 status = psa_mac_update(&hkdf->hmac,
7057 data, data_length);
7058 if (status != PSA_SUCCESS) {
7059 return status;
7060 }
7061 status = psa_mac_sign_finish(&hkdf->hmac,
7062 hkdf->prk,
7063 sizeof(hkdf->prk),
7064 &data_length);
7065 if (status != PSA_SUCCESS) {
7066 return status;
7067 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007068 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007069
Gilles Peskine2b522db2019-04-12 15:11:49 +02007070 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007071 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007072#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007074 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007076 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007078#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007079 {
7080 /* Block 0 is empty, and the next block will be
7081 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007083 }
7084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02007086 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
7089 return PSA_ERROR_INVALID_ARGUMENT;
7090 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007091#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02007092#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
7094 hkdf->state == HKDF_STATE_INIT) {
7095 return PSA_ERROR_BAD_STATE;
7096 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02007097#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (hkdf->state == HKDF_STATE_OUTPUT) {
7099 return PSA_ERROR_BAD_STATE;
7100 }
7101 if (hkdf->info_set) {
7102 return PSA_ERROR_BAD_STATE;
7103 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007104 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 if (data_length != 0) {
7106 hkdf->info = mbedtls_calloc(1, data_length);
7107 if (hkdf->info == NULL) {
7108 return PSA_ERROR_INSUFFICIENT_MEMORY;
7109 }
7110 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007111 }
7112 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007114 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007116 }
7117}
Przemek Stekiel69c46792022-06-10 12:59:51 +02007118#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01007119
John Durkop07cc04a2020-11-16 22:08:34 -08007120#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
7121 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007122static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
7123 const uint8_t *data,
7124 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01007125{
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
7127 return PSA_ERROR_BAD_STATE;
7128 }
Janos Follathf08e2652019-06-13 09:05:41 +01007129
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (data_length != 0) {
7131 prf->seed = mbedtls_calloc(1, data_length);
7132 if (prf->seed == NULL) {
7133 return PSA_ERROR_INSUFFICIENT_MEMORY;
7134 }
Janos Follathf08e2652019-06-13 09:05:41 +01007135
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007137 prf->seed_length = data_length;
7138 }
Janos Follathf08e2652019-06-13 09:05:41 +01007139
Gilles Peskine43f958b2020-12-13 14:55:14 +01007140 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01007141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01007143}
7144
Gilles Peskine449bd832023-01-11 14:50:10 +01007145static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
7146 const uint8_t *data,
7147 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01007148{
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
7150 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7151 return PSA_ERROR_BAD_STATE;
7152 }
Janos Follath81550542019-06-13 14:26:34 +01007153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (data_length != 0) {
7155 prf->secret = mbedtls_calloc(1, data_length);
7156 if (prf->secret == NULL) {
7157 return PSA_ERROR_INSUFFICIENT_MEMORY;
7158 }
Steven Cooremana6df6042021-04-29 19:32:25 +02007159
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02007161 prf->secret_length = data_length;
7162 }
Janos Follath81550542019-06-13 14:26:34 +01007163
Gilles Peskine43f958b2020-12-13 14:55:14 +01007164 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01007165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01007167}
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
7170 const uint8_t *data,
7171 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01007172{
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
7174 return PSA_ERROR_BAD_STATE;
7175 }
Janos Follath63028dd2019-06-13 09:15:47 +01007176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 if (data_length != 0) {
7178 prf->label = mbedtls_calloc(1, data_length);
7179 if (prf->label == NULL) {
7180 return PSA_ERROR_INSUFFICIENT_MEMORY;
7181 }
Janos Follath63028dd2019-06-13 09:15:47 +01007182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007184 prf->label_length = data_length;
7185 }
Janos Follath63028dd2019-06-13 09:15:47 +01007186
Gilles Peskine43f958b2020-12-13 14:55:14 +01007187 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01007188
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01007190}
7191
Gilles Peskine449bd832023-01-11 14:50:10 +01007192static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
7193 psa_key_derivation_step_t step,
7194 const uint8_t *data,
7195 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01007196{
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01007198 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01007200 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01007202 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01007204 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01007206 }
7207}
John Durkop07cc04a2020-11-16 22:08:34 -08007208#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
7209 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
7210
7211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
7212static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
7213 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08007214 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08007216{
7217 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
7219 4 + data_length + prf->other_secret_length :
7220 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007221
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
7223 return PSA_ERROR_INVALID_ARGUMENT;
7224 }
John Durkop07cc04a2020-11-16 22:08:34 -08007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 uint8_t *pms = mbedtls_calloc(1, pms_len);
7227 if (pms == NULL) {
7228 return PSA_ERROR_INSUFFICIENT_MEMORY;
7229 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007230 uint8_t *cur = pms;
7231
7232 /* pure-PSK:
7233 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08007234 *
7235 * The premaster secret is formed as follows: if the PSK is N octets
7236 * long, concatenate a uint16 with the value N, N zero octets, a second
7237 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007238 *
7239 * mixed-PSK:
7240 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
7241 * follows: concatenate a uint16 with the length of the other secret,
7242 * the other secret itself, uint16 with the length of PSK, and the
7243 * PSK itself.
7244 * For details please check:
7245 * - RFC 4279, Section 4 for the definition of RSA-PSK,
7246 * - RFC 4279, Section 3 for the definition of DHE-PSK,
7247 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08007248 */
7249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7251 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
7252 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
7253 if (prf->other_secret_length != 0) {
7254 memcpy(cur, prf->other_secret, prf->other_secret_length);
7255 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02007256 cur += prf->other_secret_length;
7257 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 } else {
7259 *cur++ = MBEDTLS_BYTE_1(data_length);
7260 *cur++ = MBEDTLS_BYTE_0(data_length);
7261 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007262 cur += data_length;
7263 }
7264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 *cur++ = MBEDTLS_BYTE_1(data_length);
7266 *cur++ = MBEDTLS_BYTE_0(data_length);
7267 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007268 cur += data_length;
7269
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007270 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08007271
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007272 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08007274}
Janos Follath6660f0e2019-06-17 08:44:03 +01007275
Przemek Stekiele47201b2022-04-19 13:53:28 +02007276static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
7277 psa_tls12_prf_key_derivation_t *prf,
7278 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02007280{
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
7282 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007283 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007284
7285 if (data_length != 0) {
7286 prf->other_secret = mbedtls_calloc(1, data_length);
7287 if (prf->other_secret == NULL) {
7288 return PSA_ERROR_INSUFFICIENT_MEMORY;
7289 }
7290
7291 memcpy(prf->other_secret, data, data_length);
7292 prf->other_secret_length = data_length;
7293 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007294 prf->other_secret_length = 0;
7295 }
7296
7297 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
7298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007300}
7301
Janos Follath6660f0e2019-06-17 08:44:03 +01007302static psa_status_t psa_tls12_prf_psk_to_ms_input(
7303 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01007304 psa_key_derivation_step_t step,
7305 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01007307{
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007309 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 return psa_tls12_prf_psk_to_ms_set_key(prf,
7311 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007312 break;
7313 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7315 data,
7316 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007317 break;
7318 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007320 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007321
Przemek Stekiele47201b2022-04-19 13:53:28 +02007322 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007323}
John Durkop07cc04a2020-11-16 22:08:34 -08007324#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007325
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007326#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7327static psa_status_t psa_tls12_ecjpake_to_pms_input(
7328 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007329 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007330 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007332{
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7334 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7335 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007336 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007337
7338 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 if (data[0] != 0x04) {
7340 return PSA_ERROR_INVALID_ARGUMENT;
7341 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007342
7343 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007345
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007347}
7348#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307349
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307350#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307351static psa_status_t psa_pbkdf2_set_input_cost(
7352 psa_pbkdf2_key_derivation_t *pbkdf2,
7353 psa_key_derivation_step_t step,
7354 uint64_t data)
7355{
7356 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7357 return PSA_ERROR_INVALID_ARGUMENT;
7358 }
7359
7360 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7361 return PSA_ERROR_BAD_STATE;
7362 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307363
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307364 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307365 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307366 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307367
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307368 if (data == 0) {
7369 return PSA_ERROR_INVALID_ARGUMENT;
7370 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307371
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307372 pbkdf2->input_cost = data;
7373 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7374
7375 return PSA_SUCCESS;
7376}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307377
7378static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7379 const uint8_t *data,
7380 size_t data_length)
7381{
Gilles Peskineead17662023-08-20 22:02:51 +02007382 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7383 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7384 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7385 /* Appending to existing salt. No state change. */
7386 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307387 return PSA_ERROR_BAD_STATE;
7388 }
7389
Gilles Peskineca57d782023-07-20 19:08:06 +02007390 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007391 /* Appending an empty string, nothing to do. */
7392 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307393 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307394
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307395 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7396 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307397 return PSA_ERROR_INSUFFICIENT_MEMORY;
7398 }
7399
Gilles Peskineead17662023-08-20 22:02:51 +02007400 if (pbkdf2->salt_length != 0) {
7401 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7402 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307403 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307404 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307405 mbedtls_free(pbkdf2->salt);
7406 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307407 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307408 return PSA_SUCCESS;
7409}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307410
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307411#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307412static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307413 const uint8_t *input,
7414 size_t input_len,
7415 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307416 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307417{
7418 psa_status_t status = PSA_SUCCESS;
7419 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007420 return psa_hash_compute(hash_alg, input, input_len, output,
7421 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7422 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307423 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307424 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007425 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307426 return status;
7427}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307428#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307429
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307430#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307431static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7432 size_t input_len,
7433 uint8_t *output,
7434 size_t *output_len)
7435{
7436 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307437 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307439 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307440 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7441 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7442 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307443 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307444 * mac_size as the driver function sets mac_output_length = mac_size
7445 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307446 status = psa_driver_wrapper_mac_compute(&attributes,
7447 zeros, sizeof(zeros),
7448 PSA_ALG_CMAC, input, input_len,
7449 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307450 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7451 128U,
7452 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307453 output_len);
7454 } else {
7455 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307456 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307457 }
7458 return status;
7459}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307460#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307461
7462static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307463 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307464 const uint8_t *data,
7465 size_t data_length)
7466{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307467 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307468 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7469 return PSA_ERROR_BAD_STATE;
7470 }
7471
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307472#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307473 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7474 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7475 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7476 pbkdf2->password,
7477 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307478 } else
7479#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7480#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7481 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307482 status = psa_pbkdf2_cmac_set_password(data, data_length,
7483 pbkdf2->password,
7484 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307485 } else
7486#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7487 {
7488 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307489 }
7490
7491 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7492
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307493 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307494}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307495
7496static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307497 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307498 psa_key_derivation_step_t step,
7499 const uint8_t *data,
7500 size_t data_length)
7501{
7502 switch (step) {
7503 case PSA_KEY_DERIVATION_INPUT_SALT:
7504 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7505 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307506 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307507 default:
7508 return PSA_ERROR_INVALID_ARGUMENT;
7509 }
7510}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307511#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307512
Gilles Peskineb8965192019-09-24 16:21:10 +02007513/** Check whether the given key type is acceptable for the given
7514 * input step of a key derivation.
7515 *
7516 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7517 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7518 * Both secret and non-secret inputs can alternatively have the type
7519 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7520 * that the input was passed as a buffer rather than via a key object.
7521 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007522static int psa_key_derivation_check_input_type(
7523 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007525{
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007527 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 if (key_type == PSA_KEY_TYPE_DERIVE) {
7529 return PSA_SUCCESS;
7530 }
7531 if (key_type == PSA_KEY_TYPE_NONE) {
7532 return PSA_SUCCESS;
7533 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007534 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007535 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 if (key_type == PSA_KEY_TYPE_DERIVE) {
7537 return PSA_SUCCESS;
7538 }
7539 if (key_type == PSA_KEY_TYPE_NONE) {
7540 return PSA_SUCCESS;
7541 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007542 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007543 case PSA_KEY_DERIVATION_INPUT_LABEL:
7544 case PSA_KEY_DERIVATION_INPUT_SALT:
7545 case PSA_KEY_DERIVATION_INPUT_INFO:
7546 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7548 return PSA_SUCCESS;
7549 }
7550 if (key_type == PSA_KEY_TYPE_NONE) {
7551 return PSA_SUCCESS;
7552 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007553 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307554 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7555 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7556 return PSA_SUCCESS;
7557 }
7558 if (key_type == PSA_KEY_TYPE_DERIVE) {
7559 return PSA_SUCCESS;
7560 }
7561 if (key_type == PSA_KEY_TYPE_NONE) {
7562 return PSA_SUCCESS;
7563 }
7564 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007565 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007567}
7568
Janos Follathb80a94e2019-06-12 15:54:46 +01007569static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007570 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007571 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007572 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007573 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007575{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007576 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007578
Waleed Elmelegy443908b2025-03-10 14:19:01 +00007579 if (kdf_alg == PSA_ALG_NONE) {
Waleed Elmelegy82cd3242025-03-03 15:04:17 +00007580 /* This is a blank or aborted operation. */
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007581 status = PSA_ERROR_BAD_STATE;
7582 goto exit;
7583 }
7584
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 status = psa_key_derivation_check_input_type(step, key_type);
7586 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007587 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007589
Przemek Stekiel69c46792022-06-10 12:59:51 +02007590#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7592 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7593 step, data, data_length);
7594 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007595#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007596#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7598 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7599 step, data, data_length);
7600 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007601#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7602#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7604 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7605 step, data, data_length);
7606 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007607#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007608#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007610 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7612 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007613#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307614#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307615 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307616 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7617 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307618 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307619#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007620 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007621 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007622 (void) data;
7623 (void) data_length;
7624 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007626 }
7627
Gilles Peskine224b0d62019-09-23 18:13:17 +02007628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 if (status != PSA_SUCCESS) {
7630 psa_key_derivation_abort(operation);
7631 }
7632 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007633}
7634
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307635static psa_status_t psa_key_derivation_input_integer_internal(
7636 psa_key_derivation_operation_t *operation,
7637 psa_key_derivation_step_t step,
7638 uint64_t value)
7639{
7640 psa_status_t status;
7641 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7642
Waleed Elmelegy443908b2025-03-10 14:19:01 +00007643 if (kdf_alg == PSA_ALG_NONE) {
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007644 /* This is a blank or aborted operation. */
7645 status = PSA_ERROR_BAD_STATE;
7646 goto exit;
7647 }
7648
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307649#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307650 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307651 status = psa_pbkdf2_set_input_cost(
7652 &operation->ctx.pbkdf2, step, value);
7653 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307654#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307655 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307656 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307657 (void) value;
7658 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307659 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307660 }
7661
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007662exit:
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307663 if (status != PSA_SUCCESS) {
7664 psa_key_derivation_abort(operation);
7665 }
7666 return status;
7667}
7668
Janos Follath51f4a0f2019-06-14 11:35:55 +01007669psa_status_t psa_key_derivation_input_bytes(
7670 psa_key_derivation_operation_t *operation,
7671 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007672 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007674{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007675 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7676 LOCAL_INPUT_DECLARE(data_external, data);
7677
7678 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7679
7680 status = psa_key_derivation_input_internal(operation, step,
7681 PSA_KEY_TYPE_NONE,
7682 data, data_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00007683#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007684exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007685#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007686 LOCAL_INPUT_FREE(data_external, data);
7687 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007688}
7689
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307690psa_status_t psa_key_derivation_input_integer(
7691 psa_key_derivation_operation_t *operation,
7692 psa_key_derivation_step_t step,
7693 uint64_t value)
7694{
7695 return psa_key_derivation_input_integer_internal(operation, step, value);
7696}
7697
Janos Follath51f4a0f2019-06-14 11:35:55 +01007698psa_status_t psa_key_derivation_input_key(
7699 psa_key_derivation_operation_t *operation,
7700 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007702{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007704 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007705 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007706
Ronald Cron5c522922020-11-14 16:35:34 +01007707 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007708 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7709 if (status != PSA_SUCCESS) {
7710 psa_key_derivation_abort(operation);
7711 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007712 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007713
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307714 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7715 * permission to output to a key object. */
7716 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7717 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007718 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007719 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007720
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 status = psa_key_derivation_input_internal(operation,
7722 step, slot->attr.type,
7723 slot->key.data,
7724 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007725
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007726 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007727
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007729}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007730
7731
7732
7733/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007734/* Key agreement */
7735/****************************************************************/
7736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7738 const uint8_t *key_buffer,
7739 size_t key_buffer_size,
7740 psa_algorithm_t alg,
7741 const uint8_t *peer_key,
7742 size_t peer_key_length,
7743 uint8_t *shared_secret,
7744 size_t shared_secret_size,
7745 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007746{
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007748#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007749 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7751 key_buffer_size, alg,
7752 peer_key, peer_key_length,
7753 shared_secret,
7754 shared_secret_size,
7755 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007756#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007757
7758#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7759 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007760 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007761 peer_key,
7762 peer_key_length,
7763 key_buffer,
7764 key_buffer_size,
7765 shared_secret,
7766 shared_secret_size,
7767 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007768#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7769
Gilles Peskine01d718c2018-09-18 12:01:02 +02007770 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007771 (void) attributes;
7772 (void) key_buffer;
7773 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007774 (void) peer_key;
7775 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007776 (void) shared_secret;
7777 (void) shared_secret_size;
7778 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007780 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007781}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007782
Aditya Deshpande17845b82022-10-13 17:21:01 +01007783/** Internal function for raw key agreement
7784 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007785 * to the driver's implementation if a driver is present.
7786 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007787 * (psa_key_agreement_raw_builtin).
7788 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007789static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7790 psa_key_slot_t *private_key,
7791 const uint8_t *peer_key,
7792 size_t peer_key_length,
7793 uint8_t *shared_secret,
7794 size_t shared_secret_size,
7795 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007796{
Gilles Peskine449bd832023-01-11 14:50:10 +01007797 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7798 return PSA_ERROR_NOT_SUPPORTED;
7799 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007800
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007801 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 private_key->key.data,
7803 private_key->key.bytes, alg,
7804 peer_key, peer_key_length,
7805 shared_secret,
7806 shared_secret_size,
7807 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007808}
7809
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007810/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007811 * to potentially free embedded data structures and wipe confidential data.
7812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007813static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7814 psa_key_derivation_step_t step,
7815 psa_key_slot_t *private_key,
7816 const uint8_t *peer_key,
7817 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007818{
7819 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007820 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007821 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007823
7824 /* Step 1: run the secret agreement algorithm to generate the shared
7825 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 status = psa_key_agreement_raw_internal(ka_alg,
7827 private_key,
7828 peer_key, peer_key_length,
7829 shared_secret,
7830 sizeof(shared_secret),
7831 &shared_secret_length);
7832 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007833 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007835
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007836 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007837 * the shared secret. A shared secret is permitted wherever a key
7838 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 status = psa_key_derivation_input_internal(operation, step,
7840 PSA_KEY_TYPE_DERIVE,
7841 shared_secret,
7842 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7845 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007846}
7847
Gilles Peskine449bd832023-01-11 14:50:10 +01007848psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7849 psa_key_derivation_step_t step,
7850 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007851 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007853{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007855 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007856 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007857 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007858
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7860 return PSA_ERROR_INVALID_ARGUMENT;
7861 }
Ronald Cron5c522922020-11-14 16:35:34 +01007862 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7864 if (status != PSA_SUCCESS) {
7865 return status;
7866 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007867
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007868 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 status = psa_key_agreement_internal(operation, step,
7870 slot,
7871 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007872
David Horstmann4a48bec2024-03-13 15:42:31 +00007873#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007874exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007875#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 if (status != PSA_SUCCESS) {
7877 psa_key_derivation_abort(operation);
7878 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007879 /* If a private key has been added as SECRET, we allow the derived
7880 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007882 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007884 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007885
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007886 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007887 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007890}
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7893 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007894 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007896 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 size_t output_size,
7898 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007899{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007901 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007902 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007903 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007904 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7905 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007906 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007909 status = PSA_ERROR_INVALID_ARGUMENT;
7910 goto exit;
7911 }
Ronald Cron5c522922020-11-14 16:35:34 +01007912 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7914 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007917
Gilles Peskine3e561302022-04-14 00:17:15 +02007918 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7919 * for the output size. The PSA specification only guarantees that this
7920 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7921 * but it might be nice to allow smaller buffers if the output fits.
7922 * At the time of writing this comment, with only ECDH implemented,
7923 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7924 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7925 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007926 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7928 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007929 status = PSA_ERROR_BUFFER_TOO_SMALL;
7930 goto exit;
7931 }
7932
Thomas Daubney81899ab2024-02-15 12:57:26 +00007933 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 status = psa_key_agreement_raw_internal(alg, slot,
7935 peer_key, peer_key_length,
7936 output, output_size,
7937 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007938
7939exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007940 /* Check for successful allocation of output,
7941 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007942 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007943 /* If an error happens and is not handled properly, the output
7944 * may be used as a key to protect sensitive data. Arrange for such
7945 * a key to be random, which is likely to result in decryption or
7946 * verification errors. This is better than filling the buffer with
7947 * some constant data such as zeros, which would result in the data
7948 * being protected with a reproducible, easily knowable key.
7949 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007950 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007951 *output_length = output_size;
7952 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007953
Thomas Daubney5390aca2024-02-22 11:06:04 +00007954 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007955 /* output allocation failed. */
7956 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007957 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007958
Ryan Everetta103ec92024-01-31 13:59:57 +00007959 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007960
Thomas Daubney81899ab2024-02-15 12:57:26 +00007961 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7962 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007964}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007965
7966
7967/****************************************************************/
7968/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007969/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007970
Gilles Peskine672a7712023-04-28 21:00:28 +02007971#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7972#include "entropy_poll.h"
7973#endif
7974
Gilles Peskine30524eb2020-11-13 17:02:26 +01007975/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007976 *
7977 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7978 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007979 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007980static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007981{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007982#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007984#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7985
Gilles Peskine30524eb2020-11-13 17:02:26 +01007986 /* Set default configuration if
7987 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007989 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 }
7991 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007992 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007994
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007996#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7997 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7998 /* The PSA entropy injection feature depends on using NV seed as an entropy
7999 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 mbedtls_entropy_add_source(&rng->entropy,
8001 mbedtls_nv_seed_poll, NULL,
8002 MBEDTLS_ENTROPY_BLOCK_SIZE,
8003 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01008004#endif
8005
Valerio Setti061d4e42024-02-26 12:52:44 +01008006 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008007#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008008}
8009
8010/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00008011 *
8012 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
8013 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01008014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008015static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01008016{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008017#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008019#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01008020 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008022#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008023}
8024
8025/** Seed the PSA random generator.
8026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008027static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01008028{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008029#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
8030 /* Do nothing: the external RNG seeds itself. */
8031 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008033#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008034 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01008035 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 drbg_seed, sizeof(drbg_seed) - 1);
8037 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008038#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008039}
8040
David Horstmann6e99bb22024-02-06 15:27:49 +00008041psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008043{
David Horstmann6e99bb22024-02-06 15:27:49 +00008044 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008045
David Horstmann6e99bb22024-02-06 15:27:49 +00008046 LOCAL_OUTPUT_DECLARE(output_external, output);
8047 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008048
David Horstmann6e99bb22024-02-06 15:27:49 +00008049 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008050
David Horstmann4a48bec2024-03-13 15:42:31 +00008051#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00008052exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00008053#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00008054 LOCAL_OUTPUT_FREE(output_external, output);
8055 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008056}
8057
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01008058#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01008059psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
8060 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008061{
Paul Elliott600472b2024-02-23 17:26:58 +00008062 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008063 return PSA_ERROR_NOT_PERMITTED;
8064 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02008065
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
8067 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
8068 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
8069 return PSA_ERROR_INVALID_ARGUMENT;
8070 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02008071
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008073}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01008074#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008075
Ronald Cron3772afe2021-02-08 16:10:05 +01008076/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02008077 *
Ronald Cron3772afe2021-02-08 16:10:05 +01008078 * \param type The key type
8079 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02008080 *
8081 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01008082 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02008083 * \retval #PSA_ERROR_INVALID_ARGUMENT
8084 * The size in bits of the key is not valid.
8085 * \retval #PSA_ERROR_NOT_SUPPORTED
8086 * The type and/or the size in bits of the key or the combination of
8087 * the two is not supported.
8088 */
Ronald Cron3772afe2021-02-08 16:10:05 +01008089static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008091{
Ronald Cronf3bb7612020-10-02 20:11:59 +02008092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008093
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 if (key_type_is_raw_bytes(type)) {
8095 status = psa_validate_unstructured_key_bit_size(type, bits);
8096 if (status != PSA_SUCCESS) {
8097 return status;
8098 }
8099 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02008100#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8102 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
8103 return PSA_ERROR_NOT_SUPPORTED;
8104 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00008105 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00008106 return PSA_ERROR_NOT_SUPPORTED;
8107 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008108
Ronald Cron01b2aba2020-10-05 09:42:02 +02008109 /* Accept only byte-aligned keys, for the same reasons as
8110 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 if (bits % 8 != 0) {
8112 return PSA_ERROR_NOT_SUPPORTED;
8113 }
8114 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02008115#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008116
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008117#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01008119 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 return PSA_SUCCESS;
8121 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008122#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008123
Valerio Settia55f0422023-07-10 15:34:41 +02008124#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008125 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02008126 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01008127 return PSA_ERROR_NOT_SUPPORTED;
8128 }
8129 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008130#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008131 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008133 }
8134
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008136}
8137
Ronald Cron55ed0592020-10-05 10:30:40 +02008138psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02008139 const psa_key_attributes_t *attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008140 const psa_custom_key_parameters_t *custom,
8141 const uint8_t *custom_data,
8142 size_t custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02008144{
8145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008146 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008147
Gilles Peskine7a18f962024-02-12 16:48:11 +01008148 /* Only used for RSA */
Gilles Peskinef36d7852024-06-06 21:11:44 +02008149 (void) custom;
8150 (void) custom_data;
8151 (void) custom_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008152
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00008154 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008155 if (status != PSA_SUCCESS) {
8156 return status;
8157 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008158
David Brown12ca5032021-02-11 11:02:00 -07008159#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01008160 if (type == PSA_KEY_TYPE_DES) {
8161 psa_des_set_key_parity(key_buffer, key_buffer_size);
8162 }
David Brown8107e312021-02-12 08:08:46 -07008163#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01008165
Valerio Setti76df8c12023-07-11 14:11:28 +02008166#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
8168 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008169 custom_data, custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008170 key_buffer,
8171 key_buffer_size,
8172 key_buffer_length);
8173 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02008174#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01008175
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008176#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8178 return mbedtls_psa_ecp_generate_key(attributes,
8179 key_buffer,
8180 key_buffer_size,
8181 key_buffer_length);
8182 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008183#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008184
Valerio Settia55f0422023-07-10 15:34:41 +02008185#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008186 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8187 return mbedtls_psa_ffdh_generate_key(attributes,
8188 key_buffer,
8189 key_buffer_size,
8190 key_buffer_length);
8191 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008192#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02008193 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 (void) key_buffer_length;
8195 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02008196 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008197
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008199}
Darryl Green0c6575a2018-11-07 16:05:30 +00008200
Gilles Peskinef36d7852024-06-06 21:11:44 +02008201psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
8202 const psa_custom_key_parameters_t *custom,
8203 const uint8_t *custom_data,
8204 size_t custom_data_length,
8205 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008206{
8207 psa_status_t status;
8208 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02008209 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02008210 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02008211
Ronald Cron81709fc2020-11-14 12:10:32 +01008212 *key = MBEDTLS_SVC_KEY_ID_INIT;
8213
Gilles Peskine0f84d622019-09-12 19:03:13 +02008214 /* Reject any attempt to create a zero-length key so that we don't
8215 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 if (psa_get_key_bits(attributes) == 0) {
8217 return PSA_ERROR_INVALID_ARGUMENT;
8218 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02008219
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008220 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008221 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008222 return PSA_ERROR_INVALID_ARGUMENT;
8223 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008224
Gilles Peskine7a18f962024-02-12 16:48:11 +01008225#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008226 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinef36d7852024-06-06 21:11:44 +02008227 if (custom->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01008228 return PSA_ERROR_INVALID_ARGUMENT;
8229 }
8230 } else
8231#endif
Gilles Peskine52504f82024-06-20 17:19:58 +02008232 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01008233 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02008234 }
8235
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
8237 &slot, &driver);
8238 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02008239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 }
Gilles Peskine11792082019-08-06 18:36:36 +02008241
Ronald Cron977c2472020-10-13 08:32:21 +02008242 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05308243 * storage ( thus not in the case of generating a key in a secure element
8244 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
8245 * buffer to hold the generated key material. */
Valerio Setti70fa89c2024-08-14 05:12:45 +02008246 if (slot->key.bytes == 0) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008247 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008249 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008250 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008252 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 }
Ronald Cron3772afe2021-02-08 16:10:05 +01008254
8255 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008256 attributes->type,
8257 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02008259 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 attributes, &key_buffer_size);
8261 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 }
Ronald Cron977c2472020-10-13 08:32:21 +02008264 }
Ronald Cron977c2472020-10-13 08:32:21 +02008265
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
8267 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02008268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 }
Ronald Cron977c2472020-10-13 08:32:21 +02008270 }
8271
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008273 custom,
8274 custom_data, custom_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01008275 slot->key.data, slot->key.bytes,
8276 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 if (status != PSA_SUCCESS) {
8278 psa_remove_key_data_from_memory(slot);
8279 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02008280
Gilles Peskine11792082019-08-06 18:36:36 +02008281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 if (status == PSA_SUCCESS) {
8283 status = psa_finish_key_creation(slot, driver, key);
8284 }
8285 if (status != PSA_SUCCESS) {
8286 psa_fail_key_creation(slot, driver);
8287 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02008288
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008290}
8291
Gilles Peskinef36d7852024-06-06 21:11:44 +02008292psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
8293 const psa_key_production_parameters_t *params,
8294 size_t params_data_length,
8295 mbedtls_svc_key_id_t *key)
8296{
8297 return psa_generate_key_custom(
8298 attributes,
8299 (const psa_custom_key_parameters_t *) params,
8300 params->data, params_data_length,
8301 key);
8302}
8303
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008304psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
8305 mbedtls_svc_key_id_t *key)
8306{
Gilles Peskinef36d7852024-06-06 21:11:44 +02008307 return psa_generate_key_custom(attributes,
8308 &default_custom_production,
8309 NULL, 0,
8310 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008311}
8312
Gilles Peskinecdb37812025-02-05 18:02:49 +01008313
8314
Gilles Peskinee59236f2018-01-27 23:32:46 +01008315/****************************************************************/
8316/* Module setup */
8317/****************************************************************/
8318
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008319#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01008320psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 void (* entropy_init)(mbedtls_entropy_context *ctx),
8322 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01008323{
Paul Elliott8e151532024-02-23 17:35:29 +00008324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8325
8326#if defined(MBEDTLS_THREADING_C)
8327 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8328#endif /* defined(MBEDTLS_THREADING_C) */
8329
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00008331 status = PSA_ERROR_BAD_STATE;
8332 } else {
8333 global_data.rng.entropy_init = entropy_init;
8334 global_data.rng.entropy_free = entropy_free;
8335 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 }
Paul Elliott8e151532024-02-23 17:35:29 +00008337
8338#if defined(MBEDTLS_THREADING_C)
8339 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8340#endif /* defined(MBEDTLS_THREADING_C) */
8341
8342 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01008343}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008344#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01008345
Gilles Peskine449bd832023-01-11 14:50:10 +01008346void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008347{
Paul Elliott47cee8e2024-03-08 21:38:02 +00008348
Paul Elliott600472b2024-02-23 17:26:58 +00008349#if defined(MBEDTLS_THREADING_C)
8350 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8351#endif /* defined(MBEDTLS_THREADING_C) */
8352
Paul Elliott47cee8e2024-03-08 21:38:02 +00008353 /* Nothing to do to free transaction. */
8354 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
8355 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01008356 }
Paul Elliott600472b2024-02-23 17:26:58 +00008357
Paul Elliott47cee8e2024-03-08 21:38:02 +00008358 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
8359 psa_wipe_all_key_slots();
8360 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8361 }
Ronald Cron9ba76912021-04-10 16:57:30 +02008362
Paul Elliott600472b2024-02-23 17:26:58 +00008363#if defined(MBEDTLS_THREADING_C)
8364 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8365#endif /* defined(MBEDTLS_THREADING_C) */
8366
Paul Elliott47cee8e2024-03-08 21:38:02 +00008367#if defined(MBEDTLS_THREADING_C)
8368 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8369#endif /* defined(MBEDTLS_THREADING_C) */
8370
Gilles Peskinec6b69072018-11-20 21:42:52 +01008371 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01008372 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008373 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008374 global_data.rng_state = RNG_NOT_INITIALIZED;
8375 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
8376
8377#if defined(MBEDTLS_THREADING_C)
8378 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8379#endif /* defined(MBEDTLS_THREADING_C) */
8380
8381#if defined(MBEDTLS_THREADING_C)
8382 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8383#endif /* defined(MBEDTLS_THREADING_C) */
Ronald Cron9ba76912021-04-10 16:57:30 +02008384
8385 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00008386 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
8387 psa_driver_wrapper_free();
8388 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8389 }
8390
8391#if defined(MBEDTLS_THREADING_C)
8392 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8393#endif /* defined(MBEDTLS_THREADING_C) */
8394
Gilles Peskinee59236f2018-01-27 23:32:46 +01008395}
8396
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008397#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8398/** Recover a transaction that was interrupted by a power failure.
8399 *
8400 * This function is called during initialization, before psa_crypto_init()
8401 * returns. If this function returns a failure status, the initialization
8402 * fails.
8403 */
8404static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008406{
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008408 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8409 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 /* TODO - fall through to the failure case until this
8411 * is implemented.
8412 * https://github.com/ARMmbed/mbed-crypto/issues/218
8413 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008414 default:
8415 /* We found an unsupported transaction in the storage.
8416 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008418 }
8419}
8420#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8421
Paul Elliott47cee8e2024-03-08 21:38:02 +00008422static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
8423{
8424 psa_status_t status = PSA_SUCCESS;
8425 uint8_t driver_wrappers_initialized = 0;
8426
8427 switch (subsystem) {
8428 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
8429
8430#if defined(MBEDTLS_THREADING_C)
8431 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8432#endif /* defined(MBEDTLS_THREADING_C) */
8433
8434 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
8435 /* Init drivers */
8436 status = psa_driver_wrapper_init();
8437
8438 /* Drivers need shutdown regardless of startup errors. */
8439 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8440
8441
8442 }
8443#if defined(MBEDTLS_THREADING_C)
8444 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8445 &mbedtls_threading_psa_globaldata_mutex));
8446#endif /* defined(MBEDTLS_THREADING_C) */
8447
8448 break;
8449
8450 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
8451
8452#if defined(MBEDTLS_THREADING_C)
8453 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8454#endif /* defined(MBEDTLS_THREADING_C) */
8455
8456 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
8457 status = psa_initialize_key_slots();
8458
8459 /* Need to wipe keys even if initialization fails. */
8460 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8461
8462 }
8463#if defined(MBEDTLS_THREADING_C)
8464 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8465 &mbedtls_threading_psa_globaldata_mutex));
8466#endif /* defined(MBEDTLS_THREADING_C) */
8467
8468 break;
8469
8470 case PSA_CRYPTO_SUBSYSTEM_RNG:
8471
8472#if defined(MBEDTLS_THREADING_C)
8473 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8474#endif /* defined(MBEDTLS_THREADING_C) */
8475
8476 driver_wrappers_initialized =
8477 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
8478
8479#if defined(MBEDTLS_THREADING_C)
8480 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8481 &mbedtls_threading_psa_globaldata_mutex));
8482#endif /* defined(MBEDTLS_THREADING_C) */
8483
8484 /* Need to use separate mutex here, as initialisation can require
8485 * testing of init flags, which requires locking the global data
8486 * mutex. */
8487#if defined(MBEDTLS_THREADING_C)
8488 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
8489#endif /* defined(MBEDTLS_THREADING_C) */
8490
8491 /* Initialize and seed the random generator. */
8492 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
8493 mbedtls_psa_random_init(&global_data.rng);
8494 global_data.rng_state = RNG_INITIALIZED;
8495
8496 status = mbedtls_psa_random_seed(&global_data.rng);
8497 if (status == PSA_SUCCESS) {
8498 global_data.rng_state = RNG_SEEDED;
8499 }
8500 }
8501
8502#if defined(MBEDTLS_THREADING_C)
8503 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8504 &mbedtls_threading_psa_rngdata_mutex));
8505#endif /* defined(MBEDTLS_THREADING_C) */
8506
Paul Elliott47cee8e2024-03-08 21:38:02 +00008507 break;
8508
8509 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
8510
8511#if defined(MBEDTLS_THREADING_C)
8512 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8513#endif /* defined(MBEDTLS_THREADING_C) */
8514
8515 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
8516#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8517 status = psa_crypto_load_transaction();
8518 if (status == PSA_SUCCESS) {
8519 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8520 if (status == PSA_SUCCESS) {
8521 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8522 }
8523 status = psa_crypto_stop_transaction();
8524 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
8525 /* There's no transaction to complete. It's all good. */
8526 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8527 status = PSA_SUCCESS;
8528 }
8529#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8530 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8531 status = PSA_SUCCESS;
8532#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8533 }
8534
8535#if defined(MBEDTLS_THREADING_C)
8536 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8537 &mbedtls_threading_psa_globaldata_mutex));
8538#endif /* defined(MBEDTLS_THREADING_C) */
8539
8540 break;
8541
8542 default:
8543 status = PSA_ERROR_CORRUPTION_DETECTED;
8544 }
8545
8546 /* Exit label only required when using threading macros. */
8547#if defined(MBEDTLS_THREADING_C)
8548exit:
8549#endif /* defined(MBEDTLS_THREADING_C) */
8550
8551 return status;
8552}
8553
Gilles Peskine449bd832023-01-11 14:50:10 +01008554psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008555{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008556 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008557
Paul Elliott47cee8e2024-03-08 21:38:02 +00008558 /* Double initialization is explicitly allowed. Early out if everything is
8559 * done. */
8560 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 return PSA_SUCCESS;
8562 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008563
Paul Elliott47cee8e2024-03-08 21:38:02 +00008564 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01008565 if (status != PSA_SUCCESS) {
8566 goto exit;
8567 }
8568
Paul Elliott47cee8e2024-03-08 21:38:02 +00008569 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008573
Paul Elliott47cee8e2024-03-08 21:38:02 +00008574 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
8575 if (status != PSA_SUCCESS) {
8576 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02008577 }
Gilles Peskinefc762652019-07-22 19:30:34 +02008578
Paul Elliott47cee8e2024-03-08 21:38:02 +00008579 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01008580
8581exit:
Paul Elliott600472b2024-02-23 17:26:58 +00008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 if (status != PSA_SUCCESS) {
8584 mbedtls_psa_crypto_free();
8585 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008586
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008588}
8589
Gilles Peskinecdb37812025-02-05 18:02:49 +01008590
8591
8592/****************************************************************/
8593/* PAKE */
8594/****************************************************************/
8595
Yanray Wangffc3c482023-07-11 12:01:04 +08008596#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008597psa_status_t psa_crypto_driver_pake_get_password_len(
8598 const psa_crypto_driver_pake_inputs_t *inputs,
8599 size_t *password_len)
8600{
8601 if (inputs->password_len == 0) {
8602 return PSA_ERROR_BAD_STATE;
8603 }
8604
8605 *password_len = inputs->password_len;
8606
8607 return PSA_SUCCESS;
8608}
8609
8610psa_status_t psa_crypto_driver_pake_get_password(
8611 const psa_crypto_driver_pake_inputs_t *inputs,
8612 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8613{
8614 if (inputs->password_len == 0) {
8615 return PSA_ERROR_BAD_STATE;
8616 }
8617
8618 if (buffer_size < inputs->password_len) {
8619 return PSA_ERROR_BUFFER_TOO_SMALL;
8620 }
8621
8622 memcpy(buffer, inputs->password, inputs->password_len);
8623 *buffer_length = inputs->password_len;
8624
8625 return PSA_SUCCESS;
8626}
8627
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008628psa_status_t psa_crypto_driver_pake_get_user_len(
8629 const psa_crypto_driver_pake_inputs_t *inputs,
8630 size_t *user_len)
8631{
8632 if (inputs->user_len == 0) {
8633 return PSA_ERROR_BAD_STATE;
8634 }
8635
8636 *user_len = inputs->user_len;
8637
8638 return PSA_SUCCESS;
8639}
8640
8641psa_status_t psa_crypto_driver_pake_get_user(
8642 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008643 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008644{
8645 if (inputs->user_len == 0) {
8646 return PSA_ERROR_BAD_STATE;
8647 }
8648
Przemek Stekielc0e62502023-03-14 11:49:36 +01008649 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008650 return PSA_ERROR_BUFFER_TOO_SMALL;
8651 }
8652
Przemek Stekielc0e62502023-03-14 11:49:36 +01008653 memcpy(user_id, inputs->user, inputs->user_len);
8654 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008655
8656 return PSA_SUCCESS;
8657}
8658
8659psa_status_t psa_crypto_driver_pake_get_peer_len(
8660 const psa_crypto_driver_pake_inputs_t *inputs,
8661 size_t *peer_len)
8662{
8663 if (inputs->peer_len == 0) {
8664 return PSA_ERROR_BAD_STATE;
8665 }
8666
8667 *peer_len = inputs->peer_len;
8668
8669 return PSA_SUCCESS;
8670}
8671
8672psa_status_t psa_crypto_driver_pake_get_peer(
8673 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008674 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008675{
8676 if (inputs->peer_len == 0) {
8677 return PSA_ERROR_BAD_STATE;
8678 }
8679
Przemek Stekielc0e62502023-03-14 11:49:36 +01008680 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008681 return PSA_ERROR_BUFFER_TOO_SMALL;
8682 }
8683
Przemek Stekielc0e62502023-03-14 11:49:36 +01008684 memcpy(peer_id, inputs->peer, inputs->peer_len);
8685 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008686
8687 return PSA_SUCCESS;
8688}
8689
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008690psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8691 const psa_crypto_driver_pake_inputs_t *inputs,
8692 psa_pake_cipher_suite_t *cipher_suite)
8693{
8694 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8695 return PSA_ERROR_BAD_STATE;
8696 }
8697
8698 *cipher_suite = inputs->cipher_suite;
8699
8700 return PSA_SUCCESS;
8701}
8702
Neil Armstronga7d08c32022-06-01 18:21:20 +02008703psa_status_t psa_pake_setup(
8704 psa_pake_operation_t *operation,
8705 const psa_pake_cipher_suite_t *cipher_suite)
8706{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008708
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008709 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008710 status = PSA_ERROR_BAD_STATE;
8711 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008712 }
8713
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008714 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008715 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008716 status = PSA_ERROR_INVALID_ARGUMENT;
8717 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008718 }
8719
Gilles Peskinea7a480b2025-02-05 19:41:52 +01008720 /* Make sure the variable-purpose part of the operation is zeroed.
8721 * Initializing the operation does not necessarily take care of it,
8722 * since the context is a union and initializing a union does not
8723 * necessarily initialize all of its members. */
8724 memset(&operation->data, 0, sizeof(operation->data));
Przemek Stekiel51eac532022-12-07 11:04:51 +01008725
Przemek Stekiele12ed362022-12-21 12:54:46 +01008726 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008727 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8728 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008729 operation->data.inputs.cipher_suite = *cipher_suite;
8730
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008731#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008732 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008733 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008734 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008735
David Horstmann16f01512023-06-14 17:21:07 +01008736 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008737 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008738 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008739#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008740 {
8741 status = PSA_ERROR_NOT_SUPPORTED;
8742 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008743 }
8744
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008745 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8746
Przemek Stekiel51eac532022-12-07 11:04:51 +01008747 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008748exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008749 psa_pake_abort(operation);
8750 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008751}
8752
8753psa_status_t psa_pake_set_password_key(
8754 psa_pake_operation_t *operation,
8755 mbedtls_svc_key_id_t password)
8756{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008758 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8759 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008760 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008761
Przemek Stekiel51eac532022-12-07 11:04:51 +01008762 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008763 status = PSA_ERROR_BAD_STATE;
8764 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008765 }
8766
Przemek Stekiel6c764412022-11-22 14:05:12 +01008767 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8768 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008769 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008770 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008771 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008772 }
8773
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008774 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008775
Przemek Stekiel51eac532022-12-07 11:04:51 +01008776 if (type != PSA_KEY_TYPE_PASSWORD &&
8777 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8778 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008779 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008780 }
8781
Przemek Stekiel51eac532022-12-07 11:04:51 +01008782 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8783 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008784 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008785 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008786 }
8787
8788 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8789 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008790 operation->data.inputs.attributes = slot->attr;
8791
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008792exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008793 if (status != PSA_SUCCESS) {
8794 psa_pake_abort(operation);
8795 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008796 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008797 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008798}
8799
8800psa_status_t psa_pake_set_user(
8801 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008802 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008803 size_t user_id_len)
8804{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008805 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008806 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008807
8808 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008809 status = PSA_ERROR_BAD_STATE;
8810 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008811 }
8812
Przemek Stekiel51eac532022-12-07 11:04:51 +01008813 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008814 status = PSA_ERROR_INVALID_ARGUMENT;
8815 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008816 }
8817
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008818 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008819 status = PSA_ERROR_BAD_STATE;
8820 goto exit;
8821 }
8822
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008823 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8824 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008825 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8826 goto exit;
8827 }
8828
David Horstmann4f534ae2024-01-22 17:35:59 +00008829 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8830
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008831 memcpy(operation->data.inputs.user, user_id, user_id_len);
8832 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008833
David Horstmann4f534ae2024-01-22 17:35:59 +00008834 status = PSA_SUCCESS;
8835
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008836exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008837 LOCAL_INPUT_FREE(user_id_external, user_id);
8838 if (status != PSA_SUCCESS) {
8839 psa_pake_abort(operation);
8840 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008841 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008842}
8843
8844psa_status_t psa_pake_set_peer(
8845 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008846 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008847 size_t peer_id_len)
8848{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008850 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008851
8852 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008853 status = PSA_ERROR_BAD_STATE;
8854 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008855 }
8856
Przemek Stekiel51eac532022-12-07 11:04:51 +01008857 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008858 status = PSA_ERROR_INVALID_ARGUMENT;
8859 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008860 }
8861
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008862 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008863 status = PSA_ERROR_BAD_STATE;
8864 goto exit;
8865 }
8866
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008867 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8868 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008869 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8870 goto exit;
8871 }
8872
David Horstmann4f534ae2024-01-22 17:35:59 +00008873 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8874
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008875 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8876 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008877
David Horstmann4f534ae2024-01-22 17:35:59 +00008878 status = PSA_SUCCESS;
8879
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008880exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008881 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8882 if (status != PSA_SUCCESS) {
8883 psa_pake_abort(operation);
8884 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008885 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008886}
8887
8888psa_status_t psa_pake_set_role(
8889 psa_pake_operation_t *operation,
8890 psa_pake_role_t role)
8891{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008892 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008893
Przemek Stekiel51eac532022-12-07 11:04:51 +01008894 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008895 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008896 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008897 }
8898
Przemek Stekiel09104b82023-03-06 14:11:51 +01008899 switch (operation->alg) {
8900#if defined(PSA_WANT_ALG_JPAKE)
8901 case PSA_ALG_JPAKE:
8902 if (role == PSA_PAKE_ROLE_NONE) {
8903 return PSA_SUCCESS;
8904 }
8905 status = PSA_ERROR_INVALID_ARGUMENT;
8906 break;
8907#endif
8908 default:
8909 (void) role;
8910 status = PSA_ERROR_NOT_SUPPORTED;
8911 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008912 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008913exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008914 psa_pake_abort(operation);
8915 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008916}
8917
David Horstmanne7f21e62023-05-12 18:17:21 +01008918/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008919#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008920static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008921 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008922{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008923 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008924 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008925 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008926
David Horstmann024e5c52023-06-14 15:48:21 +01008927 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008928 is_x1 = (stage->outputs < 1);
8929 } else {
8930 is_x1 = (stage->inputs < 1);
8931 }
8932
David Horstmann74a3d8c2023-06-14 18:28:19 +01008933 key_share_step = is_x1 ?
8934 PSA_JPAKE_X1_STEP_KEY_SHARE :
8935 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008936 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008937 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8938 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8939 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8940 } else {
8941 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008942 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008943 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008944}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008945#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008946
Przemek Stekiel2797d372022-12-22 11:19:22 +01008947static psa_status_t psa_pake_complete_inputs(
8948 psa_pake_operation_t *operation)
8949{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008951 /* Create copy of the inputs on stack as inputs share memory
8952 with the driver context which will be setup by the driver. */
8953 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008954
Przemek Stekielfde11282023-03-13 16:06:09 +01008955 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008956 return PSA_ERROR_BAD_STATE;
8957 }
8958
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008959 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008960 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8961 return PSA_ERROR_BAD_STATE;
8962 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008963 }
8964
Przemek Stekiel18620a32023-01-17 16:34:52 +01008965 /* Clear driver context */
8966 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8967
8968 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008969
8970 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008971 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008972
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008973 /* User and peer are translated to role. */
8974 mbedtls_free(inputs.user);
8975 mbedtls_free(inputs.peer);
8976
Przemek Stekiel2797d372022-12-22 11:19:22 +01008977 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008978#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008979 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008980 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008981 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008982#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008983 {
8984 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008985 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008986 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008987 return status;
8988}
8989
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008990#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008991static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008992 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008993 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008994 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008995{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008996 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8997 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8998 step != PSA_PAKE_STEP_ZK_PROOF) {
8999 return PSA_ERROR_INVALID_ARGUMENT;
9000 }
9001
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009002 psa_jpake_computation_stage_t *computation_stage =
9003 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009004
David Horstmann5da95602023-06-08 15:37:12 +01009005 if (computation_stage->round != PSA_JPAKE_FIRST &&
9006 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009007 return PSA_ERROR_BAD_STATE;
9008 }
9009
David Horstmanne7f21e62023-05-12 18:17:21 +01009010 /* Check that the step we are given is the one we were expecting */
9011 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009012 return PSA_ERROR_BAD_STATE;
9013 }
9014
David Horstmanne7f21e62023-05-12 18:17:21 +01009015 if (step == PSA_PAKE_STEP_KEY_SHARE &&
9016 computation_stage->inputs == 0 &&
9017 computation_stage->outputs == 0) {
9018 /* Start of the round, so function decides whether we are inputting
9019 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01009020 computation_stage->io_mode = io_mode;
9021 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009022 /* Middle of the round so the mode we are in must match the function
9023 * called by the user */
9024 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009025 }
9026
9027 return PSA_SUCCESS;
9028}
9029
David Horstmanne7f21e62023-05-12 18:17:21 +01009030static psa_status_t psa_jpake_epilogue(
9031 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01009032 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009033{
David Horstmanne7f21e62023-05-12 18:17:21 +01009034 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009035 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009036
David Horstmanne7f21e62023-05-12 18:17:21 +01009037 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
9038 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01009039 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009040 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01009041 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01009042 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01009043 }
9044 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01009045 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009046 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01009047 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01009048 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01009049 }
9050 }
David Horstmann246ec5a2023-06-27 10:33:06 +01009051 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
9052 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009053 /* End of a round, move to the next round */
9054 stage->inputs = 0;
9055 stage->outputs = 0;
9056 stage->round++;
9057 }
9058 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009059 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01009060 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009061 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01009062 return PSA_SUCCESS;
9063}
David Horstmanne7f21e62023-05-12 18:17:21 +01009064
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009065#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009066
Neil Armstronga7d08c32022-06-01 18:21:20 +02009067psa_status_t psa_pake_output(
9068 psa_pake_operation_t *operation,
9069 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00009070 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02009071 size_t output_size,
9072 size_t *output_length)
9073{
Przemek Stekiel51eac532022-12-07 11:04:51 +01009074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009075 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00009076 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009077 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009078
9079 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01009080 status = psa_pake_complete_inputs(operation);
9081 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009082 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009083 }
9084 }
9085
9086 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009087 status = PSA_ERROR_BAD_STATE;
9088 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009089 }
9090
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009091 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009092 status = PSA_ERROR_INVALID_ARGUMENT;
9093 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009094 }
9095
Przemek Stekiele12ed362022-12-21 12:54:46 +01009096 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009097#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009098 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009099 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009100 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009101 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009102 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009103 driver_step = convert_jpake_computation_stage_to_driver_step(
9104 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009105 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009106#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009107 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01009108 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009109 status = PSA_ERROR_NOT_SUPPORTED;
9110 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009111 }
9112
David Horstmannc75639d2024-01-22 17:56:39 +00009113 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
9114
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009115 status = psa_driver_wrapper_pake_output(operation, driver_step,
9116 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009117
9118 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009119 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009120 }
9121
9122 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009123#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009124 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009125 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009126 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009127 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009128 }
9129 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009130#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009131 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009132 status = PSA_ERROR_NOT_SUPPORTED;
9133 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009134 }
9135
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009136exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009137 LOCAL_OUTPUT_FREE(output_external, output);
9138 if (status != PSA_SUCCESS) {
9139 psa_pake_abort(operation);
9140 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009141 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009142}
9143
9144psa_status_t psa_pake_input(
9145 psa_pake_operation_t *operation,
9146 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00009147 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02009148 size_t input_length)
9149{
Przemek Stekiel51eac532022-12-07 11:04:51 +01009150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009151 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009152 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
9153 operation->primitive,
9154 step);
David Horstmannc75639d2024-01-22 17:56:39 +00009155 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01009156
9157 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01009158 status = psa_pake_complete_inputs(operation);
9159 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009160 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009161 }
9162 }
9163
9164 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009165 status = PSA_ERROR_BAD_STATE;
9166 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009167 }
9168
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009169 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009170 status = PSA_ERROR_INVALID_ARGUMENT;
9171 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009172 }
9173
Przemek Stekiele12ed362022-12-21 12:54:46 +01009174 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009175#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009176 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009177 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009178 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009179 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009180 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009181 driver_step = convert_jpake_computation_stage_to_driver_step(
9182 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009183 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009184#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009185 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01009186 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009187 status = PSA_ERROR_NOT_SUPPORTED;
9188 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009189 }
9190
David Horstmannc75639d2024-01-22 17:56:39 +00009191 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009192 status = psa_driver_wrapper_pake_input(operation, driver_step,
9193 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009194
9195 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009196 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009197 }
9198
9199 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009200#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009201 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009202 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009203 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009204 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009205 }
9206 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009207#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009208 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009209 status = PSA_ERROR_NOT_SUPPORTED;
9210 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009211 }
9212
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009213exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009214 LOCAL_INPUT_FREE(input_external, input);
9215 if (status != PSA_SUCCESS) {
9216 psa_pake_abort(operation);
9217 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009218 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009219}
9220
9221psa_status_t psa_pake_get_implicit_key(
9222 psa_pake_operation_t *operation,
9223 psa_key_derivation_operation_t *output)
9224{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009226 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01009227 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01009228 size_t shared_key_len = 0;
9229
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009230 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009231 status = PSA_ERROR_BAD_STATE;
9232 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009233 }
9234
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009235#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009236 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009237 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01009238 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01009239 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009240 status = PSA_ERROR_BAD_STATE;
9241 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009242 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01009243 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009244#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01009245 {
9246 status = PSA_ERROR_NOT_SUPPORTED;
9247 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009248 }
9249
Przemek Stekiel0c781802022-11-29 14:53:13 +01009250 status = psa_driver_wrapper_pake_get_implicit_key(operation,
9251 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01009252 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01009253 &shared_key_len);
9254
9255 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009256 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01009257 }
9258
9259 status = psa_key_derivation_input_bytes(output,
9260 PSA_KEY_DERIVATION_INPUT_SECRET,
9261 shared_key,
9262 shared_key_len);
9263
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009264 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009265exit:
9266 abort_status = psa_pake_abort(operation);
9267 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009268}
9269
9270psa_status_t psa_pake_abort(
9271 psa_pake_operation_t *operation)
9272{
Przemek Stekield69dca92023-01-31 19:59:20 +01009273 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009274
Przemek Stekield69dca92023-01-31 19:59:20 +01009275 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009276 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02009277 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009278
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009279 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
9280 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009281 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01009282 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009283 }
9284 if (operation->data.inputs.user != NULL) {
9285 mbedtls_free(operation->data.inputs.user);
9286 }
9287 if (operation->data.inputs.peer != NULL) {
9288 mbedtls_free(operation->data.inputs.peer);
9289 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009290 }
Przemek Stekield69dca92023-01-31 19:59:20 +01009291 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009292
Przemek Stekield69dca92023-01-31 19:59:20 +01009293 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009294}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01009295#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02009296
David Horstmann00d7a042023-12-07 18:39:17 +00009297/* Memory copying test hooks. These are called before input copy, after input
9298 * copy, before output copy and after output copy, respectively.
9299 * They are used by memory-poisoning tests to temporarily unpoison buffers
9300 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00009301#if defined(MBEDTLS_TEST_HOOKS)
9302void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9303void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9304void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9305void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9306#endif
David Horstmannfde97392023-10-30 20:29:43 +00009307
David Horstmann1b7279a2023-11-15 15:18:30 +00009308/** Copy from an input buffer to a local copy.
9309 *
9310 * \param[in] input Pointer to input buffer.
9311 * \param[in] input_len Length of the input buffer.
9312 * \param[out] input_copy Pointer to a local copy in which to store the input data.
9313 * \param[out] input_copy_len Length of the local copy buffer.
9314 * \return #PSA_SUCCESS, if the buffer was successfully
9315 * copied.
9316 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
9317 * copy is too small to hold contents of the
9318 * input buffer.
9319 */
9320MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00009321psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
9322 uint8_t *input_copy, size_t input_copy_len)
9323{
9324 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00009325 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00009326 }
9327
David Horstmann372b8bb2023-11-24 16:21:04 +00009328#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009329 if (psa_input_pre_copy_hook != NULL) {
9330 psa_input_pre_copy_hook(input, input_len);
9331 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009332#endif
9333
David Horstmann777e7412023-11-15 17:33:47 +00009334 if (input_len > 0) {
9335 memcpy(input_copy, input, input_len);
9336 }
David Horstmannfde97392023-10-30 20:29:43 +00009337
David Horstmann372b8bb2023-11-24 16:21:04 +00009338#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009339 if (psa_input_post_copy_hook != NULL) {
9340 psa_input_post_copy_hook(input, input_len);
9341 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009342#endif
9343
David Horstmannfde97392023-10-30 20:29:43 +00009344 return PSA_SUCCESS;
9345}
9346
David Horstmann1b7279a2023-11-15 15:18:30 +00009347/** Copy from a local output buffer into a user-supplied one.
9348 *
9349 * \param[in] output_copy Pointer to a local buffer containing the output.
9350 * \param[in] output_copy_len Length of the local buffer.
9351 * \param[out] output Pointer to user-supplied output buffer.
9352 * \param[out] output_len Length of the user-supplied output buffer.
9353 * \return #PSA_SUCCESS, if the buffer was successfully
9354 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00009355 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00009356 * user-supplied output buffer is too small to
9357 * hold the contents of the local buffer.
9358 */
9359MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00009360psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
9361 uint8_t *output, size_t output_len)
9362{
9363 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00009364 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00009365 }
David Horstmann777e7412023-11-15 17:33:47 +00009366
David Horstmann372b8bb2023-11-24 16:21:04 +00009367#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009368 if (psa_output_pre_copy_hook != NULL) {
9369 psa_output_pre_copy_hook(output, output_len);
9370 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009371#endif
9372
David Horstmann777e7412023-11-15 17:33:47 +00009373 if (output_copy_len > 0) {
9374 memcpy(output, output_copy, output_copy_len);
9375 }
9376
David Horstmann372b8bb2023-11-24 16:21:04 +00009377#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009378 if (psa_output_post_copy_hook != NULL) {
9379 psa_output_post_copy_hook(output, output_len);
9380 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009381#endif
9382
David Horstmann8978f5c2023-10-30 20:37:12 +00009383 return PSA_SUCCESS;
9384}
9385
David Horstmannf1734052023-11-20 17:15:32 +00009386psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
9387 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00009388{
9389 psa_status_t status;
9390
David Horstmann9db14482023-11-23 15:50:37 +00009391 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00009392
David Horstmannc5cc1c32023-11-15 18:11:26 +00009393 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00009394 return PSA_SUCCESS;
9395 }
9396
David Horstmannf1734052023-11-20 17:15:32 +00009397 local_input->buffer = mbedtls_calloc(input_len, 1);
9398 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00009399 /* Since we dealt with the zero-length case above, we know that
9400 * a NULL return value means a failure of allocation. */
9401 return PSA_ERROR_INSUFFICIENT_MEMORY;
9402 }
David Horstmannf1734052023-11-20 17:15:32 +00009403 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00009404
David Horstmannf1734052023-11-20 17:15:32 +00009405 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00009406
9407 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00009408 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00009409 if (status != PSA_SUCCESS) {
9410 goto error;
9411 }
9412
9413 return PSA_SUCCESS;
9414
9415error:
Gilles Peskinee847afd2025-03-06 12:41:39 +01009416 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009417 local_input->buffer = NULL;
9418 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00009419 return status;
9420}
9421
David Horstmannf1734052023-11-20 17:15:32 +00009422void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00009423{
Gilles Peskinee847afd2025-03-06 12:41:39 +01009424 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009425 local_input->buffer = NULL;
9426 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00009427}
9428
David Horstmann89875a42023-11-20 12:54:09 +00009429psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
9430 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00009431{
David Horstmann9db14482023-11-23 15:50:37 +00009432 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00009433
David Horstmannc5cc1c32023-11-15 18:11:26 +00009434 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009435 return PSA_SUCCESS;
9436 }
David Horstmann89875a42023-11-20 12:54:09 +00009437 local_output->buffer = mbedtls_calloc(output_len, 1);
9438 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009439 /* Since we dealt with the zero-length case above, we know that
9440 * a NULL return value means a failure of allocation. */
9441 return PSA_ERROR_INSUFFICIENT_MEMORY;
9442 }
David Horstmann89875a42023-11-20 12:54:09 +00009443 local_output->length = output_len;
9444 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00009445
9446 return PSA_SUCCESS;
9447}
9448
David Horstmann89875a42023-11-20 12:54:09 +00009449psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00009450{
David Horstmannc335a4e2023-11-15 15:57:32 +00009451 psa_status_t status;
9452
David Horstmann89875a42023-11-20 12:54:09 +00009453 if (local_output->buffer == NULL) {
9454 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009455 return PSA_SUCCESS;
9456 }
David Horstmann89875a42023-11-20 12:54:09 +00009457 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00009458 /* We have an internal copy but nothing to copy back to. */
9459 return PSA_ERROR_CORRUPTION_DETECTED;
9460 }
9461
David Horstmann89875a42023-11-20 12:54:09 +00009462 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
9463 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00009464 if (status != PSA_SUCCESS) {
9465 return status;
9466 }
David Horstmann9467ea32023-11-08 17:44:18 +00009467
Gilles Peskinee847afd2025-03-06 12:41:39 +01009468 mbedtls_zeroize_and_free(local_output->buffer, local_output->length);
David Horstmann89875a42023-11-20 12:54:09 +00009469 local_output->buffer = NULL;
9470 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009471
9472 return PSA_SUCCESS;
9473}
9474
Gilles Peskinee59236f2018-01-27 23:32:46 +01009475#endif /* MBEDTLS_PSA_CRYPTO_C */