blob: 4ccb75be613285ba2215faa6d53585e1a1839ea5 [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
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020076#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
78 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020079#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020080#endif
81
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082/****************************************************************/
83/* Global data, support functions and library management */
84/****************************************************************/
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020089}
90
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010091/* Values for psa_global_data_t::rng_state */
92#define RNG_NOT_INITIALIZED 0
93#define RNG_INITIALIZED 1
94#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010095
Paul Elliott0db6a902024-03-15 13:48:20 +000096/* IDs for PSA crypto subsystems. Starts at 1 to catch potential uninitialized
97 * variables as arguments. */
Paul Elliott47cee8e2024-03-08 21:38:02 +000098typedef enum {
Paul Elliott0db6a902024-03-15 13:48:20 +000099 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 1,
Paul Elliott47cee8e2024-03-08 21:38:02 +0000100 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS,
101 PSA_CRYPTO_SUBSYSTEM_RNG,
102 PSA_CRYPTO_SUBSYSTEM_TRANSACTION,
103} mbedtls_psa_crypto_subsystem;
104
Paul Elliottb24e36d2024-03-15 16:25:48 +0000105/* Initialization flags for global_data::initialized */
Paul Elliott47cee8e2024-03-08 21:38:02 +0000106#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01
107#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02
108#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04
109
110#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \
111 PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \
112 PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \
113 PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100116 uint8_t initialized;
117 uint8_t rng_state;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100118 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100119} psa_global_data_t;
120
121static psa_global_data_t global_data;
122
Paul Elliott600472b2024-02-23 17:26:58 +0000123static uint8_t psa_get_initialized(void)
124{
125 uint8_t initialized;
126
127#if defined(MBEDTLS_THREADING_C)
Paul Elliott47cee8e2024-03-08 21:38:02 +0000128 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
129#endif /* defined(MBEDTLS_THREADING_C) */
130
131 initialized = global_data.rng_state == RNG_SEEDED;
132
133#if defined(MBEDTLS_THREADING_C)
134 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
135#endif /* defined(MBEDTLS_THREADING_C) */
136
137#if defined(MBEDTLS_THREADING_C)
Paul Elliott600472b2024-02-23 17:26:58 +0000138 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
139#endif /* defined(MBEDTLS_THREADING_C) */
140
Paul Elliott47cee8e2024-03-08 21:38:02 +0000141 initialized =
142 (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED));
Paul Elliott600472b2024-02-23 17:26:58 +0000143
144#if defined(MBEDTLS_THREADING_C)
145 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
146#endif /* defined(MBEDTLS_THREADING_C) */
147
148 return initialized;
149}
150
Paul Elliott35816522024-02-23 17:47:42 +0000151static uint8_t psa_get_drivers_initialized(void)
152{
153 uint8_t initialized;
154
155#if defined(MBEDTLS_THREADING_C)
156 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
157#endif /* defined(MBEDTLS_THREADING_C) */
158
Paul Elliott47cee8e2024-03-08 21:38:02 +0000159 initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0;
Paul Elliott35816522024-02-23 17:47:42 +0000160
161#if defined(MBEDTLS_THREADING_C)
162 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
163#endif /* defined(MBEDTLS_THREADING_C) */
164
165 return initialized;
166}
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
Paul Elliott600472b2024-02-23 17:26:58 +0000169 if (psa_get_initialized() == 0) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300171
David Horstmann4a48bec2024-03-13 15:42:31 +0000172#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann36df4b22023-12-13 15:55:25 +0000173
David Horstmann62a56d92023-12-14 18:12:47 +0000174/* Declare a local copy of an input buffer and a variable that will be used
175 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000176 *
177 * Note: This macro must be called before any operations which may jump to
178 * the exit label, so that the local input copy object is safe to be freed.
179 *
180 * Assumptions:
181 * - input is the name of a pointer to the buffer to be copied
182 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000183 * - input_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000184 */
David Horstmann62a56d92023-12-14 18:12:47 +0000185#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
186 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
187 const uint8_t *input_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000188
David Horstmann62a56d92023-12-14 18:12:47 +0000189/* Allocate a copy of the buffer input and set the pointer input_copy to
190 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000191 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000192 * Assumptions:
193 * - psa_status_t status exists
194 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000195 * - input is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000196 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000197 */
David Horstmann62a56d92023-12-14 18:12:47 +0000198#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000199 status = psa_crypto_local_input_alloc(input, length, \
200 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000201 if (status != PSA_SUCCESS) { \
202 goto exit; \
203 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000204 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000205
David Horstmann36df4b22023-12-13 15:55:25 +0000206/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
207 *
David Horstmann3e72db42023-12-08 14:08:18 +0000208 * Assumptions:
David Horstmann62a56d92023-12-14 18:12:47 +0000209 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann36df4b22023-12-13 15:55:25 +0000210 * - input is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000211 */
David Horstmann62a56d92023-12-14 18:12:47 +0000212#define LOCAL_INPUT_FREE(input, input_copy) \
213 input_copy = NULL; \
David Horstmann36df4b22023-12-13 15:55:25 +0000214 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmanne9a88ab2023-11-29 17:07:13 +0000215
David Horstmann62a56d92023-12-14 18:12:47 +0000216/* Declare a local copy of an output buffer and a variable that will be used
217 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000218 *
219 * Note: This macro must be called before any operations which may jump to
220 * the exit label, so that the local output copy object is safe to be freed.
221 *
222 * Assumptions:
223 * - output is the name of a pointer to the buffer to be copied
224 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000225 * - output_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000226 */
David Horstmann62a56d92023-12-14 18:12:47 +0000227#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
228 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
229 uint8_t *output_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000230
David Horstmann62a56d92023-12-14 18:12:47 +0000231/* Allocate a copy of the buffer output and set the pointer output_copy to
232 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000233 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000234 * Assumptions:
235 * - psa_status_t status exists
236 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000237 * - output is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000238 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000239 */
David Horstmann62a56d92023-12-14 18:12:47 +0000240#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000241 status = psa_crypto_local_output_alloc(output, length, \
242 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000243 if (status != PSA_SUCCESS) { \
244 goto exit; \
245 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000246 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000247
David Horstmann62a56d92023-12-14 18:12:47 +0000248/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann3e72db42023-12-08 14:08:18 +0000249 * after first copying back its contents to the original buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000250 *
David Horstmann3e72db42023-12-08 14:08:18 +0000251 * Assumptions:
David Horstmann3e72db42023-12-08 14:08:18 +0000252 * - psa_status_t status exists
David Horstmann62a56d92023-12-14 18:12:47 +0000253 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
254 * - output is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000255 */
David Horstmann62a56d92023-12-14 18:12:47 +0000256#define LOCAL_OUTPUT_FREE(output, output_copy) \
257 output_copy = NULL; \
David Horstmann5a945f52023-12-13 14:09:08 +0000258 do { \
259 psa_status_t local_output_status; \
David Horstmann36df4b22023-12-13 15:55:25 +0000260 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann5a945f52023-12-13 14:09:08 +0000261 if (local_output_status != PSA_SUCCESS) { \
262 /* Since this error case is an internal error, it's more serious than \
263 * any existing error code and so it's fine to overwrite the existing \
264 * status. */ \
265 status = local_output_status; \
266 } \
267 } while (0)
David Horstmann4a48bec2024-03-13 15:42:31 +0000268#else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmann62a56d92023-12-14 18:12:47 +0000269#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
270 const uint8_t *input_copy_name = NULL;
271#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
272 input_copy = input;
273#define LOCAL_INPUT_FREE(input, input_copy) \
274 input_copy = NULL;
275#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
276 uint8_t *output_copy_name = NULL;
277#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
278 output_copy = output;
279#define LOCAL_OUTPUT_FREE(output, output_copy) \
280 output_copy = NULL;
David Horstmann4a48bec2024-03-13 15:42:31 +0000281#endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000282
283
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100284int psa_can_do_hash(psa_algorithm_t hash_alg)
285{
286 (void) hash_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000287 return psa_get_drivers_initialized();
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100288}
Valerio Settic6f004f2023-12-12 11:27:36 +0100289
Valerio Setti1fff4f22023-12-28 14:19:34 +0100290int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100291{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100292 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100293 (void) cipher_alg;
Paul Elliott35816522024-02-23 17:47:42 +0000294 return psa_get_drivers_initialized();
Valerio Settic6f004f2023-12-12 11:27:36 +0100295}
296
297
Valerio Settia55f0422023-07-10 15:34:41 +0200298#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200299 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200300 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200301static int psa_is_dh_key_size_valid(size_t bits)
302{
Valerio Setti504a1022024-01-17 15:19:30 +0100303 switch (bits) {
304#if defined(PSA_WANT_DH_RFC7919_2048)
305 case 2048:
306 return 1;
307#endif /* PSA_WANT_DH_RFC7919_2048 */
308#if defined(PSA_WANT_DH_RFC7919_3072)
309 case 3072:
310 return 1;
311#endif /* PSA_WANT_DH_RFC7919_3072 */
312#if defined(PSA_WANT_DH_RFC7919_4096)
313 case 4096:
314 return 1;
315#endif /* PSA_WANT_DH_RFC7919_4096 */
316#if defined(PSA_WANT_DH_RFC7919_6144)
317 case 6144:
318 return 1;
319#endif /* PSA_WANT_DH_RFC7919_6144 */
320#if defined(PSA_WANT_DH_RFC7919_8192)
321 case 8192:
322 return 1;
323#endif /* PSA_WANT_DH_RFC7919_8192 */
324 default:
325 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200326 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200327}
Valerio Settia55f0422023-07-10 15:34:41 +0200328#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200329 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200330 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100333{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100334 /* Mbed TLS error codes can combine a high-level error code and a
335 * low-level error code. The low-level error usually reflects the
336 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 int low_level_ret = -(-ret & 0x007f);
338 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100342#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
344 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100346 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
347 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100348#endif
349
350#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200351 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
352 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
353 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
354 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
355 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200357 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200359 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100361#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200362
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100363#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000364 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100367#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100368
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100369#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100370 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100372 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100374#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100375
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100376#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200377 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100379#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200380
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100381#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200382 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200384 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100386#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200387
Dave Rodgman509b5672023-08-16 19:26:23 +0100388#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100389 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100391 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100393 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100395 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100397 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100399 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100401 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100403#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
406 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100407 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
408 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100409 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100411 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
412 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100414 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100416#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100417
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100418#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100419 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100421#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100422
Gilles Peskinee59236f2018-01-27 23:32:46 +0100423 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
424 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
425 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100427
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100428#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100429 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200431 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100433 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100435#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100436
Gilles Peskine82e57d12020-11-13 21:31:17 +0100437#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100439 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
440 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100441 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100443 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
444 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100446 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100448#endif
449
Dave Rodgmandea266f2023-08-31 11:52:43 +0100450#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100451 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100453 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100455 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100457#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100458 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100460#endif
461#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100462
Dave Rodgman509b5672023-08-16 19:26:23 +0100463#if defined(MBEDTLS_BIGNUM_C)
464#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100465 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100467#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100468 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100470 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
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_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100474 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100476 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100482#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100483
Dave Rodgman509b5672023-08-16 19:26:23 +0100484#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100485 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
488 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100490#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
491 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100492 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100494#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
496 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
501 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505 case MBEDTLS_ERR_PK_INVALID_ALG:
506 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
507 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200511 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100513#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100514
Gilles Peskineff2d2002019-05-06 15:26:23 +0200515 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200517 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200519
Dave Rodgman509b5672023-08-16 19:26:23 +0100520#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100521 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100523 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100525 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100527 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100529 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
530 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100532 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100534 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100536 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100538#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200539
Dave Rodgman1dab4452023-09-01 09:59:51 +0100540#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300541 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300542 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300544 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300546 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300548 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
549 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300551 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100553 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100555
Dave Rodgman509b5672023-08-16 19:26:23 +0100556#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000557 case MBEDTLS_ERR_ECP_IN_PROGRESS:
558 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100559#endif
560#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000561
Janos Follatha13b9052019-11-22 12:48:59 +0000562 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300564
Gilles Peskinee59236f2018-01-27 23:32:46 +0100565 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100567 }
568}
569
Paul Elliottdc42ca82023-02-24 18:11:59 +0000570/**
571 * \brief For output buffers which contain "tags"
572 * (outputs that may be checked for validity like
573 * hashes, MACs and signatures), fill the unused
574 * part of the output buffer (the whole buffer on
575 * error, the trailing part on success) with
576 * something that isn't a valid tag (barring an
577 * attack on the tag and deliberately-crafted
578 * input), in case the caller doesn't check the
579 * return status properly.
580 *
581 * \param output_buffer Pointer to buffer to wipe. May not be NULL
582 * unless \p output_buffer_size is zero.
583 * \param status Status of function called to generate
584 * output_buffer originally
585 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
586 * could be NULL.
587 * \param output_buffer_length Length of data written to output_buffer, must be
588 * less than \p output_buffer_size
589 */
590static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000591 size_t output_buffer_size, size_t output_buffer_length)
592{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000593 size_t offset = 0;
594
595 if (output_buffer_size == 0) {
596 /* If output_buffer_size is 0 then we have nothing to do. We must not
597 call memset because output_buffer may be NULL in this case */
598 return;
599 }
600
601 if (status == PSA_SUCCESS) {
602 offset = output_buffer_length;
603 }
604
605 memset(output_buffer + offset, '!', output_buffer_size - offset);
606}
607
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
610 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200611{
612 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200614 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200615 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200616 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200617 case PSA_KEY_TYPE_PASSWORD:
618 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200619 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100620#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200621 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (bits != 128 && bits != 192 && bits != 256) {
623 return PSA_ERROR_INVALID_ARGUMENT;
624 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200625 break;
626#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200627#if defined(PSA_WANT_KEY_TYPE_ARIA)
628 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (bits != 128 && bits != 192 && bits != 256) {
630 return PSA_ERROR_INVALID_ARGUMENT;
631 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200632 break;
633#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100634#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200635 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (bits != 128 && bits != 192 && bits != 256) {
637 return PSA_ERROR_INVALID_ARGUMENT;
638 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639 break;
640#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100641#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200642 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (bits != 64 && bits != 128 && bits != 192) {
644 return PSA_ERROR_INVALID_ARGUMENT;
645 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200646 break;
647#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100648#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200649 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (bits != 256) {
651 return PSA_ERROR_INVALID_ARGUMENT;
652 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200653 break;
654#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200655 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200657 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (bits % 8 != 0) {
659 return PSA_ERROR_INVALID_ARGUMENT;
660 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200663}
664
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100666 *
Steven Cooremancd640932021-03-08 12:00:27 +0100667 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
668 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100669 *
670 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100671 * \param[in] key_type The key type of the key to be used with the
672 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673 *
674 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100675 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100676 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100677 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100678 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100679MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100680 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100682{
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (PSA_ALG_IS_HMAC(algorithm)) {
684 if (key_type == PSA_KEY_TYPE_HMAC) {
685 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100686 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100687 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
690 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
691 * key. */
692 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
693 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
694 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
695 * the block length (larger than 1) for block ciphers. */
696 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
697 return PSA_SUCCESS;
698 }
699 }
700 }
701
702 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100703}
704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
706 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200707{
Valerio Setti8d4f1502024-06-14 07:49:05 +0200708#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
Valerio Setti8d4f1502024-06-14 07:49:05 +0200709 if (buffer_length > ((size_t) MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)) {
710 return PSA_ERROR_NOT_SUPPORTED;
711 }
Valerio Setti8d4f1502024-06-14 07:49:05 +0200712#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (slot->key.data != NULL) {
714 return PSA_ERROR_ALREADY_EXISTS;
715 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 slot->key.data = mbedtls_calloc(1, buffer_length);
718 if (slot->key.data == NULL) {
719 return PSA_ERROR_INSUFFICIENT_MEMORY;
720 }
Valerio Setti8d4f1502024-06-14 07:49:05 +0200721#endif
Steven Cooreman75b74362020-07-28 14:30:13 +0200722
Ronald Cronea0f8a62020-11-25 17:52:23 +0100723 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200725}
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
728 const uint8_t *data,
729 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200730{
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 psa_status_t status = psa_allocate_buffer_to_slot(slot,
732 data_length);
733 if (status != PSA_SUCCESS) {
734 return status;
735 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 memcpy(slot->key.data, data, data_length);
738 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200739}
740
Ronald Crona0fe59f2020-11-29 11:14:53 +0100741psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100742 const psa_key_attributes_t *attributes,
743 const uint8_t *data, size_t data_length,
744 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100747 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100748 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (data_length == 0) {
752 return PSA_ERROR_NOT_SUPPORTED;
753 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (key_type_is_raw_bytes(type)) {
756 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200757
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100758 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 *bits);
760 if (status != PSA_SUCCESS) {
761 return status;
762 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200763
Ronald Crondd04d422020-11-28 15:14:42 +0100764 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100766 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return PSA_SUCCESS;
770 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200771#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200772 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100773 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200774 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100775 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100776 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200777 return mbedtls_psa_ffdh_import_key(attributes,
778 data, data_length,
779 key_buffer, key_buffer_size,
780 key_buffer_length,
781 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100782 }
Valerio Settia55f0422023-07-10 15:34:41 +0200783#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200784 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200785#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
787 if (PSA_KEY_TYPE_IS_ECC(type)) {
788 return mbedtls_psa_ecp_import_key(attributes,
789 data, data_length,
790 key_buffer, key_buffer_size,
791 key_buffer_length,
792 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100793 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200794#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800795 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200796#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
797 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
799 if (PSA_KEY_TYPE_IS_RSA(type)) {
800 return mbedtls_psa_rsa_import_key(attributes,
801 data, data_length,
802 key_buffer, key_buffer_size,
803 key_buffer_length,
804 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200805 }
Valerio Settife478902023-07-25 12:27:19 +0200806#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
807 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800808 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100809 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100812}
813
Gilles Peskinef603c712019-01-19 13:40:11 +0100814/** Calculate the intersection of two algorithm usage policies.
815 *
816 * Return 0 (which allows no operation) on incompatibility.
817 */
818static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100819 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100820 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100822{
Gilles Peskine549ea862019-05-22 11:45:59 +0200823 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (alg1 == alg2) {
825 return alg1;
826 }
Steven Cooreman03488022021-02-18 12:07:20 +0100827 /* If the policies are from the same hash-and-sign family, check
828 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
830 PSA_ALG_IS_SIGN_HASH(alg2) &&
831 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
832 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
833 return alg2;
834 }
835 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
836 return alg1;
837 }
Steven Cooreman03488022021-02-18 12:07:20 +0100838 }
839 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100840 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100841 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
843 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
844 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
845 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
846 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100847 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100848
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100849 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
851 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
852 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
853 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100854 }
855 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
857 (alg1_len <= alg2_len)) {
858 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100859 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
861 (alg2_len <= alg1_len)) {
862 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100863 }
864 }
865 /* If the policies are from the same MAC family, check whether one
866 * of them is a minimum-MAC-length policy. Calculate the most
867 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
869 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
870 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100871 /* Validate the combination of key type and algorithm. Since the base
872 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
874 return 0;
875 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100876
Steven Cooreman31a876d2021-03-03 20:47:40 +0100877 /* Get the (exact or at-least) output lengths for both sides of the
878 * requested intersection. None of the currently supported algorithms
879 * have an output length dependent on the actual key size, so setting it
880 * to a bogus value of 0 is currently OK.
881 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100882 * Note that for at-least-this-length wildcard algorithms, the output
883 * length is set to the shortest allowed length, which allows us to
884 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
886 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100887 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888
Steven Cooremana1d83222021-02-25 10:20:29 +0100889 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
891 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
892 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100893 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100894
895 /* If only one is an at-least-this-length policy, the intersection would
896 * be the other (fixed-length) policy as long as said fixed length is
897 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
899 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100900 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
902 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100903 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100904
Steven Cooremancd640932021-03-08 12:00:27 +0100905 /* If none of them are wildcards, check whether they define the same tag
906 * length. This is still possible here when one is default-length and
907 * the other specific-length. Ensure to always return the
908 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (alg1_len == alg2_len) {
910 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
911 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 }
913 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100915}
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917static int psa_key_algorithm_permits(psa_key_type_t key_type,
918 psa_algorithm_t policy_alg,
919 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200920{
Gilles Peskine549ea862019-05-22 11:45:59 +0200921 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (requested_alg == policy_alg) {
923 return 1;
924 }
Steven Cooreman03488022021-02-18 12:07:20 +0100925 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
926 * and requested_alg is the same hash-and-sign family with any hash,
927 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
929 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
930 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
931 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100932 }
933 /* If policy_alg is a wildcard AEAD algorithm of the same base as
934 * the requested algorithm, check the requested tag length to be
935 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (PSA_ALG_IS_AEAD(policy_alg) &&
937 PSA_ALG_IS_AEAD(requested_alg) &&
938 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
939 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
940 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
941 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
942 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100943 }
Steven Cooremancd640932021-03-08 12:00:27 +0100944 /* If policy_alg is a MAC algorithm of the same base as the requested
945 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (PSA_ALG_IS_MAC(policy_alg) &&
947 PSA_ALG_IS_MAC(requested_alg) &&
948 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
949 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100950 /* Validate the combination of key type and algorithm. Since the policy
951 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
953 return 0;
954 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100955
Steven Cooreman31a876d2021-03-03 20:47:40 +0100956 /* Get both the requested output length for the algorithm which is to be
957 * verified, and the default output length for the base algorithm.
958 * Note that none of the currently supported algorithms have an output
959 * length dependent on actual key size, so setting it to a bogus value
960 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100961 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100963 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 key_type, 0,
965 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100966
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100967 /* If the policy is default-length, only allow an algorithm with
968 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
970 return requested_output_length == default_output_length;
971 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100972
973 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100974 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
976 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
977 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100978 }
979
Steven Cooremancd640932021-03-08 12:00:27 +0100980 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
981 * check for the requested MAC length to be equal to or longer than the
982 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
984 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
985 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100986 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200987 }
Steven Cooremance48e852020-10-05 16:02:45 +0200988 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200989 * a key derivation with that key agreement should also be allowed. This
990 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
992 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
993 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
994 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200995 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100996 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200998}
999
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001000/** Test whether a policy permits an algorithm.
1001 *
1002 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001004 * \note This function requires providing the key type for which the policy is
1005 * being validated, since some algorithm policy definitions (e.g. MAC)
1006 * have different properties depending on what kind of cipher it is
1007 * combined with.
1008 *
Steven Cooreman7e39f052021-02-23 14:18:32 +01001009 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1010 * allowed by the \p policy.
1011 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1012 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1013 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1016 psa_key_type_t key_type,
1017 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001018{
Steven Cooremand788fab2021-02-25 11:29:17 +01001019 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (alg == 0) {
1021 return PSA_ERROR_INVALID_ARGUMENT;
1022 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001023
Steven Cooreman7e39f052021-02-23 14:18:32 +01001024 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (PSA_ALG_IS_WILDCARD(alg)) {
1026 return PSA_ERROR_INVALID_ARGUMENT;
1027 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1030 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1031 return PSA_SUCCESS;
1032 } else {
1033 return PSA_ERROR_NOT_PERMITTED;
1034 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035}
1036
Gilles Peskinef603c712019-01-19 13:40:11 +01001037/** Restrict a key policy based on a constraint.
1038 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001039 * \note This function requires providing the key type for which the policy is
1040 * being restricted, since some algorithm policy definitions (e.g. MAC)
1041 * have different properties depending on what kind of cipher it is
1042 * combined with.
1043 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001044 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001045 * \param[in,out] policy The policy to restrict.
1046 * \param[in] constraint The policy constraint to apply.
1047 *
1048 * \retval #PSA_SUCCESS
1049 * \c *policy contains the intersection of the original value of
1050 * \c *policy and \c *constraint.
1051 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001052 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001053 * \c *policy is unchanged.
1054 */
1055static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001056 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001057 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001059{
1060 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1062 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001063 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1065 constraint->alg2);
1066 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1067 return PSA_ERROR_INVALID_ARGUMENT;
1068 }
1069 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1070 return PSA_ERROR_INVALID_ARGUMENT;
1071 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001072 policy->usage &= constraint->usage;
1073 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001074 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001076}
1077
Przemek Stekiel061f6942022-11-30 10:51:35 +01001078/** Get the description of a key given its identifier and policy constraints
1079 * and lock it.
1080 *
1081 * The key must have allow all the usage flags set in \p usage. If \p alg is
1082 * nonzero, the key must allow operations with this algorithm. If \p alg is
1083 * zero, the algorithm is not checked.
1084 *
1085 * In case of a persistent key, the function loads the description of the key
1086 * into a key slot if not already done.
1087 *
Ryan Everett098c6652024-01-03 13:03:36 +00001088 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001089 * It is the responsibility of the caller to then unregister
1090 * once they have finished reading the contents of the slot.
1091 * The caller unregisters by calling psa_unregister_read() or
1092 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1093 * if and only if the caller already holds the global key slot mutex
1094 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1095 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +01001096 */
1097static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001098 mbedtls_svc_key_id_t key,
1099 psa_key_slot_t **p_slot,
1100 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001102{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001104 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 status = psa_get_and_lock_key_slot(key, p_slot);
1107 if (status != PSA_SUCCESS) {
1108 return status;
1109 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001110 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001111
1112 /* Enforce that usage policy for the key slot contains all the flags
1113 * required by the usage parameter. There is one exception: public
1114 * keys can always be exported, so we treat public key objects as
1115 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001117 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001121 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001122 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001123 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001124
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001125 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (alg != 0) {
1127 status = psa_key_policy_permits(&slot->attr.policy,
1128 slot->attr.type,
1129 alg);
1130 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001131 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001133 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001136
1137error:
1138 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001139 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001142}
Darryl Green940d72c2018-07-13 13:18:51 +01001143
Ronald Cron5c522922020-11-14 16:35:34 +01001144/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001145 *
1146 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001147 * available, as opposed to a key in a secure element and/or to be used
1148 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001149 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001150 * This is a temporary function that may be used instead of
1151 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1152 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001153 *
Ryan Everett098c6652024-01-03 13:03:36 +00001154 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001155 * It is the responsibility of the caller to then unregister
1156 * once they have finished reading the contents of the slot.
1157 * The caller unregisters by calling psa_unregister_read() or
1158 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1159 * if and only if the caller already holds the global key slot mutex
1160 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1161 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001162 */
Ronald Cron5c522922020-11-14 16:35:34 +01001163static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1164 mbedtls_svc_key_id_t key,
1165 psa_key_slot_t **p_slot,
1166 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001168{
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1170 usage, alg);
1171 if (status != PSA_SUCCESS) {
1172 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001173 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001176 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 *p_slot = NULL;
1178 return PSA_ERROR_NOT_SUPPORTED;
1179 }
1180
1181 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001182}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001185{
Valerio Settidbfb2ab2024-08-16 12:52:19 +02001186#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
1187 if (slot->key.bytes > 0) {
1188 mbedtls_platform_zeroize(slot->key.data, MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE);
1189 }
1190#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001192 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001194
Ronald Cronea0f8a62020-11-25 17:52:23 +01001195 slot->key.data = NULL;
Valerio Setti8d4f1502024-06-14 07:49:05 +02001196#endif /* MBEDTLS_PSA_STATIC_KEY_SLOTS */
1197
Ronald Cronea0f8a62020-11-25 17:52:23 +01001198 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001201}
1202
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001203/** Completely wipe a slot in memory, including its policy.
1204 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001206{
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 /*
1210 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001211 * do our best to report an unexpected amount of registered readers or
1212 * an unexpected state.
1213 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1214 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1216 * function is called as part of the execution of a test suite, the
1217 * execution of the test suite is stopped in error if the assertion fails.
1218 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001219 switch (slot->state) {
1220 case PSA_SLOT_FULL:
1221 /* In this state psa_wipe_key_slot() must only be called if the
1222 * caller is the last reader. */
1223 case PSA_SLOT_PENDING_DELETION:
1224 /* In this state psa_wipe_key_slot() must only be called if the
1225 * caller is the last reader. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001226 if (slot->var.occupied.registered_readers != 1) {
1227 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 1);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001228 status = PSA_ERROR_CORRUPTION_DETECTED;
1229 }
1230 break;
1231 case PSA_SLOT_FILLING:
1232 /* In this state registered_readers must be 0. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001233 if (slot->var.occupied.registered_readers != 0) {
1234 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 0);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001235 status = PSA_ERROR_CORRUPTION_DETECTED;
1236 }
1237 break;
1238 case PSA_SLOT_EMPTY:
1239 /* The slot is already empty, it cannot be wiped. */
1240 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1241 status = PSA_ERROR_CORRUPTION_DETECTED;
1242 break;
1243 default:
1244 /* The slot's state is invalid. */
1245 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001246 }
1247
Gilles Peskinee8199f52024-06-10 11:53:33 +02001248#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1249 size_t slice_index = slot->slice_index;
1250#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1251
1252
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001253 /* Multipart operations may still be using the key. This is safe
1254 * because all multipart operation objects are independent from
1255 * the key slot: if they need to access the key after the setup
1256 * phase, they have a copy of the key. Note that this means that
1257 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001258 /* At this point, key material and other type-specific content has
1259 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001260 * zeroize because the metadata is not particularly sensitive.
1261 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 memset(slot, 0, sizeof(*slot));
Gilles Peskinee8199f52024-06-10 11:53:33 +02001263
1264#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1265 /* If the slot is already corrupted, something went deeply wrong,
1266 * like a thread still using the slot or a stray pointer leading
1267 * to the slot's memory being used for another object. Let the slot
1268 * leak rather than make the corruption worse. */
1269 if (status == PSA_SUCCESS) {
1270 status = psa_free_key_slot(slice_index, slot);
1271 }
1272#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001275}
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001279 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001280 psa_status_t status; /* status of the last operation */
1281 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1283 psa_se_drv_table_entry_t *driver;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (mbedtls_svc_key_id_is_null(key)) {
1287 return PSA_SUCCESS;
1288 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001289
Ronald Cronf2911112020-10-29 17:51:10 +01001290 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001291 * Get the description of the key in a key slot, and register to read it.
1292 * In the case of a persistent key, this will load the key description
1293 * from persistent memory if not done yet.
1294 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001295 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001296 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 status = psa_get_and_lock_key_slot(key, &slot);
1298 if (status != PSA_SUCCESS) {
1299 return status;
1300 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001301
Ryan Everettc053d962024-01-25 17:56:32 +00001302#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001303 /* We cannot unlock between setting the state to PENDING_DELETION
1304 * and destroying the key in storage, as otherwise another thread
1305 * could load the key into a new slot and the key will not be
1306 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001307 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1308 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001309
1310 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1311 /* Another thread has destroyed the key between us locking the slot
1312 * and us gaining the mutex. Unregister from the slot,
1313 * and report that the key does not exist. */
1314 status = psa_unregister_read(slot);
1315
1316 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1317 &mbedtls_threading_key_slot_mutex));
1318 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1319 }
Ryan Everettc053d962024-01-25 17:56:32 +00001320#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001321 /* Set the key slot containing the key description's state to
1322 * PENDING_DELETION. This stops new operations from registering
1323 * to read the slot. Current readers can safely continue to access
1324 * the key within the slot; the last registered reader will
1325 * automatically wipe the slot when they call psa_unregister_read().
1326 * If the key is persistent, we can now delete the copy of the key
1327 * from memory. If the key is opaque, we require the driver to
1328 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001329 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1330 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001331
Ryan Everettd868b742024-03-08 18:35:09 +00001332 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001333 goto exit;
1334 }
Ronald Cronf2911112020-10-29 17:51:10 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001337 /* Refuse the destruction of a read-only key (which may or may not work
1338 * if we attempt it, depending on whether the key is merely read-only
1339 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001340 * Just do the best we can, which is to wipe the copy in memory
1341 * (done in this function's cleanup code). */
1342 overall_status = PSA_ERROR_NOT_PERMITTED;
1343 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001344 }
1345
Gilles Peskine354f7672019-07-12 23:46:38 +02001346#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1348 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001349 /* For a key in a secure element, we need to do three things:
1350 * remove the key file in internal storage, destroy the
1351 * key inside the secure element, and update the driver's
1352 * persistent data. Start a transaction that will encompass these
1353 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001355 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001357 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 status = psa_crypto_save_transaction();
1359 if (status != PSA_SUCCESS) {
1360 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001361 /* We should still try to destroy the key in the secure
1362 * element and the key metadata in storage. This is especially
1363 * important if the error is that the storage is full.
1364 * But how to do it exactly without risking an inconsistent
1365 * state after a reset?
1366 * https://github.com/ARMmbed/mbed-crypto/issues/215
1367 */
1368 overall_status = status;
1369 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 status = psa_destroy_se_key(driver,
1373 psa_key_slot_get_slot_number(slot));
1374 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001375 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001377 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1379
Darryl Greend49a4992018-06-18 17:27:26 +01001380#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001382 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001383 * The slot will still hold a copy of the key until the last reader
1384 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 status = psa_destroy_persistent_key(slot->attr.id);
1386 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001387 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 }
Darryl Greend49a4992018-06-18 17:27:26 +01001389 }
1390#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001391
Gilles Peskinefc762652019-07-22 19:30:34 +02001392#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (driver != NULL) {
1394 status = psa_save_se_persistent_data(driver);
1395 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001396 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 }
1398 status = psa_crypto_stop_transaction();
1399 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001400 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001402 }
1403#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1404
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001405exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001406 /* Unregister from reading the slot. If we are the last active reader
1407 * then this will wipe the slot. */
1408 status = psa_unregister_read(slot);
1409 /* Prioritize CORRUPTION_DETECTED from unregistering over
1410 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001412 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 }
Ryan Everettc053d962024-01-25 17:56:32 +00001414
1415#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001416 /* Don't overwrite existing errors if the unlock fails. */
1417 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001418 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1419 &mbedtls_threading_key_slot_mutex));
1420#endif
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423}
1424
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001425/** Retrieve all the publicly-accessible attributes of a key.
1426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1428 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001431 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1436 if (status != PSA_SUCCESS) {
1437 return status;
1438 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001439
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001440 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001441
1442#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1444 psa_set_key_slot_number(attributes,
1445 psa_key_slot_get_slot_number(slot));
1446 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001447#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001448
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001449 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450}
1451
Gilles Peskinec8000c02019-08-02 20:15:51 +02001452#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1453psa_status_t psa_get_key_slot_number(
1454 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001456{
Gilles Peskine972539c2024-02-28 01:49:45 +01001457 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001458 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return PSA_SUCCESS;
1460 } else {
1461 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001462 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001463}
1464#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1467 size_t key_buffer_size,
1468 uint8_t *data,
1469 size_t data_size,
1470 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001471{
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (key_buffer_size > data_size) {
1473 return PSA_ERROR_BUFFER_TOO_SMALL;
1474 }
1475 memcpy(data, key_buffer, key_buffer_size);
1476 memset(data + key_buffer_size, 0,
1477 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001478 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001480}
1481
Ronald Cron7285cda2020-11-26 14:46:37 +01001482psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001483 const psa_key_attributes_t *attributes,
1484 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001487 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (key_type_is_raw_bytes(type) ||
1490 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001491 PSA_KEY_TYPE_IS_ECC(type) ||
1492 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return psa_export_key_buffer_internal(
1494 key_buffer, key_buffer_size,
1495 data, data_size, data_length);
1496 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001497 /* This shouldn't happen in the reference implementation, but
1498 it is valid for a special-purpose implementation to omit
1499 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501 }
1502}
1503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001505 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 size_t data_size,
1507 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508{
1509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1510 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1511 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001512 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001513
Ronald Crone907e552021-01-18 13:22:38 +01001514 /* Reject a zero-length output buffer now, since this can never be a
1515 * valid key representation. This way we know that data must be a valid
1516 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (data_size == 0) {
1518 return PSA_ERROR_BUFFER_TOO_SMALL;
1519 }
Ronald Crone907e552021-01-18 13:22:38 +01001520
Ronald Cron9486f9d2020-11-26 13:36:23 +01001521 /* Set the key to empty now, so that even when there are errors, we always
1522 * set data_length to a value between 0 and data_size. On error, setting
1523 * the key to empty is a good choice because an empty key representation is
1524 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001525 *data_length = 0;
1526
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527 /* Export requires the EXPORT flag. There is an exception for public keys,
1528 * which don't require any flag, but
1529 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1532 PSA_KEY_USAGE_EXPORT, 0);
1533 if (status != PSA_SUCCESS) {
1534 return status;
1535 }
mohammad160306e79202018-03-28 13:17:44 +03001536
Ryan Everett45ac5262024-01-08 17:15:19 +00001537 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1538
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001539 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 slot->key.data, slot->key.bytes,
1541 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001542
David Horstmann4a48bec2024-03-13 15:42:31 +00001543#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001544exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001545#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001546 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001547
Ryan Everett45ac5262024-01-08 17:15:19 +00001548 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001550}
1551
Ronald Cron7285cda2020-11-26 14:46:37 +01001552psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001553 const psa_key_attributes_t *attributes,
1554 const uint8_t *key_buffer,
1555 size_t key_buffer_size,
1556 uint8_t *data,
1557 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001559{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001560 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001561
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001562 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001563 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1564 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001565 /* Exporting public -> public */
1566 return psa_export_key_buffer_internal(
1567 key_buffer, key_buffer_size,
1568 data, data_size, data_length);
1569 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001570#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001571 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1572 return mbedtls_psa_rsa_export_public_key(attributes,
1573 key_buffer,
1574 key_buffer_size,
1575 data,
1576 data_size,
1577 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001578#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001579 /* We don't know how to convert a private RSA key to public. */
1580 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001581#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001582 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001583 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001584#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001585 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1586 return mbedtls_psa_ecp_export_public_key(attributes,
1587 key_buffer,
1588 key_buffer_size,
1589 data,
1590 data_size,
1591 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001593 /* We don't know how to convert a private ECC key to public */
1594 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001597 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001598#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001599 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001600 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001601 key_buffer,
1602 key_buffer_size,
1603 data, data_size,
1604 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001605#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001606 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001607#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001608 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001610 (void) key_buffer;
1611 (void) key_buffer_size;
1612 (void) data;
1613 (void) data_size;
1614 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001616 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617}
Ronald Crond18b5f82020-11-25 16:46:05 +01001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001620 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 size_t data_size,
1622 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001623{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001625 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001626 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001627
Ryan Everettb1d2c672024-01-08 17:19:30 +00001628 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001629
Ronald Crone907e552021-01-18 13:22:38 +01001630 /* Reject a zero-length output buffer now, since this can never be a
1631 * valid key representation. This way we know that data must be a valid
1632 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (data_size == 0) {
1634 return PSA_ERROR_BUFFER_TOO_SMALL;
1635 }
Ronald Crone907e552021-01-18 13:22:38 +01001636
Darryl Greendd8fb772018-11-07 16:00:44 +00001637 /* Set the key to empty now, so that even when there are errors, we always
1638 * set data_length to a value between 0 and data_size. On error, setting
1639 * the key to empty is a good choice because an empty key representation is
1640 * unlikely to be accepted anywhere. */
1641 *data_length = 0;
1642
1643 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1645 if (status != PSA_SUCCESS) {
1646 return status;
1647 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001648
Ryan Everettb1d2c672024-01-08 17:19:30 +00001649 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1652 status = PSA_ERROR_INVALID_ARGUMENT;
1653 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001654 }
1655
Ronald Cron67227982020-11-26 15:16:05 +01001656 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001657 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001659
1660exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001661 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001662
Ryan Everettb1d2c672024-01-08 17:19:30 +00001663 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001665}
1666
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001667/** Validate that a key policy is internally well-formed.
1668 *
1669 * This function only rejects invalid policies. It does not validate the
1670 * consistency of the policy with respect to other attributes of the key
1671 * such as the key type.
1672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001673static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001674{
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1676 PSA_KEY_USAGE_COPY |
1677 PSA_KEY_USAGE_ENCRYPT |
1678 PSA_KEY_USAGE_DECRYPT |
1679 PSA_KEY_USAGE_SIGN_MESSAGE |
1680 PSA_KEY_USAGE_VERIFY_MESSAGE |
1681 PSA_KEY_USAGE_SIGN_HASH |
1682 PSA_KEY_USAGE_VERIFY_HASH |
1683 PSA_KEY_USAGE_VERIFY_DERIVATION |
1684 PSA_KEY_USAGE_DERIVE)) != 0) {
1685 return PSA_ERROR_INVALID_ARGUMENT;
1686 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001689}
1690
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001691/** Validate the internal consistency of key attributes.
1692 *
1693 * This function only rejects invalid attribute values. If does not
1694 * validate the consistency of the attributes with any key data that may
1695 * be involved in the creation of the key.
1696 *
1697 * Call this function early in the key creation process.
1698 *
1699 * \param[in] attributes Key attributes for the new key.
1700 * \param[out] p_drv On any return, the driver for the key, if any.
1701 * NULL for a transparent key.
1702 *
1703 */
1704static psa_status_t psa_validate_key_attributes(
1705 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001707{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001708 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1710 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 status = psa_validate_key_location(lifetime, p_drv);
1713 if (status != PSA_SUCCESS) {
1714 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001715 }
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_validate_key_persistence(lifetime);
1718 if (status != PSA_SUCCESS) {
1719 return status;
1720 }
1721
1722 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1723 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1724 return PSA_ERROR_INVALID_ARGUMENT;
1725 }
1726 } else {
1727 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1728 return PSA_ERROR_INVALID_ARGUMENT;
1729 }
1730 }
1731
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001732 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (status != PSA_SUCCESS) {
1734 return status;
1735 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001736
1737 /* Refuse to create overly large keys.
1738 * Note that this doesn't trigger on import if the attributes don't
1739 * explicitly specify a size (so psa_get_key_bits returns 0), so
1740 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1742 return PSA_ERROR_NOT_SUPPORTED;
1743 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001746}
1747
Gilles Peskine4747d192019-04-17 15:05:45 +02001748/** Prepare a key slot to receive key material.
1749 *
1750 * This function allocates a key slot and sets its metadata.
1751 *
1752 * If this function fails, call psa_fail_key_creation().
1753 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001754 * This function is intended to be used as follows:
1755 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 * it with the specified attributes, and in case of a volatile key assign it
1757 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 * -# Populate the slot with the key material.
1759 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1760 * In case of failure at any step, stop the sequence and call
1761 * psa_fail_key_creation().
1762 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001763 * On success, the key slot's state is PSA_SLOT_FILLING.
1764 * It is the responsibility of the caller to change the slot's state to
1765 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001766 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001767 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001769 * \param[out] p_slot On success, a pointer to the prepared slot.
1770 * \param[out] p_drv On any return, the driver for the key, if any.
1771 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001772 *
1773 * \retval #PSA_SUCCESS
1774 * The key slot is ready to receive key material.
1775 * \return If this function fails, the key slot is an invalid state.
1776 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 */
1778static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001779 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001783{
1784 psa_status_t status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001785
Gilles Peskinedf179142019-07-15 22:02:14 +02001786 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001787 *p_drv = NULL;
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 status = psa_validate_key_attributes(attributes, p_drv);
1790 if (status != PSA_SUCCESS) {
1791 return status;
1792 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793
Gilles Peskinee8199f52024-06-10 11:53:33 +02001794 int key_is_volatile = PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime);
1795 psa_key_id_t volatile_key_id;
1796
Ryan Everett3d8118d2024-01-30 16:58:47 +00001797#if defined(MBEDTLS_THREADING_C)
1798 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1799 &mbedtls_threading_key_slot_mutex));
1800#endif
Gilles Peskinee8199f52024-06-10 11:53:33 +02001801 status = psa_reserve_free_key_slot(
1802 key_is_volatile ? &volatile_key_id : NULL,
1803 p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001804#if defined(MBEDTLS_THREADING_C)
1805 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1806 &mbedtls_threading_key_slot_mutex));
1807#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (status != PSA_SUCCESS) {
1809 return status;
1810 }
Gilles Peskinee8199f52024-06-10 11:53:33 +02001811 psa_key_slot_t *slot = *p_slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001812
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001813 /* We're storing the declared bit-size of the key. It's up to each
1814 * creation mechanism to verify that this information is correct.
1815 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001816 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001817 * is optional (import, copy). In case of a volatile key, assign it the
1818 * volatile key identifier associated to the slot returned to contain its
1819 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001820
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001821 slot->attr = *attributes;
Gilles Peskinee8199f52024-06-10 11:53:33 +02001822 if (key_is_volatile) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001823#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1824 slot->attr.id = volatile_key_id;
1825#else
1826 slot->attr.id.key_id = volatile_key_id;
1827#endif
1828 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001829
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001831 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001832 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001833 * create the key file in internal storage, create the
1834 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001835 * persistent data. This is done by starting a transaction that will
1836 * encompass these three actions.
1837 * For registering a volatile key, we just need to find an appropriate
1838 * slot number inside the SE. Since the key is designated volatile, creating
1839 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001840 /* The first thing to do is to find a slot number for the new key.
1841 * We save the slot number in persistent storage as part of the
1842 * transaction data. It will be needed to recover if the power
1843 * fails during the key creation process, to clean up on the secure
1844 * element side after restarting. Obtaining a slot number from the
1845 * secure element driver updates its persistent state, but we do not yet
1846 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001847 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001849 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1851 &slot_number);
1852 if (status != PSA_SUCCESS) {
1853 return status;
1854 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001855
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001856 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001858 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001859 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001860 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 status = psa_crypto_save_transaction();
1862 if (status != PSA_SUCCESS) {
1863 (void) psa_crypto_stop_transaction();
1864 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001865 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001866 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001867
1868 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everettb5a20d32023-11-15 16:26:01 +00001870 if (status != PSA_SUCCESS) {
1871 return status;
1872 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001873 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001876 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001878 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001879#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001882}
Gilles Peskine4747d192019-04-17 15:05:45 +02001883
1884/** Finalize the creation of a key once its key material has been set.
1885 *
1886 * This entails writing the key to persistent storage.
1887 *
1888 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001889 * See the documentation of psa_start_key_creation() for the intended use
1890 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001892 * If the finalization succeeds, the function sets the key slot's state to
1893 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1894 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001895 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001896 * \param[in,out] slot Pointer to the slot with key material.
1897 * \param[in] driver The secure element driver for the key,
1898 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001899 * \param[out] key On success, identifier of the key. Note that the
1900 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001901 *
1902 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001903 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001904 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1905 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1906 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1907 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1908 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1909 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001910 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001911 * \return If this function fails, the key slot is an invalid state.
1912 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001914static psa_status_t psa_finish_key_creation(
1915 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001916 psa_se_drv_table_entry_t *driver,
1917 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001918{
1919 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001920 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001921 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001922
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001923#if defined(MBEDTLS_THREADING_C)
1924 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1925 &mbedtls_threading_key_slot_mutex));
1926#endif
1927
Gilles Peskine4747d192019-04-17 15:05:45 +02001928#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001930#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001932 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001933 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001935
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001936 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1937 sizeof(data.slot_number),
1938 "Slot number size does not match psa_se_key_data_storage_t");
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1941 status = psa_save_persistent_key(&slot->attr,
1942 (uint8_t *) &data,
1943 sizeof(data));
1944 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001945#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1946 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001947 /* Key material is saved in export representation in the slot, so
1948 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_save_persistent_key(&slot->attr,
1950 slot->key.data,
1951 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001952 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001953 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001954#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1955
Gilles Peskinecbaff462019-07-12 23:46:04 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001957 /* Finish the transaction for a key creation. This does not
1958 * happen when registering an existing key. Detect this case
1959 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001960 * creation of a persistent key in a secure element requires a transaction,
1961 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if (driver != NULL &&
1963 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1964 status = psa_save_se_persistent_data(driver);
1965 if (status != PSA_SUCCESS) {
1966 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001967
1968#if defined(MBEDTLS_THREADING_C)
1969 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1970 &mbedtls_threading_key_slot_mutex));
1971#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001973 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001975 }
1976#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001979 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001980 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1981 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001983 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001985 }
Ronald Cron50972942020-11-14 11:28:25 +01001986
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001987#if defined(MBEDTLS_THREADING_C)
1988 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1989 &mbedtls_threading_key_slot_mutex));
1990#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001992}
1993
1994/** Abort the creation of a key.
1995 *
1996 * You may call this function after calling psa_start_key_creation(),
1997 * or after psa_finish_key_creation() fails. In other circumstances, this
1998 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001999 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00002000 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02002001 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 * \param[in,out] slot Pointer to the slot with key material.
2003 * \param[in] driver The secure element driver for the key,
2004 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006static void psa_fail_key_creation(psa_key_slot_t *slot,
2007 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002008{
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 (void) driver;
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002012 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002014
Ryan Everettb7101442024-01-23 20:09:49 +00002015#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00002016 /* If the lock operation fails we still wipe the slot.
2017 * Operations will no longer work after a failed lock,
2018 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00002019 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
2020#endif
2021
Gilles Peskine011e4282019-06-26 18:34:38 +02002022#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002023 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002024 * element, and the failure happened later (when saving metadata
2025 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002026 * element.
2027 * https://github.com/ARMmbed/mbed-crypto/issues/217
2028 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002029
Gilles Peskined7729582019-08-05 15:55:54 +02002030 /* Abort the ongoing transaction if any (there may not be one if
2031 * the creation process failed before starting one, or if the
2032 * key creation is a registration of a key in a secure element).
2033 * Earlier functions must already have done what it takes to undo any
2034 * partial creation. All that's left is to update the transaction data
2035 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002037#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00002040
2041#if defined(MBEDTLS_THREADING_C)
2042 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
2043#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02002044}
2045
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002046/** Validate optional attributes during key creation.
2047 *
2048 * Some key attributes are optional during key creation. If they are
2049 * specified in the attributes structure, check that they are consistent
2050 * with the data in the slot.
2051 *
2052 * This function should be called near the end of key creation, after
2053 * the slot in memory is fully populated but before saving persistent data.
2054 */
2055static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002059 if (attributes->type != 0) {
2060 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 return PSA_ERROR_INVALID_ARGUMENT;
2062 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 }
2064
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002065 if (attributes->bits != 0) {
2066 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 return PSA_ERROR_INVALID_ARGUMENT;
2068 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002069 }
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072}
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00002075 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 size_t data_length,
2077 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002078{
2079 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00002080 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002081 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002082 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002083 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302084 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085
Ronald Cron81709fc2020-11-14 12:10:32 +01002086 *key = MBEDTLS_SVC_KEY_ID_INIT;
2087
Gilles Peskine0f84d622019-09-12 19:03:13 +02002088 /* Reject zero-length symmetric keys (including raw data key objects).
2089 * This also rejects any key which might be encoded as an empty string,
2090 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (data_length == 0) {
2092 return PSA_ERROR_INVALID_ARGUMENT;
2093 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002094
Archana449608b2021-09-08 15:36:05 +05302095 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (data_length > SIZE_MAX / 8) {
2097 return PSA_ERROR_NOT_SUPPORTED;
2098 }
Archana6ed4bda2021-08-04 10:47:15 +05302099
Ryan Everettf028fe12024-01-08 17:14:44 +00002100 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2103 &slot, &driver);
2104 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002107
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002108 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302109 * storage ( thus not in the case of importing a key in a secure element
2110 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2111 * buffer to hold the imported key material. */
Valerio Setti70fa89c2024-08-14 05:12:45 +02002112 if (slot->key.bytes == 0) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002113 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302114 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 attributes, data, data_length, &storage_size);
2116 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Archanad8a83dc2021-06-14 10:04:16 +05302119 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_allocate_buffer_to_slot(slot, storage_size);
2121 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002124 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002125
2126 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_driver_wrapper_import_key(attributes,
2128 data, data_length,
2129 slot->key.data,
2130 slot->key.bytes,
2131 &slot->key.bytes, &bits);
2132 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002137 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002139 status = PSA_ERROR_INVALID_ARGUMENT;
2140 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002141 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002142
Archana6ed4bda2021-08-04 10:47:15 +05302143 /* Enforce a size limit, and in particular ensure that the bit
2144 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
2148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 status = psa_validate_optional_attributes(slot, attributes);
2150 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002155exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002156 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (status != PSA_SUCCESS) {
2158 psa_fail_key_creation(slot, driver);
2159 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002162}
2163
Gilles Peskined7729582019-08-05 15:55:54 +02002164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2165psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002167{
2168 psa_status_t status;
2169 psa_key_slot_t *slot = NULL;
2170 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002172
2173 /* Leaving attributes unspecified is not currently supported.
2174 * It could make sense to query the key type and size from the
2175 * secure element, but not all secure elements support this
2176 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2178 return PSA_ERROR_NOT_SUPPORTED;
2179 }
2180 if (psa_get_key_bits(attributes) == 0) {
2181 return PSA_ERROR_NOT_SUPPORTED;
2182 }
Gilles Peskined7729582019-08-05 15:55:54 +02002183
Gilles Peskined72ad732024-06-13 16:06:45 +02002184 /* Not usable with volatile keys, even with an appropriate location,
2185 * due to the API design.
2186 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2187 */
2188 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2189 return PSA_ERROR_INVALID_ARGUMENT;
2190 }
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2193 &slot, &driver);
2194 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 }
Gilles Peskined7729582019-08-05 15:55:54 +02002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002199
2200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (status != PSA_SUCCESS) {
2202 psa_fail_key_creation(slot, driver);
2203 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002204
Gilles Peskined7729582019-08-05 15:55:54 +02002205 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 psa_close_key(key);
2207 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002208}
2209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2212 const psa_key_attributes_t *specified_attributes,
2213 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002214{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002216 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002217 psa_key_slot_t *source_slot = NULL;
2218 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002219 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002220 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302221 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002222
Ronald Cron81709fc2020-11-14 12:10:32 +01002223 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2224
Archana8a180362021-07-05 02:18:48 +05302225 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2227 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 status = psa_validate_optional_attributes(source_slot,
2232 specified_attributes);
2233 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236
Archana9d17bf42021-09-10 06:22:44 +05302237 /* The target key type and number of bits have been validated by
2238 * psa_validate_optional_attributes() to be either equal to zero or
2239 * equal to the ones of the source key. So it is safe to inherit
2240 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302241 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002242 actual_attributes.bits = source_slot->attr.bits;
2243 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302244
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002247 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 &source_slot->attr.policy);
2249 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2254 &target_slot, &driver);
2255 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 }
2258 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2259 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302260 /*
Archana9d17bf42021-09-10 06:22:44 +05302261 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302262 * the source key would need to be exported as plaintext and re-imported
2263 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302264 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302265 * appropriate API invocations from the application, if needed.
2266 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002267 status = PSA_ERROR_NOT_SUPPORTED;
2268 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002269 }
Archana8a180362021-07-05 02:18:48 +05302270 /*
2271 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302272 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302273 * - For opaque keys this translates to an invocation of the drivers'
2274 * copy_key entry point through the dispatch layer.
2275 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002276 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2278 &storage_size);
2279 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
Archana374fe5b2021-09-08 15:50:28 +05302282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2284 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 }
Archana374fe5b2021-09-08 15:50:28 +05302287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 status = psa_driver_wrapper_copy_key(&actual_attributes,
2289 source_slot->key.data,
2290 source_slot->key.bytes,
2291 target_slot->key.data,
2292 target_slot->key.bytes,
2293 &target_slot->key.bytes);
2294 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 }
2297 } else {
2298 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302299 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 source_slot->key.bytes);
2301 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 }
Archana8a180362021-07-05 02:18:48 +05302304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (status != PSA_SUCCESS) {
2308 psa_fail_key_creation(target_slot, driver);
2309 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002310
Ryan Everetta103ec92024-01-31 13:59:57 +00002311 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002314}
2315
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002316
2317
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002318/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002319/* Message digests */
2320/****************************************************************/
2321
Gilles Peskine808b5412024-04-10 20:05:06 +02002322static int is_hash_supported(psa_algorithm_t alg)
2323{
2324 switch (alg) {
2325#if defined(PSA_WANT_ALG_MD5)
2326 case PSA_ALG_MD5:
2327 return 1;
2328#endif
2329#if defined(PSA_WANT_ALG_RIPEMD160)
2330 case PSA_ALG_RIPEMD160:
2331 return 1;
2332#endif
2333#if defined(PSA_WANT_ALG_SHA_1)
2334 case PSA_ALG_SHA_1:
2335 return 1;
2336#endif
2337#if defined(PSA_WANT_ALG_SHA_224)
2338 case PSA_ALG_SHA_224:
2339 return 1;
2340#endif
2341#if defined(PSA_WANT_ALG_SHA_256)
2342 case PSA_ALG_SHA_256:
2343 return 1;
2344#endif
2345#if defined(PSA_WANT_ALG_SHA_384)
2346 case PSA_ALG_SHA_384:
2347 return 1;
2348#endif
2349#if defined(PSA_WANT_ALG_SHA_512)
2350 case PSA_ALG_SHA_512:
2351 return 1;
2352#endif
2353#if defined(PSA_WANT_ALG_SHA3_224)
2354 case PSA_ALG_SHA3_224:
2355 return 1;
2356#endif
2357#if defined(PSA_WANT_ALG_SHA3_256)
2358 case PSA_ALG_SHA3_256:
2359 return 1;
2360#endif
2361#if defined(PSA_WANT_ALG_SHA3_384)
2362 case PSA_ALG_SHA3_384:
2363 return 1;
2364#endif
2365#if defined(PSA_WANT_ALG_SHA3_512)
2366 case PSA_ALG_SHA3_512:
2367 return 1;
2368#endif
2369 default:
2370 return 0;
2371 }
2372}
2373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002376 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (operation->id == 0) {
2378 return PSA_SUCCESS;
2379 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002382 operation->id = 0;
2383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385}
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2388 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002391
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002392 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002394 status = PSA_ERROR_BAD_STATE;
2395 goto exit;
2396 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002399 status = PSA_ERROR_INVALID_ARGUMENT;
2400 goto exit;
2401 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002402
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002403 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2404 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
2409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (status != PSA_SUCCESS) {
2411 psa_hash_abort(operation);
2412 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413
2414 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002418 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002422 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002425 status = PSA_ERROR_BAD_STATE;
2426 goto exit;
2427 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002428
Steven Cooremanc8288352021-03-04 14:02:19 +01002429 /* Don't require hash implementations to behave correctly on a
2430 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (input_length == 0) {
2432 return PSA_SUCCESS;
2433 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002434
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002435 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002437
2438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (status != PSA_SUCCESS) {
2440 psa_hash_abort(operation);
2441 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002442
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002443 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445}
2446
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002447static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002448 uint8_t *hash,
2449 size_t hash_size,
2450 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002451{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2453
Steven Cooremanfbe09282021-03-08 18:41:12 +01002454 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (operation->id == 0) {
2456 return PSA_ERROR_BAD_STATE;
2457 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002459 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 operation, hash, hash_size, hash_length);
2461 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002462
2463 return status;
2464}
2465
2466psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2467 uint8_t *hash_external,
2468 size_t hash_size,
2469 size_t *hash_length)
2470{
2471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2472 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2473
2474 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2475 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2476
David Horstmann4a48bec2024-03-13 15:42:31 +00002477#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002478exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002479#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002480 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482}
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002485 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487{
Ronald Cron69a63422021-10-18 09:47:58 +02002488 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2491 LOCAL_INPUT_DECLARE(hash_external, hash);
2492
2493 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 operation,
2495 actual_hash, sizeof(actual_hash),
2496 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002499 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002503 status = PSA_ERROR_INVALID_SIGNATURE;
2504 goto exit;
2505 }
2506
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002507 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002508 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002509 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002511
2512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2514 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002515 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002517 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519}
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002522 const uint8_t *input_external, size_t input_length,
2523 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002525{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2527 LOCAL_INPUT_DECLARE(input_external, input);
2528 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2529
Steven Cooremanfbe09282021-03-08 18:41:12 +01002530 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (!PSA_ALG_IS_HASH(alg)) {
2532 return PSA_ERROR_INVALID_ARGUMENT;
2533 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002534
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002535 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2536 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2537 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002538 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002539
David Horstmann4a48bec2024-03-13 15:42:31 +00002540#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002541exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002542#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002543 LOCAL_INPUT_FREE(input_external, input);
2544 LOCAL_OUTPUT_FREE(hash_external, hash);
2545 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002546}
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002549 const uint8_t *input_external, size_t input_length,
2550 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551{
Ronald Cron69a63422021-10-18 09:47:58 +02002552 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002553 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2555
2556 LOCAL_INPUT_DECLARE(input_external, input);
2557 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002560 status = PSA_ERROR_INVALID_ARGUMENT;
2561 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002563
Thomas Daubney51ffac92024-01-18 15:46:26 +00002564 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2565 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 alg, input, input_length,
2567 actual_hash, sizeof(actual_hash),
2568 &actual_hash_length);
2569 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 }
2572 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002573 status = PSA_ERROR_INVALID_SIGNATURE;
2574 goto exit;
2575 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002576
2577 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002578 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002579 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002581
2582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002584
2585 LOCAL_INPUT_FREE(input_external, input);
2586 LOCAL_INPUT_FREE(hash_external, hash);
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002589}
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2592 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002593{
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (source_operation->id == 0 ||
2595 target_operation->id != 0) {
2596 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002597 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2600 target_operation);
2601 if (status != PSA_SUCCESS) {
2602 psa_hash_abort(target_operation);
2603 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002606}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002607
2608
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002609/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610/* MAC */
2611/****************************************************************/
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614{
Steven Cooremane6804192021-03-19 18:28:56 +01002615 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (operation->id == 0) {
2617 return PSA_SUCCESS;
2618 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002621 operation->mac_size = 0;
2622 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002623 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626}
2627
Ronald Cron2dff3b22021-06-17 16:33:22 +02002628static psa_status_t psa_mac_finalize_alg_and_key_validation(
2629 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002630 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 psa_key_type_t key_type = psa_get_key_type(attributes);
2635 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (!PSA_ALG_IS_MAC(alg)) {
2638 return PSA_ERROR_INVALID_ARGUMENT;
2639 }
Steven Cooremane6804192021-03-19 18:28:56 +01002640
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002641 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 status = psa_mac_key_can_do(alg, key_type);
2643 if (status != PSA_SUCCESS) {
2644 return status;
2645 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002646
Steven Cooremand1a68f12021-05-10 11:13:41 +02002647 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002651 /* A very short MAC is too short for security since it can be
2652 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2653 * so we make this our minimum, even though 32 bits is still
2654 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002656 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2659 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002660 /* It's impossible to "truncate" to a larger length than the full length
2661 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002663 }
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002666 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2667 * that is disabled in the compile-time configuration. The result can
2668 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2669 * configuration into account. In this case, force a return of
2670 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2671 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2672 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2673 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2674 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002679}
2680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2682 mbedtls_svc_key_id_t key,
2683 psa_algorithm_t alg,
2684 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002685{
2686 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2687 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002688 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689
2690 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002692 status = PSA_ERROR_BAD_STATE;
2693 goto exit;
2694 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695
2696 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 key,
2698 &slot,
2699 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2700 alg);
2701 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002705 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 &operation->mac_size);
2707 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710
2711 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002712 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (is_sign) {
2714 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002715 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 slot->key.data,
2717 slot->key.bytes,
2718 alg);
2719 } else {
2720 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002721 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 slot->key.data,
2723 slot->key.bytes,
2724 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
Gilles Peskinefbfac682018-07-08 20:51:54 +02002727exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (status != PSA_SUCCESS) {
2729 psa_mac_abort(operation);
2730 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002731
Ryan Everettfb9857f2024-02-14 12:16:41 +00002732 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735}
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2738 mbedtls_svc_key_id_t key,
2739 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002740{
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002742}
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2745 mbedtls_svc_key_id_t key,
2746 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002747{
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002749}
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002752 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2756 LOCAL_INPUT_DECLARE(input_external, input);
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002759 status = PSA_ERROR_BAD_STATE;
2760 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002763 /* Don't require hash implementations to behave correctly on a
2764 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002766 status = PSA_SUCCESS;
2767 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002769
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002770 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2771 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (status != PSA_SUCCESS) {
2774 psa_mac_abort(operation);
2775 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002776
David Horstmann4a48bec2024-03-13 15:42:31 +00002777#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002778exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002779#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002780 LOCAL_INPUT_FREE(input_external, input);
2781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783}
2784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002786 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 size_t mac_size,
2788 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002789{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2791 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002792 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002793 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002796 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002797 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002798 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002801 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002802 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002803 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002804
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002805 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2806 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002808 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002809 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002810 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002813 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002814 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002815 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002816
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 status = psa_driver_wrapper_mac_sign_finish(operation,
2819 mac, operation->mac_size,
2820 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002821
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002822exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002823 /* In case of success, set the potential excess room in the output buffer
2824 * to an invalid value, to avoid potentially leaking a longer MAC.
2825 * In case of error, set the output length and content to a safe default,
2826 * such that in case the caller misses an error check, the output would be
2827 * an unachievable MAC.
2828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002830 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002831 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002832 }
mohammad16036df908f2018-04-02 08:34:15 -07002833
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002834 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002835 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2836 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002839 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002842}
2843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002845 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2849 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002850 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002853 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002854 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002855 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002858 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002859 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002860 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002863 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002864 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002865 }
2866
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002867 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 status = psa_driver_wrapper_mac_verify_finish(operation,
2869 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002870
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002871exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002873 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876}
2877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2879 psa_algorithm_t alg,
2880 const uint8_t *input,
2881 size_t input_length,
2882 uint8_t *mac,
2883 size_t mac_size,
2884 size_t *mac_length,
2885 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002886{
2887 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002888 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2889 psa_key_slot_t *slot;
2890 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891
Ronald Cron51131b52021-06-17 17:17:20 +02002892 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 key,
2894 &slot,
2895 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2896 alg);
2897 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002900
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002901 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 &operation_mac_size);
2903 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002904 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002908 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002909 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002910 }
2911
2912 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002913 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 slot->key.data, slot->key.bytes,
2915 alg,
2916 input, input_length,
2917 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002918
2919exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002920 /* In case of success, set the potential excess room in the output buffer
2921 * to an invalid value, to avoid potentially leaking a longer MAC.
2922 * In case of error, set the output length and content to a safe default,
2923 * such that in case the caller misses an error check, the output would be
2924 * an unachievable MAC.
2925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002927 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002928 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002929 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002930
2931 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002932
Ryan Everetta103ec92024-01-31 13:59:57 +00002933 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002936}
2937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002939 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002940 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002941 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002942 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 size_t mac_size,
2944 size_t *mac_length)
2945{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2947 LOCAL_INPUT_DECLARE(input_external, input);
2948 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2949
2950 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2951 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2952 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002953 input, input_length,
2954 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002955
David Horstmann4a48bec2024-03-13 15:42:31 +00002956#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002957exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002958#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002959 LOCAL_INPUT_FREE(input_external, input);
2960 LOCAL_OUTPUT_FREE(mac_external, mac);
2961
2962 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002963}
2964
2965psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2966 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002967 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002969 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002971{
2972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002973 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2974 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002975 LOCAL_INPUT_DECLARE(input_external, input);
2976 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002977
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002978 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 status = psa_mac_compute_internal(key, alg,
2980 input, input_length,
2981 actual_mac, sizeof(actual_mac),
2982 &actual_mac_length, 0);
2983 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002984 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002988 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002989 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002990 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002991
2992 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01002993 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002994 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002995 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002996 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002997
2998exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00003000 LOCAL_INPUT_FREE(input_external, input);
3001 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01003004}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003005
Gilles Peskinee59236f2018-01-27 23:32:46 +01003006/****************************************************************/
3007/* Asymmetric cryptography */
3008/****************************************************************/
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010static psa_status_t psa_sign_verify_check_alg(int input_is_message,
3011 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003012{
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (input_is_message) {
3014 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
3015 return PSA_ERROR_INVALID_ARGUMENT;
3016 }
Gilles Peskine808b5412024-04-10 20:05:06 +02003017 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003018
Gilles Peskine808b5412024-04-10 20:05:06 +02003019 psa_algorithm_t hash_alg = 0;
3020 if (PSA_ALG_IS_SIGN_HASH(alg)) {
3021 hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
3022 }
3023
3024 /* Now hash_alg==0 if alg by itself doesn't need a hash.
3025 * This is good enough for sign-hash, but a guaranteed failure for
3026 * sign-message which needs to hash first for all algorithms
3027 * supported at the moment. */
3028
3029 if (hash_alg == 0 && input_is_message) {
3030 return PSA_ERROR_INVALID_ARGUMENT;
3031 }
3032 if (hash_alg == PSA_ALG_ANY_HASH) {
3033 return PSA_ERROR_INVALID_ARGUMENT;
3034 }
3035 /* Give up immediately if the hash is not supported. This has
3036 * several advantages:
3037 * - For mechanisms that don't use the hash at all (e.g.
3038 * ECDSA verification, randomized ECDSA signature), without
3039 * this check, the operation would succeed even though it has
3040 * been given an invalid argument. This would not be insecure
3041 * since the hash was not necessary, but it would be weird.
3042 * - For mechanisms that do use the hash, we avoid an error
3043 * deep inside the execution. In principle this doesn't matter,
3044 * but there is a little more risk of a bug in error handling
3045 * deep inside than in this preliminary check.
3046 * - When calling a driver, the driver might be capable of using
3047 * a hash that the core doesn't support. This could potentially
3048 * result in a buffer overflow if the hash is larger than the
3049 * maximum hash size assumed by the core.
3050 * - Returning a consistent error makes it possible to test
3051 * not-supported hashes in a consistent way.
3052 */
3053 if (hash_alg != 0 && !is_hash_supported(hash_alg)) {
3054 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003055 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003058}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
3061 int input_is_message,
3062 psa_algorithm_t alg,
3063 const uint8_t *input,
3064 size_t input_length,
3065 uint8_t *signature,
3066 size_t signature_size,
3067 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003068{
3069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3070 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3071 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003072
3073 *signature_length = 0;
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 status = psa_sign_verify_check_alg(input_is_message, alg);
3076 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003077 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003079
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003080 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02003081 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003082 * buffer can in principle be empty since it doesn't actually have
3083 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 if (signature_size == 0) {
3085 return PSA_ERROR_BUFFER_TOO_SMALL;
3086 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003087
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003088 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 key, &slot,
3090 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3091 PSA_KEY_USAGE_SIGN_HASH,
3092 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003099 status = PSA_ERROR_INVALID_ARGUMENT;
3100 goto exit;
3101 }
3102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003104 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003105 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003106 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 signature, signature_size, signature_length);
3108 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003109
3110 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003111 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003112 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003114 }
3115
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003116
3117exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003118 psa_wipe_tag_output_buffer(signature, status, signature_size,
3119 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003120
Ryan Everetta103ec92024-01-31 13:59:57 +00003121 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003124}
3125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3127 int input_is_message,
3128 psa_algorithm_t alg,
3129 const uint8_t *input,
3130 size_t input_length,
3131 const uint8_t *signature,
3132 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003133{
3134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3135 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3136 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 status = psa_sign_verify_check_alg(input_is_message, alg);
3139 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003140 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003142
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003143 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 key, &slot,
3145 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3146 PSA_KEY_USAGE_VERIFY_HASH,
3147 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (status != PSA_SUCCESS) {
3150 return status;
3151 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003154 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003155 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003156 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 signature, signature_length);
3158 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003159 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003160 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003161 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003163 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003164
Ryan Everetta103ec92024-01-31 13:59:57 +00003165 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003168
3169}
3170
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003171psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003172 const psa_key_attributes_t *attributes,
3173 const uint8_t *key_buffer,
3174 size_t key_buffer_size,
3175 psa_algorithm_t alg,
3176 const uint8_t *input,
3177 size_t input_length,
3178 uint8_t *signature,
3179 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003181{
3182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003185 size_t hash_length;
3186 uint8_t hash[PSA_HASH_MAX_SIZE];
3187
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003188 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ALG_SIGN_GET_HASH(alg),
3190 input, input_length,
3191 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003194 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003196
3197 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 attributes, key_buffer, key_buffer_size,
3199 alg, hash, hash_length,
3200 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003201 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003204}
3205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3207 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003208 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003210 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 size_t signature_size,
3212 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003213{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3215 LOCAL_INPUT_DECLARE(input_external, input);
3216 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3217
3218 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3219 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3220 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3221 signature_size, signature_length);
3222
David Horstmann4a48bec2024-03-13 15:42:31 +00003223#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003224exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003225#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003226 LOCAL_INPUT_FREE(input_external, input);
3227 LOCAL_OUTPUT_FREE(signature_external, signature);
3228 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003229}
3230
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003231psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003232 const psa_key_attributes_t *attributes,
3233 const uint8_t *key_buffer,
3234 size_t key_buffer_size,
3235 psa_algorithm_t alg,
3236 const uint8_t *input,
3237 size_t input_length,
3238 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003240{
3241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003244 size_t hash_length;
3245 uint8_t hash[PSA_HASH_MAX_SIZE];
3246
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003247 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 PSA_ALG_SIGN_GET_HASH(alg),
3249 input, input_length,
3250 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003253 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003255
3256 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 attributes, key_buffer, key_buffer_size,
3258 alg, hash, hash_length,
3259 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003260 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003263}
3264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3266 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003267 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003269 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003271{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3273 LOCAL_INPUT_DECLARE(input_external, input);
3274 LOCAL_INPUT_DECLARE(signature_external, signature);
3275
3276 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3277 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3278 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3279 signature_length);
3280
David Horstmann4a48bec2024-03-13 15:42:31 +00003281#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003282exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003283#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003284 LOCAL_INPUT_FREE(input_external, input);
3285 LOCAL_INPUT_FREE(signature_external, signature);
3286
3287 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003288}
3289
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003290psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003291 const psa_key_attributes_t *attributes,
3292 const uint8_t *key_buffer, size_t key_buffer_size,
3293 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003295{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003296 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3298 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003299#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3301 return mbedtls_psa_rsa_sign_hash(
3302 attributes,
3303 key_buffer, key_buffer_size,
3304 alg, hash, hash_length,
3305 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003306#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3307 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 } else {
3309 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003310 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003311 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003313#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3315 return mbedtls_psa_ecdsa_sign_hash(
3316 attributes,
3317 key_buffer, key_buffer_size,
3318 alg, hash, hash_length,
3319 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003320#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3321 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 } else {
3323 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003324 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003325 }
Ronald Cron4501c982021-03-19 20:09:32 +01003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 (void) key_buffer;
3328 (void) key_buffer_size;
3329 (void) hash;
3330 (void) hash_length;
3331 (void) signature;
3332 (void) signature_size;
3333 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003336}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3339 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003340 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003342 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 size_t signature_size,
3344 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003345{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3347 LOCAL_INPUT_DECLARE(hash_external, hash);
3348 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3349
3350 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3351 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3352 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3353 signature_size, signature_length);
3354
David Horstmann4a48bec2024-03-13 15:42:31 +00003355#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003356exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003357#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003358 LOCAL_INPUT_FREE(hash_external, hash);
3359 LOCAL_OUTPUT_FREE(signature_external, signature);
3360
3361 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003362}
3363
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003364psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003365 const psa_key_attributes_t *attributes,
3366 const uint8_t *key_buffer, size_t key_buffer_size,
3367 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003369{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003370 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3372 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003373#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3375 return mbedtls_psa_rsa_verify_hash(
3376 attributes,
3377 key_buffer, key_buffer_size,
3378 alg, hash, hash_length,
3379 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003380#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3381 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 } else {
3383 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003384 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003385 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003387#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3389 return mbedtls_psa_ecdsa_verify_hash(
3390 attributes,
3391 key_buffer, key_buffer_size,
3392 alg, hash, hash_length,
3393 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003394#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3395 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 } else {
3397 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003398 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003399 }
Ronald Cron4501c982021-03-19 20:09:32 +01003400
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 (void) key_buffer;
3402 (void) key_buffer_size;
3403 (void) hash;
3404 (void) hash_length;
3405 (void) signature;
3406 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003409}
3410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3412 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003413 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003415 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003417{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3419 LOCAL_INPUT_DECLARE(hash_external, hash);
3420 LOCAL_INPUT_DECLARE(signature_external, signature);
3421
3422 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3423 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3424 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3425 signature_length);
3426
David Horstmann4a48bec2024-03-13 15:42:31 +00003427#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003428exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003429#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003430 LOCAL_INPUT_FREE(hash_external, hash);
3431 LOCAL_INPUT_FREE(signature_external, signature);
3432
3433 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003434}
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3437 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003438 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003440 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003442 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 size_t output_size,
3444 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003445{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003447 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003448 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003449
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003450 LOCAL_INPUT_DECLARE(input_external, input);
3451 LOCAL_INPUT_DECLARE(salt_external, salt);
3452 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453
Darryl Green5cc689a2018-07-24 15:34:10 +01003454 (void) input;
3455 (void) input_length;
3456 (void) salt;
3457 (void) output;
3458 (void) output_size;
3459
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003460 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3463 return PSA_ERROR_INVALID_ARGUMENT;
3464 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003465
Valerio Setti5bb454a2024-01-15 10:43:16 +01003466 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3468 if (status != PSA_SUCCESS) {
3469 return status;
3470 }
3471 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3472 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003473 status = PSA_ERROR_INVALID_ARGUMENT;
3474 goto exit;
3475 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003476
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003477 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3478 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3479 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003480
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003481 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003482 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003483 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003485exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003486 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003487
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003488 LOCAL_INPUT_FREE(input_external, input);
3489 LOCAL_INPUT_FREE(salt_external, salt);
3490 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003491
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003493}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003494
Gilles Peskine449bd832023-01-11 14:50:10 +01003495psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3496 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003497 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003499 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003501 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 size_t output_size,
3503 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003504{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003506 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003507 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003508
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003509 LOCAL_INPUT_DECLARE(input_external, input);
3510 LOCAL_INPUT_DECLARE(salt_external, salt);
3511 LOCAL_OUTPUT_DECLARE(output_external, output);
3512
Darryl Green5cc689a2018-07-24 15:34:10 +01003513 (void) input;
3514 (void) input_length;
3515 (void) salt;
3516 (void) output;
3517 (void) output_size;
3518
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003519 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3522 return PSA_ERROR_INVALID_ARGUMENT;
3523 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003524
Valerio Setti5bb454a2024-01-15 10:43:16 +01003525 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3527 if (status != PSA_SUCCESS) {
3528 return status;
3529 }
3530 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003531 status = PSA_ERROR_INVALID_ARGUMENT;
3532 goto exit;
3533 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003535 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3536 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3537 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003538
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003539 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003540 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003541 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003543
3544exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003545 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003546
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003547 LOCAL_INPUT_FREE(input_external, input);
3548 LOCAL_INPUT_FREE(salt_external, salt);
3549 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003552}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003553
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554/****************************************************************/
3555/* Asymmetric interruptible cryptography */
3556/****************************************************************/
3557
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003558static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3559
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560void psa_interruptible_set_max_ops(uint32_t max_ops)
3561{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003562 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003563}
3564
3565uint32_t psa_interruptible_get_max_ops(void)
3566{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003567 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003568}
3569
Paul Elliott9fe12f62022-11-30 19:16:02 +00003570uint32_t psa_sign_hash_get_num_ops(
3571 const psa_sign_hash_interruptible_operation_t *operation)
3572{
Paul Elliott296ede92022-12-15 17:00:30 +00003573 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574}
3575
3576uint32_t psa_verify_hash_get_num_ops(
3577 const psa_verify_hash_interruptible_operation_t *operation)
3578{
Paul Elliott296ede92022-12-15 17:00:30 +00003579 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003580}
3581
Paul Elliott59ad9452022-12-18 15:09:02 +00003582static psa_status_t psa_sign_hash_abort_internal(
3583 psa_sign_hash_interruptible_operation_t *operation)
3584{
3585 if (operation->id == 0) {
3586 /* The object has (apparently) been initialized but it is not (yet)
3587 * in use. It's ok to call abort on such an object, and there's
3588 * nothing to do. */
3589 return PSA_SUCCESS;
3590 }
3591
3592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3593
3594 status = psa_driver_wrapper_sign_hash_abort(operation);
3595
3596 operation->id = 0;
3597
Paul Elliotte6145dc2023-02-07 12:51:21 +00003598 /* Do not clear either the error_occurred or num_ops elements here as they
3599 * only want to be cleared by the application calling abort, not by abort
3600 * being called at completion of an operation. */
3601
Paul Elliott59ad9452022-12-18 15:09:02 +00003602 return status;
3603}
3604
Paul Elliott9fe12f62022-11-30 19:16:02 +00003605psa_status_t psa_sign_hash_start(
3606 psa_sign_hash_interruptible_operation_t *operation,
3607 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003608 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003609{
3610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3611 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3612 psa_key_slot_t *slot;
3613
David Horstmann4a523a62024-03-11 13:32:16 +00003614 LOCAL_INPUT_DECLARE(hash_external, hash);
3615
Paul Elliottc9774412023-02-06 15:14:07 +00003616 /* Check that start has not been previously called, or operation has not
3617 * previously errored. */
3618 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619 return PSA_ERROR_BAD_STATE;
3620 }
3621
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622 status = psa_sign_verify_check_alg(0, alg);
3623 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003624 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625 return status;
3626 }
3627
3628 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3629 PSA_KEY_USAGE_SIGN_HASH,
3630 alg);
3631
3632 if (status != PSA_SUCCESS) {
3633 goto exit;
3634 }
3635
3636 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3637 status = PSA_ERROR_INVALID_ARGUMENT;
3638 goto exit;
3639 }
3640
David Horstmann4a523a62024-03-11 13:32:16 +00003641 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3642
Paul Elliott296ede92022-12-15 17:00:30 +00003643 /* Ensure ops count gets reset, in case of operation re-use. */
3644 operation->num_ops = 0;
3645
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003646 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003647 slot->key.data,
3648 slot->key.bytes, alg,
3649 hash, hash_length);
3650exit:
3651
3652 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003653 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003654 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003655 }
3656
Ryan Everettdcc03d52024-02-14 15:44:13 +00003657 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003658
Paul Elliottc9774412023-02-06 15:14:07 +00003659 if (unlock_status != PSA_SUCCESS) {
3660 operation->error_occurred = 1;
3661 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003662
David Horstmann4a523a62024-03-11 13:32:16 +00003663 LOCAL_INPUT_FREE(hash_external, hash);
3664
Paul Elliottc9774412023-02-06 15:14:07 +00003665 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003666}
3667
3668
3669psa_status_t psa_sign_hash_complete(
3670 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003671 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003672 size_t *signature_length)
3673{
3674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3675
David Horstmann4a523a62024-03-11 13:32:16 +00003676 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3677
Paul Elliott9fe12f62022-11-30 19:16:02 +00003678 *signature_length = 0;
3679
Paul Elliottc9774412023-02-06 15:14:07 +00003680 /* Check that start has been called first, and that operation has not
3681 * previously errored. */
3682 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003683 status = PSA_ERROR_BAD_STATE;
3684 goto exit;
3685 }
3686
Paul Elliott096abc42023-02-03 18:33:23 +00003687 /* Immediately reject a zero-length signature buffer. This guarantees that
3688 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003689 if (signature_size == 0) {
3690 status = PSA_ERROR_BUFFER_TOO_SMALL;
3691 goto exit;
3692 }
3693
David Horstmann4a523a62024-03-11 13:32:16 +00003694 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3695
Paul Elliott9fe12f62022-11-30 19:16:02 +00003696 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3697 signature_size,
3698 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003699
Paul Elliott296ede92022-12-15 17:00:30 +00003700 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003701 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003702
Paul Elliottebe225c2023-02-10 14:32:53 +00003703exit:
3704
David Horstmannc5064c82024-03-11 17:02:03 +00003705 if (signature != NULL) {
3706 psa_wipe_tag_output_buffer(signature, status, signature_size,
3707 *signature_length);
3708 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003709
Paul Elliott53bb3122023-02-10 14:22:22 +00003710 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003711 if (status != PSA_SUCCESS) {
3712 operation->error_occurred = 1;
3713 }
3714
Paul Elliott59ad9452022-12-18 15:09:02 +00003715 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003716 }
3717
David Horstmann4a523a62024-03-11 13:32:16 +00003718 LOCAL_OUTPUT_FREE(signature_external, signature);
3719
Paul Elliott9fe12f62022-11-30 19:16:02 +00003720 return status;
3721}
3722
3723psa_status_t psa_sign_hash_abort(
3724 psa_sign_hash_interruptible_operation_t *operation)
3725{
Paul Elliott59ad9452022-12-18 15:09:02 +00003726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3727
3728 status = psa_sign_hash_abort_internal(operation);
3729
3730 /* We clear the number of ops done here, so that it is not cleared when
3731 * the operation fails or succeeds, only on manual abort. */
3732 operation->num_ops = 0;
3733
Paul Elliottc9774412023-02-06 15:14:07 +00003734 /* Likewise, failure state. */
3735 operation->error_occurred = 0;
3736
Paul Elliott59ad9452022-12-18 15:09:02 +00003737 return status;
3738}
3739
3740static psa_status_t psa_verify_hash_abort_internal(
3741 psa_verify_hash_interruptible_operation_t *operation)
3742{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003743 if (operation->id == 0) {
3744 /* The object has (apparently) been initialized but it is not (yet)
3745 * in use. It's ok to call abort on such an object, and there's
3746 * nothing to do. */
3747 return PSA_SUCCESS;
3748 }
3749
Paul Elliott59ad9452022-12-18 15:09:02 +00003750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3751
3752 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003753
3754 operation->id = 0;
3755
Paul Elliotte6145dc2023-02-07 12:51:21 +00003756 /* Do not clear either the error_occurred or num_ops elements here as they
3757 * only want to be cleared by the application calling abort, not by abort
3758 * being called at completion of an operation. */
3759
Paul Elliott59ad9452022-12-18 15:09:02 +00003760 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003761}
3762
3763psa_status_t psa_verify_hash_start(
3764 psa_verify_hash_interruptible_operation_t *operation,
3765 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003766 const uint8_t *hash_external, size_t hash_length,
3767 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003768{
3769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3770 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3771 psa_key_slot_t *slot;
3772
David Horstmann0fea6a52024-03-11 13:41:05 +00003773 LOCAL_INPUT_DECLARE(hash_external, hash);
3774 LOCAL_INPUT_DECLARE(signature_external, signature);
3775
Paul Elliottc9774412023-02-06 15:14:07 +00003776 /* Check that start has not been previously called, or operation has not
3777 * previously errored. */
3778 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003779 return PSA_ERROR_BAD_STATE;
3780 }
3781
3782 status = psa_sign_verify_check_alg(0, alg);
3783 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003784 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003785 return status;
3786 }
3787
3788 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3789 PSA_KEY_USAGE_VERIFY_HASH,
3790 alg);
3791
3792 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003793 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003794 return status;
3795 }
3796
David Horstmann0fea6a52024-03-11 13:41:05 +00003797 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3798 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3799
Paul Elliott296ede92022-12-15 17:00:30 +00003800 /* Ensure ops count gets reset, in case of operation re-use. */
3801 operation->num_ops = 0;
3802
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003803 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003804 slot->key.data,
3805 slot->key.bytes,
3806 alg, hash, hash_length,
3807 signature, signature_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00003808#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann0fea6a52024-03-11 13:41:05 +00003809exit:
3810#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003811
3812 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003813 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003814 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003815 }
3816
Ryan Everett291267f2024-02-14 15:59:15 +00003817 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003818
Paul Elliottc9774412023-02-06 15:14:07 +00003819 if (unlock_status != PSA_SUCCESS) {
3820 operation->error_occurred = 1;
3821 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003822
David Horstmann0fea6a52024-03-11 13:41:05 +00003823 LOCAL_INPUT_FREE(hash_external, hash);
3824 LOCAL_INPUT_FREE(signature_external, signature);
3825
Paul Elliottc9774412023-02-06 15:14:07 +00003826 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003827}
3828
3829psa_status_t psa_verify_hash_complete(
3830 psa_verify_hash_interruptible_operation_t *operation)
3831{
3832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3833
Paul Elliottc9774412023-02-06 15:14:07 +00003834 /* Check that start has been called first, and that operation has not
3835 * previously errored. */
3836 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003837 status = PSA_ERROR_BAD_STATE;
3838 goto exit;
3839 }
3840
3841 status = psa_driver_wrapper_verify_hash_complete(operation);
3842
Paul Elliott296ede92022-12-15 17:00:30 +00003843 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003844 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003845 operation);
3846
Paul Elliottebe225c2023-02-10 14:32:53 +00003847exit:
3848
Paul Elliott9fe12f62022-11-30 19:16:02 +00003849 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003850 if (status != PSA_SUCCESS) {
3851 operation->error_occurred = 1;
3852 }
3853
Paul Elliott59ad9452022-12-18 15:09:02 +00003854 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003855 }
3856
3857 return status;
3858}
3859
3860psa_status_t psa_verify_hash_abort(
3861 psa_verify_hash_interruptible_operation_t *operation)
3862{
Paul Elliott59ad9452022-12-18 15:09:02 +00003863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003864
Paul Elliott59ad9452022-12-18 15:09:02 +00003865 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003866
Paul Elliott59ad9452022-12-18 15:09:02 +00003867 /* We clear the number of ops done here, so that it is not cleared when
3868 * the operation fails or succeeds, only on manual abort. */
3869 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003870
Paul Elliottc9774412023-02-06 15:14:07 +00003871 /* Likewise, failure state. */
3872 operation->error_occurred = 0;
3873
Paul Elliott59ad9452022-12-18 15:09:02 +00003874 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003875}
3876
Paul Elliott588f8ed2022-12-02 18:10:26 +00003877/****************************************************************/
3878/* Asymmetric interruptible cryptography internal */
3879/* implementations */
3880/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003881
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3883{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003884
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3886 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3887 defined(MBEDTLS_ECP_RESTARTABLE)
3888
3889 /* Internal implementation uses zero to indicate infinite number max ops,
3890 * therefore avoid this value, and set to minimum possible. */
3891 if (max_ops == 0) {
3892 max_ops = 1;
3893 }
3894
Paul Elliott588f8ed2022-12-02 18:10:26 +00003895 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003896#else
3897 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3899 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3900 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3901}
3902
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003904 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003905{
3906#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3907 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3908 defined(MBEDTLS_ECP_RESTARTABLE)
3909
Paul Elliott93d9ca82023-02-15 18:14:21 +00003910 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003911#else
3912 (void) operation;
3913 return 0;
3914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3915 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3916 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3917}
3918
3919uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003920 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921{
3922 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3923 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3924 defined(MBEDTLS_ECP_RESTARTABLE)
3925
Paul Elliott93d9ca82023-02-15 18:14:21 +00003926 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003927#else
3928 (void) operation;
3929 return 0;
3930#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3931 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3932 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3933}
3934
Gilles Peskine8a8aa592024-11-20 16:13:10 +01003935/* Detect supported interruptible sign/verify mechanisms precisely.
3936 * This is not strictly needed: we could accept everything, and let the
3937 * code fail later during complete() if the mechanism is unsupported
3938 * (e.g. attempting deterministic ECDSA when only the randomized variant
3939 * is available). But it's easier for applications and especially for our
3940 * test code to detect all not-supported errors during start().
3941 *
3942 * Note that this function ignores the hash component. The core code
3943 * is supposed to check the hash part by calling is_hash_supported().
3944 */
3945static inline int can_do_interruptible_sign_verify(psa_algorithm_t alg)
3946{
3947#if defined(MBEDTLS_ECP_RESTARTABLE)
3948#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3949 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
3950 return 1;
3951 }
3952#endif
3953#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
3954 if (PSA_ALG_IS_RANDOMIZED_ECDSA(alg)) {
3955 return 1;
3956 }
3957#endif
3958#endif /* defined(MBEDTLS_ECP_RESTARTABLE) */
3959 (void) alg;
3960 return 0;
3961}
3962
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963psa_status_t mbedtls_psa_sign_hash_start(
3964 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3965 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3966 size_t key_buffer_size, psa_algorithm_t alg,
3967 const uint8_t *hash, size_t hash_length)
3968{
3969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003970 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01003972 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003973 return PSA_ERROR_NOT_SUPPORTED;
3974 }
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01003975 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type);
3976 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
3977 return PSA_ERROR_INVALID_ARGUMENT;
3978 }
Paul Elliott068fe072023-01-16 13:59:15 +00003979
Gilles Peskine8a8aa592024-11-20 16:13:10 +01003980 if (!can_do_interruptible_sign_verify(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003981 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003982 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983
3984#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003985 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3986 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003987
Paul Elliotta1c94092023-02-15 16:38:04 +00003988 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3989
Paul Elliott93d9ca82023-02-15 18:14:21 +00003990 /* Ensure num_ops is zero'ed in case of context re-use. */
3991 operation->num_ops = 0;
3992
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003993 status = mbedtls_psa_ecp_load_representation(attributes->type,
3994 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003995 key_buffer,
3996 key_buffer_size,
3997 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998
Paul Elliott068fe072023-01-16 13:59:15 +00003999 if (status != PSA_SUCCESS) {
4000 return status;
4001 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004002
Paul Elliott1bc59df2023-02-05 13:41:57 +00004003 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00004004 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004005
Paul Elliott068fe072023-01-16 13:59:15 +00004006 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02004007 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00004008 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009
Paul Elliott0290a762023-02-09 14:30:24 +00004010 /* We only need to store the same length of hash as the private key size
4011 * here, it would be truncated by the internal implementation anyway. */
4012 required_hash_length = (hash_length < operation->coordinate_bytes ?
4013 hash_length : operation->coordinate_bytes);
4014
Paul Elliottba70ad42023-02-15 18:23:53 +00004015 if (required_hash_length > sizeof(operation->hash)) {
4016 /* Shouldn't happen, but better safe than sorry. */
4017 return PSA_ERROR_CORRUPTION_DETECTED;
4018 }
4019
Paul Elliott0290a762023-02-09 14:30:24 +00004020 memcpy(operation->hash, hash, required_hash_length);
4021 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004022
4023 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004024
4025#else
Paul Elliott068fe072023-01-16 13:59:15 +00004026 (void) operation;
4027 (void) key_buffer;
4028 (void) key_buffer_size;
4029 (void) alg;
4030 (void) hash;
4031 (void) hash_length;
4032 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00004033 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004034
Paul Elliott068fe072023-01-16 13:59:15 +00004035 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004036#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4037 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4038 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004039}
4040
4041psa_status_t mbedtls_psa_sign_hash_complete(
4042 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
4043 uint8_t *signature, size_t signature_size,
4044 size_t *signature_length)
4045{
Paul Elliott588f8ed2022-12-02 18:10:26 +00004046#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4047 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4048 defined(MBEDTLS_ECP_RESTARTABLE)
4049
Paul Elliotta1c94092023-02-15 16:38:04 +00004050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00004051 mbedtls_mpi r;
4052 mbedtls_mpi s;
4053
Paul Elliott46845252023-02-03 14:59:11 +00004054 mbedtls_mpi_init(&r);
4055 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004056
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004057 /* Ensure max_ops is set to the current value (or default). */
4058 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4059
Paul Elliotta1c94092023-02-15 16:38:04 +00004060 if (signature_size < 2 * operation->coordinate_bytes) {
4061 status = PSA_ERROR_BUFFER_TOO_SMALL;
4062 goto exit;
4063 }
4064
Paul Elliott588f8ed2022-12-02 18:10:26 +00004065 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00004066
Paul Elliott588f8ed2022-12-02 18:10:26 +00004067#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
4068 status = mbedtls_to_psa_error(
4069 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00004070 &r,
4071 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004072 &operation->ctx->d,
4073 operation->hash,
4074 operation->hash_length,
4075 operation->md_alg,
4076 mbedtls_psa_get_random,
4077 MBEDTLS_PSA_RANDOM_STATE,
4078 &operation->restart_ctx));
4079#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00004080 status = PSA_ERROR_NOT_SUPPORTED;
4081 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004082#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
4083 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00004084 status = mbedtls_to_psa_error(
4085 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00004086 &r,
4087 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004088 &operation->ctx->d,
4089 operation->hash,
4090 operation->hash_length,
4091 mbedtls_psa_get_random,
4092 MBEDTLS_PSA_RANDOM_STATE,
4093 mbedtls_psa_get_random,
4094 MBEDTLS_PSA_RANDOM_STATE,
4095 &operation->restart_ctx));
4096 }
4097
Paul Elliottf8e5b562023-02-19 18:43:45 +00004098 /* Hide the fact that the restart context only holds a delta of number of
4099 * ops done during the last operation, not an absolute value. */
4100 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4101
Paul Elliott724bd252023-02-08 12:35:08 +00004102 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00004103 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00004104 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004105 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004106 operation->coordinate_bytes)
4107 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00004108
4109 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00004110 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004111 }
4112
4113 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00004114 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00004115 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004116 operation->coordinate_bytes,
4117 operation->coordinate_bytes)
4118 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00004119
4120 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00004121 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004122 }
4123
Paul Elliott1bc59df2023-02-05 13:41:57 +00004124 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004125
Paul Elliott724bd252023-02-08 12:35:08 +00004126 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004127 }
Paul Elliott724bd252023-02-08 12:35:08 +00004128
4129exit:
4130
4131 mbedtls_mpi_free(&r);
4132 mbedtls_mpi_free(&s);
4133 return status;
4134
Paul Elliott588f8ed2022-12-02 18:10:26 +00004135 #else
4136
4137 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004138 (void) signature;
4139 (void) signature_size;
4140 (void) signature_length;
4141
4142 return PSA_ERROR_NOT_SUPPORTED;
4143
4144#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4145 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4146 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4147}
4148
4149psa_status_t mbedtls_psa_sign_hash_abort(
4150 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
4151{
4152
4153#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4154 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4155 defined(MBEDTLS_ECP_RESTARTABLE)
4156
4157 if (operation->ctx) {
4158 mbedtls_ecdsa_free(operation->ctx);
4159 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004160 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004161 }
4162
4163 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4164
Paul Elliott93d9ca82023-02-15 18:14:21 +00004165 operation->num_ops = 0;
4166
Paul Elliott588f8ed2022-12-02 18:10:26 +00004167 return PSA_SUCCESS;
4168
4169#else
4170
4171 (void) operation;
4172
4173 return PSA_ERROR_NOT_SUPPORTED;
4174
4175#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4176 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4177 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4178}
4179
4180psa_status_t mbedtls_psa_verify_hash_start(
4181 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
4182 const psa_key_attributes_t *attributes,
4183 const uint8_t *key_buffer, size_t key_buffer_size,
4184 psa_algorithm_t alg,
4185 const uint8_t *hash, size_t hash_length,
4186 const uint8_t *signature, size_t signature_length)
4187{
4188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00004189 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00004190 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004191
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004192 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00004193 return PSA_ERROR_NOT_SUPPORTED;
4194 }
Gilles Peskineabf9f1a2024-12-16 23:23:04 +01004195 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->type);
4196 if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
4197 return PSA_ERROR_INVALID_ARGUMENT;
4198 }
Paul Elliott068fe072023-01-16 13:59:15 +00004199
Gilles Peskine8a8aa592024-11-20 16:13:10 +01004200 if (!can_do_interruptible_sign_verify(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004201 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004202 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004203
4204#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004205 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4206 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004207
Paul Elliotta1c94092023-02-15 16:38:04 +00004208 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4209 mbedtls_mpi_init(&operation->r);
4210 mbedtls_mpi_init(&operation->s);
4211
Paul Elliott93d9ca82023-02-15 18:14:21 +00004212 /* Ensure num_ops is zero'ed in case of context re-use. */
4213 operation->num_ops = 0;
4214
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004215 status = mbedtls_psa_ecp_load_representation(attributes->type,
4216 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004217 key_buffer,
4218 key_buffer_size,
4219 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004220
Paul Elliott068fe072023-01-16 13:59:15 +00004221 if (status != PSA_SUCCESS) {
4222 return status;
4223 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004224
Paul Elliottc569fc22023-02-10 13:02:54 +00004225 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004226
Paul Elliott1bc59df2023-02-05 13:41:57 +00004227 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004228 return PSA_ERROR_INVALID_SIGNATURE;
4229 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004230
Paul Elliott068fe072023-01-16 13:59:15 +00004231 status = mbedtls_to_psa_error(
4232 mbedtls_mpi_read_binary(&operation->r,
4233 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004234 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004235
Paul Elliott068fe072023-01-16 13:59:15 +00004236 if (status != PSA_SUCCESS) {
4237 return status;
4238 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004239
Paul Elliott068fe072023-01-16 13:59:15 +00004240 status = mbedtls_to_psa_error(
4241 mbedtls_mpi_read_binary(&operation->s,
4242 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004243 coordinate_bytes,
4244 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004245
Paul Elliott068fe072023-01-16 13:59:15 +00004246 if (status != PSA_SUCCESS) {
4247 return status;
4248 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004249
Paul Elliott2c9843f2023-02-15 17:32:42 +00004250 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004251
Paul Elliott2c9843f2023-02-15 17:32:42 +00004252 if (status != PSA_SUCCESS) {
4253 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004254 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004255
Paul Elliott0290a762023-02-09 14:30:24 +00004256 /* We only need to store the same length of hash as the private key size
4257 * here, it would be truncated by the internal implementation anyway. */
4258 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4259 coordinate_bytes);
4260
Paul Elliottba70ad42023-02-15 18:23:53 +00004261 if (required_hash_length > sizeof(operation->hash)) {
4262 /* Shouldn't happen, but better safe than sorry. */
4263 return PSA_ERROR_CORRUPTION_DETECTED;
4264 }
4265
Paul Elliott0290a762023-02-09 14:30:24 +00004266 memcpy(operation->hash, hash, required_hash_length);
4267 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004268
4269 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004270#else
Paul Elliott068fe072023-01-16 13:59:15 +00004271 (void) operation;
4272 (void) key_buffer;
4273 (void) key_buffer_size;
4274 (void) alg;
4275 (void) hash;
4276 (void) hash_length;
4277 (void) signature;
4278 (void) signature_length;
4279 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004280 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004281 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004282
Paul Elliott068fe072023-01-16 13:59:15 +00004283 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004284#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4285 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4286 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004287}
4288
4289psa_status_t mbedtls_psa_verify_hash_complete(
4290 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4291{
4292
4293#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4294 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4295 defined(MBEDTLS_ECP_RESTARTABLE)
4296
Paul Elliottf8e5b562023-02-19 18:43:45 +00004297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4298
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004299 /* Ensure max_ops is set to the current value (or default). */
4300 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4301
Paul Elliottf8e5b562023-02-19 18:43:45 +00004302 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004303 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4304 operation->hash,
4305 operation->hash_length,
4306 &operation->ctx->Q,
4307 &operation->r,
4308 &operation->s,
4309 &operation->restart_ctx));
4310
Paul Elliottf8e5b562023-02-19 18:43:45 +00004311 /* Hide the fact that the restart context only holds a delta of number of
4312 * ops done during the last operation, not an absolute value. */
4313 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4314
4315 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004316#else
4317 (void) operation;
4318
4319 return PSA_ERROR_NOT_SUPPORTED;
4320
4321#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4322 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4323 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4324}
4325
4326psa_status_t mbedtls_psa_verify_hash_abort(
4327 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4328{
4329
4330#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4331 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4332 defined(MBEDTLS_ECP_RESTARTABLE)
4333
4334 if (operation->ctx) {
4335 mbedtls_ecdsa_free(operation->ctx);
4336 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004337 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004338 }
4339
4340 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4341
Paul Elliott93d9ca82023-02-15 18:14:21 +00004342 operation->num_ops = 0;
4343
Paul Elliott588f8ed2022-12-02 18:10:26 +00004344 mbedtls_mpi_free(&operation->r);
4345 mbedtls_mpi_free(&operation->s);
4346
4347 return PSA_SUCCESS;
4348
4349#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}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004358
David Horstmann6e99bb22024-02-06 15:27:49 +00004359static psa_status_t psa_generate_random_internal(uint8_t *output,
4360 size_t output_size)
4361{
4362 GUARD_MODULE_INITIALIZED;
4363
David Horstmann6e99bb22024-02-06 15:27:49 +00004364#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4365
David Horstmanndb909142024-03-12 16:07:08 +00004366 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004367 size_t output_length = 0;
4368 status = mbedtls_psa_external_get_random(&global_data.rng,
4369 output, output_size,
4370 &output_length);
4371 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004372 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004373 }
4374 /* Breaking up a request into smaller chunks is currently not supported
4375 * for the external RNG interface. */
4376 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004377 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004378 }
David Horstmanndb909142024-03-12 16:07:08 +00004379 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004380
4381#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4382
4383 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004384 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004385 size_t request_size =
4386 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4387 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4388 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004389#if defined(MBEDTLS_CTR_DRBG_C)
4390 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4391#elif defined(MBEDTLS_HMAC_DRBG_C)
4392 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4393#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004394 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004395 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004396 }
4397 output_size -= request_size;
4398 output += request_size;
4399 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004400 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004401#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004402}
4403
4404
mohammad1603503973b2018-03-12 15:59:30 +02004405/****************************************************************/
4406/* Symmetric cryptography */
4407/****************************************************************/
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4410 mbedtls_svc_key_id_t key,
4411 psa_algorithm_t alg,
4412 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004413{
4414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4415 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004416 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4418 PSA_KEY_USAGE_ENCRYPT :
4419 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004420
4421 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004423 status = PSA_ERROR_BAD_STATE;
4424 goto exit;
4425 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004428 status = PSA_ERROR_INVALID_ARGUMENT;
4429 goto exit;
4430 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4433 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004436
Ronald Cron49fafa92021-03-10 08:34:23 +01004437 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004438 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004439 * so we only set it (in the driver wrapper) after resources have been
4440 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004441 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004443 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004445 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 }
4447 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004448
4449 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (cipher_operation == MBEDTLS_ENCRYPT) {
4451 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004452 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 slot->key.data,
4454 slot->key.bytes,
4455 alg);
4456 } else {
4457 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004458 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 slot->key.data,
4460 slot->key.bytes,
4461 alg);
4462 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004463
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 if (status != PSA_SUCCESS) {
4466 psa_cipher_abort(operation);
4467 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004468
Ryan Everettc0053cc2024-02-14 16:27:13 +00004469 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004472}
4473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4475 mbedtls_svc_key_id_t key,
4476 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004477{
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004479}
4480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4482 mbedtls_svc_key_id_t key,
4483 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004484{
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004486}
4487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004489 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 size_t iv_size,
4491 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004492{
Ronald Cron6d051732020-10-01 14:10:20 +02004493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004494 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004495
Gabor Mezei358eb212024-02-07 18:10:13 +01004496 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004499 status = PSA_ERROR_BAD_STATE;
4500 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004501 }
4502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004504 status = PSA_ERROR_BAD_STATE;
4505 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004506 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004507
Ronald Cron2fb90522021-07-05 12:31:44 +02004508 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004510 status = PSA_ERROR_BUFFER_TOO_SMALL;
4511 goto exit;
4512 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004515 status = PSA_ERROR_GENERIC_ERROR;
4516 goto exit;
4517 }
4518
Gabor Mezei358eb212024-02-07 18:10:13 +01004519 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4520
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004521 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 }
Ronald Cron5618a392021-03-26 09:52:26 +01004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004527 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004528
4529exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004531 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004532 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004534 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004536 if (iv != NULL) {
4537 mbedtls_platform_zeroize(iv, default_iv_length);
4538 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004539 }
Ronald Cron6d051732020-10-01 14:10:20 +02004540
Gabor Mezei358eb212024-02-07 18:10:13 +01004541 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004543}
4544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004546 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004548{
Ronald Cron6d051732020-10-01 14:10:20 +02004549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004550
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004551 LOCAL_INPUT_DECLARE(iv_external, iv);
4552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004554 status = PSA_ERROR_BAD_STATE;
4555 goto exit;
4556 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004559 status = PSA_ERROR_BAD_STATE;
4560 goto exit;
4561 }
Ronald Crona0d68172021-03-26 10:15:08 +01004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004564 status = PSA_ERROR_INVALID_ARGUMENT;
4565 goto exit;
4566 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004567
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004568 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 status = psa_driver_wrapper_cipher_set_iv(operation,
4571 iv,
4572 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004573
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004576 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 } else {
4578 psa_cipher_abort(operation);
4579 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004580
4581 LOCAL_INPUT_FREE(iv_external, iv);
4582
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_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004587 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004589 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 size_t output_size,
4591 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004592{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004594
Gabor Mezei212eb082024-01-24 16:56:00 +01004595 LOCAL_INPUT_DECLARE(input_external, input);
4596 LOCAL_OUTPUT_DECLARE(output_external, output);
4597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004599 status = PSA_ERROR_BAD_STATE;
4600 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004601 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004604 status = PSA_ERROR_BAD_STATE;
4605 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004606 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004607
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004608 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004609 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 status = psa_driver_wrapper_cipher_update(operation,
4612 input,
4613 input_length,
4614 output,
4615 output_size,
4616 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004617
4618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if (status != PSA_SUCCESS) {
4620 psa_cipher_abort(operation);
4621 }
Ronald Cron6d051732020-10-01 14:10:20 +02004622
Gabor Mezei212eb082024-01-24 16:56:00 +01004623 LOCAL_INPUT_FREE(input_external, input);
4624 LOCAL_OUTPUT_FREE(output_external, output);
4625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004627}
4628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
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{
David Saadab4ecc272019-02-14 13:48:10 +02004634 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004635
Gabor Mezei212eb082024-01-24 16:56:00 +01004636 LOCAL_OUTPUT_DECLARE(output_external, output);
4637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004639 status = PSA_ERROR_BAD_STATE;
4640 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004641 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004644 status = PSA_ERROR_BAD_STATE;
4645 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004646 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004647
Gabor Mezei0b041162024-02-29 10:08:16 +00004648 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 status = psa_driver_wrapper_cipher_finish(operation,
4651 output,
4652 output_size,
4653 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004654
4655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if (status == PSA_SUCCESS) {
Gabor Mezei212eb082024-01-24 16:56:00 +01004657 status = psa_cipher_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004659 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02004661 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004662
4663 LOCAL_OUTPUT_FREE(output_external, output);
4664
4665 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004666}
4667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004669{
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004671 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004672 * in use. It's ok to call abort on such an object, and there's
4673 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004675 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004678
Ronald Cron49fafa92021-03-10 08:34:23 +01004679 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004680 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004681 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004684}
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4687 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004688 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004690 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 size_t output_size,
4692 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004693{
4694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004696 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004697 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4698 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004699
David Horstmann62a56d92023-12-14 18:12:47 +00004700 LOCAL_INPUT_DECLARE(input_external, input);
4701 LOCAL_OUTPUT_DECLARE(output_external, output);
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004704 status = PSA_ERROR_INVALID_ARGUMENT;
4705 goto exit;
4706 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4709 PSA_KEY_USAGE_ENCRYPT,
4710 alg);
4711 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4716 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004717 status = PSA_ERROR_GENERIC_ERROR;
4718 goto exit;
4719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (default_iv_length > 0) {
4722 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004723 status = PSA_ERROR_BUFFER_TOO_SMALL;
4724 goto exit;
4725 }
4726
David Horstmann6e99bb22024-02-06 15:27:49 +00004727 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004731 }
4732
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004733 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4734 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4735
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004736 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004737 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004738 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004739 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004741
4742exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004743 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004745 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004746 }
Ronald Cron23919522021-07-08 19:00:07 +02004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 if (status == PSA_SUCCESS) {
4749 if (default_iv_length > 0) {
4750 memcpy(output, local_iv, default_iv_length);
4751 }
4752 *output_length += default_iv_length;
4753 } else {
4754 *output_length = 0;
4755 }
4756
David Horstmann62a56d92023-12-14 18:12:47 +00004757 LOCAL_INPUT_FREE(input_external, input);
4758 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004761}
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4764 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004765 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004767 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 size_t output_size,
4769 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004770{
4771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004772 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004773 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004774
Gabor Mezei212eb082024-01-24 16:56:00 +01004775 LOCAL_INPUT_DECLARE(input_external, input);
4776 LOCAL_OUTPUT_DECLARE(output_external, output);
4777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004779 status = PSA_ERROR_INVALID_ARGUMENT;
4780 goto exit;
4781 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4784 PSA_KEY_USAGE_DECRYPT,
4785 alg);
4786 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004789
Gilles Peskineb47c3b32024-06-26 13:16:33 +02004790 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004791 status = PSA_ERROR_INVALID_ARGUMENT;
4792 goto exit;
4793 }
4794
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004795 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4796 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4797
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004798 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004799 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004800 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004802
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004803exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004804 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004806 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004810 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 }
Ronald Cron23919522021-07-08 19:00:07 +02004812
Gabor Mezei212eb082024-01-24 16:56:00 +01004813 LOCAL_INPUT_FREE(input_external, input);
4814 LOCAL_OUTPUT_FREE(output_external, output);
4815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004817}
4818
4819
mohammad16035955c982018-04-26 00:53:03 +03004820/****************************************************************/
4821/* AEAD */
4822/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004823
Paul Elliottbaff51c2021-09-28 17:44:45 +01004824/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004825static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004826{
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004828}
4829
4830/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4832 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004833{
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004837#if defined(PSA_WANT_ALG_GCM)
4838 case PSA_ALG_GCM:
4839 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 * arbitrarily large nonces. Please note that we do not generally
4841 * recommend the usage of nonces of greater length than
4842 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4843 * size, which can then lead to collisions if you encrypt a very
4844 * large number of messages.*/
4845 if (nonce_length != 0) {
4846 return PSA_SUCCESS;
4847 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004848 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004849#endif /* PSA_WANT_ALG_GCM */
4850#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004851 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (nonce_length >= 7 && nonce_length <= 13) {
4853 return PSA_SUCCESS;
4854 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004855 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004856#endif /* PSA_WANT_ALG_CCM */
4857#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004858 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (nonce_length == 12) {
4860 return PSA_SUCCESS;
4861 } else if (nonce_length == 8) {
4862 return PSA_ERROR_NOT_SUPPORTED;
4863 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004864 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004865#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004866 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004867 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004869 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004872}
4873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004875{
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4877 return PSA_ERROR_INVALID_ARGUMENT;
4878 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004881}
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4884 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004885 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004887 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004889 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004891 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 size_t ciphertext_size,
4893 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004894{
Ronald Cron215633c2021-03-16 17:15:37 +01004895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4896 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004897
David Horstmann9d09a022023-11-28 19:25:00 +00004898 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4899 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4900 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4901 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4902
mohammad1603f08a5502018-06-03 15:05:47 +03004903 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 status = psa_aead_check_algorithm(alg);
4906 if (status != PSA_SUCCESS) {
4907 return status;
4908 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004909
Ronald Cron9a986162021-03-26 12:40:07 +01004910 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4912 if (status != PSA_SUCCESS) {
4913 return status;
4914 }
mohammad16035955c982018-04-26 00:53:03 +03004915
David Horstmann9d09a022023-11-28 19:25:00 +00004916 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4917 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4918 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4919 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 status = psa_aead_check_nonce_length(alg, nonce_length);
4922 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004923 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004925
Ronald Cronde822812021-03-17 16:08:20 +01004926 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004927 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004928 alg,
4929 nonce, nonce_length,
4930 additional_data, additional_data_length,
4931 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4935 memset(ciphertext, 0, ciphertext_size);
4936 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004937
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004938exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004939 LOCAL_INPUT_FREE(nonce_external, nonce);
4940 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4941 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4942 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4943
Ryan Everetta103ec92024-01-31 13:59:57 +00004944 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004947}
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4950 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00004951 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004953 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004955 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004957 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 size_t plaintext_size,
4959 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004960{
Ronald Cron215633c2021-03-16 17:15:37 +01004961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4962 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004963
David Horstmann7f2e0402023-11-28 19:43:53 +00004964 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4965 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4966 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4967 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4968
Gilles Peskineee652a32018-06-01 19:23:52 +02004969 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 status = psa_aead_check_algorithm(alg);
4972 if (status != PSA_SUCCESS) {
4973 return status;
4974 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004975
Ronald Cron9a986162021-03-26 12:40:07 +01004976 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4978 if (status != PSA_SUCCESS) {
4979 return status;
4980 }
mohammad16035955c982018-04-26 00:53:03 +03004981
David Horstmann7f2e0402023-11-28 19:43:53 +00004982 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4983 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4984 additional_data);
4985 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4986 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 status = psa_aead_check_nonce_length(alg, nonce_length);
4989 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004990 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004992
Ronald Cronde822812021-03-17 16:08:20 +01004993 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004994 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004995 alg,
4996 nonce, nonce_length,
4997 additional_data, additional_data_length,
4998 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02005000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (status != PSA_SUCCESS && plaintext_size != 0) {
5002 memset(plaintext, 0, plaintext_size);
5003 }
mohammad160360a64d02018-06-03 17:20:42 +03005004
Paul Elliottbb0f9e12021-09-28 11:14:27 +01005005exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00005006 LOCAL_INPUT_FREE(nonce_external, nonce);
5007 LOCAL_INPUT_FREE(additional_data_external, additional_data);
5008 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
5009 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5010
Ryan Everetta103ec92024-01-31 13:59:57 +00005011 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 return status;
mohammad16035955c982018-04-26 00:53:03 +03005014}
5015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
5017{
5018 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02005021#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005023 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
5025 return PSA_ERROR_INVALID_ARGUMENT;
5026 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005027 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005028#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005029
Przemek Stekiel86679c72022-10-06 17:06:56 +02005030#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005032 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
5034 return PSA_ERROR_INVALID_ARGUMENT;
5035 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005036 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005037#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005038
Przemek Stekiel86679c72022-10-06 17:06:56 +02005039#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01005041 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (tag_len != 16) {
5043 return PSA_ERROR_INVALID_ARGUMENT;
5044 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005045 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02005046#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01005047
5048 default:
5049 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005051 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005053}
5054
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005055/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005056static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
5057 int is_encrypt,
5058 mbedtls_svc_key_id_t key,
5059 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01005060{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5062 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
5063 psa_key_slot_t *slot = NULL;
5064 psa_key_usage_t key_usage = 0;
5065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 status = psa_aead_check_algorithm(alg);
5067 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 }
Paul Elliottc2b71442021-06-24 18:17:52 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005072 status = PSA_ERROR_BAD_STATE;
5073 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005074 }
5075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (operation->nonce_set || operation->lengths_set ||
5077 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005078 status = PSA_ERROR_BAD_STATE;
5079 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01005080 }
5081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005083 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005085 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
5089 alg);
5090 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02005097
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (is_encrypt) {
5099 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005100 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 slot->key.data,
5102 slot->key.bytes,
5103 alg);
5104 } else {
5105 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005106 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 slot->key.data,
5108 slot->key.bytes,
5109 alg);
5110 }
5111 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005114
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005115 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005116
5117exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00005118 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005121 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01005123 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 } else {
5125 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005126 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01005127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01005129}
5130
Paul Elliott302ff6b2021-04-20 18:10:30 +01005131/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005132psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
5133 mbedtls_svc_key_id_t key,
5134 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005135{
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137}
5138
5139/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005140psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
5141 mbedtls_svc_key_id_t key,
5142 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143{
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145}
5146
David Horstmannfed23772023-12-19 17:49:20 +00005147static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
5148 const uint8_t *nonce,
5149 size_t nonce_length)
5150{
5151 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5152
5153 if (operation->id == 0) {
5154 status = PSA_ERROR_BAD_STATE;
5155 goto exit;
5156 }
5157
5158 if (operation->nonce_set) {
5159 status = PSA_ERROR_BAD_STATE;
5160 goto exit;
5161 }
5162
5163 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
5164 if (status != PSA_SUCCESS) {
5165 status = PSA_ERROR_INVALID_ARGUMENT;
5166 goto exit;
5167 }
5168
5169 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
5170 nonce_length);
5171
5172exit:
5173 if (status == PSA_SUCCESS) {
5174 operation->nonce_set = 1;
5175 } else {
5176 psa_aead_abort(operation);
5177 }
5178
5179 return status;
5180}
5181
Paul Elliott302ff6b2021-04-20 18:10:30 +01005182/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01005183psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00005184 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 size_t nonce_size,
5186 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005187{
5188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01005189 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07005190 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005191
David Horstmannd3cad8b2023-12-11 14:46:04 +00005192 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
5193 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
5194
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005195 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005198 status = PSA_ERROR_BAD_STATE;
5199 goto exit;
5200 }
5201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005203 status = PSA_ERROR_BAD_STATE;
5204 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005205 }
5206
Paul Elliotte193ea82021-10-01 13:00:16 +01005207 /* For CCM, this size may not be correct according to the PSA
5208 * specification. The PSA Crypto 1.0.1 specification states:
5209 *
5210 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5211 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5212 *
5213 * However this restriction that L has to be the smallest integer is not
5214 * applied in practice, and it is not implementable here since the
5215 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5217 operation->alg);
5218 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005219 status = PSA_ERROR_BUFFER_TOO_SMALL;
5220 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005221 }
5222
David Horstmann6e99bb22024-02-06 15:27:49 +00005223 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005227
David Horstmannfed23772023-12-19 17:49:20 +00005228 status = psa_aead_set_nonce_internal(operation, local_nonce,
5229 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005230
Paul Elliott39dc6b82021-05-11 19:16:09 +01005231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (status == PSA_SUCCESS) {
5233 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005234 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 } else {
5236 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005237 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005238
David Horstmannd3cad8b2023-12-11 14:46:04 +00005239 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005242}
5243
5244/* Set the nonce for a multipart authenticated encryption or decryption
5245 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005246psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005247 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005249{
David Horstmannfed23772023-12-19 17:49:20 +00005250 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005251
David Horstmann8f0ef512023-12-11 15:17:11 +00005252 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5253 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005254
David Horstmannfed23772023-12-19 17:49:20 +00005255 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005256
David Horstmann18dc0322023-12-20 16:16:43 +00005257/* Exit label is only needed for buffer copying, prevent unused warnings. */
David Horstmann4a48bec2024-03-13 15:42:31 +00005258#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005259exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005260#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005261
David Horstmann8f0ef512023-12-11 15:17:11 +00005262 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005265}
5266
5267/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005268psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5269 size_t ad_length,
5270 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005271{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005275 status = PSA_ERROR_BAD_STATE;
5276 goto exit;
5277 }
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation->lengths_set || operation->ad_started ||
5280 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005281 status = PSA_ERROR_BAD_STATE;
5282 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005286#if defined(PSA_WANT_ALG_GCM)
5287 case PSA_ALG_GCM:
5288 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 * bits. Without the guard this code will generate warnings on 32bit
5290 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005291#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (((uint64_t) ad_length) >> 61 != 0 ||
5293 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005294 status = PSA_ERROR_INVALID_ARGUMENT;
5295 goto exit;
5296 }
Paul Elliott325d3742021-09-27 17:56:28 +01005297#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005298 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005299#endif /* PSA_WANT_ALG_GCM */
5300#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005301 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005303 status = PSA_ERROR_INVALID_ARGUMENT;
5304 goto exit;
5305 }
5306 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005307#endif /* PSA_WANT_ALG_CCM */
5308#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005309 case PSA_ALG_CHACHA20_POLY1305:
5310 /* No length restrictions for ChaChaPoly. */
5311 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005312#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005313 default:
5314 break;
5315 }
Paul Elliott325d3742021-09-27 17:56:28 +01005316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5318 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005319
Paul Elliott39dc6b82021-05-11 19:16:09 +01005320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005322 operation->ad_remaining = ad_length;
5323 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005324 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 } else {
5326 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005327 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005330}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005331
5332/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005333psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005334 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005336{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5338
David Horstmann25dac6e2023-12-11 15:23:13 +00005339 LOCAL_INPUT_DECLARE(input_external, input);
5340 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5341
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005343 status = PSA_ERROR_BAD_STATE;
5344 goto exit;
5345 }
5346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005348 status = PSA_ERROR_BAD_STATE;
5349 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005350 }
5351
Paul Elliott304766f2024-04-26 18:30:11 +01005352 /* No input to add (zero length), nothing to do. */
5353 if (input_length == 0) {
5354 status = PSA_SUCCESS;
5355 goto exit;
5356 }
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 if (operation->lengths_set) {
5359 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005360 status = PSA_ERROR_INVALID_ARGUMENT;
5361 goto exit;
5362 }
5363
5364 operation->ad_remaining -= input_length;
5365 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005366#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005368 status = PSA_ERROR_BAD_STATE;
5369 goto exit;
5370 }
5371#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 status = psa_driver_wrapper_aead_update_ad(operation, input,
5374 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005375
Paul Elliott39dc6b82021-05-11 19:16:09 +01005376exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005378 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 } else {
5380 psa_aead_abort(operation);
5381 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005382
David Horstmann25dac6e2023-12-11 15:23:13 +00005383 LOCAL_INPUT_FREE(input_external, input);
5384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005386}
5387
5388/* Encrypt or decrypt a message fragment in an active multipart AEAD
5389 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005390psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005391 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005393 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 size_t output_size,
5395 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005396{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005398
David Horstmann2914fac2023-12-11 15:28:37 +00005399
5400 LOCAL_INPUT_DECLARE(input_external, input);
5401 LOCAL_OUTPUT_DECLARE(output_external, output);
5402
5403 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5404 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5405
Paul Elliott302ff6b2021-04-20 18:10:30 +01005406 *output_length = 0;
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) {
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
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005419 /* Additional data length was supplied, but not all the additional
5420 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005422 status = PSA_ERROR_INVALID_ARGUMENT;
5423 goto exit;
5424 }
5425
5426 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005428 status = PSA_ERROR_INVALID_ARGUMENT;
5429 goto exit;
5430 }
5431
5432 operation->body_remaining -= input_length;
5433 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005434#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005436 status = PSA_ERROR_BAD_STATE;
5437 goto exit;
5438 }
5439#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5442 output, output_size,
5443 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005444
Paul Elliott39dc6b82021-05-11 19:16:09 +01005445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005447 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 } else {
5449 psa_aead_abort(operation);
5450 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005451
David Horstmann2914fac2023-12-11 15:28:37 +00005452 LOCAL_INPUT_FREE(input_external, input);
5453 LOCAL_OUTPUT_FREE(output_external, output);
5454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005456}
5457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005459{
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (operation->id == 0 || !operation->nonce_set) {
5461 return PSA_ERROR_BAD_STATE;
5462 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5465 operation->body_remaining != 0)) {
5466 return PSA_ERROR_INVALID_ARGUMENT;
5467 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005470}
5471
Paul Elliott302ff6b2021-04-20 18:10:30 +01005472/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005474 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 size_t ciphertext_size,
5476 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005477 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 size_t tag_size,
5479 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005480{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5482
David Horstmann6db0e732023-12-11 15:35:59 +00005483 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5484 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5485
5486 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5487 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5488
Paul Elliott302ff6b2021-04-20 18:10:30 +01005489 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005490 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 status = psa_aead_final_checks(operation);
5493 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005496
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005498 status = PSA_ERROR_BAD_STATE;
5499 goto exit;
5500 }
5501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5503 ciphertext_size,
5504 ciphertext_length,
5505 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005506
Paul Elliott39dc6b82021-05-11 19:16:09 +01005507exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005508
5509
Paul Elliottf47b0952021-05-21 18:02:33 +01005510 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005511 * the zero tag size, make sure the tag is set to something implausible.
5512 * Even if the operation succeeds, make sure we clear the rest of the
5513 * buffer to prevent potential leakage of anything previously placed in
5514 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005515 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005518
David Horstmann6db0e732023-12-11 15:35:59 +00005519 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5520 LOCAL_OUTPUT_FREE(tag_external, tag);
5521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005523}
5524
5525/* Finish authenticating and decrypting a message in a multipart AEAD
5526 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005527psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005528 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 size_t plaintext_size,
5530 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005531 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005533{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5535
David Horstmanne000a0a2023-12-11 16:37:04 +00005536 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5537 LOCAL_INPUT_DECLARE(tag_external, tag);
5538
5539 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5540 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5541
Paul Elliott302ff6b2021-04-20 18:10:30 +01005542 *plaintext_length = 0;
5543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 status = psa_aead_final_checks(operation);
5545 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005546 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005550 status = PSA_ERROR_BAD_STATE;
5551 goto exit;
5552 }
5553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5555 plaintext_size,
5556 plaintext_length,
5557 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005558
5559exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005561
David Horstmanne000a0a2023-12-11 16:37:04 +00005562 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5563 LOCAL_INPUT_FREE(tag_external, tag);
5564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005566}
5567
5568/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005569psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005570{
5571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005574 /* The object has (apparently) been initialized but it is not (yet)
5575 * in use. It's ok to call abort on such an object, and there's
5576 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005578 }
5579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005585}
5586
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005588/* Generators */
5589/****************************************************************/
5590
Przemek Stekiel69c46792022-06-10 12:59:51 +02005591#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005592 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005593 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305594 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305595 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005596#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005597#endif /* At least one builtin KDF */
5598
Przemek Stekiel69c46792022-06-10 12:59:51 +02005599#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005600 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5601 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5602static psa_status_t psa_key_derivation_start_hmac(
5603 psa_mac_operation_t *operation,
5604 psa_algorithm_t hash_alg,
5605 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005607{
5608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5611 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5612 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005613
Steven Cooreman72f736a2021-05-07 14:14:37 +02005614 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 status = psa_driver_wrapper_mac_sign_setup(operation,
5618 &attributes,
5619 hmac_key, hmac_key_length,
5620 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 psa_reset_key_attributes(&attributes);
5623 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005624}
5625#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005626
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005627#define HKDF_STATE_INIT 0 /* no input yet */
5628#define HKDF_STATE_STARTED 1 /* got salt */
5629#define HKDF_STATE_KEYED 2 /* got key */
5630#define HKDF_STATE_OUTPUT 3 /* output started */
5631
Gilles Peskinecbe66502019-05-16 16:59:18 +02005632static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005634{
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5636 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5637 } else {
5638 return operation->alg;
5639 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005640}
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005643{
5644 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5646 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005647 /* The object has (apparently) been initialized but it is not
5648 * in use. It's ok to call abort on such an object, and there's
5649 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005651#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5653 mbedtls_free(operation->ctx.hkdf.info);
5654 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5655 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005656#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005657#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5658 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5660 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5661 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5662 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005663 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005665 }
5666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005668 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005670 }
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005673 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005675 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005676#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005678 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005680 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005681#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005682 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005683
5684 /* We leave the fields Ai and output_block to be erased safely by the
5685 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005687#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5688 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005689#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5691 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5692 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5693 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005694#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305695#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305696 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305697 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005698 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305699 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305700 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305701
5702 status = PSA_SUCCESS;
5703 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305704#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005705 {
5706 status = PSA_ERROR_BAD_STATE;
5707 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 mbedtls_platform_zeroize(operation, sizeof(*operation));
5709 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005710}
5711
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005714{
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005718 }
5719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005722}
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5725 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005726{
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (operation->alg == 0) {
5728 return PSA_ERROR_BAD_STATE;
5729 }
5730 if (capacity > operation->capacity) {
5731 return PSA_ERROR_INVALID_ARGUMENT;
5732 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005735}
5736
Przemek Stekiel69c46792022-06-10 12:59:51 +02005737#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005738/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005739static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5740 psa_algorithm_t kdf_alg,
5741 uint8_t *output,
5742 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005743{
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5745 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005746 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005747 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005748#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005750#else
5751 const uint8_t last_block = 0xff;
5752#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 if (hkdf->state < HKDF_STATE_KEYED ||
5755 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005756#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005758#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 )) {
5760 return PSA_ERROR_BAD_STATE;
5761 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 hkdf->state = HKDF_STATE_OUTPUT;
5763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005765 /* Copy what remains of the current block */
5766 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005768 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 }
5770 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005771 output += n;
5772 output_length -= n;
5773 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005775 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005777 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005778 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005779 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005780 * object was corrupted or if this function is called directly
5781 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 if (hkdf->block_number == last_block) {
5783 return PSA_ERROR_BAD_STATE;
5784 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005785
5786 /* We need a new block */
5787 ++hkdf->block_number;
5788 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5791 hash_alg,
5792 hkdf->prk,
5793 hash_length);
5794 if (status != PSA_SUCCESS) {
5795 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005796 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005797
5798 if (hkdf->block_number != 1) {
5799 status = psa_mac_update(&hkdf->hmac,
5800 hkdf->output_block,
5801 hash_length);
5802 if (status != PSA_SUCCESS) {
5803 return status;
5804 }
5805 }
5806 status = psa_mac_update(&hkdf->hmac,
5807 hkdf->info,
5808 hkdf->info_length);
5809 if (status != PSA_SUCCESS) {
5810 return status;
5811 }
5812 status = psa_mac_update(&hkdf->hmac,
5813 &hkdf->block_number, 1);
5814 if (status != PSA_SUCCESS) {
5815 return status;
5816 }
5817 status = psa_mac_sign_finish(&hkdf->hmac,
5818 hkdf->output_block,
5819 sizeof(hkdf->output_block),
5820 &hmac_output_length);
5821 if (status != PSA_SUCCESS) {
5822 return status;
5823 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005824 }
5825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005827}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005828#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005829
John Durkop07cc04a2020-11-16 22:08:34 -08005830#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5831 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005832static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5833 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005835{
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5837 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005838 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5839 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005840 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005841
Janos Follath7742fee2019-06-17 12:58:10 +01005842 /* We can't be wanting more output after block 0xff, otherwise
5843 * the capacity check in psa_key_derivation_output_bytes() would have
5844 * prevented this call. It could happen only if the operation
5845 * object was corrupted or if this function is called directly
5846 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 if (tls12_prf->block_number == 0xff) {
5848 return PSA_ERROR_CORRUPTION_DETECTED;
5849 }
Janos Follath7742fee2019-06-17 12:58:10 +01005850
5851 /* We need a new block */
5852 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005853 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005854
5855 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5856 *
5857 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5858 *
5859 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5860 * HMAC_hash(secret, A(2) + seed) +
5861 * HMAC_hash(secret, A(3) + seed) + ...
5862 *
5863 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005864 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005865 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005866 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005867 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005868 * is currently extracted as `output_block` and where i is
5869 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005870 */
5871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 status = psa_key_derivation_start_hmac(&hmac,
5873 hash_alg,
5874 tls12_prf->secret,
5875 tls12_prf->secret_length);
5876 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005877 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005879
5880 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005882 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5883 * the variable seed and in this instance means it in the context of the
5884 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 status = psa_mac_update(&hmac,
5886 tls12_prf->label,
5887 tls12_prf->label_length);
5888 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005889 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 }
5891 status = psa_mac_update(&hmac,
5892 tls12_prf->seed,
5893 tls12_prf->seed_length);
5894 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005895 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 }
5897 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005898 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5900 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005901 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005903 }
5904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 status = psa_mac_sign_finish(&hmac,
5906 tls12_prf->Ai, hash_length,
5907 &hmac_output_length);
5908 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005909 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 }
5911 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005912 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005914
5915 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 status = psa_key_derivation_start_hmac(&hmac,
5917 hash_alg,
5918 tls12_prf->secret,
5919 tls12_prf->secret_length);
5920 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005921 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 }
5923 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5924 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005925 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 }
5927 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5928 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005929 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 }
5931 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5932 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005933 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 }
5935 status = psa_mac_sign_finish(&hmac,
5936 tls12_prf->output_block, hash_length,
5937 &hmac_output_length);
5938 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005939 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005941
Janos Follath7742fee2019-06-17 12:58:10 +01005942
5943cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 cleanup_status = psa_mac_abort(&hmac);
5945 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005946 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005950}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005951
Janos Follath844eb0e2019-06-19 12:10:49 +01005952static psa_status_t psa_key_derivation_tls12_prf_read(
5953 psa_tls12_prf_key_derivation_t *tls12_prf,
5954 psa_algorithm_t alg,
5955 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005957{
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5959 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005960 psa_status_t status;
5961 uint8_t offset, length;
5962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005964 case PSA_TLS12_PRF_STATE_LABEL_SET:
5965 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5966 break;
5967 case PSA_TLS12_PRF_STATE_OUTPUT:
5968 break;
5969 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005971 }
5972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005974 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (tls12_prf->left_in_block == 0) {
5976 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5977 alg);
5978 if (status != PSA_SUCCESS) {
5979 return status;
5980 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005981
5982 continue;
5983 }
5984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005986 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005988 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005990
5991 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005993 output += length;
5994 output_length -= length;
5995 tls12_prf->left_in_block -= length;
5996 }
5997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005999}
John Durkop07cc04a2020-11-16 22:08:34 -08006000#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6001 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02006002
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006003#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6004static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
6005 psa_tls12_ecjpake_to_pms_t *ecjpake,
6006 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006008{
6009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006010 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (output_length != 32) {
6013 return PSA_ERROR_INVALID_ARGUMENT;
6014 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
6017 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
6018 &output_size);
6019 if (status != PSA_SUCCESS) {
6020 return status;
6021 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if (output_size != output_length) {
6024 return PSA_ERROR_GENERIC_ERROR;
6025 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006028}
6029#endif
6030
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306031#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306032static psa_status_t psa_key_derivation_pbkdf2_generate_block(
6033 psa_pbkdf2_key_derivation_t *pbkdf2,
6034 psa_algorithm_t prf_alg,
6035 uint8_t prf_output_length,
6036 psa_key_attributes_t *attributes)
6037{
6038 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306039 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306040 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306041 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306042 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306043 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306044 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306045
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306046 mac_operation.is_sign = 1;
6047 mac_operation.mac_size = prf_output_length;
6048 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306049
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306050 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
6051 attributes,
6052 pbkdf2->password,
6053 pbkdf2->password_length,
6054 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306055 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306056 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306057 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306058 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
6059 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306060 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306061 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306062 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306063 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306064 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306065 }
6066 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
6067 &mac_output_length);
6068 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306069 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306070 }
6071
6072 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306073 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306074 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05306075 }
6076
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05306077 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306078
6079 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306080 /* We are passing prf_output_length as mac_size because the driver
6081 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306082 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306083 status = psa_driver_wrapper_mac_compute(attributes,
6084 pbkdf2->password,
6085 pbkdf2->password_length,
6086 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05306087 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306088 &mac_output_length);
6089 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306090 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306091 }
6092
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05306093 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306094 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05306095
6096cleanup:
6097 /* Zeroise buffers to clear sensitive data from memory. */
6098 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
6099 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306100}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306101
6102static psa_status_t psa_key_derivation_pbkdf2_read(
6103 psa_pbkdf2_key_derivation_t *pbkdf2,
6104 psa_algorithm_t kdf_alg,
6105 uint8_t *output,
6106 size_t output_length)
6107{
6108 psa_status_t status;
6109 psa_algorithm_t prf_alg;
6110 uint8_t prf_output_length;
6111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6112 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
6113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
6114
6115 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6116 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
6117 prf_output_length = PSA_HASH_LENGTH(prf_alg);
6118 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306119 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6120 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306121 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05306122 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05306123 } else {
6124 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306125 }
6126
6127 switch (pbkdf2->state) {
6128 case PSA_PBKDF2_STATE_PASSWORD_SET:
6129 /* Initially we need a new block so bytes_used is equal to block size*/
6130 pbkdf2->bytes_used = prf_output_length;
6131 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
6132 break;
6133 case PSA_PBKDF2_STATE_OUTPUT:
6134 break;
6135 default:
6136 return PSA_ERROR_BAD_STATE;
6137 }
6138
6139 while (output_length != 0) {
6140 uint8_t n = prf_output_length - pbkdf2->bytes_used;
6141 if (n > output_length) {
6142 n = (uint8_t) output_length;
6143 }
6144 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
6145 output += n;
6146 output_length -= n;
6147 pbkdf2->bytes_used += n;
6148
6149 if (output_length == 0) {
6150 break;
6151 }
6152
6153 /* We need a new block */
6154 pbkdf2->bytes_used = 0;
6155 pbkdf2->block_number++;
6156
6157 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
6158 prf_output_length,
6159 &attributes);
6160 if (status != PSA_SUCCESS) {
6161 return status;
6162 }
6163 }
6164
6165 return PSA_SUCCESS;
6166}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306167#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306168
Janos Follath6c6c8fc2019-06-17 12:38:20 +01006169psa_status_t psa_key_derivation_output_bytes(
6170 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00006171 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006173{
6174 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00006175 LOCAL_OUTPUT_DECLARE(output_external, output);
6176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02006178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006180 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006182 }
6183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006185 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006186 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02006187 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
6188 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006189 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02006190 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006192 }
Ryan Everettf943e222024-01-19 14:46:39 +00006193
6194 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00006195 if (output_length > operation->capacity) {
6196 operation->capacity = 0;
6197 /* Go through the error path to wipe all confidential data now
6198 * that the operation object is useless. */
6199 status = PSA_ERROR_INSUFFICIENT_DATA;
6200 goto exit;
6201 }
6202
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006203 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006204
Przemek Stekiel69c46792022-06-10 12:59:51 +02006205#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6207 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6208 output, output_length);
6209 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006210#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6212 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6214 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6215 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6216 kdf_alg, output,
6217 output_length);
6218 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006219#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6220 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006221#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006223 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6225 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006226#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306227#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306228 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306229 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6230 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306231 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306232#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006233
Gilles Peskineeab56e42018-07-12 17:12:33 +02006234 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006235 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006236 status = PSA_ERROR_BAD_STATE;
6237 LOCAL_OUTPUT_FREE(output_external, output);
6238
6239 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006240 }
6241
6242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006244 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 * This allows us to differentiate between exhausted operations and
6246 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6247 * operations. */
6248 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006250 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006251 if (output != NULL) {
6252 memset(output, '!', output_length);
6253 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006254 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006255
6256 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006258}
6259
David Brown7807bf72021-02-09 16:28:23 -07006260#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006261static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006262{
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 if (data_size >= 8) {
6264 mbedtls_des_key_set_parity(data);
6265 }
6266 if (data_size >= 16) {
6267 mbedtls_des_key_set_parity(data + 8);
6268 }
6269 if (data_size >= 24) {
6270 mbedtls_des_key_set_parity(data + 16);
6271 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006272}
David Brown7807bf72021-02-09 16:28:23 -07006273#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006274
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006275/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 * ECC keys on a Weierstrass elliptic curve require the generation
6277 * of a private key which is an integer
6278 * in the range [1, N - 1], where N is the boundary of the private key domain:
6279 * N is the prime p for Diffie-Hellman, or the order of the
6280 * curve’s base point for ECC.
6281 *
6282 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6283 * This function generates the private key using the following process:
6284 *
6285 * 1. Draw a byte string of length ceiling(m/8) bytes.
6286 * 2. If m is not a multiple of 8, set the most significant
6287 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6288 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6289 * 4. If k > N - 2, discard the result and return to step 1.
6290 * 5. Output k + 1 as the private key.
6291 *
6292 * This method allows compliance to NIST standards, specifically the methods titled
6293 * Key-Pair Generation by Testing Candidates in the following publications:
6294 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6295 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6296 * Diffie-Hellman keys.
6297 *
6298 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6299 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6300 *
6301 * Note: Function allocates memory for *data buffer, so given *data should be
6302 * always NULL.
6303 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006304#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6305#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006306static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006307 psa_key_slot_t *slot,
6308 size_t bits,
6309 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006310 uint8_t **data
6311 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006312{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006313 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006314 mbedtls_mpi k;
6315 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006318 size_t m;
Gilles Peskinece726b22025-03-06 19:27:50 +01006319 size_t m_bytes = 0;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 mbedtls_mpi_init(&k);
6322 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006323
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006324 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006326 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006327 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006330 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006331 goto cleanup;
6332 }
6333
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006334 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006338
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006339 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006340 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006341 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006342
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006343 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006344
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006345 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006347
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006348 /* Note: This function is always called with *data == NULL and it
6349 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 *data = mbedtls_calloc(1, m_bytes);
6351 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006352 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006353 goto cleanup;
6354 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006357 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006359 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006361
6362 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006364 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006365 * (8 * ceiling(m/8) - m) bits of the first byte in
6366 * the string to zero.
6367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006369 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006370 }
6371
6372 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 * big-endian byte string.
6374 */
6375 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006376
6377 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 * Result of comparison is returned. When it indicates error
6379 * then this function is called again.
6380 */
6381 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006382 }
6383
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006384 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6386 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006387cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 if (ret != 0) {
6389 status = mbedtls_to_psa_error(ret);
6390 }
6391 if (status != PSA_SUCCESS) {
Gilles Peskine184cac12025-03-06 12:42:30 +01006392 mbedtls_zeroize_and_free(*data, m_bytes);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006393 *data = NULL;
6394 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 mbedtls_mpi_free(&k);
6396 mbedtls_mpi_free(&diff_N_2);
6397 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006398}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006399
6400/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6401 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6402 *
6403 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6404 * draw a 32-byte string and process it as specified in
6405 * Elliptic Curves for Security [RFC7748] §5.
6406 *
6407 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6408 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6409 *
6410 * Note: Function allocates memory for *data buffer, so given *data should be
6411 * always NULL.
6412 */
6413
6414static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6415 size_t bits,
6416 psa_key_derivation_operation_t *operation,
6417 uint8_t **data
6418 )
6419{
6420 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006424 case 255:
6425 output_length = 32;
6426 break;
6427 case 448:
6428 output_length = 56;
6429 break;
6430 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006432 break;
6433 }
6434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (*data == NULL) {
6438 return PSA_ERROR_INSUFFICIENT_MEMORY;
6439 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006444 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006446
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006448 case 255:
6449 (*data)[0] &= 248;
6450 (*data)[31] &= 127;
6451 (*data)[31] |= 64;
6452 break;
6453 case 448:
6454 (*data)[0] &= 252;
6455 (*data)[55] |= 128;
6456 break;
6457 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006459 break;
6460 }
6461
6462 return status;
6463}
Valerio Setti86587ab2023-06-27 13:09:30 +02006464#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6465static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6466 psa_key_slot_t *slot, size_t bits,
6467 psa_key_derivation_operation_t *operation, uint8_t **data)
6468{
6469 (void) slot;
6470 (void) bits;
6471 (void) operation;
6472 (void) data;
6473 return PSA_ERROR_NOT_SUPPORTED;
6474}
6475
6476static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6477 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6478{
6479 (void) bits;
6480 (void) operation;
6481 (void) data;
6482 return PSA_ERROR_NOT_SUPPORTED;
6483}
6484#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6485#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006486
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006487static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006488 psa_key_slot_t *slot,
6489 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006491{
6492 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306494 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6498 return PSA_ERROR_INVALID_ARGUMENT;
6499 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006500
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006501#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006502 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6504 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6505 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006506 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6508 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 }
6511 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006512 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6514 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006515 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006517 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006518 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006519#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006520 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 if (key_type_is_raw_bytes(slot->attr.type)) {
6522 if (bits % 8 != 0) {
6523 return PSA_ERROR_INVALID_ARGUMENT;
6524 }
6525 data = mbedtls_calloc(1, bytes);
6526 if (data == NULL) {
6527 return PSA_ERROR_INSUFFICIENT_MEMORY;
6528 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 status = psa_key_derivation_output_bytes(operation, data, bytes);
6531 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 }
David Brown12ca5032021-02-11 11:02:00 -07006534#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6536 psa_des_set_key_parity(data, bytes);
6537 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006538#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 } else {
6540 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006541 }
Ronald Crondd04d422020-11-28 15:14:42 +01006542
Ronald Cronbf33c932020-11-28 18:06:53 +01006543 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006544
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006545 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006546 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 &storage_size);
6548 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306549 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 }
Archanad8a83dc2021-06-14 10:04:16 +05306551 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 status = psa_allocate_buffer_to_slot(slot, storage_size);
6553 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306554 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 }
Archanad8a83dc2021-06-14 10:04:16 +05306556
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006557 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 data, bytes,
6559 slot->key.data,
6560 slot->key.bytes,
6561 &slot->key.bytes, &bits);
6562 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006563 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006565
6566exit:
Gilles Peskine184cac12025-03-06 12:42:30 +01006567 mbedtls_zeroize_and_free(data, bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006569}
6570
Gilles Peskinef36d7852024-06-06 21:11:44 +02006571static const psa_custom_key_parameters_t default_custom_production =
Gilles Peskine4a85ff32024-07-18 18:52:59 +02006572 PSA_CUSTOM_KEY_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006573
Gilles Peskine52504f82024-06-20 17:19:58 +02006574int psa_custom_key_parameters_are_default(
Gilles Peskinef36d7852024-06-06 21:11:44 +02006575 const psa_custom_key_parameters_t *custom,
6576 size_t custom_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006577{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006578 if (custom->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006579 return 0;
6580 }
Gilles Peskinef36d7852024-06-06 21:11:44 +02006581 if (custom_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006582 return 0;
6583 }
6584 return 1;
6585}
6586
Gilles Peskinef36d7852024-06-06 21:11:44 +02006587psa_status_t psa_key_derivation_output_key_custom(
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006588 const psa_key_attributes_t *attributes,
6589 psa_key_derivation_operation_t *operation,
Gilles Peskinef36d7852024-06-06 21:11:44 +02006590 const psa_custom_key_parameters_t *custom,
6591 const uint8_t *custom_data,
6592 size_t custom_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006593 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006594{
6595 psa_status_t status;
6596 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006597 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006598
Ronald Cron81709fc2020-11-14 12:10:32 +01006599 *key = MBEDTLS_SVC_KEY_ID_INIT;
6600
Gilles Peskine0f84d622019-09-12 19:03:13 +02006601 /* Reject any attempt to create a zero-length key so that we don't
6602 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 if (psa_get_key_bits(attributes) == 0) {
6604 return PSA_ERROR_INVALID_ARGUMENT;
6605 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006606
Gilles Peskinef36d7852024-06-06 21:11:44 +02006607 (void) custom_data; /* We only accept 0-length data */
Gilles Peskine52504f82024-06-20 17:19:58 +02006608 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006609 return PSA_ERROR_INVALID_ARGUMENT;
6610 }
6611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 if (operation->alg == PSA_ALG_NONE) {
6613 return PSA_ERROR_BAD_STATE;
6614 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 if (!operation->can_output_key) {
6617 return PSA_ERROR_NOT_PERMITTED;
6618 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6621 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006624 /* Deriving a key in a secure element is not implemented yet. */
6625 status = PSA_ERROR_NOT_SUPPORTED;
6626 }
6627#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 if (status == PSA_SUCCESS) {
6629 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006630 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006632 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 if (status == PSA_SUCCESS) {
6634 status = psa_finish_key_creation(slot, driver, key);
6635 }
6636 if (status != PSA_SUCCESS) {
6637 psa_fail_key_creation(slot, driver);
6638 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006641}
6642
Gilles Peskinef36d7852024-06-06 21:11:44 +02006643psa_status_t psa_key_derivation_output_key_ext(
6644 const psa_key_attributes_t *attributes,
6645 psa_key_derivation_operation_t *operation,
6646 const psa_key_production_parameters_t *params,
6647 size_t params_data_length,
6648 mbedtls_svc_key_id_t *key)
6649{
6650 return psa_key_derivation_output_key_custom(
6651 attributes, operation,
6652 (const psa_custom_key_parameters_t *) params,
6653 params->data, params_data_length,
6654 key);
6655}
6656
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006657psa_status_t psa_key_derivation_output_key(
6658 const psa_key_attributes_t *attributes,
6659 psa_key_derivation_operation_t *operation,
6660 mbedtls_svc_key_id_t *key)
6661{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006662 return psa_key_derivation_output_key_custom(attributes, operation,
6663 &default_custom_production,
6664 NULL, 0,
6665 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006666}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006667
6668
6669/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006670/* Key derivation */
6671/****************************************************************/
6672
Steven Cooreman932ffb72021-02-15 12:14:32 +01006673#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006674static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006675{
6676#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6678 return 1;
6679 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006680#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006681#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6683 return 1;
6684 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006685#endif
6686#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6688 return 1;
6689 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006690#endif
6691#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6693 return 1;
6694 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006695#endif
6696#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6698 return 1;
6699 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006700#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6703 return 1;
6704 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006705#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306706#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6707 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6708 return 1;
6709 }
6710#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306711#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6712 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6713 return 1;
6714 }
6715#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006717}
6718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006720{
6721 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 psa_status_t status = psa_hash_setup(&operation, alg);
6723 psa_hash_abort(&operation);
6724 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006725}
6726
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306727static psa_status_t psa_key_derivation_set_maximum_capacity(
6728 psa_key_derivation_operation_t *operation,
6729 psa_algorithm_t kdf_alg)
6730{
6731#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6732 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6733 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6734 return PSA_SUCCESS;
6735 }
6736#endif
6737#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6738 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6739#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306740 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306741 PSA_KEY_TYPE_AES,
6742 128U,
6743 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306744#else
6745 operation->capacity = SIZE_MAX;
6746#endif
6747 return PSA_SUCCESS;
6748 }
6749#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6750
6751 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6752 * invalid or meaningless but it does not affect this function */
6753 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6754 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306755 if (hash_size == 0) {
6756 return PSA_ERROR_NOT_SUPPORTED;
6757 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306758
6759 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6760 * we might fail later, which is somewhat unfriendly and potentially
6761 * risk-prone. */
6762 psa_status_t status = psa_hash_try_support(hash_alg);
6763 if (status != PSA_SUCCESS) {
6764 return status;
6765 }
6766
6767#if defined(PSA_WANT_ALG_HKDF)
6768 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6769 operation->capacity = 255 * hash_size;
6770 } else
6771#endif
6772#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6773 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6774 operation->capacity = hash_size;
6775 } else
6776#endif
6777#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6778 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6779 operation->capacity = 255 * hash_size;
6780 } else
6781#endif
6782#if defined(PSA_WANT_ALG_TLS12_PRF)
6783 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6784 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6785 operation->capacity = SIZE_MAX;
6786 } else
6787#endif
6788#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6789 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6790 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6791 /* Master Secret is always 48 bytes
6792 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6793 operation->capacity = 48U;
6794 } else
6795#endif
6796#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6797 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6798#if (SIZE_MAX > UINT32_MAX)
6799 operation->capacity = UINT32_MAX * hash_size;
6800#else
6801 operation->capacity = SIZE_MAX;
6802#endif
6803 } else
6804#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6805 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306806 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306807 status = PSA_ERROR_NOT_SUPPORTED;
6808 }
6809 return status;
6810}
6811
Gilles Peskine969c5d62019-01-16 15:53:06 +01006812static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006813 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006815{
Janos Follath5fe19732019-06-20 15:09:30 +01006816 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6817 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006819
Gilles Peskine969c5d62019-01-16 15:53:06 +01006820 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 if (!is_kdf_alg_supported(kdf_alg)) {
6822 return PSA_ERROR_NOT_SUPPORTED;
6823 }
John Durkop07cc04a2020-11-16 22:08:34 -08006824
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306825 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6826 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306827 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006828}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006829
Gilles Peskine449bd832023-01-11 14:50:10 +01006830static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006831{
6832#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 if (alg == PSA_ALG_ECDH) {
6834 return PSA_SUCCESS;
6835 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006836#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006837#if defined(PSA_WANT_ALG_FFDH)
6838 if (alg == PSA_ALG_FFDH) {
6839 return PSA_SUCCESS;
6840 }
6841#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006842 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006844}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006845
6846static int psa_key_derivation_allows_free_form_secret_input(
6847 psa_algorithm_t kdf_alg)
6848{
6849#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6850 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6851 return 0;
6852 }
6853#endif
6854 (void) kdf_alg;
6855 return 1;
6856}
John Durkop07cc04a2020-11-16 22:08:34 -08006857#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6860 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006861{
6862 psa_status_t status;
6863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (operation->alg != 0) {
6865 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006866 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6869 return PSA_ERROR_INVALID_ARGUMENT;
6870 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6871#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6872 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6873 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6874 status = psa_key_agreement_try_support(ka_alg);
6875 if (status != PSA_SUCCESS) {
6876 return status;
6877 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006878 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6879 return PSA_ERROR_INVALID_ARGUMENT;
6880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6882#else
6883 return PSA_ERROR_NOT_SUPPORTED;
6884#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6885 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6886#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6887 status = psa_key_derivation_setup_kdf(operation, alg);
6888#else
6889 return PSA_ERROR_NOT_SUPPORTED;
6890#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6891 } else {
6892 return PSA_ERROR_INVALID_ARGUMENT;
6893 }
6894
6895 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006896 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 }
6898 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006899}
6900
Przemek Stekiel69c46792022-06-10 12:59:51 +02006901#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006902static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6903 psa_algorithm_t kdf_alg,
6904 psa_key_derivation_step_t step,
6905 const uint8_t *data,
6906 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006907{
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006909 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006911 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006912#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6914 return PSA_ERROR_INVALID_ARGUMENT;
6915 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006916#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 if (hkdf->state != HKDF_STATE_INIT) {
6918 return PSA_ERROR_BAD_STATE;
6919 } else {
6920 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6921 hash_alg,
6922 data, data_length);
6923 if (status != PSA_SUCCESS) {
6924 return status;
6925 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006926 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006928 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006929 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006930#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006932 /* We shouldn't be in different state as HKDF_EXPAND only allows
6933 * two inputs: SECRET (this case) and INFO which does not modify
6934 * the state. It could happen only if the hkdf
6935 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 if (hkdf->state != HKDF_STATE_INIT) {
6937 return PSA_ERROR_BAD_STATE;
6938 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006939
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006940 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6942 return PSA_ERROR_INVALID_ARGUMENT;
6943 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 memcpy(hkdf->prk, data, data_length);
6946 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006947#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006948 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006949 /* HKDF: If no salt was provided, use an empty salt.
6950 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006952#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6954 return PSA_ERROR_BAD_STATE;
6955 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006956#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6958 hash_alg,
6959 NULL, 0);
6960 if (status != PSA_SUCCESS) {
6961 return status;
6962 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006963 hkdf->state = HKDF_STATE_STARTED;
6964 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (hkdf->state != HKDF_STATE_STARTED) {
6966 return PSA_ERROR_BAD_STATE;
6967 }
6968 status = psa_mac_update(&hkdf->hmac,
6969 data, data_length);
6970 if (status != PSA_SUCCESS) {
6971 return status;
6972 }
6973 status = psa_mac_sign_finish(&hkdf->hmac,
6974 hkdf->prk,
6975 sizeof(hkdf->prk),
6976 &data_length);
6977 if (status != PSA_SUCCESS) {
6978 return status;
6979 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006980 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006981
Gilles Peskine2b522db2019-04-12 15:11:49 +02006982 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006983 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006984#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006986 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006988 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006990#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006991 {
6992 /* Block 0 is empty, and the next block will be
6993 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006995 }
6996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006998 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006999#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
7001 return PSA_ERROR_INVALID_ARGUMENT;
7002 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02007003#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +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 hkdf->state == HKDF_STATE_INIT) {
7007 return PSA_ERROR_BAD_STATE;
7008 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02007009#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 if (hkdf->state == HKDF_STATE_OUTPUT) {
7011 return PSA_ERROR_BAD_STATE;
7012 }
7013 if (hkdf->info_set) {
7014 return PSA_ERROR_BAD_STATE;
7015 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007016 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (data_length != 0) {
7018 hkdf->info = mbedtls_calloc(1, data_length);
7019 if (hkdf->info == NULL) {
7020 return PSA_ERROR_INSUFFICIENT_MEMORY;
7021 }
7022 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007023 }
7024 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007026 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007028 }
7029}
Przemek Stekiel69c46792022-06-10 12:59:51 +02007030#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01007031
John Durkop07cc04a2020-11-16 22:08:34 -08007032#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
7033 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007034static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
7035 const uint8_t *data,
7036 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01007037{
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
7039 return PSA_ERROR_BAD_STATE;
7040 }
Janos Follathf08e2652019-06-13 09:05:41 +01007041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 if (data_length != 0) {
7043 prf->seed = mbedtls_calloc(1, data_length);
7044 if (prf->seed == NULL) {
7045 return PSA_ERROR_INSUFFICIENT_MEMORY;
7046 }
Janos Follathf08e2652019-06-13 09:05:41 +01007047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007049 prf->seed_length = data_length;
7050 }
Janos Follathf08e2652019-06-13 09:05:41 +01007051
Gilles Peskine43f958b2020-12-13 14:55:14 +01007052 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01007055}
7056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
7058 const uint8_t *data,
7059 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01007060{
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
7062 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7063 return PSA_ERROR_BAD_STATE;
7064 }
Janos Follath81550542019-06-13 14:26:34 +01007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (data_length != 0) {
7067 prf->secret = mbedtls_calloc(1, data_length);
7068 if (prf->secret == NULL) {
7069 return PSA_ERROR_INSUFFICIENT_MEMORY;
7070 }
Steven Cooremana6df6042021-04-29 19:32:25 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02007073 prf->secret_length = data_length;
7074 }
Janos Follath81550542019-06-13 14:26:34 +01007075
Gilles Peskine43f958b2020-12-13 14:55:14 +01007076 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01007077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01007079}
7080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
7082 const uint8_t *data,
7083 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01007084{
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
7086 return PSA_ERROR_BAD_STATE;
7087 }
Janos Follath63028dd2019-06-13 09:15:47 +01007088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (data_length != 0) {
7090 prf->label = mbedtls_calloc(1, data_length);
7091 if (prf->label == NULL) {
7092 return PSA_ERROR_INSUFFICIENT_MEMORY;
7093 }
Janos Follath63028dd2019-06-13 09:15:47 +01007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01007096 prf->label_length = data_length;
7097 }
Janos Follath63028dd2019-06-13 09:15:47 +01007098
Gilles Peskine43f958b2020-12-13 14:55:14 +01007099 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01007102}
7103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
7105 psa_key_derivation_step_t step,
7106 const uint8_t *data,
7107 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01007108{
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01007110 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01007112 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01007114 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01007116 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01007118 }
7119}
John Durkop07cc04a2020-11-16 22:08:34 -08007120#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
7121 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
7122
7123#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
7124static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
7125 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08007126 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08007128{
7129 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
7131 4 + data_length + prf->other_secret_length :
7132 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007133
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
7135 return PSA_ERROR_INVALID_ARGUMENT;
7136 }
John Durkop07cc04a2020-11-16 22:08:34 -08007137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 uint8_t *pms = mbedtls_calloc(1, pms_len);
7139 if (pms == NULL) {
7140 return PSA_ERROR_INSUFFICIENT_MEMORY;
7141 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007142 uint8_t *cur = pms;
7143
7144 /* pure-PSK:
7145 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08007146 *
7147 * The premaster secret is formed as follows: if the PSK is N octets
7148 * long, concatenate a uint16 with the value N, N zero octets, a second
7149 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007150 *
7151 * mixed-PSK:
7152 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
7153 * follows: concatenate a uint16 with the length of the other secret,
7154 * the other secret itself, uint16 with the length of PSK, and the
7155 * PSK itself.
7156 * For details please check:
7157 * - RFC 4279, Section 4 for the definition of RSA-PSK,
7158 * - RFC 4279, Section 3 for the definition of DHE-PSK,
7159 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08007160 */
7161
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7163 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
7164 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
7165 if (prf->other_secret_length != 0) {
7166 memcpy(cur, prf->other_secret, prf->other_secret_length);
7167 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02007168 cur += prf->other_secret_length;
7169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 } else {
7171 *cur++ = MBEDTLS_BYTE_1(data_length);
7172 *cur++ = MBEDTLS_BYTE_0(data_length);
7173 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007174 cur += data_length;
7175 }
7176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 *cur++ = MBEDTLS_BYTE_1(data_length);
7178 *cur++ = MBEDTLS_BYTE_0(data_length);
7179 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007180 cur += data_length;
7181
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007182 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08007183
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007184 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08007186}
Janos Follath6660f0e2019-06-17 08:44:03 +01007187
Przemek Stekiele47201b2022-04-19 13:53:28 +02007188static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
7189 psa_tls12_prf_key_derivation_t *prf,
7190 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02007192{
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
7194 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007195 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007196
7197 if (data_length != 0) {
7198 prf->other_secret = mbedtls_calloc(1, data_length);
7199 if (prf->other_secret == NULL) {
7200 return PSA_ERROR_INSUFFICIENT_MEMORY;
7201 }
7202
7203 memcpy(prf->other_secret, data, data_length);
7204 prf->other_secret_length = data_length;
7205 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007206 prf->other_secret_length = 0;
7207 }
7208
7209 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
7210
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007212}
7213
Janos Follath6660f0e2019-06-17 08:44:03 +01007214static psa_status_t psa_tls12_prf_psk_to_ms_input(
7215 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01007216 psa_key_derivation_step_t step,
7217 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01007219{
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007221 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return psa_tls12_prf_psk_to_ms_set_key(prf,
7223 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007224 break;
7225 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7227 data,
7228 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007229 break;
7230 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007232 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007233
Przemek Stekiele47201b2022-04-19 13:53:28 +02007234 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007235}
John Durkop07cc04a2020-11-16 22:08:34 -08007236#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007237
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007238#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7239static psa_status_t psa_tls12_ecjpake_to_pms_input(
7240 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007241 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007242 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007244{
Gilles Peskine449bd832023-01-11 14:50:10 +01007245 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7246 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7247 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007248 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007249
7250 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 if (data[0] != 0x04) {
7252 return PSA_ERROR_INVALID_ARGUMENT;
7253 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007254
7255 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007257
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007259}
7260#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307261
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307262#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307263static psa_status_t psa_pbkdf2_set_input_cost(
7264 psa_pbkdf2_key_derivation_t *pbkdf2,
7265 psa_key_derivation_step_t step,
7266 uint64_t data)
7267{
7268 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7269 return PSA_ERROR_INVALID_ARGUMENT;
7270 }
7271
7272 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7273 return PSA_ERROR_BAD_STATE;
7274 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307275
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307276 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307277 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307278 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307279
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307280 if (data == 0) {
7281 return PSA_ERROR_INVALID_ARGUMENT;
7282 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307283
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307284 pbkdf2->input_cost = data;
7285 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7286
7287 return PSA_SUCCESS;
7288}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307289
7290static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7291 const uint8_t *data,
7292 size_t data_length)
7293{
Gilles Peskineead17662023-08-20 22:02:51 +02007294 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7295 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7296 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7297 /* Appending to existing salt. No state change. */
7298 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307299 return PSA_ERROR_BAD_STATE;
7300 }
7301
Gilles Peskineca57d782023-07-20 19:08:06 +02007302 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007303 /* Appending an empty string, nothing to do. */
7304 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307305 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307306
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307307 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7308 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307309 return PSA_ERROR_INSUFFICIENT_MEMORY;
7310 }
7311
Gilles Peskineead17662023-08-20 22:02:51 +02007312 if (pbkdf2->salt_length != 0) {
7313 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7314 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307315 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307316 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307317 mbedtls_free(pbkdf2->salt);
7318 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307319 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307320 return PSA_SUCCESS;
7321}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307322
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307323#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307324static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307325 const uint8_t *input,
7326 size_t input_len,
7327 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307328 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307329{
7330 psa_status_t status = PSA_SUCCESS;
7331 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007332 return psa_hash_compute(hash_alg, input, input_len, output,
7333 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7334 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307335 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307336 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007337 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307338 return status;
7339}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307340#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307341
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307342#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307343static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7344 size_t input_len,
7345 uint8_t *output,
7346 size_t *output_len)
7347{
7348 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307349 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307351 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307352 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7353 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7354 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307355 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307356 * mac_size as the driver function sets mac_output_length = mac_size
7357 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307358 status = psa_driver_wrapper_mac_compute(&attributes,
7359 zeros, sizeof(zeros),
7360 PSA_ALG_CMAC, input, input_len,
7361 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307362 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7363 128U,
7364 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307365 output_len);
7366 } else {
7367 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307368 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307369 }
7370 return status;
7371}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307372#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307373
7374static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307375 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307376 const uint8_t *data,
7377 size_t data_length)
7378{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307379 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307380 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7381 return PSA_ERROR_BAD_STATE;
7382 }
7383
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307384#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307385 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7386 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7387 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7388 pbkdf2->password,
7389 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307390 } else
7391#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7392#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7393 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307394 status = psa_pbkdf2_cmac_set_password(data, data_length,
7395 pbkdf2->password,
7396 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307397 } else
7398#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7399 {
7400 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307401 }
7402
7403 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7404
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307405 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307406}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307407
7408static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307409 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307410 psa_key_derivation_step_t step,
7411 const uint8_t *data,
7412 size_t data_length)
7413{
7414 switch (step) {
7415 case PSA_KEY_DERIVATION_INPUT_SALT:
7416 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7417 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307418 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307419 default:
7420 return PSA_ERROR_INVALID_ARGUMENT;
7421 }
7422}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307423#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307424
Gilles Peskineb8965192019-09-24 16:21:10 +02007425/** Check whether the given key type is acceptable for the given
7426 * input step of a key derivation.
7427 *
7428 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7429 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7430 * Both secret and non-secret inputs can alternatively have the type
7431 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7432 * that the input was passed as a buffer rather than via a key object.
7433 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007434static int psa_key_derivation_check_input_type(
7435 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007437{
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007439 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 if (key_type == PSA_KEY_TYPE_DERIVE) {
7441 return PSA_SUCCESS;
7442 }
7443 if (key_type == PSA_KEY_TYPE_NONE) {
7444 return PSA_SUCCESS;
7445 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007446 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007447 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 if (key_type == PSA_KEY_TYPE_DERIVE) {
7449 return PSA_SUCCESS;
7450 }
7451 if (key_type == PSA_KEY_TYPE_NONE) {
7452 return PSA_SUCCESS;
7453 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007454 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007455 case PSA_KEY_DERIVATION_INPUT_LABEL:
7456 case PSA_KEY_DERIVATION_INPUT_SALT:
7457 case PSA_KEY_DERIVATION_INPUT_INFO:
7458 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7460 return PSA_SUCCESS;
7461 }
7462 if (key_type == PSA_KEY_TYPE_NONE) {
7463 return PSA_SUCCESS;
7464 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007465 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307466 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7467 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7468 return PSA_SUCCESS;
7469 }
7470 if (key_type == PSA_KEY_TYPE_DERIVE) {
7471 return PSA_SUCCESS;
7472 }
7473 if (key_type == PSA_KEY_TYPE_NONE) {
7474 return PSA_SUCCESS;
7475 }
7476 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007477 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007479}
7480
Janos Follathb80a94e2019-06-12 15:54:46 +01007481static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007482 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007483 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007484 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007485 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007487{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007488 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 status = psa_key_derivation_check_input_type(step, key_type);
7492 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007493 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007495
Przemek Stekiel69c46792022-06-10 12:59:51 +02007496#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7498 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7499 step, data, data_length);
7500 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007501#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007502#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7504 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7505 step, data, data_length);
7506 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007507#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7508#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7510 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7511 step, data, data_length);
7512 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007513#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007514#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007516 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7518 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007519#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307520#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307521 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307522 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7523 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307524 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307525#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007526 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007527 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007528 (void) data;
7529 (void) data_length;
7530 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007532 }
7533
Gilles Peskine224b0d62019-09-23 18:13:17 +02007534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (status != PSA_SUCCESS) {
7536 psa_key_derivation_abort(operation);
7537 }
7538 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007539}
7540
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307541static psa_status_t psa_key_derivation_input_integer_internal(
7542 psa_key_derivation_operation_t *operation,
7543 psa_key_derivation_step_t step,
7544 uint64_t value)
7545{
7546 psa_status_t status;
7547 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7548
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307549#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307550 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307551 status = psa_pbkdf2_set_input_cost(
7552 &operation->ctx.pbkdf2, step, value);
7553 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307554#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307555 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307556 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307557 (void) value;
7558 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307559 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307560 }
7561
7562 if (status != PSA_SUCCESS) {
7563 psa_key_derivation_abort(operation);
7564 }
7565 return status;
7566}
7567
Janos Follath51f4a0f2019-06-14 11:35:55 +01007568psa_status_t psa_key_derivation_input_bytes(
7569 psa_key_derivation_operation_t *operation,
7570 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007571 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007573{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7575 LOCAL_INPUT_DECLARE(data_external, data);
7576
7577 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7578
7579 status = psa_key_derivation_input_internal(operation, step,
7580 PSA_KEY_TYPE_NONE,
7581 data, data_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00007582#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007583exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007584#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007585 LOCAL_INPUT_FREE(data_external, data);
7586 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007587}
7588
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307589psa_status_t psa_key_derivation_input_integer(
7590 psa_key_derivation_operation_t *operation,
7591 psa_key_derivation_step_t step,
7592 uint64_t value)
7593{
7594 return psa_key_derivation_input_integer_internal(operation, step, value);
7595}
7596
Janos Follath51f4a0f2019-06-14 11:35:55 +01007597psa_status_t psa_key_derivation_input_key(
7598 psa_key_derivation_operation_t *operation,
7599 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007601{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007603 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007604 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007605
Ronald Cron5c522922020-11-14 16:35:34 +01007606 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7608 if (status != PSA_SUCCESS) {
7609 psa_key_derivation_abort(operation);
7610 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007611 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007612
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307613 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7614 * permission to output to a key object. */
7615 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7616 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007617 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 status = psa_key_derivation_input_internal(operation,
7621 step, slot->attr.type,
7622 slot->key.data,
7623 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007624
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007625 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007628}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007629
7630
7631
7632/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007633/* Key agreement */
7634/****************************************************************/
7635
Gilles Peskine449bd832023-01-11 14:50:10 +01007636psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7637 const uint8_t *key_buffer,
7638 size_t key_buffer_size,
7639 psa_algorithm_t alg,
7640 const uint8_t *peer_key,
7641 size_t peer_key_length,
7642 uint8_t *shared_secret,
7643 size_t shared_secret_size,
7644 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007645{
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007647#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007648 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7650 key_buffer_size, alg,
7651 peer_key, peer_key_length,
7652 shared_secret,
7653 shared_secret_size,
7654 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007655#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007656
7657#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7658 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007659 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007660 peer_key,
7661 peer_key_length,
7662 key_buffer,
7663 key_buffer_size,
7664 shared_secret,
7665 shared_secret_size,
7666 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007667#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7668
Gilles Peskine01d718c2018-09-18 12:01:02 +02007669 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007670 (void) attributes;
7671 (void) key_buffer;
7672 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007673 (void) peer_key;
7674 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007675 (void) shared_secret;
7676 (void) shared_secret_size;
7677 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007679 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007680}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007681
Aditya Deshpande17845b82022-10-13 17:21:01 +01007682/** Internal function for raw key agreement
7683 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007684 * to the driver's implementation if a driver is present.
7685 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007686 * (psa_key_agreement_raw_builtin).
7687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007688static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7689 psa_key_slot_t *private_key,
7690 const uint8_t *peer_key,
7691 size_t peer_key_length,
7692 uint8_t *shared_secret,
7693 size_t shared_secret_size,
7694 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007695{
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7697 return PSA_ERROR_NOT_SUPPORTED;
7698 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007699
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007700 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 private_key->key.data,
7702 private_key->key.bytes, alg,
7703 peer_key, peer_key_length,
7704 shared_secret,
7705 shared_secret_size,
7706 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007707}
7708
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007709/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007710 * to potentially free embedded data structures and wipe confidential data.
7711 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007712static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7713 psa_key_derivation_step_t step,
7714 psa_key_slot_t *private_key,
7715 const uint8_t *peer_key,
7716 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007717{
7718 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007719 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007720 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007722
7723 /* Step 1: run the secret agreement algorithm to generate the shared
7724 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 status = psa_key_agreement_raw_internal(ka_alg,
7726 private_key,
7727 peer_key, peer_key_length,
7728 shared_secret,
7729 sizeof(shared_secret),
7730 &shared_secret_length);
7731 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007734
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007735 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007736 * the shared secret. A shared secret is permitted wherever a key
7737 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 status = psa_key_derivation_input_internal(operation, step,
7739 PSA_KEY_TYPE_DERIVE,
7740 shared_secret,
7741 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007742exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7744 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007745}
7746
Gilles Peskine449bd832023-01-11 14:50:10 +01007747psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7748 psa_key_derivation_step_t step,
7749 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007750 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007752{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007754 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007755 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007756 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007757
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7759 return PSA_ERROR_INVALID_ARGUMENT;
7760 }
Ronald Cron5c522922020-11-14 16:35:34 +01007761 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7763 if (status != PSA_SUCCESS) {
7764 return status;
7765 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007766
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007767 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 status = psa_key_agreement_internal(operation, step,
7769 slot,
7770 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007771
David Horstmann4a48bec2024-03-13 15:42:31 +00007772#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007773exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007774#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 if (status != PSA_SUCCESS) {
7776 psa_key_derivation_abort(operation);
7777 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007778 /* If a private key has been added as SECRET, we allow the derived
7779 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007780 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007781 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007783 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007784
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007785 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007786 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007789}
7790
Gilles Peskine449bd832023-01-11 14:50:10 +01007791psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7792 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007793 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007795 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 size_t output_size,
7797 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007798{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007801 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007802 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007803 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7804 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007805 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007806
Gilles Peskine449bd832023-01-11 14:50:10 +01007807 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007808 status = PSA_ERROR_INVALID_ARGUMENT;
7809 goto exit;
7810 }
Ronald Cron5c522922020-11-14 16:35:34 +01007811 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7813 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007814 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007816
Gilles Peskine3e561302022-04-14 00:17:15 +02007817 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7818 * for the output size. The PSA specification only guarantees that this
7819 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7820 * but it might be nice to allow smaller buffers if the output fits.
7821 * At the time of writing this comment, with only ECDH implemented,
7822 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7823 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7824 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007825 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7827 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007828 status = PSA_ERROR_BUFFER_TOO_SMALL;
7829 goto exit;
7830 }
7831
Thomas Daubney81899ab2024-02-15 12:57:26 +00007832 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 status = psa_key_agreement_raw_internal(alg, slot,
7834 peer_key, peer_key_length,
7835 output, output_size,
7836 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007837
7838exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007839 /* Check for successful allocation of output,
7840 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007841 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007842 /* If an error happens and is not handled properly, the output
7843 * may be used as a key to protect sensitive data. Arrange for such
7844 * a key to be random, which is likely to result in decryption or
7845 * verification errors. This is better than filling the buffer with
7846 * some constant data such as zeros, which would result in the data
7847 * being protected with a reproducible, easily knowable key.
7848 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007849 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007850 *output_length = output_size;
7851 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007852
Thomas Daubney5390aca2024-02-22 11:06:04 +00007853 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007854 /* output allocation failed. */
7855 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007856 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007857
Ryan Everetta103ec92024-01-31 13:59:57 +00007858 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007859
Thomas Daubney81899ab2024-02-15 12:57:26 +00007860 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7861 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007863}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007864
7865
7866/****************************************************************/
7867/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007868/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007869
Gilles Peskine672a7712023-04-28 21:00:28 +02007870#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7871#include "entropy_poll.h"
7872#endif
7873
Gilles Peskine30524eb2020-11-13 17:02:26 +01007874/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007875 *
7876 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7877 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007879static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007880{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007881#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007883#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7884
Gilles Peskine30524eb2020-11-13 17:02:26 +01007885 /* Set default configuration if
7886 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007888 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 }
7890 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007891 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007895#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7896 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7897 /* The PSA entropy injection feature depends on using NV seed as an entropy
7898 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 mbedtls_entropy_add_source(&rng->entropy,
7900 mbedtls_nv_seed_poll, NULL,
7901 MBEDTLS_ENTROPY_BLOCK_SIZE,
7902 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007903#endif
7904
Valerio Setti061d4e42024-02-26 12:52:44 +01007905 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007906#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007907}
7908
7909/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007910 *
7911 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7912 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007914static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007915{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007916#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007918#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007919 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007921#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007922}
7923
7924/** Seed the PSA random generator.
7925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007926static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007927{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007928#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7929 /* Do nothing: the external RNG seeds itself. */
7930 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007932#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007933 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007934 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 drbg_seed, sizeof(drbg_seed) - 1);
7936 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007937#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007938}
7939
David Horstmann6e99bb22024-02-06 15:27:49 +00007940psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007942{
David Horstmann6e99bb22024-02-06 15:27:49 +00007943 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007944
David Horstmann6e99bb22024-02-06 15:27:49 +00007945 LOCAL_OUTPUT_DECLARE(output_external, output);
7946 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007947
David Horstmann6e99bb22024-02-06 15:27:49 +00007948 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007949
David Horstmann4a48bec2024-03-13 15:42:31 +00007950#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00007951exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00007952#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00007953 LOCAL_OUTPUT_FREE(output_external, output);
7954 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007955}
7956
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007957#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007958psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7959 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007960{
Paul Elliott600472b2024-02-23 17:26:58 +00007961 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 return PSA_ERROR_NOT_PERMITTED;
7963 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007964
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7966 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7967 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7968 return PSA_ERROR_INVALID_ARGUMENT;
7969 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007970
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007972}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007973#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007974
Ronald Cron3772afe2021-02-08 16:10:05 +01007975/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007976 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007977 * \param type The key type
7978 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007979 *
7980 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007981 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007982 * \retval #PSA_ERROR_INVALID_ARGUMENT
7983 * The size in bits of the key is not valid.
7984 * \retval #PSA_ERROR_NOT_SUPPORTED
7985 * The type and/or the size in bits of the key or the combination of
7986 * the two is not supported.
7987 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007988static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007990{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007991 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007992
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (key_type_is_raw_bytes(type)) {
7994 status = psa_validate_unstructured_key_bit_size(type, bits);
7995 if (status != PSA_SUCCESS) {
7996 return status;
7997 }
7998 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007999#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8001 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
8002 return PSA_ERROR_NOT_SUPPORTED;
8003 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00008004 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00008005 return PSA_ERROR_NOT_SUPPORTED;
8006 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008007
Ronald Cron01b2aba2020-10-05 09:42:02 +02008008 /* Accept only byte-aligned keys, for the same reasons as
8009 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 if (bits % 8 != 0) {
8011 return PSA_ERROR_NOT_SUPPORTED;
8012 }
8013 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02008014#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008015
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008016#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01008018 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 return PSA_SUCCESS;
8020 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02008021#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008022
Valerio Settia55f0422023-07-10 15:34:41 +02008023#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008024 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02008025 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01008026 return PSA_ERROR_NOT_SUPPORTED;
8027 }
8028 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008029#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02008030 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008032 }
8033
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008035}
8036
Ronald Cron55ed0592020-10-05 10:30:40 +02008037psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02008038 const psa_key_attributes_t *attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008039 const psa_custom_key_parameters_t *custom,
8040 const uint8_t *custom_data,
8041 size_t custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02008043{
8044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008045 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008046
Gilles Peskine7a18f962024-02-12 16:48:11 +01008047 /* Only used for RSA */
Gilles Peskinef36d7852024-06-06 21:11:44 +02008048 (void) custom;
8049 (void) custom_data;
8050 (void) custom_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02008051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00008053 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 if (status != PSA_SUCCESS) {
8055 return status;
8056 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02008057
David Brown12ca5032021-02-11 11:02:00 -07008058#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 if (type == PSA_KEY_TYPE_DES) {
8060 psa_des_set_key_parity(key_buffer, key_buffer_size);
8061 }
David Brown8107e312021-02-12 08:08:46 -07008062#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01008063 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01008064
Valerio Setti76df8c12023-07-11 14:11:28 +02008065#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
8067 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008068 custom_data, custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01008069 key_buffer,
8070 key_buffer_size,
8071 key_buffer_length);
8072 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02008073#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01008074
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008075#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8077 return mbedtls_psa_ecp_generate_key(attributes,
8078 key_buffer,
8079 key_buffer_size,
8080 key_buffer_length);
8081 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02008082#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01008083
Valerio Settia55f0422023-07-10 15:34:41 +02008084#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01008085 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
8086 return mbedtls_psa_ffdh_generate_key(attributes,
8087 key_buffer,
8088 key_buffer_size,
8089 key_buffer_length);
8090 } else
Valerio Settia55f0422023-07-10 15:34:41 +02008091#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02008092 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 (void) key_buffer_length;
8094 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02008095 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008096
Gilles Peskine449bd832023-01-11 14:50:10 +01008097 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008098}
Darryl Green0c6575a2018-11-07 16:05:30 +00008099
Gilles Peskinef36d7852024-06-06 21:11:44 +02008100psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
8101 const psa_custom_key_parameters_t *custom,
8102 const uint8_t *custom_data,
8103 size_t custom_data_length,
8104 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008105{
8106 psa_status_t status;
8107 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02008108 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02008109 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02008110
Ronald Cron81709fc2020-11-14 12:10:32 +01008111 *key = MBEDTLS_SVC_KEY_ID_INIT;
8112
Gilles Peskine0f84d622019-09-12 19:03:13 +02008113 /* Reject any attempt to create a zero-length key so that we don't
8114 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 if (psa_get_key_bits(attributes) == 0) {
8116 return PSA_ERROR_INVALID_ARGUMENT;
8117 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02008118
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008119 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008120 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 return PSA_ERROR_INVALID_ARGUMENT;
8122 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02008123
Gilles Peskine7a18f962024-02-12 16:48:11 +01008124#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008125 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinef36d7852024-06-06 21:11:44 +02008126 if (custom->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01008127 return PSA_ERROR_INVALID_ARGUMENT;
8128 }
8129 } else
8130#endif
Gilles Peskine52504f82024-06-20 17:19:58 +02008131 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01008132 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02008133 }
8134
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
8136 &slot, &driver);
8137 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02008138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 }
Gilles Peskine11792082019-08-06 18:36:36 +02008140
Ronald Cron977c2472020-10-13 08:32:21 +02008141 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05308142 * storage ( thus not in the case of generating a key in a secure element
8143 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
8144 * buffer to hold the generated key material. */
Valerio Setti70fa89c2024-08-14 05:12:45 +02008145 if (slot->key.bytes == 0) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008146 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008148 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008149 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 }
Ronald Cron3772afe2021-02-08 16:10:05 +01008153
8154 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008155 attributes->type,
8156 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02008158 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 attributes, &key_buffer_size);
8160 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 }
Ronald Cron977c2472020-10-13 08:32:21 +02008163 }
Ronald Cron977c2472020-10-13 08:32:21 +02008164
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
8166 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02008167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 }
Ronald Cron977c2472020-10-13 08:32:21 +02008169 }
8170
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008172 custom,
8173 custom_data, custom_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01008174 slot->key.data, slot->key.bytes,
8175 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01008176 if (status != PSA_SUCCESS) {
8177 psa_remove_key_data_from_memory(slot);
8178 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02008179
Gilles Peskine11792082019-08-06 18:36:36 +02008180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 if (status == PSA_SUCCESS) {
8182 status = psa_finish_key_creation(slot, driver, key);
8183 }
8184 if (status != PSA_SUCCESS) {
8185 psa_fail_key_creation(slot, driver);
8186 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02008187
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008189}
8190
Gilles Peskinef36d7852024-06-06 21:11:44 +02008191psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
8192 const psa_key_production_parameters_t *params,
8193 size_t params_data_length,
8194 mbedtls_svc_key_id_t *key)
8195{
8196 return psa_generate_key_custom(
8197 attributes,
8198 (const psa_custom_key_parameters_t *) params,
8199 params->data, params_data_length,
8200 key);
8201}
8202
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008203psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
8204 mbedtls_svc_key_id_t *key)
8205{
Gilles Peskinef36d7852024-06-06 21:11:44 +02008206 return psa_generate_key_custom(attributes,
8207 &default_custom_production,
8208 NULL, 0,
8209 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008210}
8211
Gilles Peskinee59236f2018-01-27 23:32:46 +01008212/****************************************************************/
8213/* Module setup */
8214/****************************************************************/
8215
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008216#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01008217psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 void (* entropy_init)(mbedtls_entropy_context *ctx),
8219 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01008220{
Paul Elliott8e151532024-02-23 17:35:29 +00008221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8222
8223#if defined(MBEDTLS_THREADING_C)
8224 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8225#endif /* defined(MBEDTLS_THREADING_C) */
8226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00008228 status = PSA_ERROR_BAD_STATE;
8229 } else {
8230 global_data.rng.entropy_init = entropy_init;
8231 global_data.rng.entropy_free = entropy_free;
8232 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 }
Paul Elliott8e151532024-02-23 17:35:29 +00008234
8235#if defined(MBEDTLS_THREADING_C)
8236 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8237#endif /* defined(MBEDTLS_THREADING_C) */
8238
8239 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01008240}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008241#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01008242
Gilles Peskine449bd832023-01-11 14:50:10 +01008243void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008244{
Paul Elliott47cee8e2024-03-08 21:38:02 +00008245
Paul Elliott600472b2024-02-23 17:26:58 +00008246#if defined(MBEDTLS_THREADING_C)
8247 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8248#endif /* defined(MBEDTLS_THREADING_C) */
8249
Paul Elliott47cee8e2024-03-08 21:38:02 +00008250 /* Nothing to do to free transaction. */
8251 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
8252 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01008253 }
Paul Elliott600472b2024-02-23 17:26:58 +00008254
Paul Elliott47cee8e2024-03-08 21:38:02 +00008255 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
8256 psa_wipe_all_key_slots();
8257 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8258 }
Ronald Cron9ba76912021-04-10 16:57:30 +02008259
Paul Elliott600472b2024-02-23 17:26:58 +00008260#if defined(MBEDTLS_THREADING_C)
8261 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8262#endif /* defined(MBEDTLS_THREADING_C) */
8263
Paul Elliott47cee8e2024-03-08 21:38:02 +00008264#if defined(MBEDTLS_THREADING_C)
8265 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8266#endif /* defined(MBEDTLS_THREADING_C) */
8267
Gilles Peskinec6b69072018-11-20 21:42:52 +01008268 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01008269 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008270 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008271 global_data.rng_state = RNG_NOT_INITIALIZED;
8272 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
8273
8274#if defined(MBEDTLS_THREADING_C)
8275 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8276#endif /* defined(MBEDTLS_THREADING_C) */
8277
8278#if defined(MBEDTLS_THREADING_C)
8279 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8280#endif /* defined(MBEDTLS_THREADING_C) */
Ronald Cron9ba76912021-04-10 16:57:30 +02008281
8282 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00008283 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
8284 psa_driver_wrapper_free();
8285 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8286 }
8287
8288#if defined(MBEDTLS_THREADING_C)
8289 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8290#endif /* defined(MBEDTLS_THREADING_C) */
8291
Gilles Peskinee59236f2018-01-27 23:32:46 +01008292}
8293
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008294#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8295/** Recover a transaction that was interrupted by a power failure.
8296 *
8297 * This function is called during initialization, before psa_crypto_init()
8298 * returns. If this function returns a failure status, the initialization
8299 * fails.
8300 */
8301static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008303{
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008305 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8306 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 /* TODO - fall through to the failure case until this
8308 * is implemented.
8309 * https://github.com/ARMmbed/mbed-crypto/issues/218
8310 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008311 default:
8312 /* We found an unsupported transaction in the storage.
8313 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008315 }
8316}
8317#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8318
Paul Elliott47cee8e2024-03-08 21:38:02 +00008319static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
8320{
8321 psa_status_t status = PSA_SUCCESS;
8322 uint8_t driver_wrappers_initialized = 0;
8323
8324 switch (subsystem) {
8325 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
8326
8327#if defined(MBEDTLS_THREADING_C)
8328 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8329#endif /* defined(MBEDTLS_THREADING_C) */
8330
8331 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
8332 /* Init drivers */
8333 status = psa_driver_wrapper_init();
8334
8335 /* Drivers need shutdown regardless of startup errors. */
8336 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8337
8338
8339 }
8340#if defined(MBEDTLS_THREADING_C)
8341 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8342 &mbedtls_threading_psa_globaldata_mutex));
8343#endif /* defined(MBEDTLS_THREADING_C) */
8344
8345 break;
8346
8347 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
8348
8349#if defined(MBEDTLS_THREADING_C)
8350 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8351#endif /* defined(MBEDTLS_THREADING_C) */
8352
8353 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
8354 status = psa_initialize_key_slots();
8355
8356 /* Need to wipe keys even if initialization fails. */
8357 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8358
8359 }
8360#if defined(MBEDTLS_THREADING_C)
8361 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8362 &mbedtls_threading_psa_globaldata_mutex));
8363#endif /* defined(MBEDTLS_THREADING_C) */
8364
8365 break;
8366
8367 case PSA_CRYPTO_SUBSYSTEM_RNG:
8368
8369#if defined(MBEDTLS_THREADING_C)
8370 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8371#endif /* defined(MBEDTLS_THREADING_C) */
8372
8373 driver_wrappers_initialized =
8374 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
8375
8376#if defined(MBEDTLS_THREADING_C)
8377 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8378 &mbedtls_threading_psa_globaldata_mutex));
8379#endif /* defined(MBEDTLS_THREADING_C) */
8380
8381 /* Need to use separate mutex here, as initialisation can require
8382 * testing of init flags, which requires locking the global data
8383 * mutex. */
8384#if defined(MBEDTLS_THREADING_C)
8385 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
8386#endif /* defined(MBEDTLS_THREADING_C) */
8387
8388 /* Initialize and seed the random generator. */
8389 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
8390 mbedtls_psa_random_init(&global_data.rng);
8391 global_data.rng_state = RNG_INITIALIZED;
8392
8393 status = mbedtls_psa_random_seed(&global_data.rng);
8394 if (status == PSA_SUCCESS) {
8395 global_data.rng_state = RNG_SEEDED;
8396 }
8397 }
8398
8399#if defined(MBEDTLS_THREADING_C)
8400 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8401 &mbedtls_threading_psa_rngdata_mutex));
8402#endif /* defined(MBEDTLS_THREADING_C) */
8403
Paul Elliott47cee8e2024-03-08 21:38:02 +00008404 break;
8405
8406 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
8407
8408#if defined(MBEDTLS_THREADING_C)
8409 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8410#endif /* defined(MBEDTLS_THREADING_C) */
8411
8412 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
8413#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8414 status = psa_crypto_load_transaction();
8415 if (status == PSA_SUCCESS) {
8416 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8417 if (status == PSA_SUCCESS) {
8418 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8419 }
8420 status = psa_crypto_stop_transaction();
8421 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
8422 /* There's no transaction to complete. It's all good. */
8423 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8424 status = PSA_SUCCESS;
8425 }
8426#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8427 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8428 status = PSA_SUCCESS;
8429#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8430 }
8431
8432#if defined(MBEDTLS_THREADING_C)
8433 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8434 &mbedtls_threading_psa_globaldata_mutex));
8435#endif /* defined(MBEDTLS_THREADING_C) */
8436
8437 break;
8438
8439 default:
8440 status = PSA_ERROR_CORRUPTION_DETECTED;
8441 }
8442
8443 /* Exit label only required when using threading macros. */
8444#if defined(MBEDTLS_THREADING_C)
8445exit:
8446#endif /* defined(MBEDTLS_THREADING_C) */
8447
8448 return status;
8449}
8450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008452{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008453 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008454
Paul Elliott47cee8e2024-03-08 21:38:02 +00008455 /* Double initialization is explicitly allowed. Early out if everything is
8456 * done. */
8457 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 return PSA_SUCCESS;
8459 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008460
Paul Elliott47cee8e2024-03-08 21:38:02 +00008461 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01008462 if (status != PSA_SUCCESS) {
8463 goto exit;
8464 }
8465
Paul Elliott47cee8e2024-03-08 21:38:02 +00008466 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008468 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008470
Paul Elliott47cee8e2024-03-08 21:38:02 +00008471 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
8472 if (status != PSA_SUCCESS) {
8473 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02008474 }
Gilles Peskinefc762652019-07-22 19:30:34 +02008475
Paul Elliott47cee8e2024-03-08 21:38:02 +00008476 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01008477
8478exit:
Paul Elliott600472b2024-02-23 17:26:58 +00008479
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 if (status != PSA_SUCCESS) {
8481 mbedtls_psa_crypto_free();
8482 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008483
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008485}
8486
Yanray Wangffc3c482023-07-11 12:01:04 +08008487#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008488psa_status_t psa_crypto_driver_pake_get_password_len(
8489 const psa_crypto_driver_pake_inputs_t *inputs,
8490 size_t *password_len)
8491{
8492 if (inputs->password_len == 0) {
8493 return PSA_ERROR_BAD_STATE;
8494 }
8495
8496 *password_len = inputs->password_len;
8497
8498 return PSA_SUCCESS;
8499}
8500
8501psa_status_t psa_crypto_driver_pake_get_password(
8502 const psa_crypto_driver_pake_inputs_t *inputs,
8503 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8504{
8505 if (inputs->password_len == 0) {
8506 return PSA_ERROR_BAD_STATE;
8507 }
8508
8509 if (buffer_size < inputs->password_len) {
8510 return PSA_ERROR_BUFFER_TOO_SMALL;
8511 }
8512
8513 memcpy(buffer, inputs->password, inputs->password_len);
8514 *buffer_length = inputs->password_len;
8515
8516 return PSA_SUCCESS;
8517}
8518
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008519psa_status_t psa_crypto_driver_pake_get_user_len(
8520 const psa_crypto_driver_pake_inputs_t *inputs,
8521 size_t *user_len)
8522{
8523 if (inputs->user_len == 0) {
8524 return PSA_ERROR_BAD_STATE;
8525 }
8526
8527 *user_len = inputs->user_len;
8528
8529 return PSA_SUCCESS;
8530}
8531
8532psa_status_t psa_crypto_driver_pake_get_user(
8533 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008534 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008535{
8536 if (inputs->user_len == 0) {
8537 return PSA_ERROR_BAD_STATE;
8538 }
8539
Przemek Stekielc0e62502023-03-14 11:49:36 +01008540 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008541 return PSA_ERROR_BUFFER_TOO_SMALL;
8542 }
8543
Przemek Stekielc0e62502023-03-14 11:49:36 +01008544 memcpy(user_id, inputs->user, inputs->user_len);
8545 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008546
8547 return PSA_SUCCESS;
8548}
8549
8550psa_status_t psa_crypto_driver_pake_get_peer_len(
8551 const psa_crypto_driver_pake_inputs_t *inputs,
8552 size_t *peer_len)
8553{
8554 if (inputs->peer_len == 0) {
8555 return PSA_ERROR_BAD_STATE;
8556 }
8557
8558 *peer_len = inputs->peer_len;
8559
8560 return PSA_SUCCESS;
8561}
8562
8563psa_status_t psa_crypto_driver_pake_get_peer(
8564 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008565 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008566{
8567 if (inputs->peer_len == 0) {
8568 return PSA_ERROR_BAD_STATE;
8569 }
8570
Przemek Stekielc0e62502023-03-14 11:49:36 +01008571 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008572 return PSA_ERROR_BUFFER_TOO_SMALL;
8573 }
8574
Przemek Stekielc0e62502023-03-14 11:49:36 +01008575 memcpy(peer_id, inputs->peer, inputs->peer_len);
8576 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008577
8578 return PSA_SUCCESS;
8579}
8580
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008581psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8582 const psa_crypto_driver_pake_inputs_t *inputs,
8583 psa_pake_cipher_suite_t *cipher_suite)
8584{
8585 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8586 return PSA_ERROR_BAD_STATE;
8587 }
8588
8589 *cipher_suite = inputs->cipher_suite;
8590
8591 return PSA_SUCCESS;
8592}
8593
Neil Armstronga7d08c32022-06-01 18:21:20 +02008594psa_status_t psa_pake_setup(
8595 psa_pake_operation_t *operation,
8596 const psa_pake_cipher_suite_t *cipher_suite)
8597{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008599
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008600 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008601 status = PSA_ERROR_BAD_STATE;
8602 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008603 }
8604
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008605 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008606 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008607 status = PSA_ERROR_INVALID_ARGUMENT;
8608 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008609 }
8610
Przemek Stekiel51eac532022-12-07 11:04:51 +01008611 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8612
Przemek Stekiele12ed362022-12-21 12:54:46 +01008613 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008614 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8615 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008616 operation->data.inputs.cipher_suite = *cipher_suite;
8617
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008618#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008619 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008620 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008621 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008622
David Horstmann16f01512023-06-14 17:21:07 +01008623 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008624 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008625 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008626#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008627 {
8628 status = PSA_ERROR_NOT_SUPPORTED;
8629 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008630 }
8631
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008632 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8633
Przemek Stekiel51eac532022-12-07 11:04:51 +01008634 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008635exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008636 psa_pake_abort(operation);
8637 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008638}
8639
8640psa_status_t psa_pake_set_password_key(
8641 psa_pake_operation_t *operation,
8642 mbedtls_svc_key_id_t password)
8643{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008644 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008645 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8646 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008647 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008648
Przemek Stekiel51eac532022-12-07 11:04:51 +01008649 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008650 status = PSA_ERROR_BAD_STATE;
8651 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008652 }
8653
Przemek Stekiel6c764412022-11-22 14:05:12 +01008654 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8655 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008656 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008657 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008658 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008659 }
8660
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008661 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008662
Przemek Stekiel51eac532022-12-07 11:04:51 +01008663 if (type != PSA_KEY_TYPE_PASSWORD &&
8664 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8665 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008666 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008667 }
8668
Przemek Stekiel51eac532022-12-07 11:04:51 +01008669 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8670 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008671 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008672 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008673 }
8674
8675 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8676 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008677 operation->data.inputs.attributes = slot->attr;
8678
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008679exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008680 if (status != PSA_SUCCESS) {
8681 psa_pake_abort(operation);
8682 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008683 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008684 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008685}
8686
8687psa_status_t psa_pake_set_user(
8688 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008689 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008690 size_t user_id_len)
8691{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008692 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008693 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008694
8695 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008696 status = PSA_ERROR_BAD_STATE;
8697 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008698 }
8699
Przemek Stekiel51eac532022-12-07 11:04:51 +01008700 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008701 status = PSA_ERROR_INVALID_ARGUMENT;
8702 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008703 }
8704
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008705 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008706 status = PSA_ERROR_BAD_STATE;
8707 goto exit;
8708 }
8709
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008710 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8711 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008712 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8713 goto exit;
8714 }
8715
David Horstmann4f534ae2024-01-22 17:35:59 +00008716 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8717
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008718 memcpy(operation->data.inputs.user, user_id, user_id_len);
8719 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008720
David Horstmann4f534ae2024-01-22 17:35:59 +00008721 status = PSA_SUCCESS;
8722
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008723exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008724 LOCAL_INPUT_FREE(user_id_external, user_id);
8725 if (status != PSA_SUCCESS) {
8726 psa_pake_abort(operation);
8727 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008728 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008729}
8730
8731psa_status_t psa_pake_set_peer(
8732 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008733 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008734 size_t peer_id_len)
8735{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008737 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008738
8739 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008740 status = PSA_ERROR_BAD_STATE;
8741 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008742 }
8743
Przemek Stekiel51eac532022-12-07 11:04:51 +01008744 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008745 status = PSA_ERROR_INVALID_ARGUMENT;
8746 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008747 }
8748
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008749 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008750 status = PSA_ERROR_BAD_STATE;
8751 goto exit;
8752 }
8753
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008754 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8755 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008756 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8757 goto exit;
8758 }
8759
David Horstmann4f534ae2024-01-22 17:35:59 +00008760 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8761
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008762 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8763 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008764
David Horstmann4f534ae2024-01-22 17:35:59 +00008765 status = PSA_SUCCESS;
8766
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008767exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008768 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8769 if (status != PSA_SUCCESS) {
8770 psa_pake_abort(operation);
8771 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008772 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008773}
8774
8775psa_status_t psa_pake_set_role(
8776 psa_pake_operation_t *operation,
8777 psa_pake_role_t role)
8778{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008780
Przemek Stekiel51eac532022-12-07 11:04:51 +01008781 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008782 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008783 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008784 }
8785
Przemek Stekiel09104b82023-03-06 14:11:51 +01008786 switch (operation->alg) {
8787#if defined(PSA_WANT_ALG_JPAKE)
8788 case PSA_ALG_JPAKE:
8789 if (role == PSA_PAKE_ROLE_NONE) {
8790 return PSA_SUCCESS;
8791 }
8792 status = PSA_ERROR_INVALID_ARGUMENT;
8793 break;
8794#endif
8795 default:
8796 (void) role;
8797 status = PSA_ERROR_NOT_SUPPORTED;
8798 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008799 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008800exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008801 psa_pake_abort(operation);
8802 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008803}
8804
David Horstmanne7f21e62023-05-12 18:17:21 +01008805/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008806#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008807static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008808 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008809{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008810 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008811 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008812 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008813
David Horstmann024e5c52023-06-14 15:48:21 +01008814 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008815 is_x1 = (stage->outputs < 1);
8816 } else {
8817 is_x1 = (stage->inputs < 1);
8818 }
8819
David Horstmann74a3d8c2023-06-14 18:28:19 +01008820 key_share_step = is_x1 ?
8821 PSA_JPAKE_X1_STEP_KEY_SHARE :
8822 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008823 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008824 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8825 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8826 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8827 } else {
8828 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008829 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008830 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008831}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008832#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008833
Przemek Stekiel2797d372022-12-22 11:19:22 +01008834static psa_status_t psa_pake_complete_inputs(
8835 psa_pake_operation_t *operation)
8836{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008838 /* Create copy of the inputs on stack as inputs share memory
8839 with the driver context which will be setup by the driver. */
8840 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008841
Przemek Stekielfde11282023-03-13 16:06:09 +01008842 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008843 return PSA_ERROR_BAD_STATE;
8844 }
8845
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008846 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008847 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8848 return PSA_ERROR_BAD_STATE;
8849 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008850 }
8851
Przemek Stekiel18620a32023-01-17 16:34:52 +01008852 /* Clear driver context */
8853 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8854
8855 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008856
8857 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008858 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008859
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008860 /* User and peer are translated to role. */
8861 mbedtls_free(inputs.user);
8862 mbedtls_free(inputs.peer);
8863
Przemek Stekiel2797d372022-12-22 11:19:22 +01008864 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008865#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008866 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008867 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008868 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008869#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008870 {
8871 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008872 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008873 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008874 return status;
8875}
8876
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008877#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008878static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008879 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008880 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008881 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008882{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008883 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8884 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8885 step != PSA_PAKE_STEP_ZK_PROOF) {
8886 return PSA_ERROR_INVALID_ARGUMENT;
8887 }
8888
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008889 psa_jpake_computation_stage_t *computation_stage =
8890 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008891
David Horstmann5da95602023-06-08 15:37:12 +01008892 if (computation_stage->round != PSA_JPAKE_FIRST &&
8893 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008894 return PSA_ERROR_BAD_STATE;
8895 }
8896
David Horstmanne7f21e62023-05-12 18:17:21 +01008897 /* Check that the step we are given is the one we were expecting */
8898 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008899 return PSA_ERROR_BAD_STATE;
8900 }
8901
David Horstmanne7f21e62023-05-12 18:17:21 +01008902 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8903 computation_stage->inputs == 0 &&
8904 computation_stage->outputs == 0) {
8905 /* Start of the round, so function decides whether we are inputting
8906 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008907 computation_stage->io_mode = io_mode;
8908 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008909 /* Middle of the round so the mode we are in must match the function
8910 * called by the user */
8911 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008912 }
8913
8914 return PSA_SUCCESS;
8915}
8916
David Horstmanne7f21e62023-05-12 18:17:21 +01008917static psa_status_t psa_jpake_epilogue(
8918 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008919 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008920{
David Horstmanne7f21e62023-05-12 18:17:21 +01008921 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008922 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008923
David Horstmanne7f21e62023-05-12 18:17:21 +01008924 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8925 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008926 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008927 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008928 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008929 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008930 }
8931 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008932 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008933 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008934 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008935 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008936 }
8937 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008938 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8939 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008940 /* End of a round, move to the next round */
8941 stage->inputs = 0;
8942 stage->outputs = 0;
8943 stage->round++;
8944 }
8945 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008946 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008947 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008948 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008949 return PSA_SUCCESS;
8950}
David Horstmanne7f21e62023-05-12 18:17:21 +01008951
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008952#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008953
Neil Armstronga7d08c32022-06-01 18:21:20 +02008954psa_status_t psa_pake_output(
8955 psa_pake_operation_t *operation,
8956 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008957 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008958 size_t output_size,
8959 size_t *output_length)
8960{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008962 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008963 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008964 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008965
8966 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008967 status = psa_pake_complete_inputs(operation);
8968 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008969 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008970 }
8971 }
8972
8973 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008974 status = PSA_ERROR_BAD_STATE;
8975 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008976 }
8977
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008978 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008979 status = PSA_ERROR_INVALID_ARGUMENT;
8980 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008981 }
8982
Przemek Stekiele12ed362022-12-21 12:54:46 +01008983 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008984#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008985 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008986 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008987 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008988 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008989 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008990 driver_step = convert_jpake_computation_stage_to_driver_step(
8991 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008992 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008993#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008994 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008995 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008996 status = PSA_ERROR_NOT_SUPPORTED;
8997 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008998 }
8999
David Horstmannc75639d2024-01-22 17:56:39 +00009000 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
9001
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009002 status = psa_driver_wrapper_pake_output(operation, driver_step,
9003 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009004
9005 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009006 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009007 }
9008
9009 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009010#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009011 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009012 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009013 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009014 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009015 }
9016 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009017#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009018 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009019 status = PSA_ERROR_NOT_SUPPORTED;
9020 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009021 }
9022
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009023exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009024 LOCAL_OUTPUT_FREE(output_external, output);
9025 if (status != PSA_SUCCESS) {
9026 psa_pake_abort(operation);
9027 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009028 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009029}
9030
9031psa_status_t psa_pake_input(
9032 psa_pake_operation_t *operation,
9033 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00009034 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02009035 size_t input_length)
9036{
Przemek Stekiel51eac532022-12-07 11:04:51 +01009037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009038 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009039 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
9040 operation->primitive,
9041 step);
David Horstmannc75639d2024-01-22 17:56:39 +00009042 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01009043
9044 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01009045 status = psa_pake_complete_inputs(operation);
9046 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009047 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009048 }
9049 }
9050
9051 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009052 status = PSA_ERROR_BAD_STATE;
9053 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01009054 }
9055
Przemek Stekiel256c75d2023-03-23 14:09:34 +01009056 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009057 status = PSA_ERROR_INVALID_ARGUMENT;
9058 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009059 }
9060
Przemek Stekiele12ed362022-12-21 12:54:46 +01009061 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009062#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009063 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009064 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009065 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009066 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009067 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009068 driver_step = convert_jpake_computation_stage_to_driver_step(
9069 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009070 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009071#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009072 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01009073 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009074 status = PSA_ERROR_NOT_SUPPORTED;
9075 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009076 }
9077
David Horstmannc75639d2024-01-22 17:56:39 +00009078 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01009079 status = psa_driver_wrapper_pake_input(operation, driver_step,
9080 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009081
9082 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009083 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009084 }
9085
9086 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009087#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009088 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01009089 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01009090 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009091 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009092 }
9093 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009094#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01009095 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009096 status = PSA_ERROR_NOT_SUPPORTED;
9097 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009098 }
9099
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009100exit:
David Horstmannc75639d2024-01-22 17:56:39 +00009101 LOCAL_INPUT_FREE(input_external, input);
9102 if (status != PSA_SUCCESS) {
9103 psa_pake_abort(operation);
9104 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009105 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009106}
9107
9108psa_status_t psa_pake_get_implicit_key(
9109 psa_pake_operation_t *operation,
9110 psa_key_derivation_operation_t *output)
9111{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009113 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01009114 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01009115 size_t shared_key_len = 0;
9116
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01009117 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009118 status = PSA_ERROR_BAD_STATE;
9119 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02009120 }
9121
Przemek Stekiel4aa99402023-02-27 13:00:57 +01009122#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01009123 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009124 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01009125 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01009126 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009127 status = PSA_ERROR_BAD_STATE;
9128 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009129 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01009130 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009131#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01009132 {
9133 status = PSA_ERROR_NOT_SUPPORTED;
9134 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009135 }
9136
Przemek Stekiel0c781802022-11-29 14:53:13 +01009137 status = psa_driver_wrapper_pake_get_implicit_key(operation,
9138 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01009139 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01009140 &shared_key_len);
9141
9142 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009143 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01009144 }
9145
9146 status = psa_key_derivation_input_bytes(output,
9147 PSA_KEY_DERIVATION_INPUT_SECRET,
9148 shared_key,
9149 shared_key_len);
9150
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009151 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009152exit:
9153 abort_status = psa_pake_abort(operation);
9154 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009155}
9156
9157psa_status_t psa_pake_abort(
9158 psa_pake_operation_t *operation)
9159{
Przemek Stekield69dca92023-01-31 19:59:20 +01009160 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009161
Przemek Stekield69dca92023-01-31 19:59:20 +01009162 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009163 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02009164 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009165
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009166 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
9167 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009168 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01009169 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009170 }
9171 if (operation->data.inputs.user != NULL) {
9172 mbedtls_free(operation->data.inputs.user);
9173 }
9174 if (operation->data.inputs.peer != NULL) {
9175 mbedtls_free(operation->data.inputs.peer);
9176 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009177 }
Przemek Stekield69dca92023-01-31 19:59:20 +01009178 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009179
Przemek Stekield69dca92023-01-31 19:59:20 +01009180 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009181}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01009182#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02009183
David Horstmann00d7a042023-12-07 18:39:17 +00009184/* Memory copying test hooks. These are called before input copy, after input
9185 * copy, before output copy and after output copy, respectively.
9186 * They are used by memory-poisoning tests to temporarily unpoison buffers
9187 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00009188#if defined(MBEDTLS_TEST_HOOKS)
9189void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9190void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9191void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9192void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9193#endif
David Horstmannfde97392023-10-30 20:29:43 +00009194
David Horstmann1b7279a2023-11-15 15:18:30 +00009195/** Copy from an input buffer to a local copy.
9196 *
9197 * \param[in] input Pointer to input buffer.
9198 * \param[in] input_len Length of the input buffer.
9199 * \param[out] input_copy Pointer to a local copy in which to store the input data.
9200 * \param[out] input_copy_len Length of the local copy buffer.
9201 * \return #PSA_SUCCESS, if the buffer was successfully
9202 * copied.
9203 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
9204 * copy is too small to hold contents of the
9205 * input buffer.
9206 */
9207MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00009208psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
9209 uint8_t *input_copy, size_t input_copy_len)
9210{
9211 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00009212 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00009213 }
9214
David Horstmann372b8bb2023-11-24 16:21:04 +00009215#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009216 if (psa_input_pre_copy_hook != NULL) {
9217 psa_input_pre_copy_hook(input, input_len);
9218 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009219#endif
9220
David Horstmann777e7412023-11-15 17:33:47 +00009221 if (input_len > 0) {
9222 memcpy(input_copy, input, input_len);
9223 }
David Horstmannfde97392023-10-30 20:29:43 +00009224
David Horstmann372b8bb2023-11-24 16:21:04 +00009225#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009226 if (psa_input_post_copy_hook != NULL) {
9227 psa_input_post_copy_hook(input, input_len);
9228 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009229#endif
9230
David Horstmannfde97392023-10-30 20:29:43 +00009231 return PSA_SUCCESS;
9232}
9233
David Horstmann1b7279a2023-11-15 15:18:30 +00009234/** Copy from a local output buffer into a user-supplied one.
9235 *
9236 * \param[in] output_copy Pointer to a local buffer containing the output.
9237 * \param[in] output_copy_len Length of the local buffer.
9238 * \param[out] output Pointer to user-supplied output buffer.
9239 * \param[out] output_len Length of the user-supplied output buffer.
9240 * \return #PSA_SUCCESS, if the buffer was successfully
9241 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00009242 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00009243 * user-supplied output buffer is too small to
9244 * hold the contents of the local buffer.
9245 */
9246MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00009247psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
9248 uint8_t *output, size_t output_len)
9249{
9250 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00009251 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00009252 }
David Horstmann777e7412023-11-15 17:33:47 +00009253
David Horstmann372b8bb2023-11-24 16:21:04 +00009254#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009255 if (psa_output_pre_copy_hook != NULL) {
9256 psa_output_pre_copy_hook(output, output_len);
9257 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009258#endif
9259
David Horstmann777e7412023-11-15 17:33:47 +00009260 if (output_copy_len > 0) {
9261 memcpy(output, output_copy, output_copy_len);
9262 }
9263
David Horstmann372b8bb2023-11-24 16:21:04 +00009264#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009265 if (psa_output_post_copy_hook != NULL) {
9266 psa_output_post_copy_hook(output, output_len);
9267 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009268#endif
9269
David Horstmann8978f5c2023-10-30 20:37:12 +00009270 return PSA_SUCCESS;
9271}
9272
David Horstmannf1734052023-11-20 17:15:32 +00009273psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
9274 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00009275{
9276 psa_status_t status;
9277
David Horstmann9db14482023-11-23 15:50:37 +00009278 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00009279
David Horstmannc5cc1c32023-11-15 18:11:26 +00009280 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00009281 return PSA_SUCCESS;
9282 }
9283
David Horstmannf1734052023-11-20 17:15:32 +00009284 local_input->buffer = mbedtls_calloc(input_len, 1);
9285 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00009286 /* Since we dealt with the zero-length case above, we know that
9287 * a NULL return value means a failure of allocation. */
9288 return PSA_ERROR_INSUFFICIENT_MEMORY;
9289 }
David Horstmannf1734052023-11-20 17:15:32 +00009290 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00009291
David Horstmannf1734052023-11-20 17:15:32 +00009292 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00009293
9294 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00009295 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00009296 if (status != PSA_SUCCESS) {
9297 goto error;
9298 }
9299
9300 return PSA_SUCCESS;
9301
9302error:
Gilles Peskinee847afd2025-03-06 12:41:39 +01009303 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009304 local_input->buffer = NULL;
9305 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00009306 return status;
9307}
9308
David Horstmannf1734052023-11-20 17:15:32 +00009309void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00009310{
Gilles Peskinee847afd2025-03-06 12:41:39 +01009311 mbedtls_zeroize_and_free(local_input->buffer, local_input->length);
David Horstmannf1734052023-11-20 17:15:32 +00009312 local_input->buffer = NULL;
9313 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00009314}
9315
David Horstmann89875a42023-11-20 12:54:09 +00009316psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
9317 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00009318{
David Horstmann9db14482023-11-23 15:50:37 +00009319 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00009320
David Horstmannc5cc1c32023-11-15 18:11:26 +00009321 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009322 return PSA_SUCCESS;
9323 }
David Horstmann89875a42023-11-20 12:54:09 +00009324 local_output->buffer = mbedtls_calloc(output_len, 1);
9325 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009326 /* Since we dealt with the zero-length case above, we know that
9327 * a NULL return value means a failure of allocation. */
9328 return PSA_ERROR_INSUFFICIENT_MEMORY;
9329 }
David Horstmann89875a42023-11-20 12:54:09 +00009330 local_output->length = output_len;
9331 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00009332
9333 return PSA_SUCCESS;
9334}
9335
David Horstmann89875a42023-11-20 12:54:09 +00009336psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00009337{
David Horstmannc335a4e2023-11-15 15:57:32 +00009338 psa_status_t status;
9339
David Horstmann89875a42023-11-20 12:54:09 +00009340 if (local_output->buffer == NULL) {
9341 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009342 return PSA_SUCCESS;
9343 }
David Horstmann89875a42023-11-20 12:54:09 +00009344 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00009345 /* We have an internal copy but nothing to copy back to. */
9346 return PSA_ERROR_CORRUPTION_DETECTED;
9347 }
9348
David Horstmann89875a42023-11-20 12:54:09 +00009349 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
9350 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00009351 if (status != PSA_SUCCESS) {
9352 return status;
9353 }
David Horstmann9467ea32023-11-08 17:44:18 +00009354
Gilles Peskinee847afd2025-03-06 12:41:39 +01009355 mbedtls_zeroize_and_free(local_output->buffer, local_output->length);
David Horstmann89875a42023-11-20 12:54:09 +00009356 local_output->buffer = NULL;
9357 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009358
9359 return PSA_SUCCESS;
9360}
9361
Gilles Peskinee59236f2018-01-27 23:32:46 +01009362#endif /* MBEDTLS_PSA_CRYPTO_C */