blob: 5f2cad42b7d0894e3145bb68789c4e443e9314a2 [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 Peskine04dfd702025-08-07 22:27:26 +02004860 if (unlock_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 Peskine04dfd702025-08-07 22:27:26 +02004864 /* Set *output_length to 0 if status != PSA_SUCCESS, without
4865 * leaking the value of status through a timing side channel
4866 * (status == PSA_ERROR_INVALID_PADDING is sensitive when doing
4867 * unpadded decryption, due to the risk of padding oracle attack). */
4868 mbedtls_ct_condition_t success =
4869 mbedtls_ct_bool_not(mbedtls_ct_bool(status));
4870 *output_length = mbedtls_ct_size_if_else_0(success, *output_length);
Ronald Cron23919522021-07-08 19:00:07 +02004871
Gabor Mezei212eb082024-01-24 16:56:00 +01004872 LOCAL_INPUT_FREE(input_external, input);
4873 LOCAL_OUTPUT_FREE(output_external, output);
4874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004876}
4877
4878
mohammad16035955c982018-04-26 00:53:03 +03004879/****************************************************************/
4880/* AEAD */
4881/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004882
Paul Elliottbaff51c2021-09-28 17:44:45 +01004883/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004884static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004885{
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004887}
4888
4889/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004890static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4891 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004892{
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004896#if defined(PSA_WANT_ALG_GCM)
4897 case PSA_ALG_GCM:
4898 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 * arbitrarily large nonces. Please note that we do not generally
4900 * recommend the usage of nonces of greater length than
4901 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4902 * size, which can then lead to collisions if you encrypt a very
4903 * large number of messages.*/
4904 if (nonce_length != 0) {
4905 return PSA_SUCCESS;
4906 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004908#endif /* PSA_WANT_ALG_GCM */
4909#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (nonce_length >= 7 && nonce_length <= 13) {
4912 return PSA_SUCCESS;
4913 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004914 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004915#endif /* PSA_WANT_ALG_CCM */
4916#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004917 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (nonce_length == 12) {
4919 return PSA_SUCCESS;
4920 } else if (nonce_length == 8) {
4921 return PSA_ERROR_NOT_SUPPORTED;
4922 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004923 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004924#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004925 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004926 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004928 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004931}
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004934{
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4936 return PSA_ERROR_INVALID_ARGUMENT;
4937 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004940}
4941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4943 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004944 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004946 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004948 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004950 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 size_t ciphertext_size,
4952 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004953{
Ronald Cron215633c2021-03-16 17:15:37 +01004954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4955 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004956
David Horstmann9d09a022023-11-28 19:25:00 +00004957 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4958 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4959 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4960 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4961
mohammad1603f08a5502018-06-03 15:05:47 +03004962 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 status = psa_aead_check_algorithm(alg);
4965 if (status != PSA_SUCCESS) {
4966 return status;
4967 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004968
Ronald Cron9a986162021-03-26 12:40:07 +01004969 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4971 if (status != PSA_SUCCESS) {
4972 return status;
4973 }
mohammad16035955c982018-04-26 00:53:03 +03004974
David Horstmann9d09a022023-11-28 19:25:00 +00004975 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4976 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4977 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4978 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 status = psa_aead_check_nonce_length(alg, nonce_length);
4981 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004984
Ronald Cronde822812021-03-17 16:08:20 +01004985 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004986 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004987 alg,
4988 nonce, nonce_length,
4989 additional_data, additional_data_length,
4990 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4994 memset(ciphertext, 0, ciphertext_size);
4995 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004996
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004997exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004998 LOCAL_INPUT_FREE(nonce_external, nonce);
4999 LOCAL_INPUT_FREE(additional_data_external, additional_data);
5000 LOCAL_INPUT_FREE(plaintext_external, plaintext);
5001 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5002
Ryan Everetta103ec92024-01-31 13:59:57 +00005003 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03005004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02005006}
5007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
5009 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00005010 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005012 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005014 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00005016 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 size_t plaintext_size,
5018 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03005019{
Ronald Cron215633c2021-03-16 17:15:37 +01005020 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5021 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02005022
David Horstmann7f2e0402023-11-28 19:43:53 +00005023 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5024 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
5025 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
5026 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5027
Gilles Peskineee652a32018-06-01 19:23:52 +02005028 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 status = psa_aead_check_algorithm(alg);
5031 if (status != PSA_SUCCESS) {
5032 return status;
5033 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01005034
Ronald Cron9a986162021-03-26 12:40:07 +01005035 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
5037 if (status != PSA_SUCCESS) {
5038 return status;
5039 }
mohammad16035955c982018-04-26 00:53:03 +03005040
David Horstmann7f2e0402023-11-28 19:43:53 +00005041 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
5042 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
5043 additional_data);
5044 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
5045 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 status = psa_aead_check_nonce_length(alg, nonce_length);
5048 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005051
Ronald Cronde822812021-03-17 16:08:20 +01005052 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005053 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01005054 alg,
5055 nonce, nonce_length,
5056 additional_data, additional_data_length,
5057 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02005059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (status != PSA_SUCCESS && plaintext_size != 0) {
5061 memset(plaintext, 0, plaintext_size);
5062 }
mohammad160360a64d02018-06-03 17:20:42 +03005063
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005064exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00005065 LOCAL_INPUT_FREE(nonce_external, nonce);
5066 LOCAL_INPUT_FREE(additional_data_external, additional_data);
5067 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
5068 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5069
Ryan Everetta103ec92024-01-31 13:59:57 +00005070 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 return status;
mohammad16035955c982018-04-26 00:53:03 +03005073}
5074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
5076{
5077 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02005080#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005082 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
5084 return PSA_ERROR_INVALID_ARGUMENT;
5085 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005086 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005087#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005088
Przemek Stekiel86679c72022-10-06 17:06:56 +02005089#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005091 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
5093 return PSA_ERROR_INVALID_ARGUMENT;
5094 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005095 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005096#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005097
Przemek Stekiel86679c72022-10-06 17:06:56 +02005098#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005100 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 if (tag_len != 16) {
5102 return PSA_ERROR_INVALID_ARGUMENT;
5103 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005104 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005105#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005106
5107 default:
5108 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005112}
5113
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005114/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005115static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
5116 int is_encrypt,
5117 mbedtls_svc_key_id_t key,
5118 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01005119{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5121 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
5122 psa_key_slot_t *slot = NULL;
5123 psa_key_usage_t key_usage = 0;
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 status = psa_aead_check_algorithm(alg);
5126 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 }
Paul Elliottc2b71442021-06-24 18:17:52 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005131 status = PSA_ERROR_BAD_STATE;
5132 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005133 }
5134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (operation->nonce_set || operation->lengths_set ||
5136 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005137 status = PSA_ERROR_BAD_STATE;
5138 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005139 }
5140
Gilles Peskinea7a480b2025-02-05 19:41:52 +01005141 /* Make sure the driver-dependent part of the operation is zeroed.
5142 * This is a guarantee we make to drivers. Initializing the operation
5143 * does not necessarily take care of it, since the context is a
5144 * union and initializing a union does not necessarily initialize
5145 * all of its members. */
5146 memset(&operation->ctx, 0, sizeof(operation->ctx));
5147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005149 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005151 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
5155 alg);
5156 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (is_encrypt) {
5165 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005166 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 slot->key.data,
5168 slot->key.bytes,
5169 alg);
5170 } else {
5171 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005172 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 slot->key.data,
5174 slot->key.bytes,
5175 alg);
5176 }
5177 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005180
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005181 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005182
5183exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00005184 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005187 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01005189 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 } else {
5191 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005192 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01005195}
5196
Paul Elliott302ff6b2021-04-20 18:10:30 +01005197/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
5199 mbedtls_svc_key_id_t key,
5200 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005201{
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005203}
5204
5205/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005206psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
5207 mbedtls_svc_key_id_t key,
5208 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005209{
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005211}
5212
David Horstmannfed23772023-12-19 17:49:20 +00005213static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
5214 const uint8_t *nonce,
5215 size_t nonce_length)
5216{
5217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5218
5219 if (operation->id == 0) {
5220 status = PSA_ERROR_BAD_STATE;
5221 goto exit;
5222 }
5223
5224 if (operation->nonce_set) {
5225 status = PSA_ERROR_BAD_STATE;
5226 goto exit;
5227 }
5228
5229 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
5230 if (status != PSA_SUCCESS) {
5231 status = PSA_ERROR_INVALID_ARGUMENT;
5232 goto exit;
5233 }
5234
5235 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
5236 nonce_length);
5237
5238exit:
5239 if (status == PSA_SUCCESS) {
5240 operation->nonce_set = 1;
5241 } else {
5242 psa_aead_abort(operation);
5243 }
5244
5245 return status;
5246}
5247
Paul Elliott302ff6b2021-04-20 18:10:30 +01005248/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00005250 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 size_t nonce_size,
5252 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005253{
5254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01005255 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07005256 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005257
David Horstmannd3cad8b2023-12-11 14:46:04 +00005258 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
5259 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
5260
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005261 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005264 status = PSA_ERROR_BAD_STATE;
5265 goto exit;
5266 }
5267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005269 status = PSA_ERROR_BAD_STATE;
5270 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005271 }
5272
Paul Elliotte193ea82021-10-01 13:00:16 +01005273 /* For CCM, this size may not be correct according to the PSA
5274 * specification. The PSA Crypto 1.0.1 specification states:
5275 *
5276 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5277 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5278 *
5279 * However this restriction that L has to be the smallest integer is not
5280 * applied in practice, and it is not implementable here since the
5281 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5283 operation->alg);
5284 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005285 status = PSA_ERROR_BUFFER_TOO_SMALL;
5286 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005287 }
5288
David Horstmann6e99bb22024-02-06 15:27:49 +00005289 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005291 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005293
David Horstmannfed23772023-12-19 17:49:20 +00005294 status = psa_aead_set_nonce_internal(operation, local_nonce,
5295 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005296
Paul Elliott39dc6b82021-05-11 19:16:09 +01005297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (status == PSA_SUCCESS) {
5299 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005300 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 } else {
5302 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005303 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005304
David Horstmannd3cad8b2023-12-11 14:46:04 +00005305 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005308}
5309
5310/* Set the nonce for a multipart authenticated encryption or decryption
5311 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005312psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005313 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005315{
David Horstmannfed23772023-12-19 17:49:20 +00005316 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005317
David Horstmann8f0ef512023-12-11 15:17:11 +00005318 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5319 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005320
David Horstmannfed23772023-12-19 17:49:20 +00005321 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005322
David Horstmann18dc0322023-12-20 16:16:43 +00005323/* Exit label is only needed for buffer copying, prevent unused warnings. */
David Horstmann4a48bec2024-03-13 15:42:31 +00005324#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005325exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005326#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005327
David Horstmann8f0ef512023-12-11 15:17:11 +00005328 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005331}
5332
5333/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005334psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5335 size_t ad_length,
5336 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005337{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005341 status = PSA_ERROR_BAD_STATE;
5342 goto exit;
5343 }
5344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (operation->lengths_set || operation->ad_started ||
5346 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005347 status = PSA_ERROR_BAD_STATE;
5348 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005349 }
5350
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005352#if defined(PSA_WANT_ALG_GCM)
5353 case PSA_ALG_GCM:
5354 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 * bits. Without the guard this code will generate warnings on 32bit
5356 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005357#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 if (((uint64_t) ad_length) >> 61 != 0 ||
5359 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005360 status = PSA_ERROR_INVALID_ARGUMENT;
5361 goto exit;
5362 }
Paul Elliott325d3742021-09-27 17:56:28 +01005363#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005364 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005365#endif /* PSA_WANT_ALG_GCM */
5366#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005367 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005369 status = PSA_ERROR_INVALID_ARGUMENT;
5370 goto exit;
5371 }
5372 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005373#endif /* PSA_WANT_ALG_CCM */
5374#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005375 case PSA_ALG_CHACHA20_POLY1305:
5376 /* No length restrictions for ChaChaPoly. */
5377 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005378#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005379 default:
5380 break;
5381 }
Paul Elliott325d3742021-09-27 17:56:28 +01005382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5384 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005385
Paul Elliott39dc6b82021-05-11 19:16:09 +01005386exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005388 operation->ad_remaining = ad_length;
5389 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005390 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 } else {
5392 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005393 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005396}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005397
5398/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005399psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005400 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005402{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5404
David Horstmann25dac6e2023-12-11 15:23:13 +00005405 LOCAL_INPUT_DECLARE(input_external, input);
5406 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005409 status = PSA_ERROR_BAD_STATE;
5410 goto exit;
5411 }
5412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005414 status = PSA_ERROR_BAD_STATE;
5415 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005416 }
5417
Paul Elliott304766f2024-04-26 18:30:11 +01005418 /* No input to add (zero length), nothing to do. */
5419 if (input_length == 0) {
5420 status = PSA_SUCCESS;
5421 goto exit;
5422 }
5423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (operation->lengths_set) {
5425 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005426 status = PSA_ERROR_INVALID_ARGUMENT;
5427 goto exit;
5428 }
5429
5430 operation->ad_remaining -= input_length;
5431 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005432#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005434 status = PSA_ERROR_BAD_STATE;
5435 goto exit;
5436 }
5437#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 status = psa_driver_wrapper_aead_update_ad(operation, input,
5440 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005441
Paul Elliott39dc6b82021-05-11 19:16:09 +01005442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005444 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 } else {
5446 psa_aead_abort(operation);
5447 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005448
David Horstmann25dac6e2023-12-11 15:23:13 +00005449 LOCAL_INPUT_FREE(input_external, input);
5450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005452}
5453
5454/* Encrypt or decrypt a message fragment in an active multipart AEAD
5455 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005456psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005457 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005459 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 size_t output_size,
5461 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005462{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005464
David Horstmann2914fac2023-12-11 15:28:37 +00005465
5466 LOCAL_INPUT_DECLARE(input_external, input);
5467 LOCAL_OUTPUT_DECLARE(output_external, output);
5468
5469 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5470 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5471
Paul Elliott302ff6b2021-04-20 18:10:30 +01005472 *output_length = 0;
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005475 status = PSA_ERROR_BAD_STATE;
5476 goto exit;
5477 }
5478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005480 status = PSA_ERROR_BAD_STATE;
5481 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005482 }
5483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005485 /* Additional data length was supplied, but not all the additional
5486 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005488 status = PSA_ERROR_INVALID_ARGUMENT;
5489 goto exit;
5490 }
5491
5492 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005494 status = PSA_ERROR_INVALID_ARGUMENT;
5495 goto exit;
5496 }
5497
5498 operation->body_remaining -= input_length;
5499 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005500#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005502 status = PSA_ERROR_BAD_STATE;
5503 goto exit;
5504 }
5505#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5508 output, output_size,
5509 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005510
Paul Elliott39dc6b82021-05-11 19:16:09 +01005511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005513 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 } else {
5515 psa_aead_abort(operation);
5516 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005517
David Horstmann2914fac2023-12-11 15:28:37 +00005518 LOCAL_INPUT_FREE(input_external, input);
5519 LOCAL_OUTPUT_FREE(output_external, output);
5520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005522}
5523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005525{
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 if (operation->id == 0 || !operation->nonce_set) {
5527 return PSA_ERROR_BAD_STATE;
5528 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5531 operation->body_remaining != 0)) {
5532 return PSA_ERROR_INVALID_ARGUMENT;
5533 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005536}
5537
Paul Elliott302ff6b2021-04-20 18:10:30 +01005538/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005539psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005540 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 size_t ciphertext_size,
5542 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005543 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 size_t tag_size,
5545 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005546{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5548
David Horstmann6db0e732023-12-11 15:35:59 +00005549 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5550 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5551
5552 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5553 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5554
Paul Elliott302ff6b2021-04-20 18:10:30 +01005555 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005556 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 status = psa_aead_final_checks(operation);
5559 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005564 status = PSA_ERROR_BAD_STATE;
5565 goto exit;
5566 }
5567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5569 ciphertext_size,
5570 ciphertext_length,
5571 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005572
Paul Elliott39dc6b82021-05-11 19:16:09 +01005573exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005574
5575
Paul Elliottf47b0952021-05-21 18:02:33 +01005576 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005577 * the zero tag size, make sure the tag is set to something implausible.
5578 * Even if the operation succeeds, make sure we clear the rest of the
5579 * buffer to prevent potential leakage of anything previously placed in
5580 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005581 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005584
David Horstmann6db0e732023-12-11 15:35:59 +00005585 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5586 LOCAL_OUTPUT_FREE(tag_external, tag);
5587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005589}
5590
5591/* Finish authenticating and decrypting a message in a multipart AEAD
5592 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005593psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005594 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 size_t plaintext_size,
5596 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005597 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005599{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5601
David Horstmanne000a0a2023-12-11 16:37:04 +00005602 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5603 LOCAL_INPUT_DECLARE(tag_external, tag);
5604
5605 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5606 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5607
Paul Elliott302ff6b2021-04-20 18:10:30 +01005608 *plaintext_length = 0;
5609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 status = psa_aead_final_checks(operation);
5611 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005616 status = PSA_ERROR_BAD_STATE;
5617 goto exit;
5618 }
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5621 plaintext_size,
5622 plaintext_length,
5623 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005624
5625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005627
David Horstmanne000a0a2023-12-11 16:37:04 +00005628 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5629 LOCAL_INPUT_FREE(tag_external, tag);
5630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005632}
5633
5634/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005636{
5637 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005640 /* The object has (apparently) been initialized but it is not (yet)
5641 * in use. It's ok to call abort on such an object, and there's
5642 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005644 }
5645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005651}
5652
Gilles Peskinee59236f2018-01-27 23:32:46 +01005653/****************************************************************/
Gilles Peskinecdb37812025-02-05 18:02:49 +01005654/* Key derivation: output generation */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005655/****************************************************************/
5656
Przemek Stekiel69c46792022-06-10 12:59:51 +02005657#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005658 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005659 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305660 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305661 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005662#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005663#endif /* At least one builtin KDF */
5664
Przemek Stekiel69c46792022-06-10 12:59:51 +02005665#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005666 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5667 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005668
5669/** Internal helper to set up an HMAC operation with a key passed directly.
5670 *
5671 * \param[in,out] operation A MAC operation object. It does not need to
5672 * be initialized.
5673 * \param hash_alg The hash algorithm used for HMAC.
5674 * \param hmac_key The HMAC key.
5675 * \param hmac_key_length Length of \p hmac_key in bytes.
5676 *
5677 * \return A PSA status code.
5678 */
Steven Cooreman094a77e2021-05-06 17:58:36 +02005679static psa_status_t psa_key_derivation_start_hmac(
5680 psa_mac_operation_t *operation,
5681 psa_algorithm_t hash_alg,
5682 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005684{
5685 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5688 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5689 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005690
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005691 /* Make sure the whole the operation is zeroed.
5692 * It isn't enough to require the caller to initialize operation to
5693 * PSA_MAC_OPERATION_INIT, since one field is a union and initializing
5694 * a union does not necessarily initialize all of its members.
Gilles Peskine91b29a02025-05-13 11:53:31 +02005695 * psa_mac_setup() would handle PSA_MAC_OPERATION_INIT, but here we
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005696 * bypass it and call lower-level functions directly. */
5697 memset(operation, 0, sizeof(*operation));
5698
Steven Cooreman72f736a2021-05-07 14:14:37 +02005699 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 status = psa_driver_wrapper_mac_sign_setup(operation,
5703 &attributes,
5704 hmac_key, hmac_key_length,
5705 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 psa_reset_key_attributes(&attributes);
5708 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005709}
5710#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005711
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005712#define HKDF_STATE_INIT 0 /* no input yet */
5713#define HKDF_STATE_STARTED 1 /* got salt */
5714#define HKDF_STATE_KEYED 2 /* got key */
5715#define HKDF_STATE_OUTPUT 3 /* output started */
5716
Gilles Peskinecbe66502019-05-16 16:59:18 +02005717static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005719{
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5721 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5722 } else {
5723 return operation->alg;
5724 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725}
5726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005728{
5729 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5731 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005732 /* The object has (apparently) been initialized but it is not
5733 * in use. It's ok to call abort on such an object, and there's
5734 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005736#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5738 mbedtls_free(operation->ctx.hkdf.info);
5739 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5740 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005741#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005742#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5743 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5745 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5746 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5747 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005748 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005750 }
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005753 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005755 }
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005758 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005760 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005761#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005763 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005765 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005766#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005767 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005768
5769 /* We leave the fields Ai and output_block to be erased safely by the
5770 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005772#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5773 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005774#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5776 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5777 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5778 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005779#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305780#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305781 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305782 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005783 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305784 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305785 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305786
5787 status = PSA_SUCCESS;
5788 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305789#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005790 {
5791 status = PSA_ERROR_BAD_STATE;
5792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 mbedtls_platform_zeroize(operation, sizeof(*operation));
5794 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005795}
5796
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005797psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005799{
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005803 }
5804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005807}
5808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5810 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005811{
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (operation->alg == 0) {
5813 return PSA_ERROR_BAD_STATE;
5814 }
5815 if (capacity > operation->capacity) {
5816 return PSA_ERROR_INVALID_ARGUMENT;
5817 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005818 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005820}
5821
Przemek Stekiel69c46792022-06-10 12:59:51 +02005822#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005823/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005824static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5825 psa_algorithm_t kdf_alg,
5826 uint8_t *output,
5827 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005828{
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5830 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005831 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005832 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005833#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005835#else
5836 const uint8_t last_block = 0xff;
5837#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 if (hkdf->state < HKDF_STATE_KEYED ||
5840 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005841#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005843#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 )) {
5845 return PSA_ERROR_BAD_STATE;
5846 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005847 hkdf->state = HKDF_STATE_OUTPUT;
5848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005850 /* Copy what remains of the current block */
5851 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005853 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 }
5855 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005856 output += n;
5857 output_length -= n;
5858 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005860 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005862 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005863 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005864 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005865 * object was corrupted or if this function is called directly
5866 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (hkdf->block_number == last_block) {
5868 return PSA_ERROR_BAD_STATE;
5869 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005870
5871 /* We need a new block */
5872 ++hkdf->block_number;
5873 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5876 hash_alg,
5877 hkdf->prk,
5878 hash_length);
5879 if (status != PSA_SUCCESS) {
5880 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005882
5883 if (hkdf->block_number != 1) {
5884 status = psa_mac_update(&hkdf->hmac,
5885 hkdf->output_block,
5886 hash_length);
5887 if (status != PSA_SUCCESS) {
5888 return status;
5889 }
5890 }
5891 status = psa_mac_update(&hkdf->hmac,
5892 hkdf->info,
5893 hkdf->info_length);
5894 if (status != PSA_SUCCESS) {
5895 return status;
5896 }
5897 status = psa_mac_update(&hkdf->hmac,
5898 &hkdf->block_number, 1);
5899 if (status != PSA_SUCCESS) {
5900 return status;
5901 }
5902 status = psa_mac_sign_finish(&hkdf->hmac,
5903 hkdf->output_block,
5904 sizeof(hkdf->output_block),
5905 &hmac_output_length);
5906 if (status != PSA_SUCCESS) {
5907 return status;
5908 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005909 }
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005912}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005913#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005914
John Durkop07cc04a2020-11-16 22:08:34 -08005915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5916 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005917static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5918 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005920{
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5922 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Gilles Peskinebbec1c12025-02-05 19:53:28 +01005923 psa_mac_operation_t hmac;
Steven Cooremana6df6042021-04-29 19:32:25 +02005924 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005925 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005926
Janos Follath7742fee2019-06-17 12:58:10 +01005927 /* We can't be wanting more output after block 0xff, otherwise
5928 * the capacity check in psa_key_derivation_output_bytes() would have
5929 * prevented this call. It could happen only if the operation
5930 * object was corrupted or if this function is called directly
5931 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (tls12_prf->block_number == 0xff) {
5933 return PSA_ERROR_CORRUPTION_DETECTED;
5934 }
Janos Follath7742fee2019-06-17 12:58:10 +01005935
5936 /* We need a new block */
5937 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005938 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005939
5940 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5941 *
5942 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5943 *
5944 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5945 * HMAC_hash(secret, A(2) + seed) +
5946 * HMAC_hash(secret, A(3) + seed) + ...
5947 *
5948 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005949 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005950 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005951 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005952 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005953 * is currently extracted as `output_block` and where i is
5954 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005955 */
5956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 status = psa_key_derivation_start_hmac(&hmac,
5958 hash_alg,
5959 tls12_prf->secret,
5960 tls12_prf->secret_length);
5961 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005962 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005964
5965 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005967 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5968 * the variable seed and in this instance means it in the context of the
5969 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 status = psa_mac_update(&hmac,
5971 tls12_prf->label,
5972 tls12_prf->label_length);
5973 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005974 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 }
5976 status = psa_mac_update(&hmac,
5977 tls12_prf->seed,
5978 tls12_prf->seed_length);
5979 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005980 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 }
5982 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005983 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5985 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005986 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005988 }
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 status = psa_mac_sign_finish(&hmac,
5991 tls12_prf->Ai, hash_length,
5992 &hmac_output_length);
5993 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005994 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
5996 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005997 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005999
6000 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 status = psa_key_derivation_start_hmac(&hmac,
6002 hash_alg,
6003 tls12_prf->secret,
6004 tls12_prf->secret_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->Ai, hash_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->label, tls12_prf->label_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_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
6017 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006018 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 }
6020 status = psa_mac_sign_finish(&hmac,
6021 tls12_prf->output_block, hash_length,
6022 &hmac_output_length);
6023 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006024 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 }
Janos Follathea29bfb2019-06-19 12:21:20 +01006026
Janos Follath7742fee2019-06-17 12:58:10 +01006027
6028cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 cleanup_status = psa_mac_abort(&hmac);
6030 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01006031 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 }
Janos Follathea29bfb2019-06-19 12:21:20 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01006035}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01006036
Janos Follath844eb0e2019-06-19 12:10:49 +01006037static psa_status_t psa_key_derivation_tls12_prf_read(
6038 psa_tls12_prf_key_derivation_t *tls12_prf,
6039 psa_algorithm_t alg,
6040 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01006042{
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
6044 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01006045 psa_status_t status;
6046 uint8_t offset, length;
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02006049 case PSA_TLS12_PRF_STATE_LABEL_SET:
6050 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
6051 break;
6052 case PSA_TLS12_PRF_STATE_OUTPUT:
6053 break;
6054 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02006056 }
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01006059 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (tls12_prf->left_in_block == 0) {
6061 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
6062 alg);
6063 if (status != PSA_SUCCESS) {
6064 return status;
6065 }
Janos Follath844eb0e2019-06-19 12:10:49 +01006066
6067 continue;
6068 }
6069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01006071 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01006073 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 }
Janos Follath844eb0e2019-06-19 12:10:49 +01006075
6076 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01006078 output += length;
6079 output_length -= length;
6080 tls12_prf->left_in_block -= length;
6081 }
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01006084}
John Durkop07cc04a2020-11-16 22:08:34 -08006085#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6086 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02006087
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006088#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6089static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
6090 psa_tls12_ecjpake_to_pms_t *ecjpake,
6091 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006093{
6094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006095 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (output_length != 32) {
6098 return PSA_ERROR_INVALID_ARGUMENT;
6099 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
6102 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
6103 &output_size);
6104 if (status != PSA_SUCCESS) {
6105 return status;
6106 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (output_size != output_length) {
6109 return PSA_ERROR_GENERIC_ERROR;
6110 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006113}
6114#endif
6115
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306116#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306117static psa_status_t psa_key_derivation_pbkdf2_generate_block(
6118 psa_pbkdf2_key_derivation_t *pbkdf2,
6119 psa_algorithm_t prf_alg,
6120 uint8_t prf_output_length,
6121 psa_key_attributes_t *attributes)
6122{
6123 psa_status_t status;
Gilles Peskinebbec1c12025-02-05 19:53:28 +01006124 psa_mac_operation_t mac_operation;
6125 /* Make sure the whole the operation is zeroed.
6126 * PSA_MAC_OPERATION_INIT does not necessarily do it fully,
6127 * since one field is a union and initializing a union does not
6128 * necessarily initialize all of its members.
6129 * psa_mac_setup() would do it, but here we bypass it and call
6130 * lower-level functions directly. */
6131 memset(&mac_operation, 0, sizeof(mac_operation));
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306132 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306133 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306134 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306135 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306136 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306137
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306138 mac_operation.is_sign = 1;
6139 mac_operation.mac_size = prf_output_length;
6140 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306141
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306142 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
6143 attributes,
6144 pbkdf2->password,
6145 pbkdf2->password_length,
6146 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306147 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306148 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306149 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306150 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
6151 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306152 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306153 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306154 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306155 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306156 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306157 }
6158 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
6159 &mac_output_length);
6160 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306161 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306162 }
6163
6164 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306165 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306166 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306167 }
6168
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306169 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306170
6171 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306172 /* We are passing prf_output_length as mac_size because the driver
6173 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306174 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306175 status = psa_driver_wrapper_mac_compute(attributes,
6176 pbkdf2->password,
6177 pbkdf2->password_length,
6178 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306179 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306180 &mac_output_length);
6181 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306182 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306183 }
6184
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05306185 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306186 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306187
6188cleanup:
6189 /* Zeroise buffers to clear sensitive data from memory. */
6190 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
6191 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306192}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306193
6194static psa_status_t psa_key_derivation_pbkdf2_read(
6195 psa_pbkdf2_key_derivation_t *pbkdf2,
6196 psa_algorithm_t kdf_alg,
6197 uint8_t *output,
6198 size_t output_length)
6199{
6200 psa_status_t status;
6201 psa_algorithm_t prf_alg;
6202 uint8_t prf_output_length;
6203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6204 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
6205 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
6206
6207 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6208 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
6209 prf_output_length = PSA_HASH_LENGTH(prf_alg);
6210 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306211 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6212 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306213 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306214 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05306215 } else {
6216 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306217 }
6218
6219 switch (pbkdf2->state) {
6220 case PSA_PBKDF2_STATE_PASSWORD_SET:
6221 /* Initially we need a new block so bytes_used is equal to block size*/
6222 pbkdf2->bytes_used = prf_output_length;
6223 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
6224 break;
6225 case PSA_PBKDF2_STATE_OUTPUT:
6226 break;
6227 default:
6228 return PSA_ERROR_BAD_STATE;
6229 }
6230
6231 while (output_length != 0) {
6232 uint8_t n = prf_output_length - pbkdf2->bytes_used;
6233 if (n > output_length) {
6234 n = (uint8_t) output_length;
6235 }
6236 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
6237 output += n;
6238 output_length -= n;
6239 pbkdf2->bytes_used += n;
6240
6241 if (output_length == 0) {
6242 break;
6243 }
6244
6245 /* We need a new block */
6246 pbkdf2->bytes_used = 0;
6247 pbkdf2->block_number++;
6248
6249 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
6250 prf_output_length,
6251 &attributes);
6252 if (status != PSA_SUCCESS) {
6253 return status;
6254 }
6255 }
6256
6257 return PSA_SUCCESS;
6258}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306259#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306260
Janos Follath6c6c8fc2019-06-17 12:38:20 +01006261psa_status_t psa_key_derivation_output_bytes(
6262 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00006263 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006265{
6266 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00006267 LOCAL_OUTPUT_DECLARE(output_external, output);
6268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006272 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006274 }
6275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006277 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006278 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02006279 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
6280 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006281 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02006282 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006284 }
Ryan Everettf943e222024-01-19 14:46:39 +00006285
6286 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00006287 if (output_length > operation->capacity) {
6288 operation->capacity = 0;
6289 /* Go through the error path to wipe all confidential data now
6290 * that the operation object is useless. */
6291 status = PSA_ERROR_INSUFFICIENT_DATA;
6292 goto exit;
6293 }
6294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006296
Przemek Stekiel69c46792022-06-10 12:59:51 +02006297#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6299 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6300 output, output_length);
6301 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006302#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006303#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6304 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6306 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6307 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6308 kdf_alg, output,
6309 output_length);
6310 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006311#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6312 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006313#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006315 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6317 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006318#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306319#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306320 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306321 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6322 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306323 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306324#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006325
Gilles Peskineeab56e42018-07-12 17:12:33 +02006326 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006327 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006328 status = PSA_ERROR_BAD_STATE;
6329 LOCAL_OUTPUT_FREE(output_external, output);
6330
6331 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006332 }
6333
6334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006336 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006337 * This allows us to differentiate between exhausted operations and
6338 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6339 * operations. */
6340 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006342 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006343 if (output != NULL) {
6344 memset(output, '!', output_length);
6345 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006346 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006347
6348 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006350}
6351
David Brown7807bf72021-02-09 16:28:23 -07006352#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006353static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006354{
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (data_size >= 8) {
6356 mbedtls_des_key_set_parity(data);
6357 }
6358 if (data_size >= 16) {
6359 mbedtls_des_key_set_parity(data + 8);
6360 }
6361 if (data_size >= 24) {
6362 mbedtls_des_key_set_parity(data + 16);
6363 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006364}
David Brown7807bf72021-02-09 16:28:23 -07006365#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006366
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006367/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 * ECC keys on a Weierstrass elliptic curve require the generation
6369 * of a private key which is an integer
6370 * in the range [1, N - 1], where N is the boundary of the private key domain:
6371 * N is the prime p for Diffie-Hellman, or the order of the
6372 * curve’s base point for ECC.
6373 *
6374 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6375 * This function generates the private key using the following process:
6376 *
6377 * 1. Draw a byte string of length ceiling(m/8) bytes.
6378 * 2. If m is not a multiple of 8, set the most significant
6379 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6380 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6381 * 4. If k > N - 2, discard the result and return to step 1.
6382 * 5. Output k + 1 as the private key.
6383 *
6384 * This method allows compliance to NIST standards, specifically the methods titled
6385 * Key-Pair Generation by Testing Candidates in the following publications:
6386 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6387 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6388 * Diffie-Hellman keys.
6389 *
6390 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6391 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6392 *
6393 * Note: Function allocates memory for *data buffer, so given *data should be
6394 * always NULL.
6395 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006396#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6397#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006398static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006399 psa_key_slot_t *slot,
6400 size_t bits,
6401 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006402 uint8_t **data
6403 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006404{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006405 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006406 mbedtls_mpi k;
6407 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006410 size_t m;
Gilles Peskinece726b22025-03-06 19:27:50 +01006411 size_t m_bytes = 0;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 mbedtls_mpi_init(&k);
6414 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006415
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006416 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006418 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006419 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006422 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006423 goto cleanup;
6424 }
6425
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006426 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006430
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006431 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006432 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006433 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006434
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006435 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006436
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006437 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006439
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006440 /* Note: This function is always called with *data == NULL and it
6441 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 *data = mbedtls_calloc(1, m_bytes);
6443 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006444 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006445 goto cleanup;
6446 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006449 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006451 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006453
6454 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006456 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006457 * (8 * ceiling(m/8) - m) bits of the first byte in
6458 * the string to zero.
6459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006461 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006462 }
6463
6464 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 * big-endian byte string.
6466 */
6467 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006468
6469 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 * Result of comparison is returned. When it indicates error
6471 * then this function is called again.
6472 */
6473 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006474 }
6475
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006476 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6478 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006479cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (ret != 0) {
6481 status = mbedtls_to_psa_error(ret);
6482 }
6483 if (status != PSA_SUCCESS) {
Gilles Peskine184cac12025-03-06 12:42:30 +01006484 mbedtls_zeroize_and_free(*data, m_bytes);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006485 *data = NULL;
6486 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 mbedtls_mpi_free(&k);
6488 mbedtls_mpi_free(&diff_N_2);
6489 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006490}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006491
6492/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6493 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6494 *
6495 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6496 * draw a 32-byte string and process it as specified in
6497 * Elliptic Curves for Security [RFC7748] §5.
6498 *
6499 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6500 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6501 *
6502 * Note: Function allocates memory for *data buffer, so given *data should be
6503 * always NULL.
6504 */
6505
6506static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6507 size_t bits,
6508 psa_key_derivation_operation_t *operation,
6509 uint8_t **data
6510 )
6511{
6512 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006513 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006516 case 255:
6517 output_length = 32;
6518 break;
6519 case 448:
6520 output_length = 56;
6521 break;
6522 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006524 break;
6525 }
6526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 if (*data == NULL) {
6530 return PSA_ERROR_INSUFFICIENT_MEMORY;
6531 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006536 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006540 case 255:
6541 (*data)[0] &= 248;
6542 (*data)[31] &= 127;
6543 (*data)[31] |= 64;
6544 break;
6545 case 448:
6546 (*data)[0] &= 252;
6547 (*data)[55] |= 128;
6548 break;
6549 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006551 break;
6552 }
6553
6554 return status;
6555}
Valerio Setti86587ab2023-06-27 13:09:30 +02006556#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6557static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6558 psa_key_slot_t *slot, size_t bits,
6559 psa_key_derivation_operation_t *operation, uint8_t **data)
6560{
6561 (void) slot;
6562 (void) bits;
6563 (void) operation;
6564 (void) data;
6565 return PSA_ERROR_NOT_SUPPORTED;
6566}
6567
6568static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6569 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6570{
6571 (void) bits;
6572 (void) operation;
6573 (void) data;
6574 return PSA_ERROR_NOT_SUPPORTED;
6575}
6576#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6577#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006578
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006579static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006580 psa_key_slot_t *slot,
6581 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006583{
6584 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306586 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6590 return PSA_ERROR_INVALID_ARGUMENT;
6591 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006592
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006593#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006594 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6596 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6597 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006598 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6600 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006601 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 }
6603 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006604 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6606 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006609 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006610 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006611#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006612 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 if (key_type_is_raw_bytes(slot->attr.type)) {
6614 if (bits % 8 != 0) {
6615 return PSA_ERROR_INVALID_ARGUMENT;
6616 }
6617 data = mbedtls_calloc(1, bytes);
6618 if (data == NULL) {
6619 return PSA_ERROR_INSUFFICIENT_MEMORY;
6620 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 status = psa_key_derivation_output_bytes(operation, data, bytes);
6623 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 }
David Brown12ca5032021-02-11 11:02:00 -07006626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6628 psa_des_set_key_parity(data, bytes);
6629 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006630#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 } else {
6632 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006633 }
Ronald Crondd04d422020-11-28 15:14:42 +01006634
Ronald Cronbf33c932020-11-28 18:06:53 +01006635 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006636
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006637 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006638 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 &storage_size);
6640 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306641 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 }
Archanad8a83dc2021-06-14 10:04:16 +05306643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 status = psa_allocate_buffer_to_slot(slot, storage_size);
6645 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 }
Archanad8a83dc2021-06-14 10:04:16 +05306648
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006649 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 data, bytes,
6651 slot->key.data,
6652 slot->key.bytes,
6653 &slot->key.bytes, &bits);
6654 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006655 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006657
6658exit:
Gilles Peskine184cac12025-03-06 12:42:30 +01006659 mbedtls_zeroize_and_free(data, bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006661}
6662
Gilles Peskinef36d7852024-06-06 21:11:44 +02006663static const psa_custom_key_parameters_t default_custom_production =
Gilles Peskine4a85ff32024-07-18 18:52:59 +02006664 PSA_CUSTOM_KEY_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006665
Gilles Peskine52504f82024-06-20 17:19:58 +02006666int psa_custom_key_parameters_are_default(
Gilles Peskinef36d7852024-06-06 21:11:44 +02006667 const psa_custom_key_parameters_t *custom,
6668 size_t custom_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006669{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006670 if (custom->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006671 return 0;
6672 }
Gilles Peskinef36d7852024-06-06 21:11:44 +02006673 if (custom_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006674 return 0;
6675 }
6676 return 1;
6677}
6678
Gilles Peskinef36d7852024-06-06 21:11:44 +02006679psa_status_t psa_key_derivation_output_key_custom(
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006680 const psa_key_attributes_t *attributes,
6681 psa_key_derivation_operation_t *operation,
Gilles Peskinef36d7852024-06-06 21:11:44 +02006682 const psa_custom_key_parameters_t *custom,
6683 const uint8_t *custom_data,
6684 size_t custom_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006685 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006686{
6687 psa_status_t status;
6688 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006689 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006690
Ronald Cron81709fc2020-11-14 12:10:32 +01006691 *key = MBEDTLS_SVC_KEY_ID_INIT;
6692
Gilles Peskine0f84d622019-09-12 19:03:13 +02006693 /* Reject any attempt to create a zero-length key so that we don't
6694 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 if (psa_get_key_bits(attributes) == 0) {
6696 return PSA_ERROR_INVALID_ARGUMENT;
6697 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006698
Gilles Peskinef36d7852024-06-06 21:11:44 +02006699 (void) custom_data; /* We only accept 0-length data */
Gilles Peskine52504f82024-06-20 17:19:58 +02006700 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006701 return PSA_ERROR_INVALID_ARGUMENT;
6702 }
6703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (operation->alg == PSA_ALG_NONE) {
6705 return PSA_ERROR_BAD_STATE;
6706 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006707
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 if (!operation->can_output_key) {
6709 return PSA_ERROR_NOT_PERMITTED;
6710 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6713 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006714#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006716 /* Deriving a key in a secure element is not implemented yet. */
6717 status = PSA_ERROR_NOT_SUPPORTED;
6718 }
6719#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 if (status == PSA_SUCCESS) {
6721 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006722 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006724 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 if (status == PSA_SUCCESS) {
6726 status = psa_finish_key_creation(slot, driver, key);
6727 }
6728 if (status != PSA_SUCCESS) {
6729 psa_fail_key_creation(slot, driver);
6730 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006733}
6734
Gilles Peskinef36d7852024-06-06 21:11:44 +02006735psa_status_t psa_key_derivation_output_key_ext(
6736 const psa_key_attributes_t *attributes,
6737 psa_key_derivation_operation_t *operation,
6738 const psa_key_production_parameters_t *params,
6739 size_t params_data_length,
6740 mbedtls_svc_key_id_t *key)
6741{
6742 return psa_key_derivation_output_key_custom(
6743 attributes, operation,
6744 (const psa_custom_key_parameters_t *) params,
6745 params->data, params_data_length,
6746 key);
6747}
6748
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006749psa_status_t psa_key_derivation_output_key(
6750 const psa_key_attributes_t *attributes,
6751 psa_key_derivation_operation_t *operation,
6752 mbedtls_svc_key_id_t *key)
6753{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006754 return psa_key_derivation_output_key_custom(attributes, operation,
6755 &default_custom_production,
6756 NULL, 0,
6757 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006758}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006759
6760
6761/****************************************************************/
Gilles Peskinecdb37812025-02-05 18:02:49 +01006762/* Key derivation: operation management */
Gilles Peskineea0fb492018-07-12 17:17:20 +02006763/****************************************************************/
6764
Steven Cooreman932ffb72021-02-15 12:14:32 +01006765#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006766static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006767{
6768#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6770 return 1;
6771 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006772#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006773#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6775 return 1;
6776 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006777#endif
6778#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6780 return 1;
6781 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006782#endif
6783#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6785 return 1;
6786 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006787#endif
6788#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6790 return 1;
6791 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006792#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006793#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6795 return 1;
6796 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006797#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306798#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6799 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6800 return 1;
6801 }
6802#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306803#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6804 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6805 return 1;
6806 }
6807#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006809}
6810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006812{
6813 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 psa_status_t status = psa_hash_setup(&operation, alg);
6815 psa_hash_abort(&operation);
6816 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006817}
6818
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306819static psa_status_t psa_key_derivation_set_maximum_capacity(
6820 psa_key_derivation_operation_t *operation,
6821 psa_algorithm_t kdf_alg)
6822{
6823#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6824 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6825 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6826 return PSA_SUCCESS;
6827 }
6828#endif
6829#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6830 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6831#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306832 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306833 PSA_KEY_TYPE_AES,
6834 128U,
6835 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306836#else
6837 operation->capacity = SIZE_MAX;
6838#endif
6839 return PSA_SUCCESS;
6840 }
6841#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6842
6843 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6844 * invalid or meaningless but it does not affect this function */
6845 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6846 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306847 if (hash_size == 0) {
6848 return PSA_ERROR_NOT_SUPPORTED;
6849 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306850
6851 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6852 * we might fail later, which is somewhat unfriendly and potentially
6853 * risk-prone. */
6854 psa_status_t status = psa_hash_try_support(hash_alg);
6855 if (status != PSA_SUCCESS) {
6856 return status;
6857 }
6858
6859#if defined(PSA_WANT_ALG_HKDF)
6860 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6861 operation->capacity = 255 * hash_size;
6862 } else
6863#endif
6864#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6865 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6866 operation->capacity = hash_size;
6867 } else
6868#endif
6869#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6870 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6871 operation->capacity = 255 * hash_size;
6872 } else
6873#endif
6874#if defined(PSA_WANT_ALG_TLS12_PRF)
6875 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6876 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6877 operation->capacity = SIZE_MAX;
6878 } else
6879#endif
6880#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6881 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6882 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6883 /* Master Secret is always 48 bytes
6884 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6885 operation->capacity = 48U;
6886 } else
6887#endif
6888#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6889 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6890#if (SIZE_MAX > UINT32_MAX)
6891 operation->capacity = UINT32_MAX * hash_size;
6892#else
6893 operation->capacity = SIZE_MAX;
6894#endif
6895 } else
6896#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6897 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306898 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306899 status = PSA_ERROR_NOT_SUPPORTED;
6900 }
6901 return status;
6902}
6903
Gilles Peskine969c5d62019-01-16 15:53:06 +01006904static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006905 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006907{
Janos Follath5fe19732019-06-20 15:09:30 +01006908 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6909 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006911
Gilles Peskine969c5d62019-01-16 15:53:06 +01006912 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (!is_kdf_alg_supported(kdf_alg)) {
6914 return PSA_ERROR_NOT_SUPPORTED;
6915 }
John Durkop07cc04a2020-11-16 22:08:34 -08006916
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306917 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6918 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306919 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006920}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006921
Gilles Peskine449bd832023-01-11 14:50:10 +01006922static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006923{
6924#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 if (alg == PSA_ALG_ECDH) {
6926 return PSA_SUCCESS;
6927 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006928#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006929#if defined(PSA_WANT_ALG_FFDH)
6930 if (alg == PSA_ALG_FFDH) {
6931 return PSA_SUCCESS;
6932 }
6933#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006934 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006936}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006937
6938static int psa_key_derivation_allows_free_form_secret_input(
6939 psa_algorithm_t kdf_alg)
6940{
6941#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6942 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6943 return 0;
6944 }
6945#endif
6946 (void) kdf_alg;
6947 return 1;
6948}
John Durkop07cc04a2020-11-16 22:08:34 -08006949#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6952 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006953{
6954 psa_status_t status;
6955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (operation->alg != 0) {
6957 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006958 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6961 return PSA_ERROR_INVALID_ARGUMENT;
6962 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6963#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6964 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6965 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6966 status = psa_key_agreement_try_support(ka_alg);
6967 if (status != PSA_SUCCESS) {
6968 return status;
6969 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006970 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6971 return PSA_ERROR_INVALID_ARGUMENT;
6972 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6974#else
6975 return PSA_ERROR_NOT_SUPPORTED;
6976#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6977 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6978#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6979 status = psa_key_derivation_setup_kdf(operation, alg);
6980#else
6981 return PSA_ERROR_NOT_SUPPORTED;
6982#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6983 } else {
6984 return PSA_ERROR_INVALID_ARGUMENT;
6985 }
6986
6987 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006988 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 }
6990 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006991}
6992
Przemek Stekiel69c46792022-06-10 12:59:51 +02006993#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006994static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6995 psa_algorithm_t kdf_alg,
6996 psa_key_derivation_step_t step,
6997 const uint8_t *data,
6998 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006999{
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007001 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02007003 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007004#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
7006 return PSA_ERROR_INVALID_ARGUMENT;
7007 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007008#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (hkdf->state != HKDF_STATE_INIT) {
7010 return PSA_ERROR_BAD_STATE;
7011 } else {
7012 status = psa_key_derivation_start_hmac(&hkdf->hmac,
7013 hash_alg,
7014 data, data_length);
7015 if (status != PSA_SUCCESS) {
7016 return status;
7017 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02007018 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02007020 }
Gilles Peskine03410b52019-05-16 16:05:19 +02007021 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007022#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02007024 /* We shouldn't be in different state as HKDF_EXPAND only allows
7025 * two inputs: SECRET (this case) and INFO which does not modify
7026 * the state. It could happen only if the hkdf
7027 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (hkdf->state != HKDF_STATE_INIT) {
7029 return PSA_ERROR_BAD_STATE;
7030 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007031
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02007032 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
7034 return PSA_ERROR_INVALID_ARGUMENT;
7035 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 memcpy(hkdf->prk, data, data_length);
7038 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007039#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007040 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007041 /* HKDF: If no salt was provided, use an empty salt.
7042 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007044#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
7046 return PSA_ERROR_BAD_STATE;
7047 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02007048#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 status = psa_key_derivation_start_hmac(&hkdf->hmac,
7050 hash_alg,
7051 NULL, 0);
7052 if (status != PSA_SUCCESS) {
7053 return status;
7054 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007055 hkdf->state = HKDF_STATE_STARTED;
7056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (hkdf->state != HKDF_STATE_STARTED) {
7058 return PSA_ERROR_BAD_STATE;
7059 }
7060 status = psa_mac_update(&hkdf->hmac,
7061 data, data_length);
7062 if (status != PSA_SUCCESS) {
7063 return status;
7064 }
7065 status = psa_mac_sign_finish(&hkdf->hmac,
7066 hkdf->prk,
7067 sizeof(hkdf->prk),
7068 &data_length);
7069 if (status != PSA_SUCCESS) {
7070 return status;
7071 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007072 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02007073
Gilles Peskine2b522db2019-04-12 15:11:49 +02007074 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007075 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007076#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007078 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007080 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007082#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007083 {
7084 /* Block 0 is empty, and the next block will be
7085 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02007087 }
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02007090 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007091#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
7093 return PSA_ERROR_INVALID_ARGUMENT;
7094 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007095#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02007096#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
7098 hkdf->state == HKDF_STATE_INIT) {
7099 return PSA_ERROR_BAD_STATE;
7100 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02007101#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 if (hkdf->state == HKDF_STATE_OUTPUT) {
7103 return PSA_ERROR_BAD_STATE;
7104 }
7105 if (hkdf->info_set) {
7106 return PSA_ERROR_BAD_STATE;
7107 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007108 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 if (data_length != 0) {
7110 hkdf->info = mbedtls_calloc(1, data_length);
7111 if (hkdf->info == NULL) {
7112 return PSA_ERROR_INSUFFICIENT_MEMORY;
7113 }
7114 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007115 }
7116 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007118 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007120 }
7121}
Przemek Stekiel69c46792022-06-10 12:59:51 +02007122#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01007123
John Durkop07cc04a2020-11-16 22:08:34 -08007124#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
7125 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007126static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
7127 const uint8_t *data,
7128 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01007129{
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
7131 return PSA_ERROR_BAD_STATE;
7132 }
Janos Follathf08e2652019-06-13 09:05:41 +01007133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (data_length != 0) {
7135 prf->seed = mbedtls_calloc(1, data_length);
7136 if (prf->seed == NULL) {
7137 return PSA_ERROR_INSUFFICIENT_MEMORY;
7138 }
Janos Follathf08e2652019-06-13 09:05:41 +01007139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007141 prf->seed_length = data_length;
7142 }
Janos Follathf08e2652019-06-13 09:05:41 +01007143
Gilles Peskine43f958b2020-12-13 14:55:14 +01007144 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01007145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01007147}
7148
Gilles Peskine449bd832023-01-11 14:50:10 +01007149static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
7150 const uint8_t *data,
7151 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01007152{
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
7154 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7155 return PSA_ERROR_BAD_STATE;
7156 }
Janos Follath81550542019-06-13 14:26:34 +01007157
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 if (data_length != 0) {
7159 prf->secret = mbedtls_calloc(1, data_length);
7160 if (prf->secret == NULL) {
7161 return PSA_ERROR_INSUFFICIENT_MEMORY;
7162 }
Steven Cooremana6df6042021-04-29 19:32:25 +02007163
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02007165 prf->secret_length = data_length;
7166 }
Janos Follath81550542019-06-13 14:26:34 +01007167
Gilles Peskine43f958b2020-12-13 14:55:14 +01007168 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01007169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01007171}
7172
Gilles Peskine449bd832023-01-11 14:50:10 +01007173static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
7174 const uint8_t *data,
7175 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01007176{
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
7178 return PSA_ERROR_BAD_STATE;
7179 }
Janos Follath63028dd2019-06-13 09:15:47 +01007180
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 if (data_length != 0) {
7182 prf->label = mbedtls_calloc(1, data_length);
7183 if (prf->label == NULL) {
7184 return PSA_ERROR_INSUFFICIENT_MEMORY;
7185 }
Janos Follath63028dd2019-06-13 09:15:47 +01007186
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007188 prf->label_length = data_length;
7189 }
Janos Follath63028dd2019-06-13 09:15:47 +01007190
Gilles Peskine43f958b2020-12-13 14:55:14 +01007191 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01007192
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01007194}
7195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
7197 psa_key_derivation_step_t step,
7198 const uint8_t *data,
7199 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01007200{
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01007202 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01007204 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01007206 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01007208 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01007210 }
7211}
John Durkop07cc04a2020-11-16 22:08:34 -08007212#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
7213 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
7214
7215#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
7216static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
7217 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08007218 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08007220{
7221 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
7223 4 + data_length + prf->other_secret_length :
7224 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
7227 return PSA_ERROR_INVALID_ARGUMENT;
7228 }
John Durkop07cc04a2020-11-16 22:08:34 -08007229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 uint8_t *pms = mbedtls_calloc(1, pms_len);
7231 if (pms == NULL) {
7232 return PSA_ERROR_INSUFFICIENT_MEMORY;
7233 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007234 uint8_t *cur = pms;
7235
7236 /* pure-PSK:
7237 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08007238 *
7239 * The premaster secret is formed as follows: if the PSK is N octets
7240 * long, concatenate a uint16 with the value N, N zero octets, a second
7241 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007242 *
7243 * mixed-PSK:
7244 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
7245 * follows: concatenate a uint16 with the length of the other secret,
7246 * the other secret itself, uint16 with the length of PSK, and the
7247 * PSK itself.
7248 * For details please check:
7249 * - RFC 4279, Section 4 for the definition of RSA-PSK,
7250 * - RFC 4279, Section 3 for the definition of DHE-PSK,
7251 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08007252 */
7253
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7255 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
7256 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
7257 if (prf->other_secret_length != 0) {
7258 memcpy(cur, prf->other_secret, prf->other_secret_length);
7259 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02007260 cur += prf->other_secret_length;
7261 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 } else {
7263 *cur++ = MBEDTLS_BYTE_1(data_length);
7264 *cur++ = MBEDTLS_BYTE_0(data_length);
7265 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007266 cur += data_length;
7267 }
7268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 *cur++ = MBEDTLS_BYTE_1(data_length);
7270 *cur++ = MBEDTLS_BYTE_0(data_length);
7271 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007272 cur += data_length;
7273
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007274 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08007275
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007276 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08007278}
Janos Follath6660f0e2019-06-17 08:44:03 +01007279
Przemek Stekiele47201b2022-04-19 13:53:28 +02007280static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
7281 psa_tls12_prf_key_derivation_t *prf,
7282 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02007284{
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
7286 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007287 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007288
7289 if (data_length != 0) {
7290 prf->other_secret = mbedtls_calloc(1, data_length);
7291 if (prf->other_secret == NULL) {
7292 return PSA_ERROR_INSUFFICIENT_MEMORY;
7293 }
7294
7295 memcpy(prf->other_secret, data, data_length);
7296 prf->other_secret_length = data_length;
7297 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007298 prf->other_secret_length = 0;
7299 }
7300
7301 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
7302
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007304}
7305
Janos Follath6660f0e2019-06-17 08:44:03 +01007306static psa_status_t psa_tls12_prf_psk_to_ms_input(
7307 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01007308 psa_key_derivation_step_t step,
7309 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01007311{
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007313 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 return psa_tls12_prf_psk_to_ms_set_key(prf,
7315 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007316 break;
7317 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7319 data,
7320 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007321 break;
7322 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007324 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007325
Przemek Stekiele47201b2022-04-19 13:53:28 +02007326 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007327}
John Durkop07cc04a2020-11-16 22:08:34 -08007328#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007329
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007330#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7331static psa_status_t psa_tls12_ecjpake_to_pms_input(
7332 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007333 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007334 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007336{
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7338 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7339 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007340 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007341
7342 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (data[0] != 0x04) {
7344 return PSA_ERROR_INVALID_ARGUMENT;
7345 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007346
7347 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007349
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007351}
7352#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307353
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307354#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307355static psa_status_t psa_pbkdf2_set_input_cost(
7356 psa_pbkdf2_key_derivation_t *pbkdf2,
7357 psa_key_derivation_step_t step,
7358 uint64_t data)
7359{
7360 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7361 return PSA_ERROR_INVALID_ARGUMENT;
7362 }
7363
7364 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7365 return PSA_ERROR_BAD_STATE;
7366 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307367
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307368 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307369 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307370 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307371
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307372 if (data == 0) {
7373 return PSA_ERROR_INVALID_ARGUMENT;
7374 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307375
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307376 pbkdf2->input_cost = data;
7377 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7378
7379 return PSA_SUCCESS;
7380}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307381
7382static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7383 const uint8_t *data,
7384 size_t data_length)
7385{
Gilles Peskineead17662023-08-20 22:02:51 +02007386 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7387 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7388 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7389 /* Appending to existing salt. No state change. */
7390 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307391 return PSA_ERROR_BAD_STATE;
7392 }
7393
Gilles Peskineca57d782023-07-20 19:08:06 +02007394 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007395 /* Appending an empty string, nothing to do. */
7396 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307397 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307398
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307399 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7400 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307401 return PSA_ERROR_INSUFFICIENT_MEMORY;
7402 }
7403
Gilles Peskineead17662023-08-20 22:02:51 +02007404 if (pbkdf2->salt_length != 0) {
7405 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7406 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307407 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307408 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307409 mbedtls_free(pbkdf2->salt);
7410 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307411 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307412 return PSA_SUCCESS;
7413}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307414
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307415#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307416static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307417 const uint8_t *input,
7418 size_t input_len,
7419 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307420 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307421{
7422 psa_status_t status = PSA_SUCCESS;
7423 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007424 return psa_hash_compute(hash_alg, input, input_len, output,
7425 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7426 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307427 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307428 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007429 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307430 return status;
7431}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307432#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307433
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307434#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307435static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7436 size_t input_len,
7437 uint8_t *output,
7438 size_t *output_len)
7439{
7440 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307441 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307443 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307444 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7445 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7446 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307447 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307448 * mac_size as the driver function sets mac_output_length = mac_size
7449 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307450 status = psa_driver_wrapper_mac_compute(&attributes,
7451 zeros, sizeof(zeros),
7452 PSA_ALG_CMAC, input, input_len,
7453 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307454 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7455 128U,
7456 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307457 output_len);
7458 } else {
7459 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307460 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307461 }
7462 return status;
7463}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307464#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307465
7466static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307467 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307468 const uint8_t *data,
7469 size_t data_length)
7470{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307471 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307472 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7473 return PSA_ERROR_BAD_STATE;
7474 }
7475
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307476#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307477 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7478 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7479 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7480 pbkdf2->password,
7481 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307482 } else
7483#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7484#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7485 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307486 status = psa_pbkdf2_cmac_set_password(data, data_length,
7487 pbkdf2->password,
7488 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307489 } else
7490#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7491 {
7492 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307493 }
7494
7495 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7496
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307497 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307498}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307499
7500static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307501 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307502 psa_key_derivation_step_t step,
7503 const uint8_t *data,
7504 size_t data_length)
7505{
7506 switch (step) {
7507 case PSA_KEY_DERIVATION_INPUT_SALT:
7508 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7509 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307510 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307511 default:
7512 return PSA_ERROR_INVALID_ARGUMENT;
7513 }
7514}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307515#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307516
Gilles Peskineb8965192019-09-24 16:21:10 +02007517/** Check whether the given key type is acceptable for the given
7518 * input step of a key derivation.
7519 *
7520 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7521 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7522 * Both secret and non-secret inputs can alternatively have the type
7523 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7524 * that the input was passed as a buffer rather than via a key object.
7525 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007526static int psa_key_derivation_check_input_type(
7527 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007529{
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007531 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (key_type == PSA_KEY_TYPE_DERIVE) {
7533 return PSA_SUCCESS;
7534 }
7535 if (key_type == PSA_KEY_TYPE_NONE) {
7536 return PSA_SUCCESS;
7537 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007538 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007539 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 if (key_type == PSA_KEY_TYPE_DERIVE) {
7541 return PSA_SUCCESS;
7542 }
7543 if (key_type == PSA_KEY_TYPE_NONE) {
7544 return PSA_SUCCESS;
7545 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007546 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007547 case PSA_KEY_DERIVATION_INPUT_LABEL:
7548 case PSA_KEY_DERIVATION_INPUT_SALT:
7549 case PSA_KEY_DERIVATION_INPUT_INFO:
7550 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7552 return PSA_SUCCESS;
7553 }
7554 if (key_type == PSA_KEY_TYPE_NONE) {
7555 return PSA_SUCCESS;
7556 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007557 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307558 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7559 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7560 return PSA_SUCCESS;
7561 }
7562 if (key_type == PSA_KEY_TYPE_DERIVE) {
7563 return PSA_SUCCESS;
7564 }
7565 if (key_type == PSA_KEY_TYPE_NONE) {
7566 return PSA_SUCCESS;
7567 }
7568 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007569 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007571}
7572
Janos Follathb80a94e2019-06-12 15:54:46 +01007573static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007574 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007575 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007576 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007577 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007579{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007580 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007582
Waleed Elmelegy443908b2025-03-10 14:19:01 +00007583 if (kdf_alg == PSA_ALG_NONE) {
Waleed Elmelegy82cd3242025-03-03 15:04:17 +00007584 /* This is a blank or aborted operation. */
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007585 status = PSA_ERROR_BAD_STATE;
7586 goto exit;
7587 }
7588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 status = psa_key_derivation_check_input_type(step, key_type);
7590 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007593
Przemek Stekiel69c46792022-06-10 12:59:51 +02007594#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7596 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7597 step, data, data_length);
7598 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007599#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007600#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7602 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7603 step, data, data_length);
7604 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007605#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7606#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7608 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7609 step, data, data_length);
7610 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007611#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007612#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007614 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7616 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007617#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307618#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307619 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307620 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7621 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307622 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307623#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007624 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007625 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007626 (void) data;
7627 (void) data_length;
7628 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007630 }
7631
Gilles Peskine224b0d62019-09-23 18:13:17 +02007632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 if (status != PSA_SUCCESS) {
7634 psa_key_derivation_abort(operation);
7635 }
7636 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007637}
7638
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307639static psa_status_t psa_key_derivation_input_integer_internal(
7640 psa_key_derivation_operation_t *operation,
7641 psa_key_derivation_step_t step,
7642 uint64_t value)
7643{
7644 psa_status_t status;
7645 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7646
Waleed Elmelegy443908b2025-03-10 14:19:01 +00007647 if (kdf_alg == PSA_ALG_NONE) {
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007648 /* This is a blank or aborted operation. */
7649 status = PSA_ERROR_BAD_STATE;
7650 goto exit;
7651 }
7652
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307653#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307654 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307655 status = psa_pbkdf2_set_input_cost(
7656 &operation->ctx.pbkdf2, step, value);
7657 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307658#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307659 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307660 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307661 (void) value;
7662 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307663 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307664 }
7665
Waleed Elmelegy72b391f2025-03-03 12:35:28 +00007666exit:
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307667 if (status != PSA_SUCCESS) {
7668 psa_key_derivation_abort(operation);
7669 }
7670 return status;
7671}
7672
Janos Follath51f4a0f2019-06-14 11:35:55 +01007673psa_status_t psa_key_derivation_input_bytes(
7674 psa_key_derivation_operation_t *operation,
7675 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007676 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007678{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7680 LOCAL_INPUT_DECLARE(data_external, data);
7681
7682 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7683
7684 status = psa_key_derivation_input_internal(operation, step,
7685 PSA_KEY_TYPE_NONE,
7686 data, data_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00007687#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007688exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007689#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007690 LOCAL_INPUT_FREE(data_external, data);
7691 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007692}
7693
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307694psa_status_t psa_key_derivation_input_integer(
7695 psa_key_derivation_operation_t *operation,
7696 psa_key_derivation_step_t step,
7697 uint64_t value)
7698{
7699 return psa_key_derivation_input_integer_internal(operation, step, value);
7700}
7701
Janos Follath51f4a0f2019-06-14 11:35:55 +01007702psa_status_t psa_key_derivation_input_key(
7703 psa_key_derivation_operation_t *operation,
7704 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007706{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007708 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007709 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007710
Ronald Cron5c522922020-11-14 16:35:34 +01007711 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7713 if (status != PSA_SUCCESS) {
7714 psa_key_derivation_abort(operation);
7715 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007716 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007717
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307718 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7719 * permission to output to a key object. */
7720 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7721 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007722 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 status = psa_key_derivation_input_internal(operation,
7726 step, slot->attr.type,
7727 slot->key.data,
7728 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007729
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007730 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007733}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007734
7735
7736
7737/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007738/* Key agreement */
7739/****************************************************************/
7740
Gilles Peskine449bd832023-01-11 14:50:10 +01007741psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7742 const uint8_t *key_buffer,
7743 size_t key_buffer_size,
7744 psa_algorithm_t alg,
7745 const uint8_t *peer_key,
7746 size_t peer_key_length,
7747 uint8_t *shared_secret,
7748 size_t shared_secret_size,
7749 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007750{
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007752#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007753 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7755 key_buffer_size, alg,
7756 peer_key, peer_key_length,
7757 shared_secret,
7758 shared_secret_size,
7759 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007760#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007761
7762#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7763 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007764 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007765 peer_key,
7766 peer_key_length,
7767 key_buffer,
7768 key_buffer_size,
7769 shared_secret,
7770 shared_secret_size,
7771 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007772#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7773
Gilles Peskine01d718c2018-09-18 12:01:02 +02007774 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007775 (void) attributes;
7776 (void) key_buffer;
7777 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007778 (void) peer_key;
7779 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007780 (void) shared_secret;
7781 (void) shared_secret_size;
7782 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007784 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007785}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007786
Aditya Deshpande17845b82022-10-13 17:21:01 +01007787/** Internal function for raw key agreement
7788 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007789 * to the driver's implementation if a driver is present.
7790 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007791 * (psa_key_agreement_raw_builtin).
7792 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007793static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7794 psa_key_slot_t *private_key,
7795 const uint8_t *peer_key,
7796 size_t peer_key_length,
7797 uint8_t *shared_secret,
7798 size_t shared_secret_size,
7799 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007800{
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7802 return PSA_ERROR_NOT_SUPPORTED;
7803 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007804
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007805 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 private_key->key.data,
7807 private_key->key.bytes, alg,
7808 peer_key, peer_key_length,
7809 shared_secret,
7810 shared_secret_size,
7811 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007812}
7813
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007814/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007815 * to potentially free embedded data structures and wipe confidential data.
7816 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007817static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7818 psa_key_derivation_step_t step,
7819 psa_key_slot_t *private_key,
7820 const uint8_t *peer_key,
7821 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007822{
7823 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007824 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007825 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007827
7828 /* Step 1: run the secret agreement algorithm to generate the shared
7829 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 status = psa_key_agreement_raw_internal(ka_alg,
7831 private_key,
7832 peer_key, peer_key_length,
7833 shared_secret,
7834 sizeof(shared_secret),
7835 &shared_secret_length);
7836 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007837 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007839
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007840 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007841 * the shared secret. A shared secret is permitted wherever a key
7842 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 status = psa_key_derivation_input_internal(operation, step,
7844 PSA_KEY_TYPE_DERIVE,
7845 shared_secret,
7846 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007847exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007848 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7849 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007850}
7851
Gilles Peskine449bd832023-01-11 14:50:10 +01007852psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7853 psa_key_derivation_step_t step,
7854 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007855 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007857{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007858 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007859 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007860 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007861 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007862
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7864 return PSA_ERROR_INVALID_ARGUMENT;
7865 }
Ronald Cron5c522922020-11-14 16:35:34 +01007866 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7868 if (status != PSA_SUCCESS) {
7869 return status;
7870 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007871
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007872 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 status = psa_key_agreement_internal(operation, step,
7874 slot,
7875 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007876
David Horstmann4a48bec2024-03-13 15:42:31 +00007877#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007878exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007879#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 if (status != PSA_SUCCESS) {
7881 psa_key_derivation_abort(operation);
7882 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007883 /* If a private key has been added as SECRET, we allow the derived
7884 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007886 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007888 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007889
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007890 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007891 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007894}
7895
Gilles Peskine449bd832023-01-11 14:50:10 +01007896psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7897 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007898 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007900 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 size_t output_size,
7902 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007903{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007904 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007905 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007906 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007907 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007908 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7909 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007910 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007913 status = PSA_ERROR_INVALID_ARGUMENT;
7914 goto exit;
7915 }
Ronald Cron5c522922020-11-14 16:35:34 +01007916 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7918 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007919 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007921
Gilles Peskine3e561302022-04-14 00:17:15 +02007922 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7923 * for the output size. The PSA specification only guarantees that this
7924 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7925 * but it might be nice to allow smaller buffers if the output fits.
7926 * At the time of writing this comment, with only ECDH implemented,
7927 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7928 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7929 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007930 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7932 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007933 status = PSA_ERROR_BUFFER_TOO_SMALL;
7934 goto exit;
7935 }
7936
Thomas Daubney81899ab2024-02-15 12:57:26 +00007937 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 status = psa_key_agreement_raw_internal(alg, slot,
7939 peer_key, peer_key_length,
7940 output, output_size,
7941 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007942
7943exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007944 /* Check for successful allocation of output,
7945 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007946 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007947 /* If an error happens and is not handled properly, the output
7948 * may be used as a key to protect sensitive data. Arrange for such
7949 * a key to be random, which is likely to result in decryption or
7950 * verification errors. This is better than filling the buffer with
7951 * some constant data such as zeros, which would result in the data
7952 * being protected with a reproducible, easily knowable key.
7953 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007954 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007955 *output_length = output_size;
7956 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007957
Thomas Daubney5390aca2024-02-22 11:06:04 +00007958 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007959 /* output allocation failed. */
7960 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007961 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007962
Ryan Everetta103ec92024-01-31 13:59:57 +00007963 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007964
Thomas Daubney81899ab2024-02-15 12:57:26 +00007965 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7966 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007968}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007969
7970
7971/****************************************************************/
7972/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007973/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007974
Gilles Peskine672a7712023-04-28 21:00:28 +02007975#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7976#include "entropy_poll.h"
7977#endif
7978
Gilles Peskine30524eb2020-11-13 17:02:26 +01007979/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007980 *
7981 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7982 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007983 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007984static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007985{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007986#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007988#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7989
Gilles Peskine30524eb2020-11-13 17:02:26 +01007990 /* Set default configuration if
7991 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007993 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 }
7995 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007996 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007998
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01008000#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
8001 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
8002 /* The PSA entropy injection feature depends on using NV seed as an entropy
8003 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 mbedtls_entropy_add_source(&rng->entropy,
8005 mbedtls_nv_seed_poll, NULL,
8006 MBEDTLS_ENTROPY_BLOCK_SIZE,
8007 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01008008#endif
8009
Valerio Setti061d4e42024-02-26 12:52:44 +01008010 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008011#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008012}
8013
8014/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00008015 *
8016 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
8017 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01008018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008019static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01008020{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008021#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008023#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01008024 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008026#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008027}
8028
8029/** Seed the PSA random generator.
8030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008031static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01008032{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008033#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
8034 /* Do nothing: the external RNG seeds itself. */
8035 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008037#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008038 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01008039 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 drbg_seed, sizeof(drbg_seed) - 1);
8041 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008042#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01008043}
8044
David Horstmann6e99bb22024-02-06 15:27:49 +00008045psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01008046 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008047{
David Horstmann6e99bb22024-02-06 15:27:49 +00008048 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008049
David Horstmann6e99bb22024-02-06 15:27:49 +00008050 LOCAL_OUTPUT_DECLARE(output_external, output);
8051 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008052
David Horstmann6e99bb22024-02-06 15:27:49 +00008053 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008054
David Horstmann4a48bec2024-03-13 15:42:31 +00008055#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00008056exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00008057#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00008058 LOCAL_OUTPUT_FREE(output_external, output);
8059 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008060}
8061
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01008062#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01008063psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
8064 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008065{
Paul Elliott600472b2024-02-23 17:26:58 +00008066 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008067 return PSA_ERROR_NOT_PERMITTED;
8068 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02008069
Gilles Peskine449bd832023-01-11 14:50:10 +01008070 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
8071 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
8072 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
8073 return PSA_ERROR_INVALID_ARGUMENT;
8074 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02008075
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008077}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01008078#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02008079
Ronald Cron3772afe2021-02-08 16:10:05 +01008080/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02008081 *
Ronald Cron3772afe2021-02-08 16:10:05 +01008082 * \param type The key type
8083 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02008084 *
8085 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01008086 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02008087 * \retval #PSA_ERROR_INVALID_ARGUMENT
8088 * The size in bits of the key is not valid.
8089 * \retval #PSA_ERROR_NOT_SUPPORTED
8090 * The type and/or the size in bits of the key or the combination of
8091 * the two is not supported.
8092 */
Ronald Cron3772afe2021-02-08 16:10:05 +01008093static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008095{
Ronald Cronf3bb7612020-10-02 20:11:59 +02008096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (key_type_is_raw_bytes(type)) {
8099 status = psa_validate_unstructured_key_bit_size(type, bits);
8100 if (status != PSA_SUCCESS) {
8101 return status;
8102 }
8103 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02008104#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8106 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
8107 return PSA_ERROR_NOT_SUPPORTED;
8108 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00008109 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00008110 return PSA_ERROR_NOT_SUPPORTED;
8111 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008112
Ronald Cron01b2aba2020-10-05 09:42:02 +02008113 /* Accept only byte-aligned keys, for the same reasons as
8114 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 if (bits % 8 != 0) {
8116 return PSA_ERROR_NOT_SUPPORTED;
8117 }
8118 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02008119#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008120
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008121#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01008123 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 return PSA_SUCCESS;
8125 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008126#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008127
Valerio Settia55f0422023-07-10 15:34:41 +02008128#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008129 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02008130 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01008131 return PSA_ERROR_NOT_SUPPORTED;
8132 }
8133 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008134#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008135 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008137 }
8138
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008140}
8141
Ronald Cron55ed0592020-10-05 10:30:40 +02008142psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02008143 const psa_key_attributes_t *attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008144 const psa_custom_key_parameters_t *custom,
8145 const uint8_t *custom_data,
8146 size_t custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02008148{
8149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008150 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008151
Gilles Peskine7a18f962024-02-12 16:48:11 +01008152 /* Only used for RSA */
Gilles Peskinef36d7852024-06-06 21:11:44 +02008153 (void) custom;
8154 (void) custom_data;
8155 (void) custom_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008156
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00008158 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 if (status != PSA_SUCCESS) {
8160 return status;
8161 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008162
David Brown12ca5032021-02-11 11:02:00 -07008163#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 if (type == PSA_KEY_TYPE_DES) {
8165 psa_des_set_key_parity(key_buffer, key_buffer_size);
8166 }
David Brown8107e312021-02-12 08:08:46 -07008167#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01008169
Valerio Setti76df8c12023-07-11 14:11:28 +02008170#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
8172 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008173 custom_data, custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 key_buffer,
8175 key_buffer_size,
8176 key_buffer_length);
8177 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02008178#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01008179
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008180#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8182 return mbedtls_psa_ecp_generate_key(attributes,
8183 key_buffer,
8184 key_buffer_size,
8185 key_buffer_length);
8186 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008187#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008188
Valerio Settia55f0422023-07-10 15:34:41 +02008189#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008190 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8191 return mbedtls_psa_ffdh_generate_key(attributes,
8192 key_buffer,
8193 key_buffer_size,
8194 key_buffer_length);
8195 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008196#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02008197 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 (void) key_buffer_length;
8199 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02008200 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008201
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008203}
Darryl Green0c6575a2018-11-07 16:05:30 +00008204
Gilles Peskinef36d7852024-06-06 21:11:44 +02008205psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
8206 const psa_custom_key_parameters_t *custom,
8207 const uint8_t *custom_data,
8208 size_t custom_data_length,
8209 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008210{
8211 psa_status_t status;
8212 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02008213 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02008214 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02008215
Ronald Cron81709fc2020-11-14 12:10:32 +01008216 *key = MBEDTLS_SVC_KEY_ID_INIT;
8217
Gilles Peskine0f84d622019-09-12 19:03:13 +02008218 /* Reject any attempt to create a zero-length key so that we don't
8219 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 if (psa_get_key_bits(attributes) == 0) {
8221 return PSA_ERROR_INVALID_ARGUMENT;
8222 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02008223
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008224 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008225 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 return PSA_ERROR_INVALID_ARGUMENT;
8227 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008228
Gilles Peskine7a18f962024-02-12 16:48:11 +01008229#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008230 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinef36d7852024-06-06 21:11:44 +02008231 if (custom->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01008232 return PSA_ERROR_INVALID_ARGUMENT;
8233 }
8234 } else
8235#endif
Gilles Peskine52504f82024-06-20 17:19:58 +02008236 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01008237 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02008238 }
8239
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
8241 &slot, &driver);
8242 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02008243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 }
Gilles Peskine11792082019-08-06 18:36:36 +02008245
Ronald Cron977c2472020-10-13 08:32:21 +02008246 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05308247 * storage ( thus not in the case of generating a key in a secure element
8248 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
8249 * buffer to hold the generated key material. */
Valerio Setti70fa89c2024-08-14 05:12:45 +02008250 if (slot->key.bytes == 0) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008251 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008253 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008254 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 }
Ronald Cron3772afe2021-02-08 16:10:05 +01008258
8259 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008260 attributes->type,
8261 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02008263 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 attributes, &key_buffer_size);
8265 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 }
Ronald Cron977c2472020-10-13 08:32:21 +02008268 }
Ronald Cron977c2472020-10-13 08:32:21 +02008269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
8271 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02008272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 }
Ronald Cron977c2472020-10-13 08:32:21 +02008274 }
8275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008277 custom,
8278 custom_data, custom_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01008279 slot->key.data, slot->key.bytes,
8280 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 if (status != PSA_SUCCESS) {
8282 psa_remove_key_data_from_memory(slot);
8283 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02008284
Gilles Peskine11792082019-08-06 18:36:36 +02008285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (status == PSA_SUCCESS) {
8287 status = psa_finish_key_creation(slot, driver, key);
8288 }
8289 if (status != PSA_SUCCESS) {
8290 psa_fail_key_creation(slot, driver);
8291 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008294}
8295
Gilles Peskinef36d7852024-06-06 21:11:44 +02008296psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
8297 const psa_key_production_parameters_t *params,
8298 size_t params_data_length,
8299 mbedtls_svc_key_id_t *key)
8300{
8301 return psa_generate_key_custom(
8302 attributes,
8303 (const psa_custom_key_parameters_t *) params,
8304 params->data, params_data_length,
8305 key);
8306}
8307
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008308psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
8309 mbedtls_svc_key_id_t *key)
8310{
Gilles Peskinef36d7852024-06-06 21:11:44 +02008311 return psa_generate_key_custom(attributes,
8312 &default_custom_production,
8313 NULL, 0,
8314 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008315}
8316
Gilles Peskinecdb37812025-02-05 18:02:49 +01008317
8318
Gilles Peskinee59236f2018-01-27 23:32:46 +01008319/****************************************************************/
8320/* Module setup */
8321/****************************************************************/
8322
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008323#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01008324psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 void (* entropy_init)(mbedtls_entropy_context *ctx),
8326 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01008327{
Paul Elliott8e151532024-02-23 17:35:29 +00008328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8329
8330#if defined(MBEDTLS_THREADING_C)
8331 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8332#endif /* defined(MBEDTLS_THREADING_C) */
8333
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00008335 status = PSA_ERROR_BAD_STATE;
8336 } else {
8337 global_data.rng.entropy_init = entropy_init;
8338 global_data.rng.entropy_free = entropy_free;
8339 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 }
Paul Elliott8e151532024-02-23 17:35:29 +00008341
8342#if defined(MBEDTLS_THREADING_C)
8343 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8344#endif /* defined(MBEDTLS_THREADING_C) */
8345
8346 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01008347}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008348#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01008349
Gilles Peskine449bd832023-01-11 14:50:10 +01008350void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008351{
Paul Elliott47cee8e2024-03-08 21:38:02 +00008352
Paul Elliott600472b2024-02-23 17:26:58 +00008353#if defined(MBEDTLS_THREADING_C)
8354 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8355#endif /* defined(MBEDTLS_THREADING_C) */
8356
Paul Elliott47cee8e2024-03-08 21:38:02 +00008357 /* Nothing to do to free transaction. */
8358 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
8359 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01008360 }
Paul Elliott600472b2024-02-23 17:26:58 +00008361
Paul Elliott47cee8e2024-03-08 21:38:02 +00008362 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
8363 psa_wipe_all_key_slots();
8364 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8365 }
Ronald Cron9ba76912021-04-10 16:57:30 +02008366
Paul Elliott600472b2024-02-23 17:26:58 +00008367#if defined(MBEDTLS_THREADING_C)
8368 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8369#endif /* defined(MBEDTLS_THREADING_C) */
8370
Paul Elliott47cee8e2024-03-08 21:38:02 +00008371#if defined(MBEDTLS_THREADING_C)
8372 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8373#endif /* defined(MBEDTLS_THREADING_C) */
8374
Gilles Peskinec6b69072018-11-20 21:42:52 +01008375 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01008376 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008377 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008378 global_data.rng_state = RNG_NOT_INITIALIZED;
8379 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
8380
8381#if defined(MBEDTLS_THREADING_C)
8382 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8383#endif /* defined(MBEDTLS_THREADING_C) */
8384
8385#if defined(MBEDTLS_THREADING_C)
8386 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8387#endif /* defined(MBEDTLS_THREADING_C) */
Ronald Cron9ba76912021-04-10 16:57:30 +02008388
8389 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00008390 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
8391 psa_driver_wrapper_free();
8392 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8393 }
8394
8395#if defined(MBEDTLS_THREADING_C)
8396 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8397#endif /* defined(MBEDTLS_THREADING_C) */
8398
Gilles Peskinee59236f2018-01-27 23:32:46 +01008399}
8400
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008401#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8402/** Recover a transaction that was interrupted by a power failure.
8403 *
8404 * This function is called during initialization, before psa_crypto_init()
8405 * returns. If this function returns a failure status, the initialization
8406 * fails.
8407 */
8408static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008410{
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008412 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8413 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 /* TODO - fall through to the failure case until this
8415 * is implemented.
8416 * https://github.com/ARMmbed/mbed-crypto/issues/218
8417 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008418 default:
8419 /* We found an unsupported transaction in the storage.
8420 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008422 }
8423}
8424#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8425
Paul Elliott47cee8e2024-03-08 21:38:02 +00008426static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
8427{
8428 psa_status_t status = PSA_SUCCESS;
8429 uint8_t driver_wrappers_initialized = 0;
8430
8431 switch (subsystem) {
8432 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
8433
8434#if defined(MBEDTLS_THREADING_C)
8435 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8436#endif /* defined(MBEDTLS_THREADING_C) */
8437
8438 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
8439 /* Init drivers */
8440 status = psa_driver_wrapper_init();
8441
8442 /* Drivers need shutdown regardless of startup errors. */
8443 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8444
8445
8446 }
8447#if defined(MBEDTLS_THREADING_C)
8448 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8449 &mbedtls_threading_psa_globaldata_mutex));
8450#endif /* defined(MBEDTLS_THREADING_C) */
8451
8452 break;
8453
8454 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
8455
8456#if defined(MBEDTLS_THREADING_C)
8457 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8458#endif /* defined(MBEDTLS_THREADING_C) */
8459
8460 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
8461 status = psa_initialize_key_slots();
8462
8463 /* Need to wipe keys even if initialization fails. */
8464 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8465
8466 }
8467#if defined(MBEDTLS_THREADING_C)
8468 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8469 &mbedtls_threading_psa_globaldata_mutex));
8470#endif /* defined(MBEDTLS_THREADING_C) */
8471
8472 break;
8473
8474 case PSA_CRYPTO_SUBSYSTEM_RNG:
8475
8476#if defined(MBEDTLS_THREADING_C)
8477 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8478#endif /* defined(MBEDTLS_THREADING_C) */
8479
8480 driver_wrappers_initialized =
8481 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
8482
8483#if defined(MBEDTLS_THREADING_C)
8484 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8485 &mbedtls_threading_psa_globaldata_mutex));
8486#endif /* defined(MBEDTLS_THREADING_C) */
8487
8488 /* Need to use separate mutex here, as initialisation can require
8489 * testing of init flags, which requires locking the global data
8490 * mutex. */
8491#if defined(MBEDTLS_THREADING_C)
8492 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
8493#endif /* defined(MBEDTLS_THREADING_C) */
8494
8495 /* Initialize and seed the random generator. */
8496 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
8497 mbedtls_psa_random_init(&global_data.rng);
8498 global_data.rng_state = RNG_INITIALIZED;
8499
8500 status = mbedtls_psa_random_seed(&global_data.rng);
8501 if (status == PSA_SUCCESS) {
8502 global_data.rng_state = RNG_SEEDED;
8503 }
8504 }
8505
8506#if defined(MBEDTLS_THREADING_C)
8507 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8508 &mbedtls_threading_psa_rngdata_mutex));
8509#endif /* defined(MBEDTLS_THREADING_C) */
8510
Paul Elliott47cee8e2024-03-08 21:38:02 +00008511 break;
8512
8513 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
8514
8515#if defined(MBEDTLS_THREADING_C)
8516 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8517#endif /* defined(MBEDTLS_THREADING_C) */
8518
8519 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
8520#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8521 status = psa_crypto_load_transaction();
8522 if (status == PSA_SUCCESS) {
8523 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8524 if (status == PSA_SUCCESS) {
8525 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8526 }
8527 status = psa_crypto_stop_transaction();
8528 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
8529 /* There's no transaction to complete. It's all good. */
8530 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8531 status = PSA_SUCCESS;
8532 }
8533#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8534 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8535 status = PSA_SUCCESS;
8536#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8537 }
8538
8539#if defined(MBEDTLS_THREADING_C)
8540 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8541 &mbedtls_threading_psa_globaldata_mutex));
8542#endif /* defined(MBEDTLS_THREADING_C) */
8543
8544 break;
8545
8546 default:
8547 status = PSA_ERROR_CORRUPTION_DETECTED;
8548 }
8549
8550 /* Exit label only required when using threading macros. */
8551#if defined(MBEDTLS_THREADING_C)
8552exit:
8553#endif /* defined(MBEDTLS_THREADING_C) */
8554
8555 return status;
8556}
8557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008559{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008560 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008561
Paul Elliott47cee8e2024-03-08 21:38:02 +00008562 /* Double initialization is explicitly allowed. Early out if everything is
8563 * done. */
8564 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 return PSA_SUCCESS;
8566 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008567
Paul Elliott47cee8e2024-03-08 21:38:02 +00008568 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01008569 if (status != PSA_SUCCESS) {
8570 goto exit;
8571 }
8572
Paul Elliott47cee8e2024-03-08 21:38:02 +00008573 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008577
Paul Elliott47cee8e2024-03-08 21:38:02 +00008578 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
8579 if (status != PSA_SUCCESS) {
8580 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02008581 }
Gilles Peskinefc762652019-07-22 19:30:34 +02008582
Paul Elliott47cee8e2024-03-08 21:38:02 +00008583 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01008584
8585exit:
Paul Elliott600472b2024-02-23 17:26:58 +00008586
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 if (status != PSA_SUCCESS) {
8588 mbedtls_psa_crypto_free();
8589 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008592}
8593
Gilles Peskinecdb37812025-02-05 18:02:49 +01008594
8595
8596/****************************************************************/
8597/* PAKE */
8598/****************************************************************/
8599
Yanray Wangffc3c482023-07-11 12:01:04 +08008600#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008601psa_status_t psa_crypto_driver_pake_get_password_len(
8602 const psa_crypto_driver_pake_inputs_t *inputs,
8603 size_t *password_len)
8604{
8605 if (inputs->password_len == 0) {
8606 return PSA_ERROR_BAD_STATE;
8607 }
8608
8609 *password_len = inputs->password_len;
8610
8611 return PSA_SUCCESS;
8612}
8613
8614psa_status_t psa_crypto_driver_pake_get_password(
8615 const psa_crypto_driver_pake_inputs_t *inputs,
8616 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8617{
8618 if (inputs->password_len == 0) {
8619 return PSA_ERROR_BAD_STATE;
8620 }
8621
8622 if (buffer_size < inputs->password_len) {
8623 return PSA_ERROR_BUFFER_TOO_SMALL;
8624 }
8625
8626 memcpy(buffer, inputs->password, inputs->password_len);
8627 *buffer_length = inputs->password_len;
8628
8629 return PSA_SUCCESS;
8630}
8631
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008632psa_status_t psa_crypto_driver_pake_get_user_len(
8633 const psa_crypto_driver_pake_inputs_t *inputs,
8634 size_t *user_len)
8635{
8636 if (inputs->user_len == 0) {
8637 return PSA_ERROR_BAD_STATE;
8638 }
8639
8640 *user_len = inputs->user_len;
8641
8642 return PSA_SUCCESS;
8643}
8644
8645psa_status_t psa_crypto_driver_pake_get_user(
8646 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008647 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008648{
8649 if (inputs->user_len == 0) {
8650 return PSA_ERROR_BAD_STATE;
8651 }
8652
Przemek Stekielc0e62502023-03-14 11:49:36 +01008653 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008654 return PSA_ERROR_BUFFER_TOO_SMALL;
8655 }
8656
Przemek Stekielc0e62502023-03-14 11:49:36 +01008657 memcpy(user_id, inputs->user, inputs->user_len);
8658 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008659
8660 return PSA_SUCCESS;
8661}
8662
8663psa_status_t psa_crypto_driver_pake_get_peer_len(
8664 const psa_crypto_driver_pake_inputs_t *inputs,
8665 size_t *peer_len)
8666{
8667 if (inputs->peer_len == 0) {
8668 return PSA_ERROR_BAD_STATE;
8669 }
8670
8671 *peer_len = inputs->peer_len;
8672
8673 return PSA_SUCCESS;
8674}
8675
8676psa_status_t psa_crypto_driver_pake_get_peer(
8677 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008678 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008679{
8680 if (inputs->peer_len == 0) {
8681 return PSA_ERROR_BAD_STATE;
8682 }
8683
Przemek Stekielc0e62502023-03-14 11:49:36 +01008684 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008685 return PSA_ERROR_BUFFER_TOO_SMALL;
8686 }
8687
Przemek Stekielc0e62502023-03-14 11:49:36 +01008688 memcpy(peer_id, inputs->peer, inputs->peer_len);
8689 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008690
8691 return PSA_SUCCESS;
8692}
8693
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008694psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8695 const psa_crypto_driver_pake_inputs_t *inputs,
8696 psa_pake_cipher_suite_t *cipher_suite)
8697{
8698 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8699 return PSA_ERROR_BAD_STATE;
8700 }
8701
8702 *cipher_suite = inputs->cipher_suite;
8703
8704 return PSA_SUCCESS;
8705}
8706
Neil Armstronga7d08c32022-06-01 18:21:20 +02008707psa_status_t psa_pake_setup(
8708 psa_pake_operation_t *operation,
8709 const psa_pake_cipher_suite_t *cipher_suite)
8710{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008712
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008713 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008714 status = PSA_ERROR_BAD_STATE;
8715 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008716 }
8717
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008718 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008719 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008720 status = PSA_ERROR_INVALID_ARGUMENT;
8721 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008722 }
8723
Gilles Peskinea7a480b2025-02-05 19:41:52 +01008724 /* Make sure the variable-purpose part of the operation is zeroed.
8725 * Initializing the operation does not necessarily take care of it,
8726 * since the context is a union and initializing a union does not
8727 * necessarily initialize all of its members. */
8728 memset(&operation->data, 0, sizeof(operation->data));
Przemek Stekiel51eac532022-12-07 11:04:51 +01008729
Przemek Stekiele12ed362022-12-21 12:54:46 +01008730 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008731 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8732 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008733 operation->data.inputs.cipher_suite = *cipher_suite;
8734
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008735#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008736 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008737 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008738 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008739
David Horstmann16f01512023-06-14 17:21:07 +01008740 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008741 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008742 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008743#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008744 {
8745 status = PSA_ERROR_NOT_SUPPORTED;
8746 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008747 }
8748
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008749 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8750
Przemek Stekiel51eac532022-12-07 11:04:51 +01008751 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008752exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008753 psa_pake_abort(operation);
8754 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008755}
8756
8757psa_status_t psa_pake_set_password_key(
8758 psa_pake_operation_t *operation,
8759 mbedtls_svc_key_id_t password)
8760{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008762 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8763 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008764 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008765
Przemek Stekiel51eac532022-12-07 11:04:51 +01008766 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008767 status = PSA_ERROR_BAD_STATE;
8768 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008769 }
8770
Przemek Stekiel6c764412022-11-22 14:05:12 +01008771 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8772 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008773 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008774 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008775 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008776 }
8777
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008778 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008779
Przemek Stekiel51eac532022-12-07 11:04:51 +01008780 if (type != PSA_KEY_TYPE_PASSWORD &&
8781 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8782 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008783 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008784 }
8785
Przemek Stekiel51eac532022-12-07 11:04:51 +01008786 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8787 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008788 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008789 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008790 }
8791
8792 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8793 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008794 operation->data.inputs.attributes = slot->attr;
8795
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008796exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008797 if (status != PSA_SUCCESS) {
8798 psa_pake_abort(operation);
8799 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008800 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008801 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008802}
8803
8804psa_status_t psa_pake_set_user(
8805 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008806 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008807 size_t user_id_len)
8808{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008809 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008810 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008811
8812 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008813 status = PSA_ERROR_BAD_STATE;
8814 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008815 }
8816
Przemek Stekiel51eac532022-12-07 11:04:51 +01008817 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008818 status = PSA_ERROR_INVALID_ARGUMENT;
8819 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008820 }
8821
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008822 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008823 status = PSA_ERROR_BAD_STATE;
8824 goto exit;
8825 }
8826
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008827 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8828 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008829 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8830 goto exit;
8831 }
8832
David Horstmann4f534ae2024-01-22 17:35:59 +00008833 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8834
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008835 memcpy(operation->data.inputs.user, user_id, user_id_len);
8836 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008837
David Horstmann4f534ae2024-01-22 17:35:59 +00008838 status = PSA_SUCCESS;
8839
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008840exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008841 LOCAL_INPUT_FREE(user_id_external, user_id);
8842 if (status != PSA_SUCCESS) {
8843 psa_pake_abort(operation);
8844 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008845 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008846}
8847
8848psa_status_t psa_pake_set_peer(
8849 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008850 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008851 size_t peer_id_len)
8852{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008854 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008855
8856 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008857 status = PSA_ERROR_BAD_STATE;
8858 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008859 }
8860
Przemek Stekiel51eac532022-12-07 11:04:51 +01008861 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008862 status = PSA_ERROR_INVALID_ARGUMENT;
8863 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008864 }
8865
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008866 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008867 status = PSA_ERROR_BAD_STATE;
8868 goto exit;
8869 }
8870
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008871 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8872 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008873 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8874 goto exit;
8875 }
8876
David Horstmann4f534ae2024-01-22 17:35:59 +00008877 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8878
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008879 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8880 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008881
David Horstmann4f534ae2024-01-22 17:35:59 +00008882 status = PSA_SUCCESS;
8883
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008884exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008885 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8886 if (status != PSA_SUCCESS) {
8887 psa_pake_abort(operation);
8888 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008889 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008890}
8891
8892psa_status_t psa_pake_set_role(
8893 psa_pake_operation_t *operation,
8894 psa_pake_role_t role)
8895{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008897
Przemek Stekiel51eac532022-12-07 11:04:51 +01008898 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008899 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008900 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008901 }
8902
Przemek Stekiel09104b82023-03-06 14:11:51 +01008903 switch (operation->alg) {
8904#if defined(PSA_WANT_ALG_JPAKE)
8905 case PSA_ALG_JPAKE:
8906 if (role == PSA_PAKE_ROLE_NONE) {
8907 return PSA_SUCCESS;
8908 }
8909 status = PSA_ERROR_INVALID_ARGUMENT;
8910 break;
8911#endif
8912 default:
8913 (void) role;
8914 status = PSA_ERROR_NOT_SUPPORTED;
8915 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008916 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008918 psa_pake_abort(operation);
8919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008920}
8921
David Horstmanne7f21e62023-05-12 18:17:21 +01008922/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008923#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008924static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008925 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008926{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008927 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008928 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008929 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008930
David Horstmann024e5c52023-06-14 15:48:21 +01008931 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008932 is_x1 = (stage->outputs < 1);
8933 } else {
8934 is_x1 = (stage->inputs < 1);
8935 }
8936
David Horstmann74a3d8c2023-06-14 18:28:19 +01008937 key_share_step = is_x1 ?
8938 PSA_JPAKE_X1_STEP_KEY_SHARE :
8939 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008940 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008941 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8942 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8943 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8944 } else {
8945 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008946 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008947 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008948}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008949#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008950
Przemek Stekiel2797d372022-12-22 11:19:22 +01008951static psa_status_t psa_pake_complete_inputs(
8952 psa_pake_operation_t *operation)
8953{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008955 /* Create copy of the inputs on stack as inputs share memory
8956 with the driver context which will be setup by the driver. */
8957 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008958
Przemek Stekielfde11282023-03-13 16:06:09 +01008959 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008960 return PSA_ERROR_BAD_STATE;
8961 }
8962
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008963 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008964 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8965 return PSA_ERROR_BAD_STATE;
8966 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008967 }
8968
Przemek Stekiel18620a32023-01-17 16:34:52 +01008969 /* Clear driver context */
8970 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8971
8972 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008973
8974 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008975 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008976
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008977 /* User and peer are translated to role. */
8978 mbedtls_free(inputs.user);
8979 mbedtls_free(inputs.peer);
8980
Przemek Stekiel2797d372022-12-22 11:19:22 +01008981 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008982#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008983 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008984 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008985 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008986#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008987 {
8988 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008989 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008990 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008991 return status;
8992}
8993
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008994#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008995static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008996 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008997 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008998 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008999{
Przemek Stekiele12ed362022-12-21 12:54:46 +01009000 if (step != PSA_PAKE_STEP_KEY_SHARE &&
9001 step != PSA_PAKE_STEP_ZK_PUBLIC &&
9002 step != PSA_PAKE_STEP_ZK_PROOF) {
9003 return PSA_ERROR_INVALID_ARGUMENT;
9004 }
9005
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009006 psa_jpake_computation_stage_t *computation_stage =
9007 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009008
David Horstmann5da95602023-06-08 15:37:12 +01009009 if (computation_stage->round != PSA_JPAKE_FIRST &&
9010 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009011 return PSA_ERROR_BAD_STATE;
9012 }
9013
David Horstmanne7f21e62023-05-12 18:17:21 +01009014 /* Check that the step we are given is the one we were expecting */
9015 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009016 return PSA_ERROR_BAD_STATE;
9017 }
9018
David Horstmanne7f21e62023-05-12 18:17:21 +01009019 if (step == PSA_PAKE_STEP_KEY_SHARE &&
9020 computation_stage->inputs == 0 &&
9021 computation_stage->outputs == 0) {
9022 /* Start of the round, so function decides whether we are inputting
9023 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01009024 computation_stage->io_mode = io_mode;
9025 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009026 /* Middle of the round so the mode we are in must match the function
9027 * called by the user */
9028 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009029 }
9030
9031 return PSA_SUCCESS;
9032}
9033
David Horstmanne7f21e62023-05-12 18:17:21 +01009034static psa_status_t psa_jpake_epilogue(
9035 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01009036 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009037{
David Horstmanne7f21e62023-05-12 18:17:21 +01009038 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009039 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009040
David Horstmanne7f21e62023-05-12 18:17:21 +01009041 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
9042 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01009043 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009044 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01009045 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01009046 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01009047 }
9048 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01009049 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009050 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01009051 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01009052 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01009053 }
9054 }
David Horstmann246ec5a2023-06-27 10:33:06 +01009055 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
9056 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01009057 /* End of a round, move to the next round */
9058 stage->inputs = 0;
9059 stage->outputs = 0;
9060 stage->round++;
9061 }
9062 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01009063 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01009064 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009065 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01009066 return PSA_SUCCESS;
9067}
David Horstmanne7f21e62023-05-12 18:17:21 +01009068
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009069#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009070
Neil Armstronga7d08c32022-06-01 18:21:20 +02009071psa_status_t psa_pake_output(
9072 psa_pake_operation_t *operation,
9073 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00009074 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02009075 size_t output_size,
9076 size_t *output_length)
9077{
Przemek Stekiel51eac532022-12-07 11:04:51 +01009078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009079 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00009080 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009081 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009082
9083 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01009084 status = psa_pake_complete_inputs(operation);
9085 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009086 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009087 }
9088 }
9089
9090 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009091 status = PSA_ERROR_BAD_STATE;
9092 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009093 }
9094
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009095 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009096 status = PSA_ERROR_INVALID_ARGUMENT;
9097 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009098 }
9099
Przemek Stekiele12ed362022-12-21 12:54:46 +01009100 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009101#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009102 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009103 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009104 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009105 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009106 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009107 driver_step = convert_jpake_computation_stage_to_driver_step(
9108 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009109 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009110#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009111 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01009112 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009113 status = PSA_ERROR_NOT_SUPPORTED;
9114 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009115 }
9116
David Horstmannc75639d2024-01-22 17:56:39 +00009117 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
9118
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009119 status = psa_driver_wrapper_pake_output(operation, driver_step,
9120 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009121
9122 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009123 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009124 }
9125
9126 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009127#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009128 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009129 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009130 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009131 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009132 }
9133 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009134#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009135 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009136 status = PSA_ERROR_NOT_SUPPORTED;
9137 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009138 }
9139
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009140exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009141 LOCAL_OUTPUT_FREE(output_external, output);
9142 if (status != PSA_SUCCESS) {
9143 psa_pake_abort(operation);
9144 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009145 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009146}
9147
9148psa_status_t psa_pake_input(
9149 psa_pake_operation_t *operation,
9150 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00009151 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02009152 size_t input_length)
9153{
Przemek Stekiel51eac532022-12-07 11:04:51 +01009154 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009155 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009156 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
9157 operation->primitive,
9158 step);
David Horstmannc75639d2024-01-22 17:56:39 +00009159 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01009160
9161 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01009162 status = psa_pake_complete_inputs(operation);
9163 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009164 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009165 }
9166 }
9167
9168 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009169 status = PSA_ERROR_BAD_STATE;
9170 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009171 }
9172
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009173 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009174 status = PSA_ERROR_INVALID_ARGUMENT;
9175 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009176 }
9177
Przemek Stekiele12ed362022-12-21 12:54:46 +01009178 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009179#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009180 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009181 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009182 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009183 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009184 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009185 driver_step = convert_jpake_computation_stage_to_driver_step(
9186 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009187 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009188#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009189 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01009190 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009191 status = PSA_ERROR_NOT_SUPPORTED;
9192 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009193 }
9194
David Horstmannc75639d2024-01-22 17:56:39 +00009195 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009196 status = psa_driver_wrapper_pake_input(operation, driver_step,
9197 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009198
9199 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009200 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009201 }
9202
9203 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009204#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009205 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009206 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009207 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009208 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009209 }
9210 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009211#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009212 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009213 status = PSA_ERROR_NOT_SUPPORTED;
9214 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009215 }
9216
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009217exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009218 LOCAL_INPUT_FREE(input_external, input);
9219 if (status != PSA_SUCCESS) {
9220 psa_pake_abort(operation);
9221 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009222 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009223}
9224
9225psa_status_t psa_pake_get_implicit_key(
9226 psa_pake_operation_t *operation,
9227 psa_key_derivation_operation_t *output)
9228{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009230 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01009231 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01009232 size_t shared_key_len = 0;
9233
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009234 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009235 status = PSA_ERROR_BAD_STATE;
9236 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009237 }
9238
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009239#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009240 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009241 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01009242 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01009243 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009244 status = PSA_ERROR_BAD_STATE;
9245 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009246 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01009247 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009248#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01009249 {
9250 status = PSA_ERROR_NOT_SUPPORTED;
9251 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009252 }
9253
Przemek Stekiel0c781802022-11-29 14:53:13 +01009254 status = psa_driver_wrapper_pake_get_implicit_key(operation,
9255 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01009256 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01009257 &shared_key_len);
9258
9259 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009260 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01009261 }
9262
9263 status = psa_key_derivation_input_bytes(output,
9264 PSA_KEY_DERIVATION_INPUT_SECRET,
9265 shared_key,
9266 shared_key_len);
9267
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009268 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009269exit:
9270 abort_status = psa_pake_abort(operation);
9271 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009272}
9273
9274psa_status_t psa_pake_abort(
9275 psa_pake_operation_t *operation)
9276{
Przemek Stekield69dca92023-01-31 19:59:20 +01009277 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009278
Przemek Stekield69dca92023-01-31 19:59:20 +01009279 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009280 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02009281 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009282
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009283 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
9284 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009285 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01009286 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009287 }
9288 if (operation->data.inputs.user != NULL) {
9289 mbedtls_free(operation->data.inputs.user);
9290 }
9291 if (operation->data.inputs.peer != NULL) {
9292 mbedtls_free(operation->data.inputs.peer);
9293 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009294 }
Przemek Stekield69dca92023-01-31 19:59:20 +01009295 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009296
Przemek Stekield69dca92023-01-31 19:59:20 +01009297 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009298}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01009299#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02009300
David Horstmann00d7a042023-12-07 18:39:17 +00009301/* Memory copying test hooks. These are called before input copy, after input
9302 * copy, before output copy and after output copy, respectively.
9303 * They are used by memory-poisoning tests to temporarily unpoison buffers
9304 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00009305#if defined(MBEDTLS_TEST_HOOKS)
9306void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9307void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9308void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9309void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9310#endif
David Horstmannfde97392023-10-30 20:29:43 +00009311
David Horstmann1b7279a2023-11-15 15:18:30 +00009312/** Copy from an input buffer to a local copy.
9313 *
9314 * \param[in] input Pointer to input buffer.
9315 * \param[in] input_len Length of the input buffer.
9316 * \param[out] input_copy Pointer to a local copy in which to store the input data.
9317 * \param[out] input_copy_len Length of the local copy buffer.
9318 * \return #PSA_SUCCESS, if the buffer was successfully
9319 * copied.
9320 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
9321 * copy is too small to hold contents of the
9322 * input buffer.
9323 */
9324MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00009325psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
9326 uint8_t *input_copy, size_t input_copy_len)
9327{
9328 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00009329 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00009330 }
9331
David Horstmann372b8bb2023-11-24 16:21:04 +00009332#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009333 if (psa_input_pre_copy_hook != NULL) {
9334 psa_input_pre_copy_hook(input, input_len);
9335 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009336#endif
9337
David Horstmann777e7412023-11-15 17:33:47 +00009338 if (input_len > 0) {
9339 memcpy(input_copy, input, input_len);
9340 }
David Horstmannfde97392023-10-30 20:29:43 +00009341
David Horstmann372b8bb2023-11-24 16:21:04 +00009342#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009343 if (psa_input_post_copy_hook != NULL) {
9344 psa_input_post_copy_hook(input, input_len);
9345 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009346#endif
9347
David Horstmannfde97392023-10-30 20:29:43 +00009348 return PSA_SUCCESS;
9349}
9350
David Horstmann1b7279a2023-11-15 15:18:30 +00009351/** Copy from a local output buffer into a user-supplied one.
9352 *
9353 * \param[in] output_copy Pointer to a local buffer containing the output.
9354 * \param[in] output_copy_len Length of the local buffer.
9355 * \param[out] output Pointer to user-supplied output buffer.
9356 * \param[out] output_len Length of the user-supplied output buffer.
9357 * \return #PSA_SUCCESS, if the buffer was successfully
9358 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00009359 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00009360 * user-supplied output buffer is too small to
9361 * hold the contents of the local buffer.
9362 */
9363MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00009364psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
9365 uint8_t *output, size_t output_len)
9366{
9367 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00009368 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00009369 }
David Horstmann777e7412023-11-15 17:33:47 +00009370
David Horstmann372b8bb2023-11-24 16:21:04 +00009371#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009372 if (psa_output_pre_copy_hook != NULL) {
9373 psa_output_pre_copy_hook(output, output_len);
9374 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009375#endif
9376
David Horstmann777e7412023-11-15 17:33:47 +00009377 if (output_copy_len > 0) {
9378 memcpy(output, output_copy, output_copy_len);
9379 }
9380
David Horstmann372b8bb2023-11-24 16:21:04 +00009381#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009382 if (psa_output_post_copy_hook != NULL) {
9383 psa_output_post_copy_hook(output, output_len);
9384 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009385#endif
9386
David Horstmann8978f5c2023-10-30 20:37:12 +00009387 return PSA_SUCCESS;
9388}
9389
David Horstmannf1734052023-11-20 17:15:32 +00009390psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
9391 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00009392{
9393 psa_status_t status;
9394
David Horstmann9db14482023-11-23 15:50:37 +00009395 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00009396
David Horstmannc5cc1c32023-11-15 18:11:26 +00009397 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00009398 return PSA_SUCCESS;
9399 }
9400
David Horstmannf1734052023-11-20 17:15:32 +00009401 local_input->buffer = mbedtls_calloc(input_len, 1);
9402 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00009403 /* Since we dealt with the zero-length case above, we know that
9404 * a NULL return value means a failure of allocation. */
9405 return PSA_ERROR_INSUFFICIENT_MEMORY;
9406 }
David Horstmannf1734052023-11-20 17:15:32 +00009407 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00009408
David Horstmannf1734052023-11-20 17:15:32 +00009409 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00009410
9411 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00009412 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00009413 if (status != PSA_SUCCESS) {
9414 goto error;
9415 }
9416
9417 return PSA_SUCCESS;
9418
9419error:
Gilles Peskinee847afd2025-03-06 12:41:39 +01009420 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009421 local_input->buffer = NULL;
9422 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00009423 return status;
9424}
9425
David Horstmannf1734052023-11-20 17:15:32 +00009426void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00009427{
Gilles Peskinee847afd2025-03-06 12:41:39 +01009428 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009429 local_input->buffer = NULL;
9430 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00009431}
9432
David Horstmann89875a42023-11-20 12:54:09 +00009433psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
9434 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00009435{
David Horstmann9db14482023-11-23 15:50:37 +00009436 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00009437
David Horstmannc5cc1c32023-11-15 18:11:26 +00009438 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009439 return PSA_SUCCESS;
9440 }
David Horstmann89875a42023-11-20 12:54:09 +00009441 local_output->buffer = mbedtls_calloc(output_len, 1);
9442 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009443 /* Since we dealt with the zero-length case above, we know that
9444 * a NULL return value means a failure of allocation. */
9445 return PSA_ERROR_INSUFFICIENT_MEMORY;
9446 }
David Horstmann89875a42023-11-20 12:54:09 +00009447 local_output->length = output_len;
9448 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00009449
9450 return PSA_SUCCESS;
9451}
9452
David Horstmann89875a42023-11-20 12:54:09 +00009453psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00009454{
David Horstmannc335a4e2023-11-15 15:57:32 +00009455 psa_status_t status;
9456
David Horstmann89875a42023-11-20 12:54:09 +00009457 if (local_output->buffer == NULL) {
9458 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009459 return PSA_SUCCESS;
9460 }
David Horstmann89875a42023-11-20 12:54:09 +00009461 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00009462 /* We have an internal copy but nothing to copy back to. */
9463 return PSA_ERROR_CORRUPTION_DETECTED;
9464 }
9465
David Horstmann89875a42023-11-20 12:54:09 +00009466 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
9467 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00009468 if (status != PSA_SUCCESS) {
9469 return status;
9470 }
David Horstmann9467ea32023-11-08 17:44:18 +00009471
Gilles Peskinee847afd2025-03-06 12:41:39 +01009472 mbedtls_zeroize_and_free(local_output->buffer, local_output->length);
David Horstmann89875a42023-11-20 12:54:09 +00009473 local_output->buffer = NULL;
9474 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009475
9476 return PSA_SUCCESS;
9477}
9478
Gilles Peskinee59236f2018-01-27 23:32:46 +01009479#endif /* MBEDTLS_PSA_CRYPTO_C */