blob: c4f41db10b60b26972394d633327d13829ee6c6f [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{
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (slot->key.data != NULL) {
709 return PSA_ERROR_ALREADY_EXISTS;
710 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 slot->key.data = mbedtls_calloc(1, buffer_length);
713 if (slot->key.data == NULL) {
714 return PSA_ERROR_INSUFFICIENT_MEMORY;
715 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200716
Ronald Cronea0f8a62020-11-25 17:52:23 +0100717 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200719}
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
722 const uint8_t *data,
723 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200724{
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 psa_status_t status = psa_allocate_buffer_to_slot(slot,
726 data_length);
727 if (status != PSA_SUCCESS) {
728 return status;
729 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 memcpy(slot->key.data, data, data_length);
732 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200733}
734
Ronald Crona0fe59f2020-11-29 11:14:53 +0100735psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100736 const psa_key_attributes_t *attributes,
737 const uint8_t *data, size_t data_length,
738 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100742 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200744 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (data_length == 0) {
746 return PSA_ERROR_NOT_SUPPORTED;
747 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (key_type_is_raw_bytes(type)) {
750 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200751
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100752 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 *bits);
754 if (status != PSA_SUCCESS) {
755 return status;
756 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200757
Ronald Crondd04d422020-11-28 15:14:42 +0100758 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100760 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 return PSA_SUCCESS;
764 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200765#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200766 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100767 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200768 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100769 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100770 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200771 return mbedtls_psa_ffdh_import_key(attributes,
772 data, data_length,
773 key_buffer, key_buffer_size,
774 key_buffer_length,
775 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100776 }
Valerio Settia55f0422023-07-10 15:34:41 +0200777#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200778 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200779#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
781 if (PSA_KEY_TYPE_IS_ECC(type)) {
782 return mbedtls_psa_ecp_import_key(attributes,
783 data, data_length,
784 key_buffer, key_buffer_size,
785 key_buffer_length,
786 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100787 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200788#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800789 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200790#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
791 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
793 if (PSA_KEY_TYPE_IS_RSA(type)) {
794 return mbedtls_psa_rsa_import_key(attributes,
795 data, data_length,
796 key_buffer, key_buffer_size,
797 key_buffer_length,
798 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200799 }
Valerio Settife478902023-07-25 12:27:19 +0200800#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
801 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800802 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100803 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Calculate the intersection of two algorithm usage policies.
809 *
810 * Return 0 (which allows no operation) on incompatibility.
811 */
812static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100813 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100814 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100816{
Gilles Peskine549ea862019-05-22 11:45:59 +0200817 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (alg1 == alg2) {
819 return alg1;
820 }
Steven Cooreman03488022021-02-18 12:07:20 +0100821 /* If the policies are from the same hash-and-sign family, check
822 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
824 PSA_ALG_IS_SIGN_HASH(alg2) &&
825 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
826 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
827 return alg2;
828 }
829 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
830 return alg1;
831 }
Steven Cooreman03488022021-02-18 12:07:20 +0100832 }
833 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100834 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100835 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
837 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
838 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
839 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
840 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100841 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100842
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100843 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
845 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
846 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
847 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100848 }
849 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
851 (alg1_len <= alg2_len)) {
852 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100853 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
855 (alg2_len <= alg1_len)) {
856 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100857 }
858 }
859 /* If the policies are from the same MAC family, check whether one
860 * of them is a minimum-MAC-length policy. Calculate the most
861 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
863 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
864 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100865 /* Validate the combination of key type and algorithm. Since the base
866 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
868 return 0;
869 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100870
Steven Cooreman31a876d2021-03-03 20:47:40 +0100871 /* Get the (exact or at-least) output lengths for both sides of the
872 * requested intersection. None of the currently supported algorithms
873 * have an output length dependent on the actual key size, so setting it
874 * to a bogus value of 0 is currently OK.
875 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100876 * Note that for at-least-this-length wildcard algorithms, the output
877 * length is set to the shortest allowed length, which allows us to
878 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
880 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100881 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100882
Steven Cooremana1d83222021-02-25 10:20:29 +0100883 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
885 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
886 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100887 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100888
889 /* If only one is an at-least-this-length policy, the intersection would
890 * be the other (fixed-length) policy as long as said fixed length is
891 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
893 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100894 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
896 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100898
Steven Cooremancd640932021-03-08 12:00:27 +0100899 /* If none of them are wildcards, check whether they define the same tag
900 * length. This is still possible here when one is default-length and
901 * the other specific-length. Ensure to always return the
902 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (alg1_len == alg2_len) {
904 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
905 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100906 }
907 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100909}
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911static int psa_key_algorithm_permits(psa_key_type_t key_type,
912 psa_algorithm_t policy_alg,
913 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200914{
Gilles Peskine549ea862019-05-22 11:45:59 +0200915 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (requested_alg == policy_alg) {
917 return 1;
918 }
Steven Cooreman03488022021-02-18 12:07:20 +0100919 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
920 * and requested_alg is the same hash-and-sign family with any hash,
921 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
923 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
924 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
925 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100926 }
927 /* If policy_alg is a wildcard AEAD algorithm of the same base as
928 * the requested algorithm, check the requested tag length to be
929 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (PSA_ALG_IS_AEAD(policy_alg) &&
931 PSA_ALG_IS_AEAD(requested_alg) &&
932 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
933 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
934 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
935 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
936 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100937 }
Steven Cooremancd640932021-03-08 12:00:27 +0100938 /* If policy_alg is a MAC algorithm of the same base as the requested
939 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (PSA_ALG_IS_MAC(policy_alg) &&
941 PSA_ALG_IS_MAC(requested_alg) &&
942 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
943 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100944 /* Validate the combination of key type and algorithm. Since the policy
945 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
947 return 0;
948 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100949
Steven Cooreman31a876d2021-03-03 20:47:40 +0100950 /* Get both the requested output length for the algorithm which is to be
951 * verified, and the default output length for the base algorithm.
952 * Note that none of the currently supported algorithms have an output
953 * length dependent on actual key size, so setting it to a bogus value
954 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100955 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100957 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 key_type, 0,
959 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100960
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100961 /* If the policy is default-length, only allow an algorithm with
962 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
964 return requested_output_length == default_output_length;
965 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100966
967 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100968 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
970 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
971 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100972 }
973
Steven Cooremancd640932021-03-08 12:00:27 +0100974 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
975 * check for the requested MAC length to be equal to or longer than the
976 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
978 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
979 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100980 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981 }
Steven Cooremance48e852020-10-05 16:02:45 +0200982 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200983 * a key derivation with that key agreement should also be allowed. This
984 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
986 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
987 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
988 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200989 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100990 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200992}
993
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100994/** Test whether a policy permits an algorithm.
995 *
996 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100997 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100998 * \note This function requires providing the key type for which the policy is
999 * being validated, since some algorithm policy definitions (e.g. MAC)
1000 * have different properties depending on what kind of cipher it is
1001 * combined with.
1002 *
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1004 * allowed by the \p policy.
1005 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1006 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1007 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1010 psa_key_type_t key_type,
1011 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001012{
Steven Cooremand788fab2021-02-25 11:29:17 +01001013 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (alg == 0) {
1015 return PSA_ERROR_INVALID_ARGUMENT;
1016 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001017
Steven Cooreman7e39f052021-02-23 14:18:32 +01001018 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (PSA_ALG_IS_WILDCARD(alg)) {
1020 return PSA_ERROR_INVALID_ARGUMENT;
1021 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1024 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1025 return PSA_SUCCESS;
1026 } else {
1027 return PSA_ERROR_NOT_PERMITTED;
1028 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001029}
1030
Gilles Peskinef603c712019-01-19 13:40:11 +01001031/** Restrict a key policy based on a constraint.
1032 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001033 * \note This function requires providing the key type for which the policy is
1034 * being restricted, since some algorithm policy definitions (e.g. MAC)
1035 * have different properties depending on what kind of cipher it is
1036 * combined with.
1037 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001038 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001039 * \param[in,out] policy The policy to restrict.
1040 * \param[in] constraint The policy constraint to apply.
1041 *
1042 * \retval #PSA_SUCCESS
1043 * \c *policy contains the intersection of the original value of
1044 * \c *policy and \c *constraint.
1045 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001046 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001047 * \c *policy is unchanged.
1048 */
1049static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001050 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001051 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001053{
1054 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1056 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1059 constraint->alg2);
1060 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1061 return PSA_ERROR_INVALID_ARGUMENT;
1062 }
1063 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1064 return PSA_ERROR_INVALID_ARGUMENT;
1065 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001066 policy->usage &= constraint->usage;
1067 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001068 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001070}
1071
Przemek Stekiel061f6942022-11-30 10:51:35 +01001072/** Get the description of a key given its identifier and policy constraints
1073 * and lock it.
1074 *
1075 * The key must have allow all the usage flags set in \p usage. If \p alg is
1076 * nonzero, the key must allow operations with this algorithm. If \p alg is
1077 * zero, the algorithm is not checked.
1078 *
1079 * In case of a persistent key, the function loads the description of the key
1080 * into a key slot if not already done.
1081 *
Ryan Everett098c6652024-01-03 13:03:36 +00001082 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001083 * It is the responsibility of the caller to then unregister
1084 * once they have finished reading the contents of the slot.
1085 * The caller unregisters by calling psa_unregister_read() or
1086 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1087 * if and only if the caller already holds the global key slot mutex
1088 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1089 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +01001090 */
1091static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001092 mbedtls_svc_key_id_t key,
1093 psa_key_slot_t **p_slot,
1094 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001096{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001098 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 status = psa_get_and_lock_key_slot(key, p_slot);
1101 if (status != PSA_SUCCESS) {
1102 return status;
1103 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001104 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001105
1106 /* Enforce that usage policy for the key slot contains all the flags
1107 * required by the usage parameter. There is one exception: public
1108 * keys can always be exported, so we treat public key objects as
1109 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001111 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001115 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001116 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001117 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001118
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001119 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (alg != 0) {
1121 status = psa_key_policy_permits(&slot->attr.policy,
1122 slot->attr.type,
1123 alg);
1124 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001125 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001127 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001130
1131error:
1132 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001133 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001136}
Darryl Green940d72c2018-07-13 13:18:51 +01001137
Ronald Cron5c522922020-11-14 16:35:34 +01001138/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001139 *
1140 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001141 * available, as opposed to a key in a secure element and/or to be used
1142 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001143 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001144 * This is a temporary function that may be used instead of
1145 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1146 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001147 *
Ryan Everett098c6652024-01-03 13:03:36 +00001148 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001149 * It is the responsibility of the caller to then unregister
1150 * once they have finished reading the contents of the slot.
1151 * The caller unregisters by calling psa_unregister_read() or
1152 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1153 * if and only if the caller already holds the global key slot mutex
1154 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1155 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001156 */
Ronald Cron5c522922020-11-14 16:35:34 +01001157static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1158 mbedtls_svc_key_id_t key,
1159 psa_key_slot_t **p_slot,
1160 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001162{
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1164 usage, alg);
1165 if (status != PSA_SUCCESS) {
1166 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001167 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001170 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 *p_slot = NULL;
1172 return PSA_ERROR_NOT_SUPPORTED;
1173 }
1174
1175 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001176}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001179{
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001181 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001183
Ronald Cronea0f8a62020-11-25 17:52:23 +01001184 slot->key.data = NULL;
1185 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001188}
1189
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001190/** Completely wipe a slot in memory, including its policy.
1191 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001193{
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 /*
1197 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001198 * do our best to report an unexpected amount of registered readers or
1199 * an unexpected state.
1200 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1201 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1203 * function is called as part of the execution of a test suite, the
1204 * execution of the test suite is stopped in error if the assertion fails.
1205 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001206 switch (slot->state) {
1207 case PSA_SLOT_FULL:
1208 /* In this state psa_wipe_key_slot() must only be called if the
1209 * caller is the last reader. */
1210 case PSA_SLOT_PENDING_DELETION:
1211 /* In this state psa_wipe_key_slot() must only be called if the
1212 * caller is the last reader. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001213 if (slot->var.occupied.registered_readers != 1) {
1214 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 1);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001215 status = PSA_ERROR_CORRUPTION_DETECTED;
1216 }
1217 break;
1218 case PSA_SLOT_FILLING:
1219 /* In this state registered_readers must be 0. */
Gilles Peskine47ad2f72024-06-10 11:42:41 +02001220 if (slot->var.occupied.registered_readers != 0) {
1221 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 0);
Ryan Everett7ed542e2024-01-17 11:39:09 +00001222 status = PSA_ERROR_CORRUPTION_DETECTED;
1223 }
1224 break;
1225 case PSA_SLOT_EMPTY:
1226 /* The slot is already empty, it cannot be wiped. */
1227 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1228 status = PSA_ERROR_CORRUPTION_DETECTED;
1229 break;
1230 default:
1231 /* The slot's state is invalid. */
1232 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001233 }
1234
Gilles Peskinee8199f52024-06-10 11:53:33 +02001235#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1236 size_t slice_index = slot->slice_index;
1237#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1238
1239
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001240 /* Multipart operations may still be using the key. This is safe
1241 * because all multipart operation objects are independent from
1242 * the key slot: if they need to access the key after the setup
1243 * phase, they have a copy of the key. Note that this means that
1244 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001245 /* At this point, key material and other type-specific content has
1246 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001247 * zeroize because the metadata is not particularly sensitive.
1248 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 memset(slot, 0, sizeof(*slot));
Gilles Peskinee8199f52024-06-10 11:53:33 +02001250
1251#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1252 /* If the slot is already corrupted, something went deeply wrong,
1253 * like a thread still using the slot or a stray pointer leading
1254 * to the slot's memory being used for another object. Let the slot
1255 * leak rather than make the corruption worse. */
1256 if (status == PSA_SUCCESS) {
1257 status = psa_free_key_slot(slice_index, slot);
1258 }
1259#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001262}
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001266 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001267 psa_status_t status; /* status of the last operation */
1268 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1270 psa_se_drv_table_entry_t *driver;
1271#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (mbedtls_svc_key_id_is_null(key)) {
1274 return PSA_SUCCESS;
1275 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001276
Ronald Cronf2911112020-10-29 17:51:10 +01001277 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001278 * Get the description of the key in a key slot, and register to read it.
1279 * In the case of a persistent key, this will load the key description
1280 * from persistent memory if not done yet.
1281 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001282 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001283 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 status = psa_get_and_lock_key_slot(key, &slot);
1285 if (status != PSA_SUCCESS) {
1286 return status;
1287 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001288
Ryan Everettc053d962024-01-25 17:56:32 +00001289#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001290 /* We cannot unlock between setting the state to PENDING_DELETION
1291 * and destroying the key in storage, as otherwise another thread
1292 * could load the key into a new slot and the key will not be
1293 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001294 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1295 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001296
1297 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1298 /* Another thread has destroyed the key between us locking the slot
1299 * and us gaining the mutex. Unregister from the slot,
1300 * and report that the key does not exist. */
1301 status = psa_unregister_read(slot);
1302
1303 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1304 &mbedtls_threading_key_slot_mutex));
1305 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1306 }
Ryan Everettc053d962024-01-25 17:56:32 +00001307#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001308 /* Set the key slot containing the key description's state to
1309 * PENDING_DELETION. This stops new operations from registering
1310 * to read the slot. Current readers can safely continue to access
1311 * the key within the slot; the last registered reader will
1312 * automatically wipe the slot when they call psa_unregister_read().
1313 * If the key is persistent, we can now delete the copy of the key
1314 * from memory. If the key is opaque, we require the driver to
1315 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001316 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1317 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001318
Ryan Everettd868b742024-03-08 18:35:09 +00001319 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001320 goto exit;
1321 }
Ronald Cronf2911112020-10-29 17:51:10 +01001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001324 /* Refuse the destruction of a read-only key (which may or may not work
1325 * if we attempt it, depending on whether the key is merely read-only
1326 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001327 * Just do the best we can, which is to wipe the copy in memory
1328 * (done in this function's cleanup code). */
1329 overall_status = PSA_ERROR_NOT_PERMITTED;
1330 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001331 }
1332
Gilles Peskine354f7672019-07-12 23:46:38 +02001333#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1335 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001336 /* For a key in a secure element, we need to do three things:
1337 * remove the key file in internal storage, destroy the
1338 * key inside the secure element, and update the driver's
1339 * persistent data. Start a transaction that will encompass these
1340 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 status = psa_crypto_save_transaction();
1346 if (status != PSA_SUCCESS) {
1347 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001348 /* We should still try to destroy the key in the secure
1349 * element and the key metadata in storage. This is especially
1350 * important if the error is that the storage is full.
1351 * But how to do it exactly without risking an inconsistent
1352 * state after a reset?
1353 * https://github.com/ARMmbed/mbed-crypto/issues/215
1354 */
1355 overall_status = status;
1356 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001357 }
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_destroy_se_key(driver,
1360 psa_key_slot_get_slot_number(slot));
1361 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001362 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001364 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001365#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1366
Darryl Greend49a4992018-06-18 17:27:26 +01001367#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001369 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001370 * The slot will still hold a copy of the key until the last reader
1371 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 status = psa_destroy_persistent_key(slot->attr.id);
1373 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 }
Darryl Greend49a4992018-06-18 17:27:26 +01001376 }
1377#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001378
Gilles Peskinefc762652019-07-22 19:30:34 +02001379#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (driver != NULL) {
1381 status = psa_save_se_persistent_data(driver);
1382 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001383 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 }
1385 status = psa_crypto_stop_transaction();
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 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001389 }
1390#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1391
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001392exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001393 /* Unregister from reading the slot. If we are the last active reader
1394 * then this will wipe the slot. */
1395 status = psa_unregister_read(slot);
1396 /* Prioritize CORRUPTION_DETECTED from unregistering over
1397 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001399 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 }
Ryan Everettc053d962024-01-25 17:56:32 +00001401
1402#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001403 /* Don't overwrite existing errors if the unlock fails. */
1404 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001405 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1406 &mbedtls_threading_key_slot_mutex));
1407#endif
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410}
1411
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001412/** Retrieve all the publicly-accessible attributes of a key.
1413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1415 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001416{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001418 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1423 if (status != PSA_SUCCESS) {
1424 return status;
1425 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001426
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001427 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428
1429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1431 psa_set_key_slot_number(attributes,
1432 psa_key_slot_get_slot_number(slot));
1433 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001435
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001436 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001437}
1438
Gilles Peskinec8000c02019-08-02 20:15:51 +02001439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1440psa_status_t psa_get_key_slot_number(
1441 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001443{
Gilles Peskine972539c2024-02-28 01:49:45 +01001444 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return PSA_SUCCESS;
1447 } else {
1448 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001449 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001450}
1451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1454 size_t key_buffer_size,
1455 uint8_t *data,
1456 size_t data_size,
1457 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001458{
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (key_buffer_size > data_size) {
1460 return PSA_ERROR_BUFFER_TOO_SMALL;
1461 }
1462 memcpy(data, key_buffer, key_buffer_size);
1463 memset(data + key_buffer_size, 0,
1464 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001467}
1468
Ronald Cron7285cda2020-11-26 14:46:37 +01001469psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001470 const psa_key_attributes_t *attributes,
1471 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001474 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (key_type_is_raw_bytes(type) ||
1477 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001478 PSA_KEY_TYPE_IS_ECC(type) ||
1479 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return psa_export_key_buffer_internal(
1481 key_buffer, key_buffer_size,
1482 data, data_size, data_length);
1483 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 /* This shouldn't happen in the reference implementation, but
1485 it is valid for a special-purpose implementation to omit
1486 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 }
1489}
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001492 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 size_t data_size,
1494 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001495{
1496 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1497 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1498 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001499 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500
Ronald Crone907e552021-01-18 13:22:38 +01001501 /* Reject a zero-length output buffer now, since this can never be a
1502 * valid key representation. This way we know that data must be a valid
1503 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (data_size == 0) {
1505 return PSA_ERROR_BUFFER_TOO_SMALL;
1506 }
Ronald Crone907e552021-01-18 13:22:38 +01001507
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 /* Set the key to empty now, so that even when there are errors, we always
1509 * set data_length to a value between 0 and data_size. On error, setting
1510 * the key to empty is a good choice because an empty key representation is
1511 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001512 *data_length = 0;
1513
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514 /* Export requires the EXPORT flag. There is an exception for public keys,
1515 * which don't require any flag, but
1516 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1519 PSA_KEY_USAGE_EXPORT, 0);
1520 if (status != PSA_SUCCESS) {
1521 return status;
1522 }
mohammad160306e79202018-03-28 13:17:44 +03001523
Ryan Everett45ac5262024-01-08 17:15:19 +00001524 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1525
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001526 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 slot->key.data, slot->key.bytes,
1528 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001529
David Horstmann4a48bec2024-03-13 15:42:31 +00001530#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001531exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001532#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001533 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001534
Ryan Everett45ac5262024-01-08 17:15:19 +00001535 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001537}
1538
Ronald Cron7285cda2020-11-26 14:46:37 +01001539psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001540 const psa_key_attributes_t *attributes,
1541 const uint8_t *key_buffer,
1542 size_t key_buffer_size,
1543 uint8_t *data,
1544 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001546{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001547 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001548
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001549 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001550 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1551 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001552 /* Exporting public -> public */
1553 return psa_export_key_buffer_internal(
1554 key_buffer, key_buffer_size,
1555 data, data_size, data_length);
1556 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001558 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1559 return mbedtls_psa_rsa_export_public_key(attributes,
1560 key_buffer,
1561 key_buffer_size,
1562 data,
1563 data_size,
1564 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001565#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001566 /* We don't know how to convert a private RSA key to public. */
1567 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001568#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001569 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001570 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001571#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001572 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1573 return mbedtls_psa_ecp_export_public_key(attributes,
1574 key_buffer,
1575 key_buffer_size,
1576 data,
1577 data_size,
1578 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001579#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001580 /* We don't know how to convert a private ECC key to public */
1581 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001582#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001583 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001584 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001585#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001586 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001587 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001588 key_buffer,
1589 key_buffer_size,
1590 data, data_size,
1591 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001592#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001593 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001594#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001595 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001597 (void) key_buffer;
1598 (void) key_buffer_size;
1599 (void) data;
1600 (void) data_size;
1601 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001603 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604}
Ronald Crond18b5f82020-11-25 16:46:05 +01001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001607 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 size_t data_size,
1609 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001610{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001612 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001613 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001614
Ryan Everettb1d2c672024-01-08 17:19:30 +00001615 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001616
Ronald Crone907e552021-01-18 13:22:38 +01001617 /* Reject a zero-length output buffer now, since this can never be a
1618 * valid key representation. This way we know that data must be a valid
1619 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (data_size == 0) {
1621 return PSA_ERROR_BUFFER_TOO_SMALL;
1622 }
Ronald Crone907e552021-01-18 13:22:38 +01001623
Darryl Greendd8fb772018-11-07 16:00:44 +00001624 /* Set the key to empty now, so that even when there are errors, we always
1625 * set data_length to a value between 0 and data_size. On error, setting
1626 * the key to empty is a good choice because an empty key representation is
1627 * unlikely to be accepted anywhere. */
1628 *data_length = 0;
1629
1630 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1632 if (status != PSA_SUCCESS) {
1633 return status;
1634 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001635
Ryan Everettb1d2c672024-01-08 17:19:30 +00001636 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1639 status = PSA_ERROR_INVALID_ARGUMENT;
1640 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001641 }
1642
Ronald Cron67227982020-11-26 15:16:05 +01001643 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001644 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001646
1647exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001648 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001649
Ryan Everettb1d2c672024-01-08 17:19:30 +00001650 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001652}
1653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate that a key policy is internally well-formed.
1655 *
1656 * This function only rejects invalid policies. It does not validate the
1657 * consistency of the policy with respect to other attributes of the key
1658 * such as the key type.
1659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001661{
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1663 PSA_KEY_USAGE_COPY |
1664 PSA_KEY_USAGE_ENCRYPT |
1665 PSA_KEY_USAGE_DECRYPT |
1666 PSA_KEY_USAGE_SIGN_MESSAGE |
1667 PSA_KEY_USAGE_VERIFY_MESSAGE |
1668 PSA_KEY_USAGE_SIGN_HASH |
1669 PSA_KEY_USAGE_VERIFY_HASH |
1670 PSA_KEY_USAGE_VERIFY_DERIVATION |
1671 PSA_KEY_USAGE_DERIVE)) != 0) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676}
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate the internal consistency of key attributes.
1679 *
1680 * This function only rejects invalid attribute values. If does not
1681 * validate the consistency of the attributes with any key data that may
1682 * be involved in the creation of the key.
1683 *
1684 * Call this function early in the key creation process.
1685 *
1686 * \param[in] attributes Key attributes for the new key.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
1689 *
1690 */
1691static psa_status_t psa_validate_key_attributes(
1692 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001694{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1697 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 status = psa_validate_key_location(lifetime, p_drv);
1700 if (status != PSA_SUCCESS) {
1701 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 status = psa_validate_key_persistence(lifetime);
1705 if (status != PSA_SUCCESS) {
1706 return status;
1707 }
1708
1709 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1710 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
1713 } else {
1714 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1715 return PSA_ERROR_INVALID_ARGUMENT;
1716 }
1717 }
1718
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001719 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
1724 /* Refuse to create overly large keys.
1725 * Note that this doesn't trigger on import if the attributes don't
1726 * explicitly specify a size (so psa_get_key_bits returns 0), so
1727 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1729 return PSA_ERROR_NOT_SUPPORTED;
1730 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001733}
1734
Gilles Peskine4747d192019-04-17 15:05:45 +02001735/** Prepare a key slot to receive key material.
1736 *
1737 * This function allocates a key slot and sets its metadata.
1738 *
1739 * If this function fails, call psa_fail_key_creation().
1740 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * This function is intended to be used as follows:
1742 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001743 * it with the specified attributes, and in case of a volatile key assign it
1744 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 * -# Populate the slot with the key material.
1746 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1747 * In case of failure at any step, stop the sequence and call
1748 * psa_fail_key_creation().
1749 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001750 * On success, the key slot's state is PSA_SLOT_FILLING.
1751 * It is the responsibility of the caller to change the slot's state to
1752 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001753 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001754 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 * \param[out] p_slot On success, a pointer to the prepared slot.
1757 * \param[out] p_drv On any return, the driver for the key, if any.
1758 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001759 *
1760 * \retval #PSA_SUCCESS
1761 * The key slot is ready to receive key material.
1762 * \return If this function fails, the key slot is an invalid state.
1763 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 */
1765static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001766 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001770{
1771 psa_status_t status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001772
Gilles Peskinedf179142019-07-15 22:02:14 +02001773 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 *p_drv = NULL;
1775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 status = psa_validate_key_attributes(attributes, p_drv);
1777 if (status != PSA_SUCCESS) {
1778 return status;
1779 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001780
Gilles Peskinee8199f52024-06-10 11:53:33 +02001781 int key_is_volatile = PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime);
1782 psa_key_id_t volatile_key_id;
1783
Ryan Everett3d8118d2024-01-30 16:58:47 +00001784#if defined(MBEDTLS_THREADING_C)
1785 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1786 &mbedtls_threading_key_slot_mutex));
1787#endif
Gilles Peskinee8199f52024-06-10 11:53:33 +02001788 status = psa_reserve_free_key_slot(
1789 key_is_volatile ? &volatile_key_id : NULL,
1790 p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001791#if defined(MBEDTLS_THREADING_C)
1792 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1793 &mbedtls_threading_key_slot_mutex));
1794#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (status != PSA_SUCCESS) {
1796 return status;
1797 }
Gilles Peskinee8199f52024-06-10 11:53:33 +02001798 psa_key_slot_t *slot = *p_slot;
Gilles Peskine4747d192019-04-17 15:05:45 +02001799
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001800 /* We're storing the declared bit-size of the key. It's up to each
1801 * creation mechanism to verify that this information is correct.
1802 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001803 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001804 * is optional (import, copy). In case of a volatile key, assign it the
1805 * volatile key identifier associated to the slot returned to contain its
1806 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001807
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001808 slot->attr = *attributes;
Gilles Peskinee8199f52024-06-10 11:53:33 +02001809 if (key_is_volatile) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001810#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1811 slot->attr.id = volatile_key_id;
1812#else
1813 slot->attr.id.key_id = volatile_key_id;
1814#endif
1815 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001816
Gilles Peskinecbaff462019-07-12 23:46:04 +02001817#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001818 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001820 * create the key file in internal storage, create the
1821 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 * persistent data. This is done by starting a transaction that will
1823 * encompass these three actions.
1824 * For registering a volatile key, we just need to find an appropriate
1825 * slot number inside the SE. Since the key is designated volatile, creating
1826 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001827 /* The first thing to do is to find a slot number for the new key.
1828 * We save the slot number in persistent storage as part of the
1829 * transaction data. It will be needed to recover if the power
1830 * fails during the key creation process, to clean up on the secure
1831 * element side after restarting. Obtaining a slot number from the
1832 * secure element driver updates its persistent state, but we do not yet
1833 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001834 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001836 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1838 &slot_number);
1839 if (status != PSA_SUCCESS) {
1840 return status;
1841 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001842
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001843 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001846 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001847 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 status = psa_crypto_save_transaction();
1849 if (status != PSA_SUCCESS) {
1850 (void) psa_crypto_stop_transaction();
1851 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001852 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001853 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001854
1855 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everettb5a20d32023-11-15 16:26:01 +00001857 if (status != PSA_SUCCESS) {
1858 return status;
1859 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001860 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001865 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001866#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001869}
Gilles Peskine4747d192019-04-17 15:05:45 +02001870
1871/** Finalize the creation of a key once its key material has been set.
1872 *
1873 * This entails writing the key to persistent storage.
1874 *
1875 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001876 * See the documentation of psa_start_key_creation() for the intended use
1877 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001879 * If the finalization succeeds, the function sets the key slot's state to
1880 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1881 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001882 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001883 * \param[in,out] slot Pointer to the slot with key material.
1884 * \param[in] driver The secure element driver for the key,
1885 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 * \param[out] key On success, identifier of the key. Note that the
1887 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001888 *
1889 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001890 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1892 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1893 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1894 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1895 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1896 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001897 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001898 * \return If this function fails, the key slot is an invalid state.
1899 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001901static psa_status_t psa_finish_key_creation(
1902 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001903 psa_se_drv_table_entry_t *driver,
1904 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001905{
1906 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001907 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001909
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001910#if defined(MBEDTLS_THREADING_C)
1911 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1912 &mbedtls_threading_key_slot_mutex));
1913#endif
1914
Gilles Peskine4747d192019-04-17 15:05:45 +02001915#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001917#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001919 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001920 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001922
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001923 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1924 sizeof(data.slot_number),
1925 "Slot number size does not match psa_se_key_data_storage_t");
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1928 status = psa_save_persistent_key(&slot->attr,
1929 (uint8_t *) &data,
1930 sizeof(data));
1931 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001932#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1933 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001934 /* Key material is saved in export representation in the slot, so
1935 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_save_persistent_key(&slot->attr,
1937 slot->key.data,
1938 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001939 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001940 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001941#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1942
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001944 /* Finish the transaction for a key creation. This does not
1945 * happen when registering an existing key. Detect this case
1946 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001947 * creation of a persistent key in a secure element requires a transaction,
1948 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (driver != NULL &&
1950 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1951 status = psa_save_se_persistent_data(driver);
1952 if (status != PSA_SUCCESS) {
1953 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001954
1955#if defined(MBEDTLS_THREADING_C)
1956 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1957 &mbedtls_threading_key_slot_mutex));
1958#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001960 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001962 }
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001966 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001967 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1968 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001970 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001972 }
Ronald Cron50972942020-11-14 11:28:25 +01001973
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001974#if defined(MBEDTLS_THREADING_C)
1975 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1976 &mbedtls_threading_key_slot_mutex));
1977#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001979}
1980
1981/** Abort the creation of a key.
1982 *
1983 * You may call this function after calling psa_start_key_creation(),
1984 * or after psa_finish_key_creation() fails. In other circumstances, this
1985 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001986 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001987 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001988 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001989 * \param[in,out] slot Pointer to the slot with key material.
1990 * \param[in] driver The secure element driver for the key,
1991 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001992 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993static void psa_fail_key_creation(psa_key_slot_t *slot,
1994 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001995{
Gilles Peskine011e4282019-06-26 18:34:38 +02001996 (void) driver;
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001999 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002001
Ryan Everettb7101442024-01-23 20:09:49 +00002002#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00002003 /* If the lock operation fails we still wipe the slot.
2004 * Operations will no longer work after a failed lock,
2005 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00002006 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
2007#endif
2008
Gilles Peskine011e4282019-06-26 18:34:38 +02002009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002010 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002011 * element, and the failure happened later (when saving metadata
2012 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002013 * element.
2014 * https://github.com/ARMmbed/mbed-crypto/issues/217
2015 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002016
Gilles Peskined7729582019-08-05 15:55:54 +02002017 /* Abort the ongoing transaction if any (there may not be one if
2018 * the creation process failed before starting one, or if the
2019 * key creation is a registration of a key in a secure element).
2020 * Earlier functions must already have done what it takes to undo any
2021 * partial creation. All that's left is to update the transaction data
2022 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002024#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00002027
2028#if defined(MBEDTLS_THREADING_C)
2029 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
2030#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02002031}
2032
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002033/** Validate optional attributes during key creation.
2034 *
2035 * Some key attributes are optional during key creation. If they are
2036 * specified in the attributes structure, check that they are consistent
2037 * with the data in the slot.
2038 *
2039 * This function should be called near the end of key creation, after
2040 * the slot in memory is fully populated but before saving persistent data.
2041 */
2042static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002046 if (attributes->type != 0) {
2047 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return PSA_ERROR_INVALID_ARGUMENT;
2049 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050 }
2051
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002052 if (attributes->bits != 0) {
2053 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 return PSA_ERROR_INVALID_ARGUMENT;
2055 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 }
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059}
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00002062 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 size_t data_length,
2064 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002065{
2066 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00002067 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002068 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002069 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002070 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302071 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072
Ronald Cron81709fc2020-11-14 12:10:32 +01002073 *key = MBEDTLS_SVC_KEY_ID_INIT;
2074
Gilles Peskine0f84d622019-09-12 19:03:13 +02002075 /* Reject zero-length symmetric keys (including raw data key objects).
2076 * This also rejects any key which might be encoded as an empty string,
2077 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (data_length == 0) {
2079 return PSA_ERROR_INVALID_ARGUMENT;
2080 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002081
Archana449608b2021-09-08 15:36:05 +05302082 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (data_length > SIZE_MAX / 8) {
2084 return PSA_ERROR_NOT_SUPPORTED;
2085 }
Archana6ed4bda2021-08-04 10:47:15 +05302086
Ryan Everettf028fe12024-01-08 17:14:44 +00002087 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2090 &slot, &driver);
2091 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002094
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002095 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302096 * storage ( thus not in the case of importing a key in a secure element
2097 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2098 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002100 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302101 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 attributes, data, data_length, &storage_size);
2103 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 }
Archanad8a83dc2021-06-14 10:04:16 +05302106 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 status = psa_allocate_buffer_to_slot(slot, storage_size);
2108 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002111 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002112
2113 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 status = psa_driver_wrapper_import_key(attributes,
2115 data, data_length,
2116 slot->key.data,
2117 slot->key.bytes,
2118 &slot->key.bytes, &bits);
2119 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002124 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002126 status = PSA_ERROR_INVALID_ARGUMENT;
2127 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002128 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002129
Archana6ed4bda2021-08-04 10:47:15 +05302130 /* Enforce a size limit, and in particular ensure that the bit
2131 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302133 status = PSA_ERROR_NOT_SUPPORTED;
2134 goto exit;
2135 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 status = psa_validate_optional_attributes(slot, attributes);
2137 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002142exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002143 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (status != PSA_SUCCESS) {
2145 psa_fail_key_creation(slot, driver);
2146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002149}
2150
Gilles Peskined7729582019-08-05 15:55:54 +02002151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2152psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002154{
2155 psa_status_t status;
2156 psa_key_slot_t *slot = NULL;
2157 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002159
2160 /* Leaving attributes unspecified is not currently supported.
2161 * It could make sense to query the key type and size from the
2162 * secure element, but not all secure elements support this
2163 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2165 return PSA_ERROR_NOT_SUPPORTED;
2166 }
2167 if (psa_get_key_bits(attributes) == 0) {
2168 return PSA_ERROR_NOT_SUPPORTED;
2169 }
Gilles Peskined7729582019-08-05 15:55:54 +02002170
Gilles Peskined72ad732024-06-13 16:06:45 +02002171 /* Not usable with volatile keys, even with an appropriate location,
2172 * due to the API design.
2173 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2174 */
2175 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2176 return PSA_ERROR_INVALID_ARGUMENT;
2177 }
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2180 &slot, &driver);
2181 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002182 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Gilles Peskined7729582019-08-05 15:55:54 +02002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002186
2187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (status != PSA_SUCCESS) {
2189 psa_fail_key_creation(slot, driver);
2190 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002191
Gilles Peskined7729582019-08-05 15:55:54 +02002192 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 psa_close_key(key);
2194 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002195}
2196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2199 const psa_key_attributes_t *specified_attributes,
2200 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002201{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002203 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 psa_key_slot_t *source_slot = NULL;
2205 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002206 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002207 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302208 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209
Ronald Cron81709fc2020-11-14 12:10:32 +01002210 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2211
Archana8a180362021-07-05 02:18:48 +05302212 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2214 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 status = psa_validate_optional_attributes(source_slot,
2219 specified_attributes);
2220 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223
Archana9d17bf42021-09-10 06:22:44 +05302224 /* The target key type and number of bits have been validated by
2225 * psa_validate_optional_attributes() to be either equal to zero or
2226 * equal to the ones of the source key. So it is safe to inherit
2227 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302228 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002229 actual_attributes.bits = source_slot->attr.bits;
2230 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302231
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002234 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 &source_slot->attr.policy);
2236 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2241 &target_slot, &driver);
2242 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 }
2245 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2246 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302247 /*
Archana9d17bf42021-09-10 06:22:44 +05302248 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302249 * the source key would need to be exported as plaintext and re-imported
2250 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302251 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302252 * appropriate API invocations from the application, if needed.
2253 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002254 status = PSA_ERROR_NOT_SUPPORTED;
2255 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002256 }
Archana8a180362021-07-05 02:18:48 +05302257 /*
2258 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302259 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302260 * - For opaque keys this translates to an invocation of the drivers'
2261 * copy_key entry point through the dispatch layer.
2262 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002263 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2265 &storage_size);
2266 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 }
Archana374fe5b2021-09-08 15:50:28 +05302269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2271 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 }
Archana374fe5b2021-09-08 15:50:28 +05302274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 status = psa_driver_wrapper_copy_key(&actual_attributes,
2276 source_slot->key.data,
2277 source_slot->key.bytes,
2278 target_slot->key.data,
2279 target_slot->key.bytes,
2280 &target_slot->key.bytes);
2281 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
2284 } else {
2285 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302286 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 source_slot->key.bytes);
2288 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 }
Archana8a180362021-07-05 02:18:48 +05302291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (status != PSA_SUCCESS) {
2295 psa_fail_key_creation(target_slot, driver);
2296 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297
Ryan Everetta103ec92024-01-31 13:59:57 +00002298 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002301}
2302
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002303
2304
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002305/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002306/* Message digests */
2307/****************************************************************/
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002311 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (operation->id == 0) {
2313 return PSA_SUCCESS;
2314 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002317 operation->id = 0;
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2323 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002329 status = PSA_ERROR_BAD_STATE;
2330 goto exit;
2331 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002334 status = PSA_ERROR_INVALID_ARGUMENT;
2335 goto exit;
2336 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002338 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2339 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002343
2344exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (status != PSA_SUCCESS) {
2346 psa_hash_abort(operation);
2347 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002348
2349 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350}
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002353 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002357 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002360 status = PSA_ERROR_BAD_STATE;
2361 goto exit;
2362 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002363
Steven Cooremanc8288352021-03-04 14:02:19 +01002364 /* Don't require hash implementations to behave correctly on a
2365 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (input_length == 0) {
2367 return PSA_SUCCESS;
2368 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002369
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002370 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002372
2373exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (status != PSA_SUCCESS) {
2375 psa_hash_abort(operation);
2376 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002377
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002378 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380}
2381
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002382static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002383 uint8_t *hash,
2384 size_t hash_size,
2385 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2388
Steven Cooremanfbe09282021-03-08 18:41:12 +01002389 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (operation->id == 0) {
2391 return PSA_ERROR_BAD_STATE;
2392 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002394 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 operation, hash, hash_size, hash_length);
2396 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002397
2398 return status;
2399}
2400
2401psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2402 uint8_t *hash_external,
2403 size_t hash_size,
2404 size_t *hash_length)
2405{
2406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2408
2409 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2410 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2411
David Horstmann4a48bec2024-03-13 15:42:31 +00002412#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002413exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002414#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002415 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417}
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002420 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422{
Ronald Cron69a63422021-10-18 09:47:58 +02002423 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2426 LOCAL_INPUT_DECLARE(hash_external, hash);
2427
2428 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 operation,
2430 actual_hash, sizeof(actual_hash),
2431 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002438 status = PSA_ERROR_INVALID_SIGNATURE;
2439 goto exit;
2440 }
2441
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002442 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002443 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002444 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002446
2447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2449 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002450 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002452 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454}
2455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002457 const uint8_t *input_external, size_t input_length,
2458 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002460{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2462 LOCAL_INPUT_DECLARE(input_external, input);
2463 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2464
Steven Cooremanfbe09282021-03-08 18:41:12 +01002465 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!PSA_ALG_IS_HASH(alg)) {
2467 return PSA_ERROR_INVALID_ARGUMENT;
2468 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002469
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002470 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2471 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2472 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002473 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002474
David Horstmann4a48bec2024-03-13 15:42:31 +00002475#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002476exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002477#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002478 LOCAL_INPUT_FREE(input_external, input);
2479 LOCAL_OUTPUT_FREE(hash_external, hash);
2480 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002481}
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002484 const uint8_t *input_external, size_t input_length,
2485 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002486{
Ronald Cron69a63422021-10-18 09:47:58 +02002487 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002488 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2490
2491 LOCAL_INPUT_DECLARE(input_external, input);
2492 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002495 status = PSA_ERROR_INVALID_ARGUMENT;
2496 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002498
Thomas Daubney51ffac92024-01-18 15:46:26 +00002499 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2500 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 alg, input, input_length,
2502 actual_hash, sizeof(actual_hash),
2503 &actual_hash_length);
2504 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002505 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 }
2507 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002508 status = PSA_ERROR_INVALID_SIGNATURE;
2509 goto exit;
2510 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002511
2512 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002513 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002514 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002516
2517exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002519
2520 LOCAL_INPUT_FREE(input_external, input);
2521 LOCAL_INPUT_FREE(hash_external, hash);
2522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002524}
2525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2527 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002528{
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (source_operation->id == 0 ||
2530 target_operation->id != 0) {
2531 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002532 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2535 target_operation);
2536 if (status != PSA_SUCCESS) {
2537 psa_hash_abort(target_operation);
2538 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002541}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542
2543
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545/* MAC */
2546/****************************************************************/
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002549{
Steven Cooremane6804192021-03-19 18:28:56 +01002550 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (operation->id == 0) {
2552 return PSA_SUCCESS;
2553 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002556 operation->mac_size = 0;
2557 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002558 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561}
2562
Ronald Cron2dff3b22021-06-17 16:33:22 +02002563static psa_status_t psa_mac_finalize_alg_and_key_validation(
2564 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002565 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 psa_key_type_t key_type = psa_get_key_type(attributes);
2570 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (!PSA_ALG_IS_MAC(alg)) {
2573 return PSA_ERROR_INVALID_ARGUMENT;
2574 }
Steven Cooremane6804192021-03-19 18:28:56 +01002575
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002576 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 status = psa_mac_key_can_do(alg, key_type);
2578 if (status != PSA_SUCCESS) {
2579 return status;
2580 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002581
Steven Cooremand1a68f12021-05-10 11:13:41 +02002582 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002586 /* A very short MAC is too short for security since it can be
2587 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2588 * so we make this our minimum, even though 32 bits is still
2589 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002591 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2594 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002595 /* It's impossible to "truncate" to a larger length than the full length
2596 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002598 }
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002601 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2602 * that is disabled in the compile-time configuration. The result can
2603 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2604 * configuration into account. In this case, force a return of
2605 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2606 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2607 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2608 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2609 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002611 }
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002614}
2615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2617 mbedtls_svc_key_id_t key,
2618 psa_algorithm_t alg,
2619 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002620{
2621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2622 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002623 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624
2625 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002627 status = PSA_ERROR_BAD_STATE;
2628 goto exit;
2629 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630
2631 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 key,
2633 &slot,
2634 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2635 alg);
2636 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002640 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 &operation->mac_size);
2642 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645
2646 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002647 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (is_sign) {
2649 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002650 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 slot->key.data,
2652 slot->key.bytes,
2653 alg);
2654 } else {
2655 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002656 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 slot->key.data,
2658 slot->key.bytes,
2659 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002661
Gilles Peskinefbfac682018-07-08 20:51:54 +02002662exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (status != PSA_SUCCESS) {
2664 psa_mac_abort(operation);
2665 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002666
Ryan Everettfb9857f2024-02-14 12:16:41 +00002667 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670}
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2673 mbedtls_svc_key_id_t key,
2674 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002675{
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002677}
2678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2680 mbedtls_svc_key_id_t key,
2681 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002682{
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002684}
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002687 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2691 LOCAL_INPUT_DECLARE(input_external, input);
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002694 status = PSA_ERROR_BAD_STATE;
2695 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002698 /* Don't require hash implementations to behave correctly on a
2699 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002701 status = PSA_SUCCESS;
2702 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002704
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002705 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2706 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (status != PSA_SUCCESS) {
2709 psa_mac_abort(operation);
2710 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002711
David Horstmann4a48bec2024-03-13 15:42:31 +00002712#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002713exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002714#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002715 LOCAL_INPUT_FREE(input_external, input);
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718}
2719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002721 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 size_t mac_size,
2723 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002724{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2726 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002727 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002728 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002731 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002732 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002733 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002736 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002737 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002739
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002740 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2741 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002743 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002744 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002745 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002748 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002749 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002750 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002751
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 status = psa_driver_wrapper_mac_sign_finish(operation,
2754 mac, operation->mac_size,
2755 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002756
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002757exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002758 /* In case of success, set the potential excess room in the output buffer
2759 * to an invalid value, to avoid potentially leaking a longer MAC.
2760 * In case of error, set the output length and content to a safe default,
2761 * such that in case the caller misses an error check, the output would be
2762 * an unachievable MAC.
2763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002765 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002766 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002767 }
mohammad16036df908f2018-04-02 08:34:15 -07002768
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002769 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002770 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2771 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002774 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002777}
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002780 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2784 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002785 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002788 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002789 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002790 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002793 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002794 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002795 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002798 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002799 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002800 }
2801
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002802 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 status = psa_driver_wrapper_mac_verify_finish(operation,
2804 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002805
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002806exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002808 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811}
2812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
2817 uint8_t *mac,
2818 size_t mac_size,
2819 size_t *mac_length,
2820 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002821{
2822 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002823 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2824 psa_key_slot_t *slot;
2825 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826
Ronald Cron51131b52021-06-17 17:17:20 +02002827 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 key,
2829 &slot,
2830 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2831 alg);
2832 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002833 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002835
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002836 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 &operation_mac_size);
2838 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002843 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002845 }
2846
2847 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002848 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 slot->key.data, slot->key.bytes,
2850 alg,
2851 input, input_length,
2852 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853
2854exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002855 /* In case of success, set the potential excess room in the output buffer
2856 * to an invalid value, to avoid potentially leaking a longer MAC.
2857 * In case of error, set the output length and content to a safe default,
2858 * such that in case the caller misses an error check, the output would be
2859 * an unachievable MAC.
2860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002862 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002863 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002864 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002865
2866 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002867
Ryan Everetta103ec92024-01-31 13:59:57 +00002868 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002871}
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002874 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002875 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002876 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002877 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 size_t mac_size,
2879 size_t *mac_length)
2880{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2882 LOCAL_INPUT_DECLARE(input_external, input);
2883 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2884
2885 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2886 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2887 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002888 input, input_length,
2889 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002890
David Horstmann4a48bec2024-03-13 15:42:31 +00002891#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002892exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002893#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002894 LOCAL_INPUT_FREE(input_external, input);
2895 LOCAL_OUTPUT_FREE(mac_external, mac);
2896
2897 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002898}
2899
2900psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2901 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002902 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002904 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002906{
2907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002908 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2909 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002910 LOCAL_INPUT_DECLARE(input_external, input);
2911 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002912
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002913 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 status = psa_mac_compute_internal(key, alg,
2915 input, input_length,
2916 actual_mac, sizeof(actual_mac),
2917 &actual_mac_length, 0);
2918 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002919 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002923 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002924 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002925 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002926
2927 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01002928 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002929 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002930 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002931 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002932
2933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002935 LOCAL_INPUT_FREE(input_external, input);
2936 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002939}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002940
Gilles Peskinee59236f2018-01-27 23:32:46 +01002941/****************************************************************/
2942/* Asymmetric cryptography */
2943/****************************************************************/
2944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2946 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947{
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (input_is_message) {
2949 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2950 return PSA_ERROR_INVALID_ARGUMENT;
2951 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2954 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2955 return PSA_ERROR_INVALID_ARGUMENT;
2956 }
2957 }
2958 } else {
2959 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2960 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002961 }
2962 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002965}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2968 int input_is_message,
2969 psa_algorithm_t alg,
2970 const uint8_t *input,
2971 size_t input_length,
2972 uint8_t *signature,
2973 size_t signature_size,
2974 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975{
2976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2977 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2978 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
2980 *signature_length = 0;
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 status = psa_sign_verify_check_alg(input_is_message, alg);
2983 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002984 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002986
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002988 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989 * buffer can in principle be empty since it doesn't actually have
2990 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (signature_size == 0) {
2992 return PSA_ERROR_BUFFER_TOO_SMALL;
2993 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002994
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 key, &slot,
2997 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2998 PSA_KEY_USAGE_SIGN_HASH,
2999 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003002 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003006 status = PSA_ERROR_INVALID_ARGUMENT;
3007 goto exit;
3008 }
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003011 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003012 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 signature, signature_size, signature_length);
3015 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003016
3017 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003018 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003019 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003021 }
3022
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003023
3024exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003025 psa_wipe_tag_output_buffer(signature, status, signature_size,
3026 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003027
Ryan Everetta103ec92024-01-31 13:59:57 +00003028 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003031}
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3034 int input_is_message,
3035 psa_algorithm_t alg,
3036 const uint8_t *input,
3037 size_t input_length,
3038 const uint8_t *signature,
3039 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003040{
3041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3042 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3043 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 status = psa_sign_verify_check_alg(input_is_message, alg);
3046 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003047 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003049
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003050 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 key, &slot,
3052 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3053 PSA_KEY_USAGE_VERIFY_HASH,
3054 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (status != PSA_SUCCESS) {
3057 return status;
3058 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003061 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003062 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003063 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 signature, signature_length);
3065 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003066 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003067 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003068 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003070 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003071
Ryan Everetta103ec92024-01-31 13:59:57 +00003072 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003075
3076}
3077
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003078psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003079 const psa_key_attributes_t *attributes,
3080 const uint8_t *key_buffer,
3081 size_t key_buffer_size,
3082 psa_algorithm_t alg,
3083 const uint8_t *input,
3084 size_t input_length,
3085 uint8_t *signature,
3086 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003088{
3089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003092 size_t hash_length;
3093 uint8_t hash[PSA_HASH_MAX_SIZE];
3094
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003095 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 PSA_ALG_SIGN_GET_HASH(alg),
3097 input, input_length,
3098 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003101 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003103
3104 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 attributes, key_buffer, key_buffer_size,
3106 alg, hash, hash_length,
3107 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003108 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003111}
3112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3114 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003115 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003117 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 size_t signature_size,
3119 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003120{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3122 LOCAL_INPUT_DECLARE(input_external, input);
3123 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3124
3125 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3126 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3127 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3128 signature_size, signature_length);
3129
David Horstmann4a48bec2024-03-13 15:42:31 +00003130#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003131exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003132#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003133 LOCAL_INPUT_FREE(input_external, input);
3134 LOCAL_OUTPUT_FREE(signature_external, signature);
3135 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003136}
3137
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003138psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003139 const psa_key_attributes_t *attributes,
3140 const uint8_t *key_buffer,
3141 size_t key_buffer_size,
3142 psa_algorithm_t alg,
3143 const uint8_t *input,
3144 size_t input_length,
3145 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003147{
3148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003151 size_t hash_length;
3152 uint8_t hash[PSA_HASH_MAX_SIZE];
3153
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003154 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 PSA_ALG_SIGN_GET_HASH(alg),
3156 input, input_length,
3157 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003160 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003162
3163 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 attributes, key_buffer, key_buffer_size,
3165 alg, hash, hash_length,
3166 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003167 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003170}
3171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3173 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003174 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003176 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003178{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3180 LOCAL_INPUT_DECLARE(input_external, input);
3181 LOCAL_INPUT_DECLARE(signature_external, signature);
3182
3183 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3184 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3185 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3186 signature_length);
3187
David Horstmann4a48bec2024-03-13 15:42:31 +00003188#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003189exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003190#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003191 LOCAL_INPUT_FREE(input_external, input);
3192 LOCAL_INPUT_FREE(signature_external, signature);
3193
3194 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003195}
3196
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003197psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003198 const psa_key_attributes_t *attributes,
3199 const uint8_t *key_buffer, size_t key_buffer_size,
3200 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003202{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003203 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3205 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003206#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3208 return mbedtls_psa_rsa_sign_hash(
3209 attributes,
3210 key_buffer, key_buffer_size,
3211 alg, hash, hash_length,
3212 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003213#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3214 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 } else {
3216 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003217 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003218 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003220#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3222 return mbedtls_psa_ecdsa_sign_hash(
3223 attributes,
3224 key_buffer, key_buffer_size,
3225 alg, hash, hash_length,
3226 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003227#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3228 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 } else {
3230 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003231 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003232 }
Ronald Cron4501c982021-03-19 20:09:32 +01003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 (void) key_buffer;
3235 (void) key_buffer_size;
3236 (void) hash;
3237 (void) hash_length;
3238 (void) signature;
3239 (void) signature_size;
3240 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003243}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3246 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003247 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003249 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 size_t signature_size,
3251 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003252{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3254 LOCAL_INPUT_DECLARE(hash_external, hash);
3255 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3256
3257 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3258 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3259 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3260 signature_size, signature_length);
3261
David Horstmann4a48bec2024-03-13 15:42:31 +00003262#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003263exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003264#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003265 LOCAL_INPUT_FREE(hash_external, hash);
3266 LOCAL_OUTPUT_FREE(signature_external, signature);
3267
3268 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003269}
3270
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003271psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003272 const psa_key_attributes_t *attributes,
3273 const uint8_t *key_buffer, size_t key_buffer_size,
3274 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003276{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003277 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3279 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3282 return mbedtls_psa_rsa_verify_hash(
3283 attributes,
3284 key_buffer, key_buffer_size,
3285 alg, hash, hash_length,
3286 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003287#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3288 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 } else {
3290 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003291 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003292 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003294#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3296 return mbedtls_psa_ecdsa_verify_hash(
3297 attributes,
3298 key_buffer, key_buffer_size,
3299 alg, hash, hash_length,
3300 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003301#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3302 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 } else {
3304 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003305 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003306 }
Ronald Cron4501c982021-03-19 20:09:32 +01003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 (void) key_buffer;
3309 (void) key_buffer_size;
3310 (void) hash;
3311 (void) hash_length;
3312 (void) signature;
3313 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003316}
3317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3319 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003320 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003322 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003324{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3326 LOCAL_INPUT_DECLARE(hash_external, hash);
3327 LOCAL_INPUT_DECLARE(signature_external, signature);
3328
3329 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3330 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3331 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3332 signature_length);
3333
David Horstmann4a48bec2024-03-13 15:42:31 +00003334#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003335exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003336#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003337 LOCAL_INPUT_FREE(hash_external, hash);
3338 LOCAL_INPUT_FREE(signature_external, signature);
3339
3340 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003341}
3342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3344 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003345 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003347 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003349 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 size_t output_size,
3351 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003354 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003355 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003356
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003357 LOCAL_INPUT_DECLARE(input_external, input);
3358 LOCAL_INPUT_DECLARE(salt_external, salt);
3359 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360
Darryl Green5cc689a2018-07-24 15:34:10 +01003361 (void) input;
3362 (void) input_length;
3363 (void) salt;
3364 (void) output;
3365 (void) output_size;
3366
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003367 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3370 return PSA_ERROR_INVALID_ARGUMENT;
3371 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003372
Valerio Setti5bb454a2024-01-15 10:43:16 +01003373 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3375 if (status != PSA_SUCCESS) {
3376 return status;
3377 }
3378 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3379 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003380 status = PSA_ERROR_INVALID_ARGUMENT;
3381 goto exit;
3382 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003383
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003384 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3385 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3386 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003387
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003388 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003389 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003390 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003392exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003393 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003394
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003395 LOCAL_INPUT_FREE(input_external, input);
3396 LOCAL_INPUT_FREE(salt_external, salt);
3397 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003400}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3403 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003404 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003406 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003408 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 size_t output_size,
3410 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003412 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003413 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003414 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003415
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003416 LOCAL_INPUT_DECLARE(input_external, input);
3417 LOCAL_INPUT_DECLARE(salt_external, salt);
3418 LOCAL_OUTPUT_DECLARE(output_external, output);
3419
Darryl Green5cc689a2018-07-24 15:34:10 +01003420 (void) input;
3421 (void) input_length;
3422 (void) salt;
3423 (void) output;
3424 (void) output_size;
3425
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003426 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3429 return PSA_ERROR_INVALID_ARGUMENT;
3430 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003431
Valerio Setti5bb454a2024-01-15 10:43:16 +01003432 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3434 if (status != PSA_SUCCESS) {
3435 return status;
3436 }
3437 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003438 status = PSA_ERROR_INVALID_ARGUMENT;
3439 goto exit;
3440 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003441
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003442 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3443 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3444 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003445
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003446 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003447 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003448 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003450
3451exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003452 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003453
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003454 LOCAL_INPUT_FREE(input_external, input);
3455 LOCAL_INPUT_FREE(salt_external, salt);
3456 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003459}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461/****************************************************************/
3462/* Asymmetric interruptible cryptography */
3463/****************************************************************/
3464
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003465static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3466
Paul Elliott9fe12f62022-11-30 19:16:02 +00003467void psa_interruptible_set_max_ops(uint32_t max_ops)
3468{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003469 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470}
3471
3472uint32_t psa_interruptible_get_max_ops(void)
3473{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003474 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475}
3476
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477uint32_t psa_sign_hash_get_num_ops(
3478 const psa_sign_hash_interruptible_operation_t *operation)
3479{
Paul Elliott296ede92022-12-15 17:00:30 +00003480 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481}
3482
3483uint32_t psa_verify_hash_get_num_ops(
3484 const psa_verify_hash_interruptible_operation_t *operation)
3485{
Paul Elliott296ede92022-12-15 17:00:30 +00003486 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487}
3488
Paul Elliott59ad9452022-12-18 15:09:02 +00003489static psa_status_t psa_sign_hash_abort_internal(
3490 psa_sign_hash_interruptible_operation_t *operation)
3491{
3492 if (operation->id == 0) {
3493 /* The object has (apparently) been initialized but it is not (yet)
3494 * in use. It's ok to call abort on such an object, and there's
3495 * nothing to do. */
3496 return PSA_SUCCESS;
3497 }
3498
3499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3500
3501 status = psa_driver_wrapper_sign_hash_abort(operation);
3502
3503 operation->id = 0;
3504
Paul Elliotte6145dc2023-02-07 12:51:21 +00003505 /* Do not clear either the error_occurred or num_ops elements here as they
3506 * only want to be cleared by the application calling abort, not by abort
3507 * being called at completion of an operation. */
3508
Paul Elliott59ad9452022-12-18 15:09:02 +00003509 return status;
3510}
3511
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512psa_status_t psa_sign_hash_start(
3513 psa_sign_hash_interruptible_operation_t *operation,
3514 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003515 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516{
3517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3518 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3519 psa_key_slot_t *slot;
3520
David Horstmann4a523a62024-03-11 13:32:16 +00003521 LOCAL_INPUT_DECLARE(hash_external, hash);
3522
Paul Elliottc9774412023-02-06 15:14:07 +00003523 /* Check that start has not been previously called, or operation has not
3524 * previously errored. */
3525 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003526 return PSA_ERROR_BAD_STATE;
3527 }
3528
Paul Elliott9fe12f62022-11-30 19:16:02 +00003529 status = psa_sign_verify_check_alg(0, alg);
3530 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003531 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003532 return status;
3533 }
3534
3535 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3536 PSA_KEY_USAGE_SIGN_HASH,
3537 alg);
3538
3539 if (status != PSA_SUCCESS) {
3540 goto exit;
3541 }
3542
3543 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3544 status = PSA_ERROR_INVALID_ARGUMENT;
3545 goto exit;
3546 }
3547
David Horstmann4a523a62024-03-11 13:32:16 +00003548 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3549
Paul Elliott296ede92022-12-15 17:00:30 +00003550 /* Ensure ops count gets reset, in case of operation re-use. */
3551 operation->num_ops = 0;
3552
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003553 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554 slot->key.data,
3555 slot->key.bytes, alg,
3556 hash, hash_length);
3557exit:
3558
3559 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003560 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003561 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003562 }
3563
Ryan Everettdcc03d52024-02-14 15:44:13 +00003564 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003565
Paul Elliottc9774412023-02-06 15:14:07 +00003566 if (unlock_status != PSA_SUCCESS) {
3567 operation->error_occurred = 1;
3568 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003569
David Horstmann4a523a62024-03-11 13:32:16 +00003570 LOCAL_INPUT_FREE(hash_external, hash);
3571
Paul Elliottc9774412023-02-06 15:14:07 +00003572 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573}
3574
3575
3576psa_status_t psa_sign_hash_complete(
3577 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003578 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003579 size_t *signature_length)
3580{
3581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3582
David Horstmann4a523a62024-03-11 13:32:16 +00003583 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3584
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585 *signature_length = 0;
3586
Paul Elliottc9774412023-02-06 15:14:07 +00003587 /* Check that start has been called first, and that operation has not
3588 * previously errored. */
3589 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003590 status = PSA_ERROR_BAD_STATE;
3591 goto exit;
3592 }
3593
Paul Elliott096abc42023-02-03 18:33:23 +00003594 /* Immediately reject a zero-length signature buffer. This guarantees that
3595 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003596 if (signature_size == 0) {
3597 status = PSA_ERROR_BUFFER_TOO_SMALL;
3598 goto exit;
3599 }
3600
David Horstmann4a523a62024-03-11 13:32:16 +00003601 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3602
Paul Elliott9fe12f62022-11-30 19:16:02 +00003603 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3604 signature_size,
3605 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003606
Paul Elliott296ede92022-12-15 17:00:30 +00003607 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003608 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003609
Paul Elliottebe225c2023-02-10 14:32:53 +00003610exit:
3611
David Horstmannc5064c82024-03-11 17:02:03 +00003612 if (signature != NULL) {
3613 psa_wipe_tag_output_buffer(signature, status, signature_size,
3614 *signature_length);
3615 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003616
Paul Elliott53bb3122023-02-10 14:22:22 +00003617 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003618 if (status != PSA_SUCCESS) {
3619 operation->error_occurred = 1;
3620 }
3621
Paul Elliott59ad9452022-12-18 15:09:02 +00003622 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003623 }
3624
David Horstmann4a523a62024-03-11 13:32:16 +00003625 LOCAL_OUTPUT_FREE(signature_external, signature);
3626
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627 return status;
3628}
3629
3630psa_status_t psa_sign_hash_abort(
3631 psa_sign_hash_interruptible_operation_t *operation)
3632{
Paul Elliott59ad9452022-12-18 15:09:02 +00003633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3634
3635 status = psa_sign_hash_abort_internal(operation);
3636
3637 /* We clear the number of ops done here, so that it is not cleared when
3638 * the operation fails or succeeds, only on manual abort. */
3639 operation->num_ops = 0;
3640
Paul Elliottc9774412023-02-06 15:14:07 +00003641 /* Likewise, failure state. */
3642 operation->error_occurred = 0;
3643
Paul Elliott59ad9452022-12-18 15:09:02 +00003644 return status;
3645}
3646
3647static psa_status_t psa_verify_hash_abort_internal(
3648 psa_verify_hash_interruptible_operation_t *operation)
3649{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003650 if (operation->id == 0) {
3651 /* The object has (apparently) been initialized but it is not (yet)
3652 * in use. It's ok to call abort on such an object, and there's
3653 * nothing to do. */
3654 return PSA_SUCCESS;
3655 }
3656
Paul Elliott59ad9452022-12-18 15:09:02 +00003657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3658
3659 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003660
3661 operation->id = 0;
3662
Paul Elliotte6145dc2023-02-07 12:51:21 +00003663 /* Do not clear either the error_occurred or num_ops elements here as they
3664 * only want to be cleared by the application calling abort, not by abort
3665 * being called at completion of an operation. */
3666
Paul Elliott59ad9452022-12-18 15:09:02 +00003667 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003668}
3669
3670psa_status_t psa_verify_hash_start(
3671 psa_verify_hash_interruptible_operation_t *operation,
3672 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003673 const uint8_t *hash_external, size_t hash_length,
3674 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003675{
3676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3677 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3678 psa_key_slot_t *slot;
3679
David Horstmann0fea6a52024-03-11 13:41:05 +00003680 LOCAL_INPUT_DECLARE(hash_external, hash);
3681 LOCAL_INPUT_DECLARE(signature_external, signature);
3682
Paul Elliottc9774412023-02-06 15:14:07 +00003683 /* Check that start has not been previously called, or operation has not
3684 * previously errored. */
3685 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003686 return PSA_ERROR_BAD_STATE;
3687 }
3688
3689 status = psa_sign_verify_check_alg(0, alg);
3690 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003691 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003692 return status;
3693 }
3694
3695 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3696 PSA_KEY_USAGE_VERIFY_HASH,
3697 alg);
3698
3699 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003700 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003701 return status;
3702 }
3703
David Horstmann0fea6a52024-03-11 13:41:05 +00003704 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3705 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3706
Paul Elliott296ede92022-12-15 17:00:30 +00003707 /* Ensure ops count gets reset, in case of operation re-use. */
3708 operation->num_ops = 0;
3709
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003710 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003711 slot->key.data,
3712 slot->key.bytes,
3713 alg, hash, hash_length,
3714 signature, signature_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00003715#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann0fea6a52024-03-11 13:41:05 +00003716exit:
3717#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003718
3719 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003720 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003721 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003722 }
3723
Ryan Everett291267f2024-02-14 15:59:15 +00003724 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003725
Paul Elliottc9774412023-02-06 15:14:07 +00003726 if (unlock_status != PSA_SUCCESS) {
3727 operation->error_occurred = 1;
3728 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003729
David Horstmann0fea6a52024-03-11 13:41:05 +00003730 LOCAL_INPUT_FREE(hash_external, hash);
3731 LOCAL_INPUT_FREE(signature_external, signature);
3732
Paul Elliottc9774412023-02-06 15:14:07 +00003733 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003734}
3735
3736psa_status_t psa_verify_hash_complete(
3737 psa_verify_hash_interruptible_operation_t *operation)
3738{
3739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3740
Paul Elliottc9774412023-02-06 15:14:07 +00003741 /* Check that start has been called first, and that operation has not
3742 * previously errored. */
3743 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003744 status = PSA_ERROR_BAD_STATE;
3745 goto exit;
3746 }
3747
3748 status = psa_driver_wrapper_verify_hash_complete(operation);
3749
Paul Elliott296ede92022-12-15 17:00:30 +00003750 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003751 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003752 operation);
3753
Paul Elliottebe225c2023-02-10 14:32:53 +00003754exit:
3755
Paul Elliott9fe12f62022-11-30 19:16:02 +00003756 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003757 if (status != PSA_SUCCESS) {
3758 operation->error_occurred = 1;
3759 }
3760
Paul Elliott59ad9452022-12-18 15:09:02 +00003761 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003762 }
3763
3764 return status;
3765}
3766
3767psa_status_t psa_verify_hash_abort(
3768 psa_verify_hash_interruptible_operation_t *operation)
3769{
Paul Elliott59ad9452022-12-18 15:09:02 +00003770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003771
Paul Elliott59ad9452022-12-18 15:09:02 +00003772 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003773
Paul Elliott59ad9452022-12-18 15:09:02 +00003774 /* We clear the number of ops done here, so that it is not cleared when
3775 * the operation fails or succeeds, only on manual abort. */
3776 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003777
Paul Elliottc9774412023-02-06 15:14:07 +00003778 /* Likewise, failure state. */
3779 operation->error_occurred = 0;
3780
Paul Elliott59ad9452022-12-18 15:09:02 +00003781 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003782}
3783
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784/****************************************************************/
3785/* Asymmetric interruptible cryptography internal */
3786/* implementations */
3787/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003788
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3790{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003791
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3793 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3794 defined(MBEDTLS_ECP_RESTARTABLE)
3795
3796 /* Internal implementation uses zero to indicate infinite number max ops,
3797 * therefore avoid this value, and set to minimum possible. */
3798 if (max_ops == 0) {
3799 max_ops = 1;
3800 }
3801
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003803#else
3804 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3806 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3807 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3808}
3809
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003811 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812{
3813#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3814 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3815 defined(MBEDTLS_ECP_RESTARTABLE)
3816
Paul Elliott93d9ca82023-02-15 18:14:21 +00003817 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003818#else
3819 (void) operation;
3820 return 0;
3821#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3822 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3823 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3824}
3825
3826uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003827 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828{
3829 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3830 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3831 defined(MBEDTLS_ECP_RESTARTABLE)
3832
Paul Elliott93d9ca82023-02-15 18:14:21 +00003833 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834#else
3835 (void) operation;
3836 return 0;
3837#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3838 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3839 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3840}
3841
3842psa_status_t mbedtls_psa_sign_hash_start(
3843 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3844 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3845 size_t key_buffer_size, psa_algorithm_t alg,
3846 const uint8_t *hash, size_t hash_length)
3847{
3848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003849 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003851 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003852 return PSA_ERROR_NOT_SUPPORTED;
3853 }
3854
3855 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003856 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003857 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858
3859#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003860 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3861 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862
Paul Elliotta1c94092023-02-15 16:38:04 +00003863 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3864
Paul Elliott93d9ca82023-02-15 18:14:21 +00003865 /* Ensure num_ops is zero'ed in case of context re-use. */
3866 operation->num_ops = 0;
3867
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003868 status = mbedtls_psa_ecp_load_representation(attributes->type,
3869 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003870 key_buffer,
3871 key_buffer_size,
3872 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873
Paul Elliott068fe072023-01-16 13:59:15 +00003874 if (status != PSA_SUCCESS) {
3875 return status;
3876 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003877
Paul Elliott1bc59df2023-02-05 13:41:57 +00003878 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003879 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880
Paul Elliott068fe072023-01-16 13:59:15 +00003881 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003882 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003883 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884
Paul Elliott0290a762023-02-09 14:30:24 +00003885 /* We only need to store the same length of hash as the private key size
3886 * here, it would be truncated by the internal implementation anyway. */
3887 required_hash_length = (hash_length < operation->coordinate_bytes ?
3888 hash_length : operation->coordinate_bytes);
3889
Paul Elliottba70ad42023-02-15 18:23:53 +00003890 if (required_hash_length > sizeof(operation->hash)) {
3891 /* Shouldn't happen, but better safe than sorry. */
3892 return PSA_ERROR_CORRUPTION_DETECTED;
3893 }
3894
Paul Elliott0290a762023-02-09 14:30:24 +00003895 memcpy(operation->hash, hash, required_hash_length);
3896 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003897
3898 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003899
3900#else
Paul Elliott068fe072023-01-16 13:59:15 +00003901 (void) operation;
3902 (void) key_buffer;
3903 (void) key_buffer_size;
3904 (void) alg;
3905 (void) hash;
3906 (void) hash_length;
3907 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003908 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003909
Paul Elliott068fe072023-01-16 13:59:15 +00003910 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003911#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3912 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3913 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914}
3915
3916psa_status_t mbedtls_psa_sign_hash_complete(
3917 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3918 uint8_t *signature, size_t signature_size,
3919 size_t *signature_length)
3920{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3922 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3923 defined(MBEDTLS_ECP_RESTARTABLE)
3924
Paul Elliotta1c94092023-02-15 16:38:04 +00003925 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003926 mbedtls_mpi r;
3927 mbedtls_mpi s;
3928
Paul Elliott46845252023-02-03 14:59:11 +00003929 mbedtls_mpi_init(&r);
3930 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003932 /* Ensure max_ops is set to the current value (or default). */
3933 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3934
Paul Elliotta1c94092023-02-15 16:38:04 +00003935 if (signature_size < 2 * operation->coordinate_bytes) {
3936 status = PSA_ERROR_BUFFER_TOO_SMALL;
3937 goto exit;
3938 }
3939
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003941
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3943 status = mbedtls_to_psa_error(
3944 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003945 &r,
3946 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947 &operation->ctx->d,
3948 operation->hash,
3949 operation->hash_length,
3950 operation->md_alg,
3951 mbedtls_psa_get_random,
3952 MBEDTLS_PSA_RANDOM_STATE,
3953 &operation->restart_ctx));
3954#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003955 status = PSA_ERROR_NOT_SUPPORTED;
3956 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3958 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959 status = mbedtls_to_psa_error(
3960 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003961 &r,
3962 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963 &operation->ctx->d,
3964 operation->hash,
3965 operation->hash_length,
3966 mbedtls_psa_get_random,
3967 MBEDTLS_PSA_RANDOM_STATE,
3968 mbedtls_psa_get_random,
3969 MBEDTLS_PSA_RANDOM_STATE,
3970 &operation->restart_ctx));
3971 }
3972
Paul Elliottf8e5b562023-02-19 18:43:45 +00003973 /* Hide the fact that the restart context only holds a delta of number of
3974 * ops done during the last operation, not an absolute value. */
3975 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3976
Paul Elliott724bd252023-02-08 12:35:08 +00003977 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003979 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003981 operation->coordinate_bytes)
3982 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983
3984 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003985 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003986 }
3987
3988 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003989 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003990 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003991 operation->coordinate_bytes,
3992 operation->coordinate_bytes)
3993 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003994
3995 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003996 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997 }
3998
Paul Elliott1bc59df2023-02-05 13:41:57 +00003999 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004000
Paul Elliott724bd252023-02-08 12:35:08 +00004001 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004002 }
Paul Elliott724bd252023-02-08 12:35:08 +00004003
4004exit:
4005
4006 mbedtls_mpi_free(&r);
4007 mbedtls_mpi_free(&s);
4008 return status;
4009
Paul Elliott588f8ed2022-12-02 18:10:26 +00004010 #else
4011
4012 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004013 (void) signature;
4014 (void) signature_size;
4015 (void) signature_length;
4016
4017 return PSA_ERROR_NOT_SUPPORTED;
4018
4019#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4020 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4021 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4022}
4023
4024psa_status_t mbedtls_psa_sign_hash_abort(
4025 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
4026{
4027
4028#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4029 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4030 defined(MBEDTLS_ECP_RESTARTABLE)
4031
4032 if (operation->ctx) {
4033 mbedtls_ecdsa_free(operation->ctx);
4034 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004035 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004036 }
4037
4038 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4039
Paul Elliott93d9ca82023-02-15 18:14:21 +00004040 operation->num_ops = 0;
4041
Paul Elliott588f8ed2022-12-02 18:10:26 +00004042 return PSA_SUCCESS;
4043
4044#else
4045
4046 (void) operation;
4047
4048 return PSA_ERROR_NOT_SUPPORTED;
4049
4050#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4051 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4052 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4053}
4054
4055psa_status_t mbedtls_psa_verify_hash_start(
4056 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
4057 const psa_key_attributes_t *attributes,
4058 const uint8_t *key_buffer, size_t key_buffer_size,
4059 psa_algorithm_t alg,
4060 const uint8_t *hash, size_t hash_length,
4061 const uint8_t *signature, size_t signature_length)
4062{
4063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00004064 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00004065 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004066
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004067 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00004068 return PSA_ERROR_NOT_SUPPORTED;
4069 }
4070
4071 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004072 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004073 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004074
4075#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004076 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4077 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004078
Paul Elliotta1c94092023-02-15 16:38:04 +00004079 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4080 mbedtls_mpi_init(&operation->r);
4081 mbedtls_mpi_init(&operation->s);
4082
Paul Elliott93d9ca82023-02-15 18:14:21 +00004083 /* Ensure num_ops is zero'ed in case of context re-use. */
4084 operation->num_ops = 0;
4085
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004086 status = mbedtls_psa_ecp_load_representation(attributes->type,
4087 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004088 key_buffer,
4089 key_buffer_size,
4090 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004091
Paul Elliott068fe072023-01-16 13:59:15 +00004092 if (status != PSA_SUCCESS) {
4093 return status;
4094 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004095
Paul Elliottc569fc22023-02-10 13:02:54 +00004096 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004097
Paul Elliott1bc59df2023-02-05 13:41:57 +00004098 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004099 return PSA_ERROR_INVALID_SIGNATURE;
4100 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004101
Paul Elliott068fe072023-01-16 13:59:15 +00004102 status = mbedtls_to_psa_error(
4103 mbedtls_mpi_read_binary(&operation->r,
4104 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004105 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004106
Paul Elliott068fe072023-01-16 13:59:15 +00004107 if (status != PSA_SUCCESS) {
4108 return status;
4109 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004110
Paul Elliott068fe072023-01-16 13:59:15 +00004111 status = mbedtls_to_psa_error(
4112 mbedtls_mpi_read_binary(&operation->s,
4113 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004114 coordinate_bytes,
4115 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004116
Paul Elliott068fe072023-01-16 13:59:15 +00004117 if (status != PSA_SUCCESS) {
4118 return status;
4119 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004120
Paul Elliott2c9843f2023-02-15 17:32:42 +00004121 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004122
Paul Elliott2c9843f2023-02-15 17:32:42 +00004123 if (status != PSA_SUCCESS) {
4124 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004125 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004126
Paul Elliott0290a762023-02-09 14:30:24 +00004127 /* We only need to store the same length of hash as the private key size
4128 * here, it would be truncated by the internal implementation anyway. */
4129 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4130 coordinate_bytes);
4131
Paul Elliottba70ad42023-02-15 18:23:53 +00004132 if (required_hash_length > sizeof(operation->hash)) {
4133 /* Shouldn't happen, but better safe than sorry. */
4134 return PSA_ERROR_CORRUPTION_DETECTED;
4135 }
4136
Paul Elliott0290a762023-02-09 14:30:24 +00004137 memcpy(operation->hash, hash, required_hash_length);
4138 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004139
4140 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004141#else
Paul Elliott068fe072023-01-16 13:59:15 +00004142 (void) operation;
4143 (void) key_buffer;
4144 (void) key_buffer_size;
4145 (void) alg;
4146 (void) hash;
4147 (void) hash_length;
4148 (void) signature;
4149 (void) signature_length;
4150 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004151 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004152 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004153
Paul Elliott068fe072023-01-16 13:59:15 +00004154 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004155#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4156 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4157 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004158}
4159
4160psa_status_t mbedtls_psa_verify_hash_complete(
4161 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4162{
4163
4164#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4165 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4166 defined(MBEDTLS_ECP_RESTARTABLE)
4167
Paul Elliottf8e5b562023-02-19 18:43:45 +00004168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4169
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004170 /* Ensure max_ops is set to the current value (or default). */
4171 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4172
Paul Elliottf8e5b562023-02-19 18:43:45 +00004173 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004174 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4175 operation->hash,
4176 operation->hash_length,
4177 &operation->ctx->Q,
4178 &operation->r,
4179 &operation->s,
4180 &operation->restart_ctx));
4181
Paul Elliottf8e5b562023-02-19 18:43:45 +00004182 /* Hide the fact that the restart context only holds a delta of number of
4183 * ops done during the last operation, not an absolute value. */
4184 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4185
4186 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004187#else
4188 (void) operation;
4189
4190 return PSA_ERROR_NOT_SUPPORTED;
4191
4192#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4193 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4194 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4195}
4196
4197psa_status_t mbedtls_psa_verify_hash_abort(
4198 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4199{
4200
4201#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4202 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4203 defined(MBEDTLS_ECP_RESTARTABLE)
4204
4205 if (operation->ctx) {
4206 mbedtls_ecdsa_free(operation->ctx);
4207 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004208 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004209 }
4210
4211 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4212
Paul Elliott93d9ca82023-02-15 18:14:21 +00004213 operation->num_ops = 0;
4214
Paul Elliott588f8ed2022-12-02 18:10:26 +00004215 mbedtls_mpi_free(&operation->r);
4216 mbedtls_mpi_free(&operation->s);
4217
4218 return PSA_SUCCESS;
4219
4220#else
4221 (void) operation;
4222
4223 return PSA_ERROR_NOT_SUPPORTED;
4224
4225#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4226 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4227 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4228}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004229
David Horstmann6e99bb22024-02-06 15:27:49 +00004230static psa_status_t psa_generate_random_internal(uint8_t *output,
4231 size_t output_size)
4232{
4233 GUARD_MODULE_INITIALIZED;
4234
David Horstmann6e99bb22024-02-06 15:27:49 +00004235#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4236
David Horstmanndb909142024-03-12 16:07:08 +00004237 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004238 size_t output_length = 0;
4239 status = mbedtls_psa_external_get_random(&global_data.rng,
4240 output, output_size,
4241 &output_length);
4242 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004243 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004244 }
4245 /* Breaking up a request into smaller chunks is currently not supported
4246 * for the external RNG interface. */
4247 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004248 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004249 }
David Horstmanndb909142024-03-12 16:07:08 +00004250 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004251
4252#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4253
4254 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004255 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004256 size_t request_size =
4257 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4258 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4259 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004260#if defined(MBEDTLS_CTR_DRBG_C)
4261 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4262#elif defined(MBEDTLS_HMAC_DRBG_C)
4263 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4264#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004265 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004266 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004267 }
4268 output_size -= request_size;
4269 output += request_size;
4270 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004271 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004272#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004273}
4274
4275
mohammad1603503973b2018-03-12 15:59:30 +02004276/****************************************************************/
4277/* Symmetric cryptography */
4278/****************************************************************/
4279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4281 mbedtls_svc_key_id_t key,
4282 psa_algorithm_t alg,
4283 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004284{
4285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4286 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004287 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4289 PSA_KEY_USAGE_ENCRYPT :
4290 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004291
4292 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004294 status = PSA_ERROR_BAD_STATE;
4295 goto exit;
4296 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004299 status = PSA_ERROR_INVALID_ARGUMENT;
4300 goto exit;
4301 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4304 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004307
Ronald Cron49fafa92021-03-10 08:34:23 +01004308 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004309 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004310 * so we only set it (in the driver wrapper) after resources have been
4311 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004312 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004314 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004316 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 }
4318 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004319
4320 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (cipher_operation == MBEDTLS_ENCRYPT) {
4322 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004323 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 slot->key.data,
4325 slot->key.bytes,
4326 alg);
4327 } else {
4328 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004329 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 slot->key.data,
4331 slot->key.bytes,
4332 alg);
4333 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004334
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004335exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (status != PSA_SUCCESS) {
4337 psa_cipher_abort(operation);
4338 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004339
Ryan Everettc0053cc2024-02-14 16:27:13 +00004340 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004343}
4344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4346 mbedtls_svc_key_id_t key,
4347 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004348{
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004350}
4351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4353 mbedtls_svc_key_id_t key,
4354 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004355{
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004357}
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004360 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 size_t iv_size,
4362 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004363{
Ronald Cron6d051732020-10-01 14:10:20 +02004364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004365 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004366
Gabor Mezei358eb212024-02-07 18:10:13 +01004367 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004370 status = PSA_ERROR_BAD_STATE;
4371 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004372 }
4373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004375 status = PSA_ERROR_BAD_STATE;
4376 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004377 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004378
Ronald Cron2fb90522021-07-05 12:31:44 +02004379 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004381 status = PSA_ERROR_BUFFER_TOO_SMALL;
4382 goto exit;
4383 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004386 status = PSA_ERROR_GENERIC_ERROR;
4387 goto exit;
4388 }
4389
Gabor Mezei358eb212024-02-07 18:10:13 +01004390 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4391
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004392 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 }
Ronald Cron5618a392021-03-26 09:52:26 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004398 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004399
4400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004402 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004403 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004405 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004407 if (iv != NULL) {
4408 mbedtls_platform_zeroize(iv, default_iv_length);
4409 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004410 }
Ronald Cron6d051732020-10-01 14:10:20 +02004411
Gabor Mezei358eb212024-02-07 18:10:13 +01004412 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004414}
4415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004417 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004419{
Ronald Cron6d051732020-10-01 14:10:20 +02004420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004421
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004422 LOCAL_INPUT_DECLARE(iv_external, iv);
4423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004425 status = PSA_ERROR_BAD_STATE;
4426 goto exit;
4427 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004430 status = PSA_ERROR_BAD_STATE;
4431 goto exit;
4432 }
Ronald Crona0d68172021-03-26 10:15:08 +01004433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004435 status = PSA_ERROR_INVALID_ARGUMENT;
4436 goto exit;
4437 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004438
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004439 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 status = psa_driver_wrapper_cipher_set_iv(operation,
4442 iv,
4443 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004444
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004447 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 } else {
4449 psa_cipher_abort(operation);
4450 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004451
4452 LOCAL_INPUT_FREE(iv_external, iv);
4453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004455}
4456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004458 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004460 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 size_t output_size,
4462 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004463{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004464 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004465
Gabor Mezei212eb082024-01-24 16:56:00 +01004466 LOCAL_INPUT_DECLARE(input_external, input);
4467 LOCAL_OUTPUT_DECLARE(output_external, output);
4468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004470 status = PSA_ERROR_BAD_STATE;
4471 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004472 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004475 status = PSA_ERROR_BAD_STATE;
4476 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004477 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004478
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004479 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004480 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 status = psa_driver_wrapper_cipher_update(operation,
4483 input,
4484 input_length,
4485 output,
4486 output_size,
4487 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004488
4489exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (status != PSA_SUCCESS) {
4491 psa_cipher_abort(operation);
4492 }
Ronald Cron6d051732020-10-01 14:10:20 +02004493
Gabor Mezei212eb082024-01-24 16:56:00 +01004494 LOCAL_INPUT_FREE(input_external, input);
4495 LOCAL_OUTPUT_FREE(output_external, output);
4496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004498}
4499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004501 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 size_t output_size,
4503 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004504{
David Saadab4ecc272019-02-14 13:48:10 +02004505 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004506
Gabor Mezei212eb082024-01-24 16:56:00 +01004507 LOCAL_OUTPUT_DECLARE(output_external, output);
4508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004510 status = PSA_ERROR_BAD_STATE;
4511 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004512 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004515 status = PSA_ERROR_BAD_STATE;
4516 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004517 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004518
Gabor Mezei0b041162024-02-29 10:08:16 +00004519 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 status = psa_driver_wrapper_cipher_finish(operation,
4522 output,
4523 output_size,
4524 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004525
4526exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 if (status == PSA_SUCCESS) {
Gabor Mezei212eb082024-01-24 16:56:00 +01004528 status = psa_cipher_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004530 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02004532 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004533
4534 LOCAL_OUTPUT_FREE(output_external, output);
4535
4536 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004537}
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004540{
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004542 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004543 * in use. It's ok to call abort on such an object, and there's
4544 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004546 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004549
Ronald Cron49fafa92021-03-10 08:34:23 +01004550 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004551 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004552 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004555}
4556
Gilles Peskine449bd832023-01-11 14:50:10 +01004557psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4558 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004559 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004561 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 size_t output_size,
4563 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004564{
4565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004566 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004567 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004568 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4569 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004570
David Horstmann62a56d92023-12-14 18:12:47 +00004571 LOCAL_INPUT_DECLARE(input_external, input);
4572 LOCAL_OUTPUT_DECLARE(output_external, output);
4573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004575 status = PSA_ERROR_INVALID_ARGUMENT;
4576 goto exit;
4577 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4580 PSA_KEY_USAGE_ENCRYPT,
4581 alg);
4582 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4587 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004588 status = PSA_ERROR_GENERIC_ERROR;
4589 goto exit;
4590 }
4591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 if (default_iv_length > 0) {
4593 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004594 status = PSA_ERROR_BUFFER_TOO_SMALL;
4595 goto exit;
4596 }
4597
David Horstmann6e99bb22024-02-06 15:27:49 +00004598 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004602 }
4603
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004604 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4605 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4606
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004607 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004608 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004609 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004610 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004612
4613exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004614 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004616 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004617 }
Ronald Cron23919522021-07-08 19:00:07 +02004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if (status == PSA_SUCCESS) {
4620 if (default_iv_length > 0) {
4621 memcpy(output, local_iv, default_iv_length);
4622 }
4623 *output_length += default_iv_length;
4624 } else {
4625 *output_length = 0;
4626 }
4627
David Horstmann62a56d92023-12-14 18:12:47 +00004628 LOCAL_INPUT_FREE(input_external, input);
4629 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004632}
4633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4635 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004636 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004638 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 size_t output_size,
4640 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004641{
4642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004643 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004644 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004645
Gabor Mezei212eb082024-01-24 16:56:00 +01004646 LOCAL_INPUT_DECLARE(input_external, input);
4647 LOCAL_OUTPUT_DECLARE(output_external, output);
4648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004650 status = PSA_ERROR_INVALID_ARGUMENT;
4651 goto exit;
4652 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4655 PSA_KEY_USAGE_DECRYPT,
4656 alg);
4657 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004660
Gilles Peskineb47c3b32024-06-26 13:16:33 +02004661 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004662 status = PSA_ERROR_INVALID_ARGUMENT;
4663 goto exit;
4664 }
4665
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004666 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4667 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4668
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004669 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004670 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004671 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004673
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004674exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004675 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004677 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004681 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 }
Ronald Cron23919522021-07-08 19:00:07 +02004683
Gabor Mezei212eb082024-01-24 16:56:00 +01004684 LOCAL_INPUT_FREE(input_external, input);
4685 LOCAL_OUTPUT_FREE(output_external, output);
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004688}
4689
4690
mohammad16035955c982018-04-26 00:53:03 +03004691/****************************************************************/
4692/* AEAD */
4693/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004694
Paul Elliottbaff51c2021-09-28 17:44:45 +01004695/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004696static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004697{
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004699}
4700
4701/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4703 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004704{
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004708#if defined(PSA_WANT_ALG_GCM)
4709 case PSA_ALG_GCM:
4710 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 * arbitrarily large nonces. Please note that we do not generally
4712 * recommend the usage of nonces of greater length than
4713 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4714 * size, which can then lead to collisions if you encrypt a very
4715 * large number of messages.*/
4716 if (nonce_length != 0) {
4717 return PSA_SUCCESS;
4718 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004719 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004720#endif /* PSA_WANT_ALG_GCM */
4721#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004722 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (nonce_length >= 7 && nonce_length <= 13) {
4724 return PSA_SUCCESS;
4725 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004726 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004727#endif /* PSA_WANT_ALG_CCM */
4728#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004729 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (nonce_length == 12) {
4731 return PSA_SUCCESS;
4732 } else if (nonce_length == 8) {
4733 return PSA_ERROR_NOT_SUPPORTED;
4734 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004735 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004736#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004737 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004738 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004740 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004743}
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004746{
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4748 return PSA_ERROR_INVALID_ARGUMENT;
4749 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004752}
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4755 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004756 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004758 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004760 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004762 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 size_t ciphertext_size,
4764 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004765{
Ronald Cron215633c2021-03-16 17:15:37 +01004766 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4767 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004768
David Horstmann9d09a022023-11-28 19:25:00 +00004769 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4770 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4771 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4772 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4773
mohammad1603f08a5502018-06-03 15:05:47 +03004774 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 status = psa_aead_check_algorithm(alg);
4777 if (status != PSA_SUCCESS) {
4778 return status;
4779 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004780
Ronald Cron9a986162021-03-26 12:40:07 +01004781 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4783 if (status != PSA_SUCCESS) {
4784 return status;
4785 }
mohammad16035955c982018-04-26 00:53:03 +03004786
David Horstmann9d09a022023-11-28 19:25:00 +00004787 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4788 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4789 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4790 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_aead_check_nonce_length(alg, nonce_length);
4793 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004796
Ronald Cronde822812021-03-17 16:08:20 +01004797 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004798 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004799 alg,
4800 nonce, nonce_length,
4801 additional_data, additional_data_length,
4802 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4806 memset(ciphertext, 0, ciphertext_size);
4807 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004808
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004809exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004810 LOCAL_INPUT_FREE(nonce_external, nonce);
4811 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4812 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4813 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4814
Ryan Everetta103ec92024-01-31 13:59:57 +00004815 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004818}
4819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4821 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00004822 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004824 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004826 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004828 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 size_t plaintext_size,
4830 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004831{
Ronald Cron215633c2021-03-16 17:15:37 +01004832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4833 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004834
David Horstmann7f2e0402023-11-28 19:43:53 +00004835 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4836 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4837 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4838 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4839
Gilles Peskineee652a32018-06-01 19:23:52 +02004840 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 status = psa_aead_check_algorithm(alg);
4843 if (status != PSA_SUCCESS) {
4844 return status;
4845 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004846
Ronald Cron9a986162021-03-26 12:40:07 +01004847 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4849 if (status != PSA_SUCCESS) {
4850 return status;
4851 }
mohammad16035955c982018-04-26 00:53:03 +03004852
David Horstmann7f2e0402023-11-28 19:43:53 +00004853 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4854 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4855 additional_data);
4856 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4857 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 status = psa_aead_check_nonce_length(alg, nonce_length);
4860 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004861 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004863
Ronald Cronde822812021-03-17 16:08:20 +01004864 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004865 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004866 alg,
4867 nonce, nonce_length,
4868 additional_data, additional_data_length,
4869 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 if (status != PSA_SUCCESS && plaintext_size != 0) {
4873 memset(plaintext, 0, plaintext_size);
4874 }
mohammad160360a64d02018-06-03 17:20:42 +03004875
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004876exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00004877 LOCAL_INPUT_FREE(nonce_external, nonce);
4878 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4879 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4880 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4881
Ryan Everetta103ec92024-01-31 13:59:57 +00004882 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 return status;
mohammad16035955c982018-04-26 00:53:03 +03004885}
4886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4888{
4889 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004892#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004894 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4896 return PSA_ERROR_INVALID_ARGUMENT;
4897 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004898 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004899#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004900
Przemek Stekiel86679c72022-10-06 17:06:56 +02004901#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004903 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4905 return PSA_ERROR_INVALID_ARGUMENT;
4906 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004907 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004908#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004909
Przemek Stekiel86679c72022-10-06 17:06:56 +02004910#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004912 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (tag_len != 16) {
4914 return PSA_ERROR_INVALID_ARGUMENT;
4915 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004916 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004917#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004918
4919 default:
4920 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004922 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004924}
4925
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004926/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004927static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4928 int is_encrypt,
4929 mbedtls_svc_key_id_t key,
4930 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004931{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4933 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4934 psa_key_slot_t *slot = NULL;
4935 psa_key_usage_t key_usage = 0;
4936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 status = psa_aead_check_algorithm(alg);
4938 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004939 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004943 status = PSA_ERROR_BAD_STATE;
4944 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004945 }
4946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (operation->nonce_set || operation->lengths_set ||
4948 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004954 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004956 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4960 alg);
4961 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004962 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004966 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 if (is_encrypt) {
4970 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004971 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 slot->key.data,
4973 slot->key.bytes,
4974 alg);
4975 } else {
4976 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004977 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 slot->key.data,
4979 slot->key.bytes,
4980 alg);
4981 }
4982 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004985
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004986 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004987
4988exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004989 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004992 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004994 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 } else {
4996 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004997 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01005000}
5001
Paul Elliott302ff6b2021-04-20 18:10:30 +01005002/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005003psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
5004 mbedtls_svc_key_id_t key,
5005 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005006{
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008}
5009
5010/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005011psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
5012 mbedtls_svc_key_id_t key,
5013 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005014{
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005016}
5017
David Horstmannfed23772023-12-19 17:49:20 +00005018static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
5019 const uint8_t *nonce,
5020 size_t nonce_length)
5021{
5022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5023
5024 if (operation->id == 0) {
5025 status = PSA_ERROR_BAD_STATE;
5026 goto exit;
5027 }
5028
5029 if (operation->nonce_set) {
5030 status = PSA_ERROR_BAD_STATE;
5031 goto exit;
5032 }
5033
5034 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
5035 if (status != PSA_SUCCESS) {
5036 status = PSA_ERROR_INVALID_ARGUMENT;
5037 goto exit;
5038 }
5039
5040 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
5041 nonce_length);
5042
5043exit:
5044 if (status == PSA_SUCCESS) {
5045 operation->nonce_set = 1;
5046 } else {
5047 psa_aead_abort(operation);
5048 }
5049
5050 return status;
5051}
5052
Paul Elliott302ff6b2021-04-20 18:10:30 +01005053/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00005055 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 size_t nonce_size,
5057 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058{
5059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01005060 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07005061 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005062
David Horstmannd3cad8b2023-12-11 14:46:04 +00005063 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
5064 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
5065
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005066 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005069 status = PSA_ERROR_BAD_STATE;
5070 goto exit;
5071 }
5072
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005074 status = PSA_ERROR_BAD_STATE;
5075 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005076 }
5077
Paul Elliotte193ea82021-10-01 13:00:16 +01005078 /* For CCM, this size may not be correct according to the PSA
5079 * specification. The PSA Crypto 1.0.1 specification states:
5080 *
5081 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5082 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5083 *
5084 * However this restriction that L has to be the smallest integer is not
5085 * applied in practice, and it is not implementable here since the
5086 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5088 operation->alg);
5089 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005090 status = PSA_ERROR_BUFFER_TOO_SMALL;
5091 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005092 }
5093
David Horstmann6e99bb22024-02-06 15:27:49 +00005094 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005098
David Horstmannfed23772023-12-19 17:49:20 +00005099 status = psa_aead_set_nonce_internal(operation, local_nonce,
5100 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101
Paul Elliott39dc6b82021-05-11 19:16:09 +01005102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 if (status == PSA_SUCCESS) {
5104 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005105 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 } else {
5107 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005108 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005109
David Horstmannd3cad8b2023-12-11 14:46:04 +00005110 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005113}
5114
5115/* Set the nonce for a multipart authenticated encryption or decryption
5116 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005117psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005118 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005120{
David Horstmannfed23772023-12-19 17:49:20 +00005121 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005122
David Horstmann8f0ef512023-12-11 15:17:11 +00005123 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5124 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005125
David Horstmannfed23772023-12-19 17:49:20 +00005126 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005127
David Horstmann18dc0322023-12-20 16:16:43 +00005128/* Exit label is only needed for buffer copying, prevent unused warnings. */
David Horstmann4a48bec2024-03-13 15:42:31 +00005129#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005130exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005131#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005132
David Horstmann8f0ef512023-12-11 15:17:11 +00005133 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136}
5137
5138/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5140 size_t ad_length,
5141 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005142{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005146 status = PSA_ERROR_BAD_STATE;
5147 goto exit;
5148 }
5149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (operation->lengths_set || operation->ad_started ||
5151 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005152 status = PSA_ERROR_BAD_STATE;
5153 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005154 }
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005157#if defined(PSA_WANT_ALG_GCM)
5158 case PSA_ALG_GCM:
5159 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 * bits. Without the guard this code will generate warnings on 32bit
5161 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005162#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (((uint64_t) ad_length) >> 61 != 0 ||
5164 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005165 status = PSA_ERROR_INVALID_ARGUMENT;
5166 goto exit;
5167 }
Paul Elliott325d3742021-09-27 17:56:28 +01005168#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005169 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005170#endif /* PSA_WANT_ALG_GCM */
5171#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005172 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005174 status = PSA_ERROR_INVALID_ARGUMENT;
5175 goto exit;
5176 }
5177 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005178#endif /* PSA_WANT_ALG_CCM */
5179#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005180 case PSA_ALG_CHACHA20_POLY1305:
5181 /* No length restrictions for ChaChaPoly. */
5182 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005183#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005184 default:
5185 break;
5186 }
Paul Elliott325d3742021-09-27 17:56:28 +01005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5189 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005190
Paul Elliott39dc6b82021-05-11 19:16:09 +01005191exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005193 operation->ad_remaining = ad_length;
5194 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005195 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 } else {
5197 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005198 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005201}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005202
5203/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005204psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005205 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005207{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5209
David Horstmann25dac6e2023-12-11 15:23:13 +00005210 LOCAL_INPUT_DECLARE(input_external, input);
5211 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5212
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005214 status = PSA_ERROR_BAD_STATE;
5215 goto exit;
5216 }
5217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005219 status = PSA_ERROR_BAD_STATE;
5220 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005221 }
5222
Paul Elliott304766f2024-04-26 18:30:11 +01005223 /* No input to add (zero length), nothing to do. */
5224 if (input_length == 0) {
5225 status = PSA_SUCCESS;
5226 goto exit;
5227 }
5228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (operation->lengths_set) {
5230 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005231 status = PSA_ERROR_INVALID_ARGUMENT;
5232 goto exit;
5233 }
5234
5235 operation->ad_remaining -= input_length;
5236 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005237#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005239 status = PSA_ERROR_BAD_STATE;
5240 goto exit;
5241 }
5242#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 status = psa_driver_wrapper_aead_update_ad(operation, input,
5245 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005246
Paul Elliott39dc6b82021-05-11 19:16:09 +01005247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005249 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 } else {
5251 psa_aead_abort(operation);
5252 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005253
David Horstmann25dac6e2023-12-11 15:23:13 +00005254 LOCAL_INPUT_FREE(input_external, input);
5255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005257}
5258
5259/* Encrypt or decrypt a message fragment in an active multipart AEAD
5260 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005261psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005262 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005264 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 size_t output_size,
5266 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005267{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005269
David Horstmann2914fac2023-12-11 15:28:37 +00005270
5271 LOCAL_INPUT_DECLARE(input_external, input);
5272 LOCAL_OUTPUT_DECLARE(output_external, output);
5273
5274 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5275 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5276
Paul Elliott302ff6b2021-04-20 18:10:30 +01005277 *output_length = 0;
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005280 status = PSA_ERROR_BAD_STATE;
5281 goto exit;
5282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005285 status = PSA_ERROR_BAD_STATE;
5286 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005287 }
5288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005290 /* Additional data length was supplied, but not all the additional
5291 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005293 status = PSA_ERROR_INVALID_ARGUMENT;
5294 goto exit;
5295 }
5296
5297 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005299 status = PSA_ERROR_INVALID_ARGUMENT;
5300 goto exit;
5301 }
5302
5303 operation->body_remaining -= input_length;
5304 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005305#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005307 status = PSA_ERROR_BAD_STATE;
5308 goto exit;
5309 }
5310#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005311
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5313 output, output_size,
5314 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005315
Paul Elliott39dc6b82021-05-11 19:16:09 +01005316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005318 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 } else {
5320 psa_aead_abort(operation);
5321 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005322
David Horstmann2914fac2023-12-11 15:28:37 +00005323 LOCAL_INPUT_FREE(input_external, input);
5324 LOCAL_OUTPUT_FREE(output_external, output);
5325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005327}
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005330{
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 if (operation->id == 0 || !operation->nonce_set) {
5332 return PSA_ERROR_BAD_STATE;
5333 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5336 operation->body_remaining != 0)) {
5337 return PSA_ERROR_INVALID_ARGUMENT;
5338 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005341}
5342
Paul Elliott302ff6b2021-04-20 18:10:30 +01005343/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005345 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 size_t ciphertext_size,
5347 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005348 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 size_t tag_size,
5350 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005351{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5353
David Horstmann6db0e732023-12-11 15:35:59 +00005354 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5355 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5356
5357 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5358 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5359
Paul Elliott302ff6b2021-04-20 18:10:30 +01005360 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005361 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 status = psa_aead_final_checks(operation);
5364 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005369 status = PSA_ERROR_BAD_STATE;
5370 goto exit;
5371 }
5372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5374 ciphertext_size,
5375 ciphertext_length,
5376 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005377
Paul Elliott39dc6b82021-05-11 19:16:09 +01005378exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005379
5380
Paul Elliottf47b0952021-05-21 18:02:33 +01005381 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005382 * the zero tag size, make sure the tag is set to something implausible.
5383 * Even if the operation succeeds, make sure we clear the rest of the
5384 * buffer to prevent potential leakage of anything previously placed in
5385 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005386 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005389
David Horstmann6db0e732023-12-11 15:35:59 +00005390 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5391 LOCAL_OUTPUT_FREE(tag_external, tag);
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005394}
5395
5396/* Finish authenticating and decrypting a message in a multipart AEAD
5397 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005398psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005399 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 size_t plaintext_size,
5401 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005402 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005404{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5406
David Horstmanne000a0a2023-12-11 16:37:04 +00005407 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5408 LOCAL_INPUT_DECLARE(tag_external, tag);
5409
5410 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5411 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5412
Paul Elliott302ff6b2021-04-20 18:10:30 +01005413 *plaintext_length = 0;
5414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 status = psa_aead_final_checks(operation);
5416 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005419
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005421 status = PSA_ERROR_BAD_STATE;
5422 goto exit;
5423 }
5424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5426 plaintext_size,
5427 plaintext_length,
5428 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005429
5430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005432
David Horstmanne000a0a2023-12-11 16:37:04 +00005433 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5434 LOCAL_INPUT_FREE(tag_external, tag);
5435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005437}
5438
5439/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005440psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005441{
5442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005445 /* The object has (apparently) been initialized but it is not (yet)
5446 * in use. It's ok to call abort on such an object, and there's
5447 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005449 }
5450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005456}
5457
Gilles Peskinee59236f2018-01-27 23:32:46 +01005458/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005459/* Generators */
5460/****************************************************************/
5461
Przemek Stekiel69c46792022-06-10 12:59:51 +02005462#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005463 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005464 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305465 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305466 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005467#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005468#endif /* At least one builtin KDF */
5469
Przemek Stekiel69c46792022-06-10 12:59:51 +02005470#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005471 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5472 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5473static psa_status_t psa_key_derivation_start_hmac(
5474 psa_mac_operation_t *operation,
5475 psa_algorithm_t hash_alg,
5476 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005478{
5479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5482 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5483 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005484
Steven Cooreman72f736a2021-05-07 14:14:37 +02005485 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 status = psa_driver_wrapper_mac_sign_setup(operation,
5489 &attributes,
5490 hmac_key, hmac_key_length,
5491 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 psa_reset_key_attributes(&attributes);
5494 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005495}
5496#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005497
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005498#define HKDF_STATE_INIT 0 /* no input yet */
5499#define HKDF_STATE_STARTED 1 /* got salt */
5500#define HKDF_STATE_KEYED 2 /* got key */
5501#define HKDF_STATE_OUTPUT 3 /* output started */
5502
Gilles Peskinecbe66502019-05-16 16:59:18 +02005503static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005505{
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5507 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5508 } else {
5509 return operation->alg;
5510 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005511}
5512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005514{
5515 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5517 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005518 /* The object has (apparently) been initialized but it is not
5519 * in use. It's ok to call abort on such an object, and there's
5520 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005522#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5524 mbedtls_free(operation->ctx.hkdf.info);
5525 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5526 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005527#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005528#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5531 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5532 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5533 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005534 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005536 }
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005539 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005541 }
5542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005544 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005546 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005547#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005549 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005551 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005553 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005554
5555 /* We leave the fields Ai and output_block to be erased safely by the
5556 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5559 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005560#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5562 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5563 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5564 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005565#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305566#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305567 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305568 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005569 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305570 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305571 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305572
5573 status = PSA_SUCCESS;
5574 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305575#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576 {
5577 status = PSA_ERROR_BAD_STATE;
5578 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 mbedtls_platform_zeroize(operation, sizeof(*operation));
5580 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581}
5582
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005585{
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005589 }
5590
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593}
5594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5596 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005597{
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 if (operation->alg == 0) {
5599 return PSA_ERROR_BAD_STATE;
5600 }
5601 if (capacity > operation->capacity) {
5602 return PSA_ERROR_INVALID_ARGUMENT;
5603 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005606}
5607
Przemek Stekiel69c46792022-06-10 12:59:51 +02005608#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005609/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005610static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5611 psa_algorithm_t kdf_alg,
5612 uint8_t *output,
5613 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005614{
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5616 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005617 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005618 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005619#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005621#else
5622 const uint8_t last_block = 0xff;
5623#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 if (hkdf->state < HKDF_STATE_KEYED ||
5626 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005627#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005629#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 )) {
5631 return PSA_ERROR_BAD_STATE;
5632 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005633 hkdf->state = HKDF_STATE_OUTPUT;
5634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005636 /* Copy what remains of the current block */
5637 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005639 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 }
5641 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005642 output += n;
5643 output_length -= n;
5644 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005646 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005648 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005649 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005651 * object was corrupted or if this function is called directly
5652 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 if (hkdf->block_number == last_block) {
5654 return PSA_ERROR_BAD_STATE;
5655 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005656
5657 /* We need a new block */
5658 ++hkdf->block_number;
5659 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5662 hash_alg,
5663 hkdf->prk,
5664 hash_length);
5665 if (status != PSA_SUCCESS) {
5666 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005668
5669 if (hkdf->block_number != 1) {
5670 status = psa_mac_update(&hkdf->hmac,
5671 hkdf->output_block,
5672 hash_length);
5673 if (status != PSA_SUCCESS) {
5674 return status;
5675 }
5676 }
5677 status = psa_mac_update(&hkdf->hmac,
5678 hkdf->info,
5679 hkdf->info_length);
5680 if (status != PSA_SUCCESS) {
5681 return status;
5682 }
5683 status = psa_mac_update(&hkdf->hmac,
5684 &hkdf->block_number, 1);
5685 if (status != PSA_SUCCESS) {
5686 return status;
5687 }
5688 status = psa_mac_sign_finish(&hkdf->hmac,
5689 hkdf->output_block,
5690 sizeof(hkdf->output_block),
5691 &hmac_output_length);
5692 if (status != PSA_SUCCESS) {
5693 return status;
5694 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005695 }
5696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005698}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005699#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005700
John Durkop07cc04a2020-11-16 22:08:34 -08005701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5702 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005703static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5704 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005706{
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5708 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005709 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5710 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005711 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005712
Janos Follath7742fee2019-06-17 12:58:10 +01005713 /* We can't be wanting more output after block 0xff, otherwise
5714 * the capacity check in psa_key_derivation_output_bytes() would have
5715 * prevented this call. It could happen only if the operation
5716 * object was corrupted or if this function is called directly
5717 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 if (tls12_prf->block_number == 0xff) {
5719 return PSA_ERROR_CORRUPTION_DETECTED;
5720 }
Janos Follath7742fee2019-06-17 12:58:10 +01005721
5722 /* We need a new block */
5723 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005724 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005725
5726 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5727 *
5728 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5729 *
5730 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5731 * HMAC_hash(secret, A(2) + seed) +
5732 * HMAC_hash(secret, A(3) + seed) + ...
5733 *
5734 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005735 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005736 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005737 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005738 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005739 * is currently extracted as `output_block` and where i is
5740 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005741 */
5742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 status = psa_key_derivation_start_hmac(&hmac,
5744 hash_alg,
5745 tls12_prf->secret,
5746 tls12_prf->secret_length);
5747 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005748 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005750
5751 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005753 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5754 * the variable seed and in this instance means it in the context of the
5755 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 status = psa_mac_update(&hmac,
5757 tls12_prf->label,
5758 tls12_prf->label_length);
5759 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005760 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 }
5762 status = psa_mac_update(&hmac,
5763 tls12_prf->seed,
5764 tls12_prf->seed_length);
5765 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005766 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 }
5768 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005769 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5771 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005772 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005774 }
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 status = psa_mac_sign_finish(&hmac,
5777 tls12_prf->Ai, hash_length,
5778 &hmac_output_length);
5779 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005780 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 }
5782 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005783 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005785
5786 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 status = psa_key_derivation_start_hmac(&hmac,
5788 hash_alg,
5789 tls12_prf->secret,
5790 tls12_prf->secret_length);
5791 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005792 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 }
5794 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5795 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005796 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 }
5798 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5799 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005800 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 }
5802 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5803 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005804 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 }
5806 status = psa_mac_sign_finish(&hmac,
5807 tls12_prf->output_block, hash_length,
5808 &hmac_output_length);
5809 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005810 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005812
Janos Follath7742fee2019-06-17 12:58:10 +01005813
5814cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 cleanup_status = psa_mac_abort(&hmac);
5816 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005817 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005821}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005822
Janos Follath844eb0e2019-06-19 12:10:49 +01005823static psa_status_t psa_key_derivation_tls12_prf_read(
5824 psa_tls12_prf_key_derivation_t *tls12_prf,
5825 psa_algorithm_t alg,
5826 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005828{
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5830 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005831 psa_status_t status;
5832 uint8_t offset, length;
5833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005835 case PSA_TLS12_PRF_STATE_LABEL_SET:
5836 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5837 break;
5838 case PSA_TLS12_PRF_STATE_OUTPUT:
5839 break;
5840 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005842 }
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005845 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 if (tls12_prf->left_in_block == 0) {
5847 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5848 alg);
5849 if (status != PSA_SUCCESS) {
5850 return status;
5851 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005852
5853 continue;
5854 }
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005857 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005859 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005861
5862 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005864 output += length;
5865 output_length -= length;
5866 tls12_prf->left_in_block -= length;
5867 }
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005870}
John Durkop07cc04a2020-11-16 22:08:34 -08005871#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5872 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005873
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005874#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5875static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5876 psa_tls12_ecjpake_to_pms_t *ecjpake,
5877 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005879{
5880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005881 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (output_length != 32) {
5884 return PSA_ERROR_INVALID_ARGUMENT;
5885 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5888 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5889 &output_size);
5890 if (status != PSA_SUCCESS) {
5891 return status;
5892 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (output_size != output_length) {
5895 return PSA_ERROR_GENERIC_ERROR;
5896 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005899}
5900#endif
5901
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305902#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305903static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5904 psa_pbkdf2_key_derivation_t *pbkdf2,
5905 psa_algorithm_t prf_alg,
5906 uint8_t prf_output_length,
5907 psa_key_attributes_t *attributes)
5908{
5909 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305910 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305911 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305912 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305913 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305914 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305915 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305916
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305917 mac_operation.is_sign = 1;
5918 mac_operation.mac_size = prf_output_length;
5919 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305920
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305921 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5922 attributes,
5923 pbkdf2->password,
5924 pbkdf2->password_length,
5925 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305926 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305927 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305928 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305929 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5930 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305931 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305932 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305933 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305934 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305935 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305936 }
5937 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5938 &mac_output_length);
5939 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305940 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305941 }
5942
5943 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305944 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305945 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305946 }
5947
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305948 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305949
5950 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305951 /* We are passing prf_output_length as mac_size because the driver
5952 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305953 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305954 status = psa_driver_wrapper_mac_compute(attributes,
5955 pbkdf2->password,
5956 pbkdf2->password_length,
5957 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305958 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305959 &mac_output_length);
5960 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305961 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305962 }
5963
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305964 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305965 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305966
5967cleanup:
5968 /* Zeroise buffers to clear sensitive data from memory. */
5969 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5970 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305971}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305972
5973static psa_status_t psa_key_derivation_pbkdf2_read(
5974 psa_pbkdf2_key_derivation_t *pbkdf2,
5975 psa_algorithm_t kdf_alg,
5976 uint8_t *output,
5977 size_t output_length)
5978{
5979 psa_status_t status;
5980 psa_algorithm_t prf_alg;
5981 uint8_t prf_output_length;
5982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5983 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5984 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5985
5986 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5987 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5988 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5989 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305990 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5991 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305992 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305993 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305994 } else {
5995 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305996 }
5997
5998 switch (pbkdf2->state) {
5999 case PSA_PBKDF2_STATE_PASSWORD_SET:
6000 /* Initially we need a new block so bytes_used is equal to block size*/
6001 pbkdf2->bytes_used = prf_output_length;
6002 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
6003 break;
6004 case PSA_PBKDF2_STATE_OUTPUT:
6005 break;
6006 default:
6007 return PSA_ERROR_BAD_STATE;
6008 }
6009
6010 while (output_length != 0) {
6011 uint8_t n = prf_output_length - pbkdf2->bytes_used;
6012 if (n > output_length) {
6013 n = (uint8_t) output_length;
6014 }
6015 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
6016 output += n;
6017 output_length -= n;
6018 pbkdf2->bytes_used += n;
6019
6020 if (output_length == 0) {
6021 break;
6022 }
6023
6024 /* We need a new block */
6025 pbkdf2->bytes_used = 0;
6026 pbkdf2->block_number++;
6027
6028 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
6029 prf_output_length,
6030 &attributes);
6031 if (status != PSA_SUCCESS) {
6032 return status;
6033 }
6034 }
6035
6036 return PSA_SUCCESS;
6037}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306038#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306039
Janos Follath6c6c8fc2019-06-17 12:38:20 +01006040psa_status_t psa_key_derivation_output_bytes(
6041 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00006042 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006044{
6045 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00006046 LOCAL_OUTPUT_DECLARE(output_external, output);
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006051 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006053 }
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006056 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006057 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02006058 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
6059 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006060 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02006061 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006063 }
Ryan Everettf943e222024-01-19 14:46:39 +00006064
6065 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00006066 if (output_length > operation->capacity) {
6067 operation->capacity = 0;
6068 /* Go through the error path to wipe all confidential data now
6069 * that the operation object is useless. */
6070 status = PSA_ERROR_INSUFFICIENT_DATA;
6071 goto exit;
6072 }
6073
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006074 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006075
Przemek Stekiel69c46792022-06-10 12:59:51 +02006076#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6078 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6079 output, output_length);
6080 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006081#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006082#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6083 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6085 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6086 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6087 kdf_alg, output,
6088 output_length);
6089 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006090#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6091 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006092#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006094 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6096 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006097#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306098#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306099 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306100 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6101 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306102 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306103#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006104
Gilles Peskineeab56e42018-07-12 17:12:33 +02006105 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006106 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006107 status = PSA_ERROR_BAD_STATE;
6108 LOCAL_OUTPUT_FREE(output_external, output);
6109
6110 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006111 }
6112
6113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006115 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116 * This allows us to differentiate between exhausted operations and
6117 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6118 * operations. */
6119 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006121 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006122 if (output != NULL) {
6123 memset(output, '!', output_length);
6124 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006125 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006126
6127 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006129}
6130
David Brown7807bf72021-02-09 16:28:23 -07006131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006132static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006133{
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 if (data_size >= 8) {
6135 mbedtls_des_key_set_parity(data);
6136 }
6137 if (data_size >= 16) {
6138 mbedtls_des_key_set_parity(data + 8);
6139 }
6140 if (data_size >= 24) {
6141 mbedtls_des_key_set_parity(data + 16);
6142 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006143}
David Brown7807bf72021-02-09 16:28:23 -07006144#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006145
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006146/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 * ECC keys on a Weierstrass elliptic curve require the generation
6148 * of a private key which is an integer
6149 * in the range [1, N - 1], where N is the boundary of the private key domain:
6150 * N is the prime p for Diffie-Hellman, or the order of the
6151 * curve’s base point for ECC.
6152 *
6153 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6154 * This function generates the private key using the following process:
6155 *
6156 * 1. Draw a byte string of length ceiling(m/8) bytes.
6157 * 2. If m is not a multiple of 8, set the most significant
6158 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6159 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6160 * 4. If k > N - 2, discard the result and return to step 1.
6161 * 5. Output k + 1 as the private key.
6162 *
6163 * This method allows compliance to NIST standards, specifically the methods titled
6164 * Key-Pair Generation by Testing Candidates in the following publications:
6165 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6166 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6167 * Diffie-Hellman keys.
6168 *
6169 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6170 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6171 *
6172 * Note: Function allocates memory for *data buffer, so given *data should be
6173 * always NULL.
6174 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006175#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6176#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006177static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006178 psa_key_slot_t *slot,
6179 size_t bits,
6180 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006181 uint8_t **data
6182 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006183{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006184 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006185 mbedtls_mpi k;
6186 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006189 size_t m;
6190 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 mbedtls_mpi_init(&k);
6193 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006194
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006195 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006197 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006198 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006201 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006202 goto cleanup;
6203 }
6204
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006205 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006209
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006210 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006211 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006212 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006213
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006214 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006215
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006216 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006218
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006219 /* Note: This function is always called with *data == NULL and it
6220 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 *data = mbedtls_calloc(1, m_bytes);
6222 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006223 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006224 goto cleanup;
6225 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006228 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006230 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006232
6233 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006235 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006236 * (8 * ceiling(m/8) - m) bits of the first byte in
6237 * the string to zero.
6238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006240 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006241 }
6242
6243 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 * big-endian byte string.
6245 */
6246 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006247
6248 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 * Result of comparison is returned. When it indicates error
6250 * then this function is called again.
6251 */
6252 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006253 }
6254
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006255 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6257 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006258cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if (ret != 0) {
6260 status = mbedtls_to_psa_error(ret);
6261 }
6262 if (status != PSA_SUCCESS) {
6263 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006264 *data = NULL;
6265 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 mbedtls_mpi_free(&k);
6267 mbedtls_mpi_free(&diff_N_2);
6268 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006269}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006270
6271/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6272 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6273 *
6274 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6275 * draw a 32-byte string and process it as specified in
6276 * Elliptic Curves for Security [RFC7748] §5.
6277 *
6278 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6279 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6280 *
6281 * Note: Function allocates memory for *data buffer, so given *data should be
6282 * always NULL.
6283 */
6284
6285static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6286 size_t bits,
6287 psa_key_derivation_operation_t *operation,
6288 uint8_t **data
6289 )
6290{
6291 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006295 case 255:
6296 output_length = 32;
6297 break;
6298 case 448:
6299 output_length = 56;
6300 break;
6301 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006303 break;
6304 }
6305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 if (*data == NULL) {
6309 return PSA_ERROR_INSUFFICIENT_MEMORY;
6310 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006315 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006319 case 255:
6320 (*data)[0] &= 248;
6321 (*data)[31] &= 127;
6322 (*data)[31] |= 64;
6323 break;
6324 case 448:
6325 (*data)[0] &= 252;
6326 (*data)[55] |= 128;
6327 break;
6328 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006330 break;
6331 }
6332
6333 return status;
6334}
Valerio Setti86587ab2023-06-27 13:09:30 +02006335#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6336static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6337 psa_key_slot_t *slot, size_t bits,
6338 psa_key_derivation_operation_t *operation, uint8_t **data)
6339{
6340 (void) slot;
6341 (void) bits;
6342 (void) operation;
6343 (void) data;
6344 return PSA_ERROR_NOT_SUPPORTED;
6345}
6346
6347static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6348 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6349{
6350 (void) bits;
6351 (void) operation;
6352 (void) data;
6353 return PSA_ERROR_NOT_SUPPORTED;
6354}
6355#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6356#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006357
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006358static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006359 psa_key_slot_t *slot,
6360 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006362{
6363 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306365 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6369 return PSA_ERROR_INVALID_ARGUMENT;
6370 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006371
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006372#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006373 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6375 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6376 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006377 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6379 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 }
6382 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006383 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6385 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006386 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006388 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006389 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006390#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006391 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (key_type_is_raw_bytes(slot->attr.type)) {
6393 if (bits % 8 != 0) {
6394 return PSA_ERROR_INVALID_ARGUMENT;
6395 }
6396 data = mbedtls_calloc(1, bytes);
6397 if (data == NULL) {
6398 return PSA_ERROR_INSUFFICIENT_MEMORY;
6399 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 status = psa_key_derivation_output_bytes(operation, data, bytes);
6402 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006403 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 }
David Brown12ca5032021-02-11 11:02:00 -07006405#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6407 psa_des_set_key_parity(data, bytes);
6408 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006409#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 } else {
6411 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006412 }
Ronald Crondd04d422020-11-28 15:14:42 +01006413
Ronald Cronbf33c932020-11-28 18:06:53 +01006414 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006415
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006416 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006417 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 &storage_size);
6419 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306420 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 }
Archanad8a83dc2021-06-14 10:04:16 +05306422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 status = psa_allocate_buffer_to_slot(slot, storage_size);
6424 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306425 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 }
Archanad8a83dc2021-06-14 10:04:16 +05306427
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006428 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 data, bytes,
6430 slot->key.data,
6431 slot->key.bytes,
6432 &slot->key.bytes, &bits);
6433 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006434 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006436
6437exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 mbedtls_free(data);
6439 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006440}
6441
Gilles Peskinef36d7852024-06-06 21:11:44 +02006442static const psa_custom_key_parameters_t default_custom_production =
Gilles Peskine4a85ff32024-07-18 18:52:59 +02006443 PSA_CUSTOM_KEY_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006444
Gilles Peskine52504f82024-06-20 17:19:58 +02006445int psa_custom_key_parameters_are_default(
Gilles Peskinef36d7852024-06-06 21:11:44 +02006446 const psa_custom_key_parameters_t *custom,
6447 size_t custom_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006448{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006449 if (custom->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006450 return 0;
6451 }
Gilles Peskinef36d7852024-06-06 21:11:44 +02006452 if (custom_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006453 return 0;
6454 }
6455 return 1;
6456}
6457
Gilles Peskinef36d7852024-06-06 21:11:44 +02006458psa_status_t psa_key_derivation_output_key_custom(
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006459 const psa_key_attributes_t *attributes,
6460 psa_key_derivation_operation_t *operation,
Gilles Peskinef36d7852024-06-06 21:11:44 +02006461 const psa_custom_key_parameters_t *custom,
6462 const uint8_t *custom_data,
6463 size_t custom_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006464 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006465{
6466 psa_status_t status;
6467 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006468 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006469
Ronald Cron81709fc2020-11-14 12:10:32 +01006470 *key = MBEDTLS_SVC_KEY_ID_INIT;
6471
Gilles Peskine0f84d622019-09-12 19:03:13 +02006472 /* Reject any attempt to create a zero-length key so that we don't
6473 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 if (psa_get_key_bits(attributes) == 0) {
6475 return PSA_ERROR_INVALID_ARGUMENT;
6476 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006477
Gilles Peskinef36d7852024-06-06 21:11:44 +02006478 (void) custom_data; /* We only accept 0-length data */
Gilles Peskine52504f82024-06-20 17:19:58 +02006479 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006480 return PSA_ERROR_INVALID_ARGUMENT;
6481 }
6482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (operation->alg == PSA_ALG_NONE) {
6484 return PSA_ERROR_BAD_STATE;
6485 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 if (!operation->can_output_key) {
6488 return PSA_ERROR_NOT_PERMITTED;
6489 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6492 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006493#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006495 /* Deriving a key in a secure element is not implemented yet. */
6496 status = PSA_ERROR_NOT_SUPPORTED;
6497 }
6498#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 if (status == PSA_SUCCESS) {
6500 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006501 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006503 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (status == PSA_SUCCESS) {
6505 status = psa_finish_key_creation(slot, driver, key);
6506 }
6507 if (status != PSA_SUCCESS) {
6508 psa_fail_key_creation(slot, driver);
6509 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006512}
6513
Gilles Peskinef36d7852024-06-06 21:11:44 +02006514psa_status_t psa_key_derivation_output_key_ext(
6515 const psa_key_attributes_t *attributes,
6516 psa_key_derivation_operation_t *operation,
6517 const psa_key_production_parameters_t *params,
6518 size_t params_data_length,
6519 mbedtls_svc_key_id_t *key)
6520{
6521 return psa_key_derivation_output_key_custom(
6522 attributes, operation,
6523 (const psa_custom_key_parameters_t *) params,
6524 params->data, params_data_length,
6525 key);
6526}
6527
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006528psa_status_t psa_key_derivation_output_key(
6529 const psa_key_attributes_t *attributes,
6530 psa_key_derivation_operation_t *operation,
6531 mbedtls_svc_key_id_t *key)
6532{
Gilles Peskinef36d7852024-06-06 21:11:44 +02006533 return psa_key_derivation_output_key_custom(attributes, operation,
6534 &default_custom_production,
6535 NULL, 0,
6536 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006537}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006538
6539
6540/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006541/* Key derivation */
6542/****************************************************************/
6543
Steven Cooreman932ffb72021-02-15 12:14:32 +01006544#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006545static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006546{
6547#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6549 return 1;
6550 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006551#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006552#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6554 return 1;
6555 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006556#endif
6557#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6559 return 1;
6560 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006561#endif
6562#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6564 return 1;
6565 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006566#endif
6567#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6569 return 1;
6570 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006571#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006572#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6574 return 1;
6575 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006576#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306577#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6578 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6579 return 1;
6580 }
6581#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306582#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6583 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6584 return 1;
6585 }
6586#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006588}
6589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006591{
6592 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 psa_status_t status = psa_hash_setup(&operation, alg);
6594 psa_hash_abort(&operation);
6595 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006596}
6597
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306598static psa_status_t psa_key_derivation_set_maximum_capacity(
6599 psa_key_derivation_operation_t *operation,
6600 psa_algorithm_t kdf_alg)
6601{
6602#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6603 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6604 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6605 return PSA_SUCCESS;
6606 }
6607#endif
6608#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6609 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6610#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306611 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306612 PSA_KEY_TYPE_AES,
6613 128U,
6614 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306615#else
6616 operation->capacity = SIZE_MAX;
6617#endif
6618 return PSA_SUCCESS;
6619 }
6620#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6621
6622 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6623 * invalid or meaningless but it does not affect this function */
6624 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6625 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306626 if (hash_size == 0) {
6627 return PSA_ERROR_NOT_SUPPORTED;
6628 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306629
6630 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6631 * we might fail later, which is somewhat unfriendly and potentially
6632 * risk-prone. */
6633 psa_status_t status = psa_hash_try_support(hash_alg);
6634 if (status != PSA_SUCCESS) {
6635 return status;
6636 }
6637
6638#if defined(PSA_WANT_ALG_HKDF)
6639 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6640 operation->capacity = 255 * hash_size;
6641 } else
6642#endif
6643#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6644 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6645 operation->capacity = hash_size;
6646 } else
6647#endif
6648#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6649 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6650 operation->capacity = 255 * hash_size;
6651 } else
6652#endif
6653#if defined(PSA_WANT_ALG_TLS12_PRF)
6654 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6655 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6656 operation->capacity = SIZE_MAX;
6657 } else
6658#endif
6659#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6660 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6661 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6662 /* Master Secret is always 48 bytes
6663 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6664 operation->capacity = 48U;
6665 } else
6666#endif
6667#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6668 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6669#if (SIZE_MAX > UINT32_MAX)
6670 operation->capacity = UINT32_MAX * hash_size;
6671#else
6672 operation->capacity = SIZE_MAX;
6673#endif
6674 } else
6675#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6676 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306677 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306678 status = PSA_ERROR_NOT_SUPPORTED;
6679 }
6680 return status;
6681}
6682
Gilles Peskine969c5d62019-01-16 15:53:06 +01006683static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006684 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006686{
Janos Follath5fe19732019-06-20 15:09:30 +01006687 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6688 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006690
Gilles Peskine969c5d62019-01-16 15:53:06 +01006691 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 if (!is_kdf_alg_supported(kdf_alg)) {
6693 return PSA_ERROR_NOT_SUPPORTED;
6694 }
John Durkop07cc04a2020-11-16 22:08:34 -08006695
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306696 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6697 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306698 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006699}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006702{
6703#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (alg == PSA_ALG_ECDH) {
6705 return PSA_SUCCESS;
6706 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006707#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006708#if defined(PSA_WANT_ALG_FFDH)
6709 if (alg == PSA_ALG_FFDH) {
6710 return PSA_SUCCESS;
6711 }
6712#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006713 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006715}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006716
6717static int psa_key_derivation_allows_free_form_secret_input(
6718 psa_algorithm_t kdf_alg)
6719{
6720#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6721 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6722 return 0;
6723 }
6724#endif
6725 (void) kdf_alg;
6726 return 1;
6727}
John Durkop07cc04a2020-11-16 22:08:34 -08006728#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6731 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006732{
6733 psa_status_t status;
6734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (operation->alg != 0) {
6736 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006737 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006738
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6740 return PSA_ERROR_INVALID_ARGUMENT;
6741 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6742#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6743 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6744 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6745 status = psa_key_agreement_try_support(ka_alg);
6746 if (status != PSA_SUCCESS) {
6747 return status;
6748 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006749 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6750 return PSA_ERROR_INVALID_ARGUMENT;
6751 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6753#else
6754 return PSA_ERROR_NOT_SUPPORTED;
6755#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6756 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6757#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6758 status = psa_key_derivation_setup_kdf(operation, alg);
6759#else
6760 return PSA_ERROR_NOT_SUPPORTED;
6761#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6762 } else {
6763 return PSA_ERROR_INVALID_ARGUMENT;
6764 }
6765
6766 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006767 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 }
6769 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006770}
6771
Przemek Stekiel69c46792022-06-10 12:59:51 +02006772#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006773static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6774 psa_algorithm_t kdf_alg,
6775 psa_key_derivation_step_t step,
6776 const uint8_t *data,
6777 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006778{
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006780 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006782 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006783#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6785 return PSA_ERROR_INVALID_ARGUMENT;
6786 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006787#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 if (hkdf->state != HKDF_STATE_INIT) {
6789 return PSA_ERROR_BAD_STATE;
6790 } else {
6791 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6792 hash_alg,
6793 data, data_length);
6794 if (status != PSA_SUCCESS) {
6795 return status;
6796 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006797 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006799 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006800 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006801#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006803 /* We shouldn't be in different state as HKDF_EXPAND only allows
6804 * two inputs: SECRET (this case) and INFO which does not modify
6805 * the state. It could happen only if the hkdf
6806 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if (hkdf->state != HKDF_STATE_INIT) {
6808 return PSA_ERROR_BAD_STATE;
6809 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006810
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006811 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6813 return PSA_ERROR_INVALID_ARGUMENT;
6814 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 memcpy(hkdf->prk, data, data_length);
6817 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006818#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006819 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006820 /* HKDF: If no salt was provided, use an empty salt.
6821 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006823#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6825 return PSA_ERROR_BAD_STATE;
6826 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006827#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6829 hash_alg,
6830 NULL, 0);
6831 if (status != PSA_SUCCESS) {
6832 return status;
6833 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006834 hkdf->state = HKDF_STATE_STARTED;
6835 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if (hkdf->state != HKDF_STATE_STARTED) {
6837 return PSA_ERROR_BAD_STATE;
6838 }
6839 status = psa_mac_update(&hkdf->hmac,
6840 data, data_length);
6841 if (status != PSA_SUCCESS) {
6842 return status;
6843 }
6844 status = psa_mac_sign_finish(&hkdf->hmac,
6845 hkdf->prk,
6846 sizeof(hkdf->prk),
6847 &data_length);
6848 if (status != PSA_SUCCESS) {
6849 return status;
6850 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006851 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006852
Gilles Peskine2b522db2019-04-12 15:11:49 +02006853 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006854 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006855#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006857 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006859 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006861#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006862 {
6863 /* Block 0 is empty, and the next block will be
6864 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006866 }
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006869 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006870#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6872 return PSA_ERROR_INVALID_ARGUMENT;
6873 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006874#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006875#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6877 hkdf->state == HKDF_STATE_INIT) {
6878 return PSA_ERROR_BAD_STATE;
6879 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006880#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 if (hkdf->state == HKDF_STATE_OUTPUT) {
6882 return PSA_ERROR_BAD_STATE;
6883 }
6884 if (hkdf->info_set) {
6885 return PSA_ERROR_BAD_STATE;
6886 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006887 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 if (data_length != 0) {
6889 hkdf->info = mbedtls_calloc(1, data_length);
6890 if (hkdf->info == NULL) {
6891 return PSA_ERROR_INSUFFICIENT_MEMORY;
6892 }
6893 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006894 }
6895 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006897 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006899 }
6900}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006901#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006902
John Durkop07cc04a2020-11-16 22:08:34 -08006903#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6904 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006905static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6906 const uint8_t *data,
6907 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006908{
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6910 return PSA_ERROR_BAD_STATE;
6911 }
Janos Follathf08e2652019-06-13 09:05:41 +01006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (data_length != 0) {
6914 prf->seed = mbedtls_calloc(1, data_length);
6915 if (prf->seed == NULL) {
6916 return PSA_ERROR_INSUFFICIENT_MEMORY;
6917 }
Janos Follathf08e2652019-06-13 09:05:41 +01006918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006920 prf->seed_length = data_length;
6921 }
Janos Follathf08e2652019-06-13 09:05:41 +01006922
Gilles Peskine43f958b2020-12-13 14:55:14 +01006923 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006924
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006926}
6927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6929 const uint8_t *data,
6930 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006931{
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6933 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6934 return PSA_ERROR_BAD_STATE;
6935 }
Janos Follath81550542019-06-13 14:26:34 +01006936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 if (data_length != 0) {
6938 prf->secret = mbedtls_calloc(1, data_length);
6939 if (prf->secret == NULL) {
6940 return PSA_ERROR_INSUFFICIENT_MEMORY;
6941 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006944 prf->secret_length = data_length;
6945 }
Janos Follath81550542019-06-13 14:26:34 +01006946
Gilles Peskine43f958b2020-12-13 14:55:14 +01006947 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006948
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006950}
6951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6953 const uint8_t *data,
6954 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006955{
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6957 return PSA_ERROR_BAD_STATE;
6958 }
Janos Follath63028dd2019-06-13 09:15:47 +01006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 if (data_length != 0) {
6961 prf->label = mbedtls_calloc(1, data_length);
6962 if (prf->label == NULL) {
6963 return PSA_ERROR_INSUFFICIENT_MEMORY;
6964 }
Janos Follath63028dd2019-06-13 09:15:47 +01006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006967 prf->label_length = data_length;
6968 }
Janos Follath63028dd2019-06-13 09:15:47 +01006969
Gilles Peskine43f958b2020-12-13 14:55:14 +01006970 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006971
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006973}
6974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6976 psa_key_derivation_step_t step,
6977 const uint8_t *data,
6978 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006979{
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006981 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006983 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006985 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006987 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006989 }
6990}
John Durkop07cc04a2020-11-16 22:08:34 -08006991#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6992 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6993
6994#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6995static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6996 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006997 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006999{
7000 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
7002 4 + data_length + prf->other_secret_length :
7003 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
7006 return PSA_ERROR_INVALID_ARGUMENT;
7007 }
John Durkop07cc04a2020-11-16 22:08:34 -08007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 uint8_t *pms = mbedtls_calloc(1, pms_len);
7010 if (pms == NULL) {
7011 return PSA_ERROR_INSUFFICIENT_MEMORY;
7012 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007013 uint8_t *cur = pms;
7014
7015 /* pure-PSK:
7016 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08007017 *
7018 * The premaster secret is formed as follows: if the PSK is N octets
7019 * long, concatenate a uint16 with the value N, N zero octets, a second
7020 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007021 *
7022 * mixed-PSK:
7023 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
7024 * follows: concatenate a uint16 with the length of the other secret,
7025 * the other secret itself, uint16 with the length of PSK, and the
7026 * PSK itself.
7027 * For details please check:
7028 * - RFC 4279, Section 4 for the definition of RSA-PSK,
7029 * - RFC 4279, Section 3 for the definition of DHE-PSK,
7030 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08007031 */
7032
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
7034 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
7035 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
7036 if (prf->other_secret_length != 0) {
7037 memcpy(cur, prf->other_secret, prf->other_secret_length);
7038 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02007039 cur += prf->other_secret_length;
7040 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 } else {
7042 *cur++ = MBEDTLS_BYTE_1(data_length);
7043 *cur++ = MBEDTLS_BYTE_0(data_length);
7044 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007045 cur += data_length;
7046 }
7047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 *cur++ = MBEDTLS_BYTE_1(data_length);
7049 *cur++ = MBEDTLS_BYTE_0(data_length);
7050 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007051 cur += data_length;
7052
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007053 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08007054
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007055 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08007057}
Janos Follath6660f0e2019-06-17 08:44:03 +01007058
Przemek Stekiele47201b2022-04-19 13:53:28 +02007059static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
7060 psa_tls12_prf_key_derivation_t *prf,
7061 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02007063{
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
7065 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007067
7068 if (data_length != 0) {
7069 prf->other_secret = mbedtls_calloc(1, data_length);
7070 if (prf->other_secret == NULL) {
7071 return PSA_ERROR_INSUFFICIENT_MEMORY;
7072 }
7073
7074 memcpy(prf->other_secret, data, data_length);
7075 prf->other_secret_length = data_length;
7076 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007077 prf->other_secret_length = 0;
7078 }
7079
7080 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
7081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007083}
7084
Janos Follath6660f0e2019-06-17 08:44:03 +01007085static psa_status_t psa_tls12_prf_psk_to_ms_input(
7086 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01007087 psa_key_derivation_step_t step,
7088 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01007090{
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007092 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 return psa_tls12_prf_psk_to_ms_set_key(prf,
7094 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007095 break;
7096 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7098 data,
7099 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007100 break;
7101 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007103 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007104
Przemek Stekiele47201b2022-04-19 13:53:28 +02007105 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007106}
John Durkop07cc04a2020-11-16 22:08:34 -08007107#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007108
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007109#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7110static psa_status_t psa_tls12_ecjpake_to_pms_input(
7111 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007112 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007113 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007115{
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7117 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7118 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007119 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007120
7121 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 if (data[0] != 0x04) {
7123 return PSA_ERROR_INVALID_ARGUMENT;
7124 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007125
7126 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007130}
7131#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307132
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307133#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307134static psa_status_t psa_pbkdf2_set_input_cost(
7135 psa_pbkdf2_key_derivation_t *pbkdf2,
7136 psa_key_derivation_step_t step,
7137 uint64_t data)
7138{
7139 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7140 return PSA_ERROR_INVALID_ARGUMENT;
7141 }
7142
7143 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7144 return PSA_ERROR_BAD_STATE;
7145 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307146
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307147 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307148 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307149 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307150
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307151 if (data == 0) {
7152 return PSA_ERROR_INVALID_ARGUMENT;
7153 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307154
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307155 pbkdf2->input_cost = data;
7156 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7157
7158 return PSA_SUCCESS;
7159}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307160
7161static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7162 const uint8_t *data,
7163 size_t data_length)
7164{
Gilles Peskineead17662023-08-20 22:02:51 +02007165 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7166 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7167 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7168 /* Appending to existing salt. No state change. */
7169 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307170 return PSA_ERROR_BAD_STATE;
7171 }
7172
Gilles Peskineca57d782023-07-20 19:08:06 +02007173 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007174 /* Appending an empty string, nothing to do. */
7175 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307176 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307177
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307178 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7179 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307180 return PSA_ERROR_INSUFFICIENT_MEMORY;
7181 }
7182
Gilles Peskineead17662023-08-20 22:02:51 +02007183 if (pbkdf2->salt_length != 0) {
7184 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7185 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307186 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307187 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307188 mbedtls_free(pbkdf2->salt);
7189 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307190 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307191 return PSA_SUCCESS;
7192}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307193
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307194#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307195static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307196 const uint8_t *input,
7197 size_t input_len,
7198 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307199 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307200{
7201 psa_status_t status = PSA_SUCCESS;
7202 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007203 return psa_hash_compute(hash_alg, input, input_len, output,
7204 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7205 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307206 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307207 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007208 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307209 return status;
7210}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307211#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307212
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307213#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307214static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7215 size_t input_len,
7216 uint8_t *output,
7217 size_t *output_len)
7218{
7219 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307220 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307222 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307223 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7224 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307226 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307227 * mac_size as the driver function sets mac_output_length = mac_size
7228 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307229 status = psa_driver_wrapper_mac_compute(&attributes,
7230 zeros, sizeof(zeros),
7231 PSA_ALG_CMAC, input, input_len,
7232 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307233 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7234 128U,
7235 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307236 output_len);
7237 } else {
7238 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307239 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307240 }
7241 return status;
7242}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307243#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307244
7245static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307246 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307247 const uint8_t *data,
7248 size_t data_length)
7249{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307250 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307251 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7252 return PSA_ERROR_BAD_STATE;
7253 }
7254
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307255#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307256 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7257 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7258 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7259 pbkdf2->password,
7260 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307261 } else
7262#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7263#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7264 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307265 status = psa_pbkdf2_cmac_set_password(data, data_length,
7266 pbkdf2->password,
7267 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307268 } else
7269#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7270 {
7271 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307272 }
7273
7274 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7275
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307276 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307277}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307278
7279static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307280 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307281 psa_key_derivation_step_t step,
7282 const uint8_t *data,
7283 size_t data_length)
7284{
7285 switch (step) {
7286 case PSA_KEY_DERIVATION_INPUT_SALT:
7287 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7288 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307289 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307290 default:
7291 return PSA_ERROR_INVALID_ARGUMENT;
7292 }
7293}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307294#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307295
Gilles Peskineb8965192019-09-24 16:21:10 +02007296/** Check whether the given key type is acceptable for the given
7297 * input step of a key derivation.
7298 *
7299 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7300 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7301 * Both secret and non-secret inputs can alternatively have the type
7302 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7303 * that the input was passed as a buffer rather than via a key object.
7304 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007305static int psa_key_derivation_check_input_type(
7306 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007308{
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007310 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 if (key_type == PSA_KEY_TYPE_DERIVE) {
7312 return PSA_SUCCESS;
7313 }
7314 if (key_type == PSA_KEY_TYPE_NONE) {
7315 return PSA_SUCCESS;
7316 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007317 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007318 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 if (key_type == PSA_KEY_TYPE_DERIVE) {
7320 return PSA_SUCCESS;
7321 }
7322 if (key_type == PSA_KEY_TYPE_NONE) {
7323 return PSA_SUCCESS;
7324 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007325 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007326 case PSA_KEY_DERIVATION_INPUT_LABEL:
7327 case PSA_KEY_DERIVATION_INPUT_SALT:
7328 case PSA_KEY_DERIVATION_INPUT_INFO:
7329 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7331 return PSA_SUCCESS;
7332 }
7333 if (key_type == PSA_KEY_TYPE_NONE) {
7334 return PSA_SUCCESS;
7335 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007336 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307337 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7338 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7339 return PSA_SUCCESS;
7340 }
7341 if (key_type == PSA_KEY_TYPE_DERIVE) {
7342 return PSA_SUCCESS;
7343 }
7344 if (key_type == PSA_KEY_TYPE_NONE) {
7345 return PSA_SUCCESS;
7346 }
7347 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007348 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007350}
7351
Janos Follathb80a94e2019-06-12 15:54:46 +01007352static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007353 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007354 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007355 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007356 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007358{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007359 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 status = psa_key_derivation_check_input_type(step, key_type);
7363 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007366
Przemek Stekiel69c46792022-06-10 12:59:51 +02007367#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7369 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7370 step, data, data_length);
7371 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007372#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007373#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7375 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7376 step, data, data_length);
7377 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007378#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7379#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7381 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7382 step, data, data_length);
7383 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007384#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007385#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007387 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7389 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007390#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307391#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307392 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307393 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7394 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307395 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307396#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007397 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007398 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007399 (void) data;
7400 (void) data_length;
7401 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007403 }
7404
Gilles Peskine224b0d62019-09-23 18:13:17 +02007405exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 if (status != PSA_SUCCESS) {
7407 psa_key_derivation_abort(operation);
7408 }
7409 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007410}
7411
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307412static psa_status_t psa_key_derivation_input_integer_internal(
7413 psa_key_derivation_operation_t *operation,
7414 psa_key_derivation_step_t step,
7415 uint64_t value)
7416{
7417 psa_status_t status;
7418 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7419
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307420#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307421 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307422 status = psa_pbkdf2_set_input_cost(
7423 &operation->ctx.pbkdf2, step, value);
7424 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307425#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307426 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307427 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307428 (void) value;
7429 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307430 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307431 }
7432
7433 if (status != PSA_SUCCESS) {
7434 psa_key_derivation_abort(operation);
7435 }
7436 return status;
7437}
7438
Janos Follath51f4a0f2019-06-14 11:35:55 +01007439psa_status_t psa_key_derivation_input_bytes(
7440 psa_key_derivation_operation_t *operation,
7441 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007442 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007444{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7446 LOCAL_INPUT_DECLARE(data_external, data);
7447
7448 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7449
7450 status = psa_key_derivation_input_internal(operation, step,
7451 PSA_KEY_TYPE_NONE,
7452 data, data_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00007453#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007454exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007455#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007456 LOCAL_INPUT_FREE(data_external, data);
7457 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007458}
7459
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307460psa_status_t psa_key_derivation_input_integer(
7461 psa_key_derivation_operation_t *operation,
7462 psa_key_derivation_step_t step,
7463 uint64_t value)
7464{
7465 return psa_key_derivation_input_integer_internal(operation, step, value);
7466}
7467
Janos Follath51f4a0f2019-06-14 11:35:55 +01007468psa_status_t psa_key_derivation_input_key(
7469 psa_key_derivation_operation_t *operation,
7470 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007472{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007475 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007476
Ronald Cron5c522922020-11-14 16:35:34 +01007477 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7479 if (status != PSA_SUCCESS) {
7480 psa_key_derivation_abort(operation);
7481 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007482 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007483
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307484 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7485 * permission to output to a key object. */
7486 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7487 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007488 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 status = psa_key_derivation_input_internal(operation,
7492 step, slot->attr.type,
7493 slot->key.data,
7494 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007495
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007496 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007499}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007500
7501
7502
7503/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007504/* Key agreement */
7505/****************************************************************/
7506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7508 const uint8_t *key_buffer,
7509 size_t key_buffer_size,
7510 psa_algorithm_t alg,
7511 const uint8_t *peer_key,
7512 size_t peer_key_length,
7513 uint8_t *shared_secret,
7514 size_t shared_secret_size,
7515 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007516{
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007518#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007519 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7521 key_buffer_size, alg,
7522 peer_key, peer_key_length,
7523 shared_secret,
7524 shared_secret_size,
7525 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007526#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007527
7528#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7529 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007530 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007531 peer_key,
7532 peer_key_length,
7533 key_buffer,
7534 key_buffer_size,
7535 shared_secret,
7536 shared_secret_size,
7537 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007538#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7539
Gilles Peskine01d718c2018-09-18 12:01:02 +02007540 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007541 (void) attributes;
7542 (void) key_buffer;
7543 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007544 (void) peer_key;
7545 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007546 (void) shared_secret;
7547 (void) shared_secret_size;
7548 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007550 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007551}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007552
Aditya Deshpande17845b82022-10-13 17:21:01 +01007553/** Internal function for raw key agreement
7554 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007555 * to the driver's implementation if a driver is present.
7556 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007557 * (psa_key_agreement_raw_builtin).
7558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007559static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7560 psa_key_slot_t *private_key,
7561 const uint8_t *peer_key,
7562 size_t peer_key_length,
7563 uint8_t *shared_secret,
7564 size_t shared_secret_size,
7565 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566{
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7568 return PSA_ERROR_NOT_SUPPORTED;
7569 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007570
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007571 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 private_key->key.data,
7573 private_key->key.bytes, alg,
7574 peer_key, peer_key_length,
7575 shared_secret,
7576 shared_secret_size,
7577 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007578}
7579
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007580/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007581 * to potentially free embedded data structures and wipe confidential data.
7582 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007583static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7584 psa_key_derivation_step_t step,
7585 psa_key_slot_t *private_key,
7586 const uint8_t *peer_key,
7587 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007588{
7589 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007590 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007591 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007593
7594 /* Step 1: run the secret agreement algorithm to generate the shared
7595 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 status = psa_key_agreement_raw_internal(ka_alg,
7597 private_key,
7598 peer_key, peer_key_length,
7599 shared_secret,
7600 sizeof(shared_secret),
7601 &shared_secret_length);
7602 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007605
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007606 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007607 * the shared secret. A shared secret is permitted wherever a key
7608 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 status = psa_key_derivation_input_internal(operation, step,
7610 PSA_KEY_TYPE_DERIVE,
7611 shared_secret,
7612 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7615 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007616}
7617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7619 psa_key_derivation_step_t step,
7620 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007621 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007623{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007625 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007626 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007627 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7630 return PSA_ERROR_INVALID_ARGUMENT;
7631 }
Ronald Cron5c522922020-11-14 16:35:34 +01007632 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7634 if (status != PSA_SUCCESS) {
7635 return status;
7636 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007637
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007638 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 status = psa_key_agreement_internal(operation, step,
7640 slot,
7641 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007642
David Horstmann4a48bec2024-03-13 15:42:31 +00007643#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007644exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007645#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 if (status != PSA_SUCCESS) {
7647 psa_key_derivation_abort(operation);
7648 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007649 /* If a private key has been added as SECRET, we allow the derived
7650 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007652 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007654 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007655
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007656 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007657 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007660}
7661
Gilles Peskine449bd832023-01-11 14:50:10 +01007662psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7663 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007664 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007666 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 size_t output_size,
7668 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007669{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007671 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007672 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007673 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007674 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7675 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007676 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007677
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007679 status = PSA_ERROR_INVALID_ARGUMENT;
7680 goto exit;
7681 }
Ronald Cron5c522922020-11-14 16:35:34 +01007682 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7684 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007685 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007687
Gilles Peskine3e561302022-04-14 00:17:15 +02007688 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7689 * for the output size. The PSA specification only guarantees that this
7690 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7691 * but it might be nice to allow smaller buffers if the output fits.
7692 * At the time of writing this comment, with only ECDH implemented,
7693 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7694 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7695 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007696 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7698 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007699 status = PSA_ERROR_BUFFER_TOO_SMALL;
7700 goto exit;
7701 }
7702
Thomas Daubney81899ab2024-02-15 12:57:26 +00007703 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 status = psa_key_agreement_raw_internal(alg, slot,
7705 peer_key, peer_key_length,
7706 output, output_size,
7707 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007708
7709exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007710 /* Check for successful allocation of output,
7711 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007712 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007713 /* If an error happens and is not handled properly, the output
7714 * may be used as a key to protect sensitive data. Arrange for such
7715 * a key to be random, which is likely to result in decryption or
7716 * verification errors. This is better than filling the buffer with
7717 * some constant data such as zeros, which would result in the data
7718 * being protected with a reproducible, easily knowable key.
7719 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007720 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007721 *output_length = output_size;
7722 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007723
Thomas Daubney5390aca2024-02-22 11:06:04 +00007724 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007725 /* output allocation failed. */
7726 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007727 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007728
Ryan Everetta103ec92024-01-31 13:59:57 +00007729 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007730
Thomas Daubney81899ab2024-02-15 12:57:26 +00007731 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7732 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007734}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007735
7736
7737/****************************************************************/
7738/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007739/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007740
Gilles Peskine672a7712023-04-28 21:00:28 +02007741#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7742#include "entropy_poll.h"
7743#endif
7744
Gilles Peskine30524eb2020-11-13 17:02:26 +01007745/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007746 *
7747 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7748 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007750static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007751{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007752#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007754#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7755
Gilles Peskine30524eb2020-11-13 17:02:26 +01007756 /* Set default configuration if
7757 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007759 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 }
7761 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007762 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007764
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007766#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7767 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7768 /* The PSA entropy injection feature depends on using NV seed as an entropy
7769 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 mbedtls_entropy_add_source(&rng->entropy,
7771 mbedtls_nv_seed_poll, NULL,
7772 MBEDTLS_ENTROPY_BLOCK_SIZE,
7773 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007774#endif
7775
Valerio Setti061d4e42024-02-26 12:52:44 +01007776 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007777#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007778}
7779
7780/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007781 *
7782 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7783 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007785static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007786{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007787#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007789#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007790 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007792#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007793}
7794
7795/** Seed the PSA random generator.
7796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007797static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007798{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007799#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7800 /* Do nothing: the external RNG seeds itself. */
7801 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007803#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007804 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007805 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 drbg_seed, sizeof(drbg_seed) - 1);
7807 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007808#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007809}
7810
David Horstmann6e99bb22024-02-06 15:27:49 +00007811psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007813{
David Horstmann6e99bb22024-02-06 15:27:49 +00007814 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007815
David Horstmann6e99bb22024-02-06 15:27:49 +00007816 LOCAL_OUTPUT_DECLARE(output_external, output);
7817 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007818
David Horstmann6e99bb22024-02-06 15:27:49 +00007819 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007820
David Horstmann4a48bec2024-03-13 15:42:31 +00007821#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00007822exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00007823#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00007824 LOCAL_OUTPUT_FREE(output_external, output);
7825 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007826}
7827
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007828#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007829psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7830 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007831{
Paul Elliott600472b2024-02-23 17:26:58 +00007832 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 return PSA_ERROR_NOT_PERMITTED;
7834 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007835
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7837 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7838 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7839 return PSA_ERROR_INVALID_ARGUMENT;
7840 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007841
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007843}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007844#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007845
Ronald Cron3772afe2021-02-08 16:10:05 +01007846/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007847 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007848 * \param type The key type
7849 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007850 *
7851 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007852 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007853 * \retval #PSA_ERROR_INVALID_ARGUMENT
7854 * The size in bits of the key is not valid.
7855 * \retval #PSA_ERROR_NOT_SUPPORTED
7856 * The type and/or the size in bits of the key or the combination of
7857 * the two is not supported.
7858 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007859static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007861{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 if (key_type_is_raw_bytes(type)) {
7865 status = psa_validate_unstructured_key_bit_size(type, bits);
7866 if (status != PSA_SUCCESS) {
7867 return status;
7868 }
7869 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007870#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007871 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7872 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7873 return PSA_ERROR_NOT_SUPPORTED;
7874 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007875 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007876 return PSA_ERROR_NOT_SUPPORTED;
7877 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007878
Ronald Cron01b2aba2020-10-05 09:42:02 +02007879 /* Accept only byte-aligned keys, for the same reasons as
7880 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 if (bits % 8 != 0) {
7882 return PSA_ERROR_NOT_SUPPORTED;
7883 }
7884 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007885#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007886
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007887#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007889 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 return PSA_SUCCESS;
7891 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007892#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007893
Valerio Settia55f0422023-07-10 15:34:41 +02007894#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007895 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007896 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007897 return PSA_ERROR_NOT_SUPPORTED;
7898 }
7899 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007900#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007901 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007903 }
7904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007906}
7907
Ronald Cron55ed0592020-10-05 10:30:40 +02007908psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007909 const psa_key_attributes_t *attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02007910 const psa_custom_key_parameters_t *custom,
7911 const uint8_t *custom_data,
7912 size_t custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007914{
7915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007916 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007917
Gilles Peskine7a18f962024-02-12 16:48:11 +01007918 /* Only used for RSA */
Gilles Peskinef36d7852024-06-06 21:11:44 +02007919 (void) custom;
7920 (void) custom_data;
7921 (void) custom_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00007924 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 if (status != PSA_SUCCESS) {
7926 return status;
7927 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007928
David Brown12ca5032021-02-11 11:02:00 -07007929#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (type == PSA_KEY_TYPE_DES) {
7931 psa_des_set_key_parity(key_buffer, key_buffer_size);
7932 }
David Brown8107e312021-02-12 08:08:46 -07007933#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007935
Valerio Setti76df8c12023-07-11 14:11:28 +02007936#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7938 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02007939 custom_data, custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 key_buffer,
7941 key_buffer_size,
7942 key_buffer_length);
7943 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007944#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007945
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007946#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7948 return mbedtls_psa_ecp_generate_key(attributes,
7949 key_buffer,
7950 key_buffer_size,
7951 key_buffer_length);
7952 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007953#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007954
Valerio Settia55f0422023-07-10 15:34:41 +02007955#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007956 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7957 return mbedtls_psa_ffdh_generate_key(attributes,
7958 key_buffer,
7959 key_buffer_size,
7960 key_buffer_length);
7961 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007962#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007963 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 (void) key_buffer_length;
7965 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007966 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007967
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007969}
Darryl Green0c6575a2018-11-07 16:05:30 +00007970
Gilles Peskinef36d7852024-06-06 21:11:44 +02007971psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
7972 const psa_custom_key_parameters_t *custom,
7973 const uint8_t *custom_data,
7974 size_t custom_data_length,
7975 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007976{
7977 psa_status_t status;
7978 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007979 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007980 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007981
Ronald Cron81709fc2020-11-14 12:10:32 +01007982 *key = MBEDTLS_SVC_KEY_ID_INIT;
7983
Gilles Peskine0f84d622019-09-12 19:03:13 +02007984 /* Reject any attempt to create a zero-length key so that we don't
7985 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if (psa_get_key_bits(attributes) == 0) {
7987 return PSA_ERROR_INVALID_ARGUMENT;
7988 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007989
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007990 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007991 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 return PSA_ERROR_INVALID_ARGUMENT;
7993 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007994
Gilles Peskine7a18f962024-02-12 16:48:11 +01007995#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007996 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinef36d7852024-06-06 21:11:44 +02007997 if (custom->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007998 return PSA_ERROR_INVALID_ARGUMENT;
7999 }
8000 } else
8001#endif
Gilles Peskine52504f82024-06-20 17:19:58 +02008002 if (!psa_custom_key_parameters_are_default(custom, custom_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01008003 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02008004 }
8005
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
8007 &slot, &driver);
8008 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02008009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 }
Gilles Peskine11792082019-08-06 18:36:36 +02008011
Ronald Cron977c2472020-10-13 08:32:21 +02008012 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05308013 * storage ( thus not in the case of generating a key in a secure element
8014 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
8015 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008016 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008017 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008019 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008020 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 }
Ronald Cron3772afe2021-02-08 16:10:05 +01008024
8025 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01008026 attributes->type,
8027 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02008029 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 attributes, &key_buffer_size);
8031 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01008032 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008033 }
Ronald Cron977c2472020-10-13 08:32:21 +02008034 }
Ronald Cron977c2472020-10-13 08:32:21 +02008035
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
8037 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02008038 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 }
Ronald Cron977c2472020-10-13 08:32:21 +02008040 }
8041
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +02008043 custom,
8044 custom_data, custom_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01008045 slot->key.data, slot->key.bytes,
8046 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 if (status != PSA_SUCCESS) {
8048 psa_remove_key_data_from_memory(slot);
8049 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02008050
Gilles Peskine11792082019-08-06 18:36:36 +02008051exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if (status == PSA_SUCCESS) {
8053 status = psa_finish_key_creation(slot, driver, key);
8054 }
8055 if (status != PSA_SUCCESS) {
8056 psa_fail_key_creation(slot, driver);
8057 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02008058
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008060}
8061
Gilles Peskinef36d7852024-06-06 21:11:44 +02008062psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
8063 const psa_key_production_parameters_t *params,
8064 size_t params_data_length,
8065 mbedtls_svc_key_id_t *key)
8066{
8067 return psa_generate_key_custom(
8068 attributes,
8069 (const psa_custom_key_parameters_t *) params,
8070 params->data, params_data_length,
8071 key);
8072}
8073
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008074psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
8075 mbedtls_svc_key_id_t *key)
8076{
Gilles Peskinef36d7852024-06-06 21:11:44 +02008077 return psa_generate_key_custom(attributes,
8078 &default_custom_production,
8079 NULL, 0,
8080 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008081}
8082
Gilles Peskinee59236f2018-01-27 23:32:46 +01008083/****************************************************************/
8084/* Module setup */
8085/****************************************************************/
8086
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008087#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01008088psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 void (* entropy_init)(mbedtls_entropy_context *ctx),
8090 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01008091{
Paul Elliott8e151532024-02-23 17:35:29 +00008092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8093
8094#if defined(MBEDTLS_THREADING_C)
8095 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8096#endif /* defined(MBEDTLS_THREADING_C) */
8097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00008099 status = PSA_ERROR_BAD_STATE;
8100 } else {
8101 global_data.rng.entropy_init = entropy_init;
8102 global_data.rng.entropy_free = entropy_free;
8103 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008104 }
Paul Elliott8e151532024-02-23 17:35:29 +00008105
8106#if defined(MBEDTLS_THREADING_C)
8107 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8108#endif /* defined(MBEDTLS_THREADING_C) */
8109
8110 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01008111}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008112#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01008113
Gilles Peskine449bd832023-01-11 14:50:10 +01008114void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008115{
Paul Elliott47cee8e2024-03-08 21:38:02 +00008116
Paul Elliott600472b2024-02-23 17:26:58 +00008117#if defined(MBEDTLS_THREADING_C)
8118 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8119#endif /* defined(MBEDTLS_THREADING_C) */
8120
Paul Elliott47cee8e2024-03-08 21:38:02 +00008121 /* Nothing to do to free transaction. */
8122 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
8123 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01008124 }
Paul Elliott600472b2024-02-23 17:26:58 +00008125
Paul Elliott47cee8e2024-03-08 21:38:02 +00008126 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
8127 psa_wipe_all_key_slots();
8128 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8129 }
Ronald Cron9ba76912021-04-10 16:57:30 +02008130
Paul Elliott600472b2024-02-23 17:26:58 +00008131#if defined(MBEDTLS_THREADING_C)
8132 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8133#endif /* defined(MBEDTLS_THREADING_C) */
8134
Paul Elliott47cee8e2024-03-08 21:38:02 +00008135#if defined(MBEDTLS_THREADING_C)
8136 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8137#endif /* defined(MBEDTLS_THREADING_C) */
8138
Gilles Peskinec6b69072018-11-20 21:42:52 +01008139 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01008140 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008141 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008142 global_data.rng_state = RNG_NOT_INITIALIZED;
8143 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
8144
8145#if defined(MBEDTLS_THREADING_C)
8146 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8147#endif /* defined(MBEDTLS_THREADING_C) */
8148
8149#if defined(MBEDTLS_THREADING_C)
8150 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8151#endif /* defined(MBEDTLS_THREADING_C) */
Ronald Cron9ba76912021-04-10 16:57:30 +02008152
8153 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00008154 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
8155 psa_driver_wrapper_free();
8156 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8157 }
8158
8159#if defined(MBEDTLS_THREADING_C)
8160 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8161#endif /* defined(MBEDTLS_THREADING_C) */
8162
Gilles Peskinee59236f2018-01-27 23:32:46 +01008163}
8164
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008165#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8166/** Recover a transaction that was interrupted by a power failure.
8167 *
8168 * This function is called during initialization, before psa_crypto_init()
8169 * returns. If this function returns a failure status, the initialization
8170 * fails.
8171 */
8172static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008174{
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008176 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8177 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 /* TODO - fall through to the failure case until this
8179 * is implemented.
8180 * https://github.com/ARMmbed/mbed-crypto/issues/218
8181 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008182 default:
8183 /* We found an unsupported transaction in the storage.
8184 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008185 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008186 }
8187}
8188#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8189
Paul Elliott47cee8e2024-03-08 21:38:02 +00008190static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
8191{
8192 psa_status_t status = PSA_SUCCESS;
8193 uint8_t driver_wrappers_initialized = 0;
8194
8195 switch (subsystem) {
8196 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
8197
8198#if defined(MBEDTLS_THREADING_C)
8199 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8200#endif /* defined(MBEDTLS_THREADING_C) */
8201
8202 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
8203 /* Init drivers */
8204 status = psa_driver_wrapper_init();
8205
8206 /* Drivers need shutdown regardless of startup errors. */
8207 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8208
8209
8210 }
8211#if defined(MBEDTLS_THREADING_C)
8212 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8213 &mbedtls_threading_psa_globaldata_mutex));
8214#endif /* defined(MBEDTLS_THREADING_C) */
8215
8216 break;
8217
8218 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
8219
8220#if defined(MBEDTLS_THREADING_C)
8221 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8222#endif /* defined(MBEDTLS_THREADING_C) */
8223
8224 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
8225 status = psa_initialize_key_slots();
8226
8227 /* Need to wipe keys even if initialization fails. */
8228 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8229
8230 }
8231#if defined(MBEDTLS_THREADING_C)
8232 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8233 &mbedtls_threading_psa_globaldata_mutex));
8234#endif /* defined(MBEDTLS_THREADING_C) */
8235
8236 break;
8237
8238 case PSA_CRYPTO_SUBSYSTEM_RNG:
8239
8240#if defined(MBEDTLS_THREADING_C)
8241 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8242#endif /* defined(MBEDTLS_THREADING_C) */
8243
8244 driver_wrappers_initialized =
8245 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
8246
8247#if defined(MBEDTLS_THREADING_C)
8248 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8249 &mbedtls_threading_psa_globaldata_mutex));
8250#endif /* defined(MBEDTLS_THREADING_C) */
8251
8252 /* Need to use separate mutex here, as initialisation can require
8253 * testing of init flags, which requires locking the global data
8254 * mutex. */
8255#if defined(MBEDTLS_THREADING_C)
8256 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
8257#endif /* defined(MBEDTLS_THREADING_C) */
8258
8259 /* Initialize and seed the random generator. */
8260 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
8261 mbedtls_psa_random_init(&global_data.rng);
8262 global_data.rng_state = RNG_INITIALIZED;
8263
8264 status = mbedtls_psa_random_seed(&global_data.rng);
8265 if (status == PSA_SUCCESS) {
8266 global_data.rng_state = RNG_SEEDED;
8267 }
8268 }
8269
8270#if defined(MBEDTLS_THREADING_C)
8271 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8272 &mbedtls_threading_psa_rngdata_mutex));
8273#endif /* defined(MBEDTLS_THREADING_C) */
8274
Paul Elliott47cee8e2024-03-08 21:38:02 +00008275 break;
8276
8277 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
8278
8279#if defined(MBEDTLS_THREADING_C)
8280 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8281#endif /* defined(MBEDTLS_THREADING_C) */
8282
8283 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
8284#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8285 status = psa_crypto_load_transaction();
8286 if (status == PSA_SUCCESS) {
8287 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8288 if (status == PSA_SUCCESS) {
8289 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8290 }
8291 status = psa_crypto_stop_transaction();
8292 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
8293 /* There's no transaction to complete. It's all good. */
8294 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8295 status = PSA_SUCCESS;
8296 }
8297#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8298 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8299 status = PSA_SUCCESS;
8300#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8301 }
8302
8303#if defined(MBEDTLS_THREADING_C)
8304 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8305 &mbedtls_threading_psa_globaldata_mutex));
8306#endif /* defined(MBEDTLS_THREADING_C) */
8307
8308 break;
8309
8310 default:
8311 status = PSA_ERROR_CORRUPTION_DETECTED;
8312 }
8313
8314 /* Exit label only required when using threading macros. */
8315#if defined(MBEDTLS_THREADING_C)
8316exit:
8317#endif /* defined(MBEDTLS_THREADING_C) */
8318
8319 return status;
8320}
8321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008323{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008324 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008325
Paul Elliott47cee8e2024-03-08 21:38:02 +00008326 /* Double initialization is explicitly allowed. Early out if everything is
8327 * done. */
8328 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 return PSA_SUCCESS;
8330 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008331
Paul Elliott47cee8e2024-03-08 21:38:02 +00008332 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01008333 if (status != PSA_SUCCESS) {
8334 goto exit;
8335 }
8336
Paul Elliott47cee8e2024-03-08 21:38:02 +00008337 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008341
Paul Elliott47cee8e2024-03-08 21:38:02 +00008342 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
8343 if (status != PSA_SUCCESS) {
8344 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02008345 }
Gilles Peskinefc762652019-07-22 19:30:34 +02008346
Paul Elliott47cee8e2024-03-08 21:38:02 +00008347 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01008348
8349exit:
Paul Elliott600472b2024-02-23 17:26:58 +00008350
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 if (status != PSA_SUCCESS) {
8352 mbedtls_psa_crypto_free();
8353 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008356}
8357
Yanray Wangffc3c482023-07-11 12:01:04 +08008358#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008359psa_status_t psa_crypto_driver_pake_get_password_len(
8360 const psa_crypto_driver_pake_inputs_t *inputs,
8361 size_t *password_len)
8362{
8363 if (inputs->password_len == 0) {
8364 return PSA_ERROR_BAD_STATE;
8365 }
8366
8367 *password_len = inputs->password_len;
8368
8369 return PSA_SUCCESS;
8370}
8371
8372psa_status_t psa_crypto_driver_pake_get_password(
8373 const psa_crypto_driver_pake_inputs_t *inputs,
8374 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8375{
8376 if (inputs->password_len == 0) {
8377 return PSA_ERROR_BAD_STATE;
8378 }
8379
8380 if (buffer_size < inputs->password_len) {
8381 return PSA_ERROR_BUFFER_TOO_SMALL;
8382 }
8383
8384 memcpy(buffer, inputs->password, inputs->password_len);
8385 *buffer_length = inputs->password_len;
8386
8387 return PSA_SUCCESS;
8388}
8389
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008390psa_status_t psa_crypto_driver_pake_get_user_len(
8391 const psa_crypto_driver_pake_inputs_t *inputs,
8392 size_t *user_len)
8393{
8394 if (inputs->user_len == 0) {
8395 return PSA_ERROR_BAD_STATE;
8396 }
8397
8398 *user_len = inputs->user_len;
8399
8400 return PSA_SUCCESS;
8401}
8402
8403psa_status_t psa_crypto_driver_pake_get_user(
8404 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008405 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008406{
8407 if (inputs->user_len == 0) {
8408 return PSA_ERROR_BAD_STATE;
8409 }
8410
Przemek Stekielc0e62502023-03-14 11:49:36 +01008411 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008412 return PSA_ERROR_BUFFER_TOO_SMALL;
8413 }
8414
Przemek Stekielc0e62502023-03-14 11:49:36 +01008415 memcpy(user_id, inputs->user, inputs->user_len);
8416 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008417
8418 return PSA_SUCCESS;
8419}
8420
8421psa_status_t psa_crypto_driver_pake_get_peer_len(
8422 const psa_crypto_driver_pake_inputs_t *inputs,
8423 size_t *peer_len)
8424{
8425 if (inputs->peer_len == 0) {
8426 return PSA_ERROR_BAD_STATE;
8427 }
8428
8429 *peer_len = inputs->peer_len;
8430
8431 return PSA_SUCCESS;
8432}
8433
8434psa_status_t psa_crypto_driver_pake_get_peer(
8435 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008436 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008437{
8438 if (inputs->peer_len == 0) {
8439 return PSA_ERROR_BAD_STATE;
8440 }
8441
Przemek Stekielc0e62502023-03-14 11:49:36 +01008442 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008443 return PSA_ERROR_BUFFER_TOO_SMALL;
8444 }
8445
Przemek Stekielc0e62502023-03-14 11:49:36 +01008446 memcpy(peer_id, inputs->peer, inputs->peer_len);
8447 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008448
8449 return PSA_SUCCESS;
8450}
8451
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008452psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8453 const psa_crypto_driver_pake_inputs_t *inputs,
8454 psa_pake_cipher_suite_t *cipher_suite)
8455{
8456 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8457 return PSA_ERROR_BAD_STATE;
8458 }
8459
8460 *cipher_suite = inputs->cipher_suite;
8461
8462 return PSA_SUCCESS;
8463}
8464
Neil Armstronga7d08c32022-06-01 18:21:20 +02008465psa_status_t psa_pake_setup(
8466 psa_pake_operation_t *operation,
8467 const psa_pake_cipher_suite_t *cipher_suite)
8468{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008469 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008470
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008471 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008472 status = PSA_ERROR_BAD_STATE;
8473 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008474 }
8475
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008476 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008477 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008478 status = PSA_ERROR_INVALID_ARGUMENT;
8479 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008480 }
8481
Przemek Stekiel51eac532022-12-07 11:04:51 +01008482 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8483
Przemek Stekiele12ed362022-12-21 12:54:46 +01008484 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008485 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8486 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008487 operation->data.inputs.cipher_suite = *cipher_suite;
8488
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008489#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008490 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008491 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008492 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008493
David Horstmann16f01512023-06-14 17:21:07 +01008494 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008495 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008496 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008497#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008498 {
8499 status = PSA_ERROR_NOT_SUPPORTED;
8500 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008501 }
8502
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008503 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8504
Przemek Stekiel51eac532022-12-07 11:04:51 +01008505 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008506exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008507 psa_pake_abort(operation);
8508 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008509}
8510
8511psa_status_t psa_pake_set_password_key(
8512 psa_pake_operation_t *operation,
8513 mbedtls_svc_key_id_t password)
8514{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008516 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8517 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008518 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008519
Przemek Stekiel51eac532022-12-07 11:04:51 +01008520 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008521 status = PSA_ERROR_BAD_STATE;
8522 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008523 }
8524
Przemek Stekiel6c764412022-11-22 14:05:12 +01008525 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8526 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008527 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008528 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008529 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008530 }
8531
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008532 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008533
Przemek Stekiel51eac532022-12-07 11:04:51 +01008534 if (type != PSA_KEY_TYPE_PASSWORD &&
8535 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8536 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008537 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008538 }
8539
Przemek Stekiel51eac532022-12-07 11:04:51 +01008540 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8541 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008542 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008543 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008544 }
8545
8546 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8547 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008548 operation->data.inputs.attributes = slot->attr;
8549
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008550exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008551 if (status != PSA_SUCCESS) {
8552 psa_pake_abort(operation);
8553 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008554 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008555 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008556}
8557
8558psa_status_t psa_pake_set_user(
8559 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008560 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008561 size_t user_id_len)
8562{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008564 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008565
8566 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008567 status = PSA_ERROR_BAD_STATE;
8568 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008569 }
8570
Przemek Stekiel51eac532022-12-07 11:04:51 +01008571 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008572 status = PSA_ERROR_INVALID_ARGUMENT;
8573 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008574 }
8575
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008576 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008577 status = PSA_ERROR_BAD_STATE;
8578 goto exit;
8579 }
8580
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008581 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8582 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008583 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8584 goto exit;
8585 }
8586
David Horstmann4f534ae2024-01-22 17:35:59 +00008587 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8588
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008589 memcpy(operation->data.inputs.user, user_id, user_id_len);
8590 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008591
David Horstmann4f534ae2024-01-22 17:35:59 +00008592 status = PSA_SUCCESS;
8593
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008594exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008595 LOCAL_INPUT_FREE(user_id_external, user_id);
8596 if (status != PSA_SUCCESS) {
8597 psa_pake_abort(operation);
8598 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008599 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008600}
8601
8602psa_status_t psa_pake_set_peer(
8603 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008604 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008605 size_t peer_id_len)
8606{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008608 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008609
8610 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008611 status = PSA_ERROR_BAD_STATE;
8612 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008613 }
8614
Przemek Stekiel51eac532022-12-07 11:04:51 +01008615 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008616 status = PSA_ERROR_INVALID_ARGUMENT;
8617 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008618 }
8619
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008620 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008621 status = PSA_ERROR_BAD_STATE;
8622 goto exit;
8623 }
8624
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008625 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8626 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008627 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8628 goto exit;
8629 }
8630
David Horstmann4f534ae2024-01-22 17:35:59 +00008631 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8632
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008633 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8634 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008635
David Horstmann4f534ae2024-01-22 17:35:59 +00008636 status = PSA_SUCCESS;
8637
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008638exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008639 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8640 if (status != PSA_SUCCESS) {
8641 psa_pake_abort(operation);
8642 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008643 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008644}
8645
8646psa_status_t psa_pake_set_role(
8647 psa_pake_operation_t *operation,
8648 psa_pake_role_t role)
8649{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008651
Przemek Stekiel51eac532022-12-07 11:04:51 +01008652 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008653 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008654 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008655 }
8656
Przemek Stekiel09104b82023-03-06 14:11:51 +01008657 switch (operation->alg) {
8658#if defined(PSA_WANT_ALG_JPAKE)
8659 case PSA_ALG_JPAKE:
8660 if (role == PSA_PAKE_ROLE_NONE) {
8661 return PSA_SUCCESS;
8662 }
8663 status = PSA_ERROR_INVALID_ARGUMENT;
8664 break;
8665#endif
8666 default:
8667 (void) role;
8668 status = PSA_ERROR_NOT_SUPPORTED;
8669 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008670 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008671exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008672 psa_pake_abort(operation);
8673 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008674}
8675
David Horstmanne7f21e62023-05-12 18:17:21 +01008676/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008677#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008678static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008679 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008680{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008681 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008682 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008683 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008684
David Horstmann024e5c52023-06-14 15:48:21 +01008685 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008686 is_x1 = (stage->outputs < 1);
8687 } else {
8688 is_x1 = (stage->inputs < 1);
8689 }
8690
David Horstmann74a3d8c2023-06-14 18:28:19 +01008691 key_share_step = is_x1 ?
8692 PSA_JPAKE_X1_STEP_KEY_SHARE :
8693 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008694 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008695 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8696 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8697 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8698 } else {
8699 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008700 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008701 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008702}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008703#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008704
Przemek Stekiel2797d372022-12-22 11:19:22 +01008705static psa_status_t psa_pake_complete_inputs(
8706 psa_pake_operation_t *operation)
8707{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008709 /* Create copy of the inputs on stack as inputs share memory
8710 with the driver context which will be setup by the driver. */
8711 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008712
Przemek Stekielfde11282023-03-13 16:06:09 +01008713 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008714 return PSA_ERROR_BAD_STATE;
8715 }
8716
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008717 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008718 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8719 return PSA_ERROR_BAD_STATE;
8720 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008721 }
8722
Przemek Stekiel18620a32023-01-17 16:34:52 +01008723 /* Clear driver context */
8724 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8725
8726 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008727
8728 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008729 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008730
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008731 /* User and peer are translated to role. */
8732 mbedtls_free(inputs.user);
8733 mbedtls_free(inputs.peer);
8734
Przemek Stekiel2797d372022-12-22 11:19:22 +01008735 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008736#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008737 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008738 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008739 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008740#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008741 {
8742 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008743 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008744 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008745 return status;
8746}
8747
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008748#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008749static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008750 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008751 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008752 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008753{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008754 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8755 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8756 step != PSA_PAKE_STEP_ZK_PROOF) {
8757 return PSA_ERROR_INVALID_ARGUMENT;
8758 }
8759
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008760 psa_jpake_computation_stage_t *computation_stage =
8761 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008762
David Horstmann5da95602023-06-08 15:37:12 +01008763 if (computation_stage->round != PSA_JPAKE_FIRST &&
8764 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008765 return PSA_ERROR_BAD_STATE;
8766 }
8767
David Horstmanne7f21e62023-05-12 18:17:21 +01008768 /* Check that the step we are given is the one we were expecting */
8769 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008770 return PSA_ERROR_BAD_STATE;
8771 }
8772
David Horstmanne7f21e62023-05-12 18:17:21 +01008773 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8774 computation_stage->inputs == 0 &&
8775 computation_stage->outputs == 0) {
8776 /* Start of the round, so function decides whether we are inputting
8777 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008778 computation_stage->io_mode = io_mode;
8779 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008780 /* Middle of the round so the mode we are in must match the function
8781 * called by the user */
8782 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008783 }
8784
8785 return PSA_SUCCESS;
8786}
8787
David Horstmanne7f21e62023-05-12 18:17:21 +01008788static psa_status_t psa_jpake_epilogue(
8789 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008790 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008791{
David Horstmanne7f21e62023-05-12 18:17:21 +01008792 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008793 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008794
David Horstmanne7f21e62023-05-12 18:17:21 +01008795 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8796 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008797 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008798 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008799 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008800 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008801 }
8802 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008803 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008804 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008805 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008806 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008807 }
8808 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008809 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8810 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008811 /* End of a round, move to the next round */
8812 stage->inputs = 0;
8813 stage->outputs = 0;
8814 stage->round++;
8815 }
8816 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008817 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008818 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008819 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008820 return PSA_SUCCESS;
8821}
David Horstmanne7f21e62023-05-12 18:17:21 +01008822
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008823#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008824
Neil Armstronga7d08c32022-06-01 18:21:20 +02008825psa_status_t psa_pake_output(
8826 psa_pake_operation_t *operation,
8827 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008828 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008829 size_t output_size,
8830 size_t *output_length)
8831{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008833 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008834 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008835 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008836
8837 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008838 status = psa_pake_complete_inputs(operation);
8839 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008840 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008841 }
8842 }
8843
8844 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008845 status = PSA_ERROR_BAD_STATE;
8846 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008847 }
8848
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008849 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008850 status = PSA_ERROR_INVALID_ARGUMENT;
8851 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008852 }
8853
Przemek Stekiele12ed362022-12-21 12:54:46 +01008854 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008855#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008856 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008857 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008858 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008859 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008860 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008861 driver_step = convert_jpake_computation_stage_to_driver_step(
8862 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008863 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008864#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008865 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008866 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008867 status = PSA_ERROR_NOT_SUPPORTED;
8868 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008869 }
8870
David Horstmannc75639d2024-01-22 17:56:39 +00008871 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
8872
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008873 status = psa_driver_wrapper_pake_output(operation, driver_step,
8874 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008875
8876 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008877 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008878 }
8879
8880 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008881#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008882 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008883 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008884 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008885 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008886 }
8887 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008888#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008889 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008890 status = PSA_ERROR_NOT_SUPPORTED;
8891 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008892 }
8893
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008894exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008895 LOCAL_OUTPUT_FREE(output_external, output);
8896 if (status != PSA_SUCCESS) {
8897 psa_pake_abort(operation);
8898 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008899 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008900}
8901
8902psa_status_t psa_pake_input(
8903 psa_pake_operation_t *operation,
8904 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008905 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008906 size_t input_length)
8907{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008909 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008910 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8911 operation->primitive,
8912 step);
David Horstmannc75639d2024-01-22 17:56:39 +00008913 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008914
8915 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008916 status = psa_pake_complete_inputs(operation);
8917 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008918 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008919 }
8920 }
8921
8922 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008923 status = PSA_ERROR_BAD_STATE;
8924 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008925 }
8926
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008927 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008928 status = PSA_ERROR_INVALID_ARGUMENT;
8929 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008930 }
8931
Przemek Stekiele12ed362022-12-21 12:54:46 +01008932 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008933#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008934 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008935 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008936 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008937 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008938 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008939 driver_step = convert_jpake_computation_stage_to_driver_step(
8940 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008941 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008942#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008943 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008944 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008945 status = PSA_ERROR_NOT_SUPPORTED;
8946 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008947 }
8948
David Horstmannc75639d2024-01-22 17:56:39 +00008949 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008950 status = psa_driver_wrapper_pake_input(operation, driver_step,
8951 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008952
8953 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008954 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008955 }
8956
8957 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008958#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008959 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008960 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008961 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008962 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008963 }
8964 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008965#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008966 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008967 status = PSA_ERROR_NOT_SUPPORTED;
8968 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008969 }
8970
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008971exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008972 LOCAL_INPUT_FREE(input_external, input);
8973 if (status != PSA_SUCCESS) {
8974 psa_pake_abort(operation);
8975 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008976 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008977}
8978
8979psa_status_t psa_pake_get_implicit_key(
8980 psa_pake_operation_t *operation,
8981 psa_key_derivation_operation_t *output)
8982{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008984 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008985 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008986 size_t shared_key_len = 0;
8987
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008988 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008989 status = PSA_ERROR_BAD_STATE;
8990 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008991 }
8992
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008993#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008994 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008995 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008996 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008997 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008998 status = PSA_ERROR_BAD_STATE;
8999 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009000 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01009001 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009002#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01009003 {
9004 status = PSA_ERROR_NOT_SUPPORTED;
9005 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009006 }
9007
Przemek Stekiel0c781802022-11-29 14:53:13 +01009008 status = psa_driver_wrapper_pake_get_implicit_key(operation,
9009 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01009010 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01009011 &shared_key_len);
9012
9013 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009014 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01009015 }
9016
9017 status = psa_key_derivation_input_bytes(output,
9018 PSA_KEY_DERIVATION_INPUT_SECRET,
9019 shared_key,
9020 shared_key_len);
9021
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01009022 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01009023exit:
9024 abort_status = psa_pake_abort(operation);
9025 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009026}
9027
9028psa_status_t psa_pake_abort(
9029 psa_pake_operation_t *operation)
9030{
Przemek Stekield69dca92023-01-31 19:59:20 +01009031 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01009032
Przemek Stekield69dca92023-01-31 19:59:20 +01009033 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009034 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02009035 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009036
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009037 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
9038 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009039 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01009040 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01009041 }
9042 if (operation->data.inputs.user != NULL) {
9043 mbedtls_free(operation->data.inputs.user);
9044 }
9045 if (operation->data.inputs.peer != NULL) {
9046 mbedtls_free(operation->data.inputs.peer);
9047 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009048 }
Przemek Stekield69dca92023-01-31 19:59:20 +01009049 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01009050
Przemek Stekield69dca92023-01-31 19:59:20 +01009051 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02009052}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01009053#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02009054
David Horstmann00d7a042023-12-07 18:39:17 +00009055/* Memory copying test hooks. These are called before input copy, after input
9056 * copy, before output copy and after output copy, respectively.
9057 * They are used by memory-poisoning tests to temporarily unpoison buffers
9058 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00009059#if defined(MBEDTLS_TEST_HOOKS)
9060void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9061void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9062void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9063void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9064#endif
David Horstmannfde97392023-10-30 20:29:43 +00009065
David Horstmann1b7279a2023-11-15 15:18:30 +00009066/** Copy from an input buffer to a local copy.
9067 *
9068 * \param[in] input Pointer to input buffer.
9069 * \param[in] input_len Length of the input buffer.
9070 * \param[out] input_copy Pointer to a local copy in which to store the input data.
9071 * \param[out] input_copy_len Length of the local copy buffer.
9072 * \return #PSA_SUCCESS, if the buffer was successfully
9073 * copied.
9074 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
9075 * copy is too small to hold contents of the
9076 * input buffer.
9077 */
9078MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00009079psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
9080 uint8_t *input_copy, size_t input_copy_len)
9081{
9082 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00009083 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00009084 }
9085
David Horstmann372b8bb2023-11-24 16:21:04 +00009086#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009087 if (psa_input_pre_copy_hook != NULL) {
9088 psa_input_pre_copy_hook(input, input_len);
9089 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009090#endif
9091
David Horstmann777e7412023-11-15 17:33:47 +00009092 if (input_len > 0) {
9093 memcpy(input_copy, input, input_len);
9094 }
David Horstmannfde97392023-10-30 20:29:43 +00009095
David Horstmann372b8bb2023-11-24 16:21:04 +00009096#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009097 if (psa_input_post_copy_hook != NULL) {
9098 psa_input_post_copy_hook(input, input_len);
9099 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009100#endif
9101
David Horstmannfde97392023-10-30 20:29:43 +00009102 return PSA_SUCCESS;
9103}
9104
David Horstmann1b7279a2023-11-15 15:18:30 +00009105/** Copy from a local output buffer into a user-supplied one.
9106 *
9107 * \param[in] output_copy Pointer to a local buffer containing the output.
9108 * \param[in] output_copy_len Length of the local buffer.
9109 * \param[out] output Pointer to user-supplied output buffer.
9110 * \param[out] output_len Length of the user-supplied output buffer.
9111 * \return #PSA_SUCCESS, if the buffer was successfully
9112 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00009113 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00009114 * user-supplied output buffer is too small to
9115 * hold the contents of the local buffer.
9116 */
9117MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00009118psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
9119 uint8_t *output, size_t output_len)
9120{
9121 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00009122 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00009123 }
David Horstmann777e7412023-11-15 17:33:47 +00009124
David Horstmann372b8bb2023-11-24 16:21:04 +00009125#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009126 if (psa_output_pre_copy_hook != NULL) {
9127 psa_output_pre_copy_hook(output, output_len);
9128 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009129#endif
9130
David Horstmann777e7412023-11-15 17:33:47 +00009131 if (output_copy_len > 0) {
9132 memcpy(output, output_copy, output_copy_len);
9133 }
9134
David Horstmann372b8bb2023-11-24 16:21:04 +00009135#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009136 if (psa_output_post_copy_hook != NULL) {
9137 psa_output_post_copy_hook(output, output_len);
9138 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009139#endif
9140
David Horstmann8978f5c2023-10-30 20:37:12 +00009141 return PSA_SUCCESS;
9142}
9143
David Horstmannf1734052023-11-20 17:15:32 +00009144psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
9145 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00009146{
9147 psa_status_t status;
9148
David Horstmann9db14482023-11-23 15:50:37 +00009149 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00009150
David Horstmannc5cc1c32023-11-15 18:11:26 +00009151 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00009152 return PSA_SUCCESS;
9153 }
9154
David Horstmannf1734052023-11-20 17:15:32 +00009155 local_input->buffer = mbedtls_calloc(input_len, 1);
9156 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00009157 /* Since we dealt with the zero-length case above, we know that
9158 * a NULL return value means a failure of allocation. */
9159 return PSA_ERROR_INSUFFICIENT_MEMORY;
9160 }
David Horstmannf1734052023-11-20 17:15:32 +00009161 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00009162
David Horstmannf1734052023-11-20 17:15:32 +00009163 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00009164
9165 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00009166 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00009167 if (status != PSA_SUCCESS) {
9168 goto error;
9169 }
9170
9171 return PSA_SUCCESS;
9172
9173error:
David Horstmannf1734052023-11-20 17:15:32 +00009174 mbedtls_free(local_input->buffer);
9175 local_input->buffer = NULL;
9176 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00009177 return status;
9178}
9179
David Horstmannf1734052023-11-20 17:15:32 +00009180void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00009181{
David Horstmannf1734052023-11-20 17:15:32 +00009182 mbedtls_free(local_input->buffer);
9183 local_input->buffer = NULL;
9184 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00009185}
9186
David Horstmann89875a42023-11-20 12:54:09 +00009187psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
9188 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00009189{
David Horstmann9db14482023-11-23 15:50:37 +00009190 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00009191
David Horstmannc5cc1c32023-11-15 18:11:26 +00009192 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009193 return PSA_SUCCESS;
9194 }
David Horstmann89875a42023-11-20 12:54:09 +00009195 local_output->buffer = mbedtls_calloc(output_len, 1);
9196 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009197 /* Since we dealt with the zero-length case above, we know that
9198 * a NULL return value means a failure of allocation. */
9199 return PSA_ERROR_INSUFFICIENT_MEMORY;
9200 }
David Horstmann89875a42023-11-20 12:54:09 +00009201 local_output->length = output_len;
9202 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00009203
9204 return PSA_SUCCESS;
9205}
9206
David Horstmann89875a42023-11-20 12:54:09 +00009207psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00009208{
David Horstmannc335a4e2023-11-15 15:57:32 +00009209 psa_status_t status;
9210
David Horstmann89875a42023-11-20 12:54:09 +00009211 if (local_output->buffer == NULL) {
9212 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009213 return PSA_SUCCESS;
9214 }
David Horstmann89875a42023-11-20 12:54:09 +00009215 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00009216 /* We have an internal copy but nothing to copy back to. */
9217 return PSA_ERROR_CORRUPTION_DETECTED;
9218 }
9219
David Horstmann89875a42023-11-20 12:54:09 +00009220 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
9221 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00009222 if (status != PSA_SUCCESS) {
9223 return status;
9224 }
David Horstmann9467ea32023-11-08 17:44:18 +00009225
David Horstmann89875a42023-11-20 12:54:09 +00009226 mbedtls_free(local_output->buffer);
9227 local_output->buffer = NULL;
9228 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009229
9230 return PSA_SUCCESS;
9231}
9232
Gilles Peskinee59236f2018-01-27 23:32:46 +01009233#endif /* MBEDTLS_PSA_CRYPTO_C */