blob: 28bcd21be2a3b45e563323b41ad1dbe095ef0517 [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. */
1213 if (slot->registered_readers != 1) {
1214 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1215 status = PSA_ERROR_CORRUPTION_DETECTED;
1216 }
1217 break;
1218 case PSA_SLOT_FILLING:
1219 /* In this state registered_readers must be 0. */
1220 if (slot->registered_readers != 0) {
1221 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1222 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 Peskine3f7cd622019-08-13 15:01:08 +02001235 /* Multipart operations may still be using the key. This is safe
1236 * because all multipart operation objects are independent from
1237 * the key slot: if they need to access the key after the setup
1238 * phase, they have a copy of the key. Note that this means that
1239 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001240 /* At this point, key material and other type-specific content has
1241 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001242 * zeroize because the metadata is not particularly sensitive.
1243 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 memset(slot, 0, sizeof(*slot));
1245 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001246}
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001250 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001251 psa_status_t status; /* status of the last operation */
1252 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1254 psa_se_drv_table_entry_t *driver;
1255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (mbedtls_svc_key_id_is_null(key)) {
1258 return PSA_SUCCESS;
1259 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001260
Ronald Cronf2911112020-10-29 17:51:10 +01001261 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001262 * Get the description of the key in a key slot, and register to read it.
1263 * In the case of a persistent key, this will load the key description
1264 * from persistent memory if not done yet.
1265 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001266 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001267 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 status = psa_get_and_lock_key_slot(key, &slot);
1269 if (status != PSA_SUCCESS) {
1270 return status;
1271 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001272
Ryan Everettc053d962024-01-25 17:56:32 +00001273#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001274 /* We cannot unlock between setting the state to PENDING_DELETION
1275 * and destroying the key in storage, as otherwise another thread
1276 * could load the key into a new slot and the key will not be
1277 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001278 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1279 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001280
1281 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1282 /* Another thread has destroyed the key between us locking the slot
1283 * and us gaining the mutex. Unregister from the slot,
1284 * and report that the key does not exist. */
1285 status = psa_unregister_read(slot);
1286
1287 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1288 &mbedtls_threading_key_slot_mutex));
1289 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1290 }
Ryan Everettc053d962024-01-25 17:56:32 +00001291#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001292 /* Set the key slot containing the key description's state to
1293 * PENDING_DELETION. This stops new operations from registering
1294 * to read the slot. Current readers can safely continue to access
1295 * the key within the slot; the last registered reader will
1296 * automatically wipe the slot when they call psa_unregister_read().
1297 * If the key is persistent, we can now delete the copy of the key
1298 * from memory. If the key is opaque, we require the driver to
1299 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001300 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1301 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001302
Ryan Everettd868b742024-03-08 18:35:09 +00001303 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001304 goto exit;
1305 }
Ronald Cronf2911112020-10-29 17:51:10 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001308 /* Refuse the destruction of a read-only key (which may or may not work
1309 * if we attempt it, depending on whether the key is merely read-only
1310 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001311 * Just do the best we can, which is to wipe the copy in memory
1312 * (done in this function's cleanup code). */
1313 overall_status = PSA_ERROR_NOT_PERMITTED;
1314 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001315 }
1316
Gilles Peskine354f7672019-07-12 23:46:38 +02001317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1319 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001320 /* For a key in a secure element, we need to do three things:
1321 * remove the key file in internal storage, destroy the
1322 * key inside the secure element, and update the driver's
1323 * persistent data. Start a transaction that will encompass these
1324 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001326 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001328 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 status = psa_crypto_save_transaction();
1330 if (status != PSA_SUCCESS) {
1331 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001332 /* We should still try to destroy the key in the secure
1333 * element and the key metadata in storage. This is especially
1334 * important if the error is that the storage is full.
1335 * But how to do it exactly without risking an inconsistent
1336 * state after a reset?
1337 * https://github.com/ARMmbed/mbed-crypto/issues/215
1338 */
1339 overall_status = status;
1340 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001341 }
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 status = psa_destroy_se_key(driver,
1344 psa_key_slot_get_slot_number(slot));
1345 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001346 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001348 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001349#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1350
Darryl Greend49a4992018-06-18 17:27:26 +01001351#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001353 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001354 * The slot will still hold a copy of the key until the last reader
1355 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 status = psa_destroy_persistent_key(slot->attr.id);
1357 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001358 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 }
Darryl Greend49a4992018-06-18 17:27:26 +01001360 }
1361#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001362
Gilles Peskinefc762652019-07-22 19:30:34 +02001363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (driver != NULL) {
1365 status = psa_save_se_persistent_data(driver);
1366 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001367 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 }
1369 status = psa_crypto_stop_transaction();
1370 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001371 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001373 }
1374#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1375
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001376exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001377 /* Unregister from reading the slot. If we are the last active reader
1378 * then this will wipe the slot. */
1379 status = psa_unregister_read(slot);
1380 /* Prioritize CORRUPTION_DETECTED from unregistering over
1381 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001383 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 }
Ryan Everettc053d962024-01-25 17:56:32 +00001385
1386#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001387 /* Don't overwrite existing errors if the unlock fails. */
1388 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001389 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1390 &mbedtls_threading_key_slot_mutex));
1391#endif
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394}
1395
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001396/** Retrieve all the publicly-accessible attributes of a key.
1397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1399 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001402 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1407 if (status != PSA_SUCCESS) {
1408 return status;
1409 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001410
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001411 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001412
1413#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1415 psa_set_key_slot_number(attributes,
1416 psa_key_slot_get_slot_number(slot));
1417 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001418#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001419
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001420 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421}
1422
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1424psa_status_t psa_get_key_slot_number(
1425 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001427{
Gilles Peskine972539c2024-02-28 01:49:45 +01001428 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001429 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 return PSA_SUCCESS;
1431 } else {
1432 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001433 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434}
1435#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1438 size_t key_buffer_size,
1439 uint8_t *data,
1440 size_t data_size,
1441 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001442{
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (key_buffer_size > data_size) {
1444 return PSA_ERROR_BUFFER_TOO_SMALL;
1445 }
1446 memcpy(data, key_buffer, key_buffer_size);
1447 memset(data + key_buffer_size, 0,
1448 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001449 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001451}
1452
Ronald Cron7285cda2020-11-26 14:46:37 +01001453psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 const psa_key_attributes_t *attributes,
1455 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001458 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (key_type_is_raw_bytes(type) ||
1461 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001462 PSA_KEY_TYPE_IS_ECC(type) ||
1463 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return psa_export_key_buffer_internal(
1465 key_buffer, key_buffer_size,
1466 data, data_size, data_length);
1467 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001468 /* This shouldn't happen in the reference implementation, but
1469 it is valid for a special-purpose implementation to omit
1470 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001472 }
1473}
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001476 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 size_t data_size,
1478 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479{
1480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1481 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1482 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001483 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484
Ronald Crone907e552021-01-18 13:22:38 +01001485 /* Reject a zero-length output buffer now, since this can never be a
1486 * valid key representation. This way we know that data must be a valid
1487 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (data_size == 0) {
1489 return PSA_ERROR_BUFFER_TOO_SMALL;
1490 }
Ronald Crone907e552021-01-18 13:22:38 +01001491
Ronald Cron9486f9d2020-11-26 13:36:23 +01001492 /* Set the key to empty now, so that even when there are errors, we always
1493 * set data_length to a value between 0 and data_size. On error, setting
1494 * the key to empty is a good choice because an empty key representation is
1495 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001496 *data_length = 0;
1497
Ronald Cron9486f9d2020-11-26 13:36:23 +01001498 /* Export requires the EXPORT flag. There is an exception for public keys,
1499 * which don't require any flag, but
1500 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1503 PSA_KEY_USAGE_EXPORT, 0);
1504 if (status != PSA_SUCCESS) {
1505 return status;
1506 }
mohammad160306e79202018-03-28 13:17:44 +03001507
Ryan Everett45ac5262024-01-08 17:15:19 +00001508 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1509
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001510 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 slot->key.data, slot->key.bytes,
1512 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001513
David Horstmann4a48bec2024-03-13 15:42:31 +00001514#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001515exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001516#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001517 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001518
Ryan Everett45ac5262024-01-08 17:15:19 +00001519 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001521}
1522
Ronald Cron7285cda2020-11-26 14:46:37 +01001523psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001524 const psa_key_attributes_t *attributes,
1525 const uint8_t *key_buffer,
1526 size_t key_buffer_size,
1527 uint8_t *data,
1528 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001530{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001531 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001532
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001533 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001534 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1535 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001536 /* Exporting public -> public */
1537 return psa_export_key_buffer_internal(
1538 key_buffer, key_buffer_size,
1539 data, data_size, data_length);
1540 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001542 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1543 return mbedtls_psa_rsa_export_public_key(attributes,
1544 key_buffer,
1545 key_buffer_size,
1546 data,
1547 data_size,
1548 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001549#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001550 /* We don't know how to convert a private RSA key to public. */
1551 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001552#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001553 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001554 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001555#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001556 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1557 return mbedtls_psa_ecp_export_public_key(attributes,
1558 key_buffer,
1559 key_buffer_size,
1560 data,
1561 data_size,
1562 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001563#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001564 /* We don't know how to convert a private ECC key to public */
1565 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001566#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001567 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001568 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001569#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001570 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001571 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001572 key_buffer,
1573 key_buffer_size,
1574 data, data_size,
1575 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001576#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001577 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001578#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001579 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001581 (void) key_buffer;
1582 (void) key_buffer_size;
1583 (void) data;
1584 (void) data_size;
1585 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588}
Ronald Crond18b5f82020-11-25 16:46:05 +01001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001591 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 size_t data_size,
1593 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001594{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001596 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001597 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001598
Ryan Everettb1d2c672024-01-08 17:19:30 +00001599 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001600
Ronald Crone907e552021-01-18 13:22:38 +01001601 /* Reject a zero-length output buffer now, since this can never be a
1602 * valid key representation. This way we know that data must be a valid
1603 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (data_size == 0) {
1605 return PSA_ERROR_BUFFER_TOO_SMALL;
1606 }
Ronald Crone907e552021-01-18 13:22:38 +01001607
Darryl Greendd8fb772018-11-07 16:00:44 +00001608 /* Set the key to empty now, so that even when there are errors, we always
1609 * set data_length to a value between 0 and data_size. On error, setting
1610 * the key to empty is a good choice because an empty key representation is
1611 * unlikely to be accepted anywhere. */
1612 *data_length = 0;
1613
1614 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1616 if (status != PSA_SUCCESS) {
1617 return status;
1618 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001619
Ryan Everettb1d2c672024-01-08 17:19:30 +00001620 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1623 status = PSA_ERROR_INVALID_ARGUMENT;
1624 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001625 }
1626
Ronald Cron67227982020-11-26 15:16:05 +01001627 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001628 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001630
1631exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001632 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001633
Ryan Everettb1d2c672024-01-08 17:19:30 +00001634 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001636}
1637
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001638/** Validate that a key policy is internally well-formed.
1639 *
1640 * This function only rejects invalid policies. It does not validate the
1641 * consistency of the policy with respect to other attributes of the key
1642 * such as the key type.
1643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001645{
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1647 PSA_KEY_USAGE_COPY |
1648 PSA_KEY_USAGE_ENCRYPT |
1649 PSA_KEY_USAGE_DECRYPT |
1650 PSA_KEY_USAGE_SIGN_MESSAGE |
1651 PSA_KEY_USAGE_VERIFY_MESSAGE |
1652 PSA_KEY_USAGE_SIGN_HASH |
1653 PSA_KEY_USAGE_VERIFY_HASH |
1654 PSA_KEY_USAGE_VERIFY_DERIVATION |
1655 PSA_KEY_USAGE_DERIVE)) != 0) {
1656 return PSA_ERROR_INVALID_ARGUMENT;
1657 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001660}
1661
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001662/** Validate the internal consistency of key attributes.
1663 *
1664 * This function only rejects invalid attribute values. If does not
1665 * validate the consistency of the attributes with any key data that may
1666 * be involved in the creation of the key.
1667 *
1668 * Call this function early in the key creation process.
1669 *
1670 * \param[in] attributes Key attributes for the new key.
1671 * \param[out] p_drv On any return, the driver for the key, if any.
1672 * NULL for a transparent key.
1673 *
1674 */
1675static psa_status_t psa_validate_key_attributes(
1676 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001678{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001679 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1681 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 status = psa_validate_key_location(lifetime, p_drv);
1684 if (status != PSA_SUCCESS) {
1685 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001686 }
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 status = psa_validate_key_persistence(lifetime);
1689 if (status != PSA_SUCCESS) {
1690 return status;
1691 }
1692
1693 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1694 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1695 return PSA_ERROR_INVALID_ARGUMENT;
1696 }
1697 } else {
1698 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1699 return PSA_ERROR_INVALID_ARGUMENT;
1700 }
1701 }
1702
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001703 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 if (status != PSA_SUCCESS) {
1705 return status;
1706 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001707
1708 /* Refuse to create overly large keys.
1709 * Note that this doesn't trigger on import if the attributes don't
1710 * explicitly specify a size (so psa_get_key_bits returns 0), so
1711 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1713 return PSA_ERROR_NOT_SUPPORTED;
1714 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001717}
1718
Gilles Peskine4747d192019-04-17 15:05:45 +02001719/** Prepare a key slot to receive key material.
1720 *
1721 * This function allocates a key slot and sets its metadata.
1722 *
1723 * If this function fails, call psa_fail_key_creation().
1724 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001725 * This function is intended to be used as follows:
1726 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001727 * it with the specified attributes, and in case of a volatile key assign it
1728 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001729 * -# Populate the slot with the key material.
1730 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1731 * In case of failure at any step, stop the sequence and call
1732 * psa_fail_key_creation().
1733 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001734 * On success, the key slot's state is PSA_SLOT_FILLING.
1735 * It is the responsibility of the caller to change the slot's state to
1736 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001737 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001738 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001740 * \param[out] p_slot On success, a pointer to the prepared slot.
1741 * \param[out] p_drv On any return, the driver for the key, if any.
1742 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001743 *
1744 * \retval #PSA_SUCCESS
1745 * The key slot is ready to receive key material.
1746 * \return If this function fails, the key slot is an invalid state.
1747 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 */
1749static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001750 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001754{
1755 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001756 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 psa_key_slot_t *slot;
1758
Gilles Peskinedf179142019-07-15 22:02:14 +02001759 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 *p_drv = NULL;
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 status = psa_validate_key_attributes(attributes, p_drv);
1763 if (status != PSA_SUCCESS) {
1764 return status;
1765 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001766
Ryan Everett3d8118d2024-01-30 16:58:47 +00001767#if defined(MBEDTLS_THREADING_C)
1768 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1769 &mbedtls_threading_key_slot_mutex));
1770#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001771 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001772#if defined(MBEDTLS_THREADING_C)
1773 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1774 &mbedtls_threading_key_slot_mutex));
1775#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (status != PSA_SUCCESS) {
1777 return status;
1778 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 slot = *p_slot;
1780
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001781 /* We're storing the declared bit-size of the key. It's up to each
1782 * creation mechanism to verify that this information is correct.
1783 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001784 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001785 * is optional (import, copy). In case of a volatile key, assign it the
1786 * volatile key identifier associated to the slot returned to contain its
1787 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001788
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001789 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001791#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1792 slot->attr.id = volatile_key_id;
1793#else
1794 slot->attr.id.key_id = volatile_key_id;
1795#endif
1796 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001797
Gilles Peskinecbaff462019-07-12 23:46:04 +02001798#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001799 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001800 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001801 * create the key file in internal storage, create the
1802 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001803 * persistent data. This is done by starting a transaction that will
1804 * encompass these three actions.
1805 * For registering a volatile key, we just need to find an appropriate
1806 * slot number inside the SE. Since the key is designated volatile, creating
1807 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001808 /* The first thing to do is to find a slot number for the new key.
1809 * We save the slot number in persistent storage as part of the
1810 * transaction data. It will be needed to recover if the power
1811 * fails during the key creation process, to clean up on the secure
1812 * element side after restarting. Obtaining a slot number from the
1813 * secure element driver updates its persistent state, but we do not yet
1814 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001815 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001817 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1819 &slot_number);
1820 if (status != PSA_SUCCESS) {
1821 return status;
1822 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001824 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001827 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001828 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 status = psa_crypto_save_transaction();
1830 if (status != PSA_SUCCESS) {
1831 (void) psa_crypto_stop_transaction();
1832 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001834 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001835
1836 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Ryan Everettb5a20d32023-11-15 16:26:01 +00001838 if (status != PSA_SUCCESS) {
1839 return status;
1840 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001841 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001844 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001846 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001847#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001850}
Gilles Peskine4747d192019-04-17 15:05:45 +02001851
1852/** Finalize the creation of a key once its key material has been set.
1853 *
1854 * This entails writing the key to persistent storage.
1855 *
1856 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001857 * See the documentation of psa_start_key_creation() for the intended use
1858 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001860 * If the finalization succeeds, the function sets the key slot's state to
1861 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1862 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001863 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 * \param[in,out] slot Pointer to the slot with key material.
1865 * \param[in] driver The secure element driver for the key,
1866 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001867 * \param[out] key On success, identifier of the key. Note that the
1868 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001869 *
1870 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001871 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001872 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1873 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1874 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1875 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1876 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1877 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001878 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001879 * \return If this function fails, the key slot is an invalid state.
1880 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001882static psa_status_t psa_finish_key_creation(
1883 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 psa_se_drv_table_entry_t *driver,
1885 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001886{
1887 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001888 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001889 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001890
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001891#if defined(MBEDTLS_THREADING_C)
1892 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1893 &mbedtls_threading_key_slot_mutex));
1894#endif
1895
Gilles Peskine4747d192019-04-17 15:05:45 +02001896#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001898#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001900 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001901 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001903
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001904 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1905 sizeof(data.slot_number),
1906 "Slot number size does not match psa_se_key_data_storage_t");
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1909 status = psa_save_persistent_key(&slot->attr,
1910 (uint8_t *) &data,
1911 sizeof(data));
1912 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1914 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001915 /* Key material is saved in export representation in the slot, so
1916 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_save_persistent_key(&slot->attr,
1918 slot->key.data,
1919 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001922#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1923
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001925 /* Finish the transaction for a key creation. This does not
1926 * happen when registering an existing key. Detect this case
1927 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001928 * creation of a persistent key in a secure element requires a transaction,
1929 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (driver != NULL &&
1931 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1932 status = psa_save_se_persistent_data(driver);
1933 if (status != PSA_SUCCESS) {
1934 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001935
1936#if defined(MBEDTLS_THREADING_C)
1937 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1938 &mbedtls_threading_key_slot_mutex));
1939#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001941 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943 }
1944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001947 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001948 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1949 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001951 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001953 }
Ronald Cron50972942020-11-14 11:28:25 +01001954
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001955#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 Peskine4747d192019-04-17 15:05:45 +02001960}
1961
1962/** Abort the creation of a key.
1963 *
1964 * You may call this function after calling psa_start_key_creation(),
1965 * or after psa_finish_key_creation() fails. In other circumstances, this
1966 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001967 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001968 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001969 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001970 * \param[in,out] slot Pointer to the slot with key material.
1971 * \param[in] driver The secure element driver for the key,
1972 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974static void psa_fail_key_creation(psa_key_slot_t *slot,
1975 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001976{
Gilles Peskine011e4282019-06-26 18:34:38 +02001977 (void) driver;
1978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001980 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001982
Ryan Everettb7101442024-01-23 20:09:49 +00001983#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001984 /* If the lock operation fails we still wipe the slot.
1985 * Operations will no longer work after a failed lock,
1986 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001987 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1988#endif
1989
Gilles Peskine011e4282019-06-26 18:34:38 +02001990#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001991 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 * element, and the failure happened later (when saving metadata
1993 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001994 * element.
1995 * https://github.com/ARMmbed/mbed-crypto/issues/217
1996 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001997
Gilles Peskined7729582019-08-05 15:55:54 +02001998 /* Abort the ongoing transaction if any (there may not be one if
1999 * the creation process failed before starting one, or if the
2000 * key creation is a registration of a key in a secure element).
2001 * Earlier functions must already have done what it takes to undo any
2002 * partial creation. All that's left is to update the transaction data
2003 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002005#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00002008
2009#if defined(MBEDTLS_THREADING_C)
2010 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
2011#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02002012}
2013
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002014/** Validate optional attributes during key creation.
2015 *
2016 * Some key attributes are optional during key creation. If they are
2017 * specified in the attributes structure, check that they are consistent
2018 * with the data in the slot.
2019 *
2020 * This function should be called near the end of key creation, after
2021 * the slot in memory is fully populated but before saving persistent data.
2022 */
2023static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002026{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002027 if (attributes->type != 0) {
2028 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 return PSA_ERROR_INVALID_ARGUMENT;
2030 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 }
2032
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002033 if (attributes->bits != 0) {
2034 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 return PSA_ERROR_INVALID_ARGUMENT;
2036 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037 }
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002040}
2041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00002043 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 size_t data_length,
2045 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002046{
2047 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00002048 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002049 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002050 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002051 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302052 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002053
Ronald Cron81709fc2020-11-14 12:10:32 +01002054 *key = MBEDTLS_SVC_KEY_ID_INIT;
2055
Gilles Peskine0f84d622019-09-12 19:03:13 +02002056 /* Reject zero-length symmetric keys (including raw data key objects).
2057 * This also rejects any key which might be encoded as an empty string,
2058 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (data_length == 0) {
2060 return PSA_ERROR_INVALID_ARGUMENT;
2061 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002062
Archana449608b2021-09-08 15:36:05 +05302063 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (data_length > SIZE_MAX / 8) {
2065 return PSA_ERROR_NOT_SUPPORTED;
2066 }
Archana6ed4bda2021-08-04 10:47:15 +05302067
Ryan Everettf028fe12024-01-08 17:14:44 +00002068 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2071 &slot, &driver);
2072 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002076 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302077 * storage ( thus not in the case of importing a key in a secure element
2078 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2079 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002081 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302082 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 attributes, data, data_length, &storage_size);
2084 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
Archanad8a83dc2021-06-14 10:04:16 +05302087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 status = psa_allocate_buffer_to_slot(slot, storage_size);
2089 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002092 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002093
2094 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 status = psa_driver_wrapper_import_key(attributes,
2096 data, data_length,
2097 slot->key.data,
2098 slot->key.bytes,
2099 &slot->key.bytes, &bits);
2100 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002105 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002107 status = PSA_ERROR_INVALID_ARGUMENT;
2108 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002109 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002110
Archana6ed4bda2021-08-04 10:47:15 +05302111 /* Enforce a size limit, and in particular ensure that the bit
2112 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302114 status = PSA_ERROR_NOT_SUPPORTED;
2115 goto exit;
2116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 status = psa_validate_optional_attributes(slot, attributes);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002123exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002124 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (status != PSA_SUCCESS) {
2126 psa_fail_key_creation(slot, driver);
2127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002130}
2131
Gilles Peskined7729582019-08-05 15:55:54 +02002132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2133psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002135{
2136 psa_status_t status;
2137 psa_key_slot_t *slot = NULL;
2138 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002140
2141 /* Leaving attributes unspecified is not currently supported.
2142 * It could make sense to query the key type and size from the
2143 * secure element, but not all secure elements support this
2144 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
2148 if (psa_get_key_bits(attributes) == 0) {
2149 return PSA_ERROR_NOT_SUPPORTED;
2150 }
Gilles Peskined7729582019-08-05 15:55:54 +02002151
Gilles Peskined72ad732024-06-13 16:06:45 +02002152 /* Not usable with volatile keys, even with an appropriate location,
2153 * due to the API design.
2154 * https://github.com/Mbed-TLS/mbedtls/issues/9253
2155 */
2156 if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
2157 return PSA_ERROR_INVALID_ARGUMENT;
2158 }
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2161 &slot, &driver);
2162 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
Gilles Peskined7729582019-08-05 15:55:54 +02002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002167
2168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (status != PSA_SUCCESS) {
2170 psa_fail_key_creation(slot, driver);
2171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002172
Gilles Peskined7729582019-08-05 15:55:54 +02002173 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_close_key(key);
2175 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002176}
2177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2180 const psa_key_attributes_t *specified_attributes,
2181 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002182{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002184 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002185 psa_key_slot_t *source_slot = NULL;
2186 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002187 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002188 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302189 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002190
Ronald Cron81709fc2020-11-14 12:10:32 +01002191 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2192
Archana8a180362021-07-05 02:18:48 +05302193 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2195 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 status = psa_validate_optional_attributes(source_slot,
2200 specified_attributes);
2201 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002204
Archana9d17bf42021-09-10 06:22:44 +05302205 /* The target key type and number of bits have been validated by
2206 * psa_validate_optional_attributes() to be either equal to zero or
2207 * equal to the ones of the source key. So it is safe to inherit
2208 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302209 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002210 actual_attributes.bits = source_slot->attr.bits;
2211 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302212
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002215 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 &source_slot->attr.policy);
2217 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2222 &target_slot, &driver);
2223 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
2226 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2227 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302228 /*
Archana9d17bf42021-09-10 06:22:44 +05302229 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302230 * the source key would need to be exported as plaintext and re-imported
2231 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302232 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302233 * appropriate API invocations from the application, if needed.
2234 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002235 status = PSA_ERROR_NOT_SUPPORTED;
2236 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002237 }
Archana8a180362021-07-05 02:18:48 +05302238 /*
2239 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302240 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302241 * - For opaque keys this translates to an invocation of the drivers'
2242 * copy_key entry point through the dispatch layer.
2243 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002244 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2246 &storage_size);
2247 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 }
Archana374fe5b2021-09-08 15:50:28 +05302250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2252 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 }
Archana374fe5b2021-09-08 15:50:28 +05302255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 status = psa_driver_wrapper_copy_key(&actual_attributes,
2257 source_slot->key.data,
2258 source_slot->key.bytes,
2259 target_slot->key.data,
2260 target_slot->key.bytes,
2261 &target_slot->key.bytes);
2262 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 }
2265 } else {
2266 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302267 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 source_slot->key.bytes);
2269 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302270 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 }
Archana8a180362021-07-05 02:18:48 +05302272 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (status != PSA_SUCCESS) {
2276 psa_fail_key_creation(target_slot, driver);
2277 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002278
Ryan Everetta103ec92024-01-31 13:59:57 +00002279 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002282}
2283
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002284
2285
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002286/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002287/* Message digests */
2288/****************************************************************/
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002292 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (operation->id == 0) {
2294 return PSA_SUCCESS;
2295 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002298 operation->id = 0;
2299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301}
2302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2304 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002307
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002308 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002310 status = PSA_ERROR_BAD_STATE;
2311 goto exit;
2312 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002315 status = PSA_ERROR_INVALID_ARGUMENT;
2316 goto exit;
2317 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002318
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002319 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2320 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002324
2325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (status != PSA_SUCCESS) {
2327 psa_hash_abort(operation);
2328 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002329
2330 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002334 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002338 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002341 status = PSA_ERROR_BAD_STATE;
2342 goto exit;
2343 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002344
Steven Cooremanc8288352021-03-04 14:02:19 +01002345 /* Don't require hash implementations to behave correctly on a
2346 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (input_length == 0) {
2348 return PSA_SUCCESS;
2349 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002350
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002351 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353
2354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (status != PSA_SUCCESS) {
2356 psa_hash_abort(operation);
2357 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002358
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002359 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361}
2362
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002363static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002364 uint8_t *hash,
2365 size_t hash_size,
2366 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002368 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2369
Steven Cooremanfbe09282021-03-08 18:41:12 +01002370 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (operation->id == 0) {
2372 return PSA_ERROR_BAD_STATE;
2373 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002375 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 operation, hash, hash_size, hash_length);
2377 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002378
2379 return status;
2380}
2381
2382psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2383 uint8_t *hash_external,
2384 size_t hash_size,
2385 size_t *hash_length)
2386{
2387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2388 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2389
2390 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2391 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2392
David Horstmann4a48bec2024-03-13 15:42:31 +00002393#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002394exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002395#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002396 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398}
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002401 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403{
Ronald Cron69a63422021-10-18 09:47:58 +02002404 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2407 LOCAL_INPUT_DECLARE(hash_external, hash);
2408
2409 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 operation,
2411 actual_hash, sizeof(actual_hash),
2412 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419 status = PSA_ERROR_INVALID_SIGNATURE;
2420 goto exit;
2421 }
2422
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002423 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002424 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002425 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427
2428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2430 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002431 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002433 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435}
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002438 const uint8_t *input_external, size_t input_length,
2439 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002441{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2443 LOCAL_INPUT_DECLARE(input_external, input);
2444 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2445
Steven Cooremanfbe09282021-03-08 18:41:12 +01002446 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (!PSA_ALG_IS_HASH(alg)) {
2448 return PSA_ERROR_INVALID_ARGUMENT;
2449 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002450
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002451 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2452 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2453 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002454 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002455
David Horstmann4a48bec2024-03-13 15:42:31 +00002456#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002457exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002458#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002459 LOCAL_INPUT_FREE(input_external, input);
2460 LOCAL_OUTPUT_FREE(hash_external, hash);
2461 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002462}
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002465 const uint8_t *input_external, size_t input_length,
2466 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002467{
Ronald Cron69a63422021-10-18 09:47:58 +02002468 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002469 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2471
2472 LOCAL_INPUT_DECLARE(input_external, input);
2473 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002476 status = PSA_ERROR_INVALID_ARGUMENT;
2477 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002479
Thomas Daubney51ffac92024-01-18 15:46:26 +00002480 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2481 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 alg, input, input_length,
2483 actual_hash, sizeof(actual_hash),
2484 &actual_hash_length);
2485 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002486 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 }
2488 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002489 status = PSA_ERROR_INVALID_SIGNATURE;
2490 goto exit;
2491 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002492
2493 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002494 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002495 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002497
2498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002500
2501 LOCAL_INPUT_FREE(input_external, input);
2502 LOCAL_INPUT_FREE(hash_external, hash);
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002505}
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2508 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002509{
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (source_operation->id == 0 ||
2511 target_operation->id != 0) {
2512 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002513 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2516 target_operation);
2517 if (status != PSA_SUCCESS) {
2518 psa_hash_abort(target_operation);
2519 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002522}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002523
2524
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002525/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526/* MAC */
2527/****************************************************************/
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002530{
Steven Cooremane6804192021-03-19 18:28:56 +01002531 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (operation->id == 0) {
2533 return PSA_SUCCESS;
2534 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002537 operation->mac_size = 0;
2538 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002539 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002542}
2543
Ronald Cron2dff3b22021-06-17 16:33:22 +02002544static psa_status_t psa_mac_finalize_alg_and_key_validation(
2545 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002546 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 psa_key_type_t key_type = psa_get_key_type(attributes);
2551 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (!PSA_ALG_IS_MAC(alg)) {
2554 return PSA_ERROR_INVALID_ARGUMENT;
2555 }
Steven Cooremane6804192021-03-19 18:28:56 +01002556
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002557 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 status = psa_mac_key_can_do(alg, key_type);
2559 if (status != PSA_SUCCESS) {
2560 return status;
2561 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002562
Steven Cooremand1a68f12021-05-10 11:13:41 +02002563 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002567 /* A very short MAC is too short for security since it can be
2568 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2569 * so we make this our minimum, even though 32 bits is still
2570 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002572 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2575 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002576 /* It's impossible to "truncate" to a larger length than the full length
2577 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002579 }
2580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002582 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2583 * that is disabled in the compile-time configuration. The result can
2584 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2585 * configuration into account. In this case, force a return of
2586 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2587 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2588 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2589 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2590 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002592 }
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002595}
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2598 mbedtls_svc_key_id_t key,
2599 psa_algorithm_t alg,
2600 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002601{
2602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2603 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002604 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605
2606 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002608 status = PSA_ERROR_BAD_STATE;
2609 goto exit;
2610 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611
2612 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 key,
2614 &slot,
2615 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2616 alg);
2617 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002618 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002621 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 &operation->mac_size);
2623 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626
2627 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002628 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (is_sign) {
2630 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002631 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 slot->key.data,
2633 slot->key.bytes,
2634 alg);
2635 } else {
2636 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002637 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 slot->key.data,
2639 slot->key.bytes,
2640 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002642
Gilles Peskinefbfac682018-07-08 20:51:54 +02002643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (status != PSA_SUCCESS) {
2645 psa_mac_abort(operation);
2646 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002647
Ryan Everettfb9857f2024-02-14 12:16:41 +00002648 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651}
2652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2654 mbedtls_svc_key_id_t key,
2655 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656{
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002658}
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2661 mbedtls_svc_key_id_t key,
2662 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002663{
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002665}
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002668 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2672 LOCAL_INPUT_DECLARE(input_external, input);
2673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002675 status = PSA_ERROR_BAD_STATE;
2676 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002679 /* Don't require hash implementations to behave correctly on a
2680 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002682 status = PSA_SUCCESS;
2683 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002685
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002686 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2687 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (status != PSA_SUCCESS) {
2690 psa_mac_abort(operation);
2691 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002692
David Horstmann4a48bec2024-03-13 15:42:31 +00002693#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002694exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002695#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002696 LOCAL_INPUT_FREE(input_external, input);
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699}
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002702 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 size_t mac_size,
2704 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002705{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2707 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002708 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002709 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002712 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002713 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002714 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002717 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002718 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002719 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002720
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002721 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2722 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002724 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002725 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002726 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002729 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002730 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002731 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002732
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 status = psa_driver_wrapper_mac_sign_finish(operation,
2735 mac, operation->mac_size,
2736 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002737
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002738exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002739 /* In case of success, set the potential excess room in the output buffer
2740 * to an invalid value, to avoid potentially leaking a longer MAC.
2741 * In case of error, set the output length and content to a safe default,
2742 * such that in case the caller misses an error check, the output would be
2743 * an unachievable MAC.
2744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002746 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002747 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002748 }
mohammad16036df908f2018-04-02 08:34:15 -07002749
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002750 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002751 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2752 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002755 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002758}
2759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002761 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002764 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2765 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002766 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002769 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002770 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002771 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002774 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002775 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002776 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002779 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002780 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002781 }
2782
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002783 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_driver_wrapper_mac_verify_finish(operation,
2785 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002786
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002789 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792}
2793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2795 psa_algorithm_t alg,
2796 const uint8_t *input,
2797 size_t input_length,
2798 uint8_t *mac,
2799 size_t mac_size,
2800 size_t *mac_length,
2801 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002802{
2803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002804 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2805 psa_key_slot_t *slot;
2806 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807
Ronald Cron51131b52021-06-17 17:17:20 +02002808 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 key,
2810 &slot,
2811 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2812 alg);
2813 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002816
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002817 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 &operation_mac_size);
2819 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002820 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002824 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002825 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002826 }
2827
2828 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002829 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 slot->key.data, slot->key.bytes,
2831 alg,
2832 input, input_length,
2833 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002834
2835exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002836 /* In case of success, set the potential excess room in the output buffer
2837 * to an invalid value, to avoid potentially leaking a longer MAC.
2838 * In case of error, set the output length and content to a safe default,
2839 * such that in case the caller misses an error check, the output would be
2840 * an unachievable MAC.
2841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002843 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002844 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002845 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002846
2847 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002848
Ryan Everetta103ec92024-01-31 13:59:57 +00002849 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852}
2853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002856 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002857 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002858 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 size_t mac_size,
2860 size_t *mac_length)
2861{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2863 LOCAL_INPUT_DECLARE(input_external, input);
2864 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2865
2866 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2867 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2868 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002869 input, input_length,
2870 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002871
David Horstmann4a48bec2024-03-13 15:42:31 +00002872#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002873exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002874#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002875 LOCAL_INPUT_FREE(input_external, input);
2876 LOCAL_OUTPUT_FREE(mac_external, mac);
2877
2878 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879}
2880
2881psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2882 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002883 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002885 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002887{
2888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002889 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2890 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002891 LOCAL_INPUT_DECLARE(input_external, input);
2892 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002893
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002894 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 status = psa_mac_compute_internal(key, alg,
2896 input, input_length,
2897 actual_mac, sizeof(actual_mac),
2898 &actual_mac_length, 0);
2899 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002904 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002905 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002906 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002907
2908 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01002909 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002910 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002911 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002912 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002913
2914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002916 LOCAL_INPUT_FREE(input_external, input);
2917 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002920}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002921
Gilles Peskinee59236f2018-01-27 23:32:46 +01002922/****************************************************************/
2923/* Asymmetric cryptography */
2924/****************************************************************/
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2927 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002928{
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (input_is_message) {
2930 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2931 return PSA_ERROR_INVALID_ARGUMENT;
2932 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2935 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2936 return PSA_ERROR_INVALID_ARGUMENT;
2937 }
2938 }
2939 } else {
2940 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2941 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002942 }
2943 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002946}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2949 int input_is_message,
2950 psa_algorithm_t alg,
2951 const uint8_t *input,
2952 size_t input_length,
2953 uint8_t *signature,
2954 size_t signature_size,
2955 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002956{
2957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2959 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002960
2961 *signature_length = 0;
2962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 status = psa_sign_verify_check_alg(input_is_message, alg);
2964 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002965 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002967
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002969 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002970 * buffer can in principle be empty since it doesn't actually have
2971 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (signature_size == 0) {
2973 return PSA_ERROR_BUFFER_TOO_SMALL;
2974 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 key, &slot,
2978 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2979 PSA_KEY_USAGE_SIGN_HASH,
2980 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987 status = PSA_ERROR_INVALID_ARGUMENT;
2988 goto exit;
2989 }
2990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002992 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002993 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002994 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 signature, signature_size, signature_length);
2996 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002997
2998 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002999 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003002 }
3003
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003004
3005exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003006 psa_wipe_tag_output_buffer(signature, status, signature_size,
3007 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003008
Ryan Everetta103ec92024-01-31 13:59:57 +00003009 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003012}
3013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3015 int input_is_message,
3016 psa_algorithm_t alg,
3017 const uint8_t *input,
3018 size_t input_length,
3019 const uint8_t *signature,
3020 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003021{
3022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3023 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3024 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 status = psa_sign_verify_check_alg(input_is_message, alg);
3027 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003028 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003030
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003031 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 key, &slot,
3033 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3034 PSA_KEY_USAGE_VERIFY_HASH,
3035 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (status != PSA_SUCCESS) {
3038 return status;
3039 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003042 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003043 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003044 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 signature, signature_length);
3046 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003047 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003048 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003049 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003051 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003052
Ryan Everetta103ec92024-01-31 13:59:57 +00003053 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003056
3057}
3058
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003059psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003060 const psa_key_attributes_t *attributes,
3061 const uint8_t *key_buffer,
3062 size_t key_buffer_size,
3063 psa_algorithm_t alg,
3064 const uint8_t *input,
3065 size_t input_length,
3066 uint8_t *signature,
3067 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003069{
3070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003073 size_t hash_length;
3074 uint8_t hash[PSA_HASH_MAX_SIZE];
3075
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003076 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 PSA_ALG_SIGN_GET_HASH(alg),
3078 input, input_length,
3079 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003082 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003084
3085 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 attributes, key_buffer, key_buffer_size,
3087 alg, hash, hash_length,
3088 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003089 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003092}
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3095 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003096 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003098 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 size_t signature_size,
3100 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3103 LOCAL_INPUT_DECLARE(input_external, input);
3104 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3105
3106 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3107 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3108 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3109 signature_size, signature_length);
3110
David Horstmann4a48bec2024-03-13 15:42:31 +00003111#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003112exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003113#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003114 LOCAL_INPUT_FREE(input_external, input);
3115 LOCAL_OUTPUT_FREE(signature_external, signature);
3116 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003117}
3118
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003119psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003120 const psa_key_attributes_t *attributes,
3121 const uint8_t *key_buffer,
3122 size_t key_buffer_size,
3123 psa_algorithm_t alg,
3124 const uint8_t *input,
3125 size_t input_length,
3126 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003128{
3129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003132 size_t hash_length;
3133 uint8_t hash[PSA_HASH_MAX_SIZE];
3134
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003135 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 PSA_ALG_SIGN_GET_HASH(alg),
3137 input, input_length,
3138 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003141 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003143
3144 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 attributes, key_buffer, key_buffer_size,
3146 alg, hash, hash_length,
3147 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003148 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003151}
3152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3154 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003155 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003157 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003159{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3161 LOCAL_INPUT_DECLARE(input_external, input);
3162 LOCAL_INPUT_DECLARE(signature_external, signature);
3163
3164 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3165 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3166 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3167 signature_length);
3168
David Horstmann4a48bec2024-03-13 15:42:31 +00003169#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003170exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003171#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003172 LOCAL_INPUT_FREE(input_external, input);
3173 LOCAL_INPUT_FREE(signature_external, signature);
3174
3175 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003176}
3177
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003178psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003179 const psa_key_attributes_t *attributes,
3180 const uint8_t *key_buffer, size_t key_buffer_size,
3181 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003183{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003184 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3186 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003187#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3189 return mbedtls_psa_rsa_sign_hash(
3190 attributes,
3191 key_buffer, key_buffer_size,
3192 alg, hash, hash_length,
3193 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003194#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3195 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 } else {
3197 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003198 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003199 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003201#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3203 return mbedtls_psa_ecdsa_sign_hash(
3204 attributes,
3205 key_buffer, key_buffer_size,
3206 alg, hash, hash_length,
3207 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003208#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3209 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 } else {
3211 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003212 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003213 }
Ronald Cron4501c982021-03-19 20:09:32 +01003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 (void) key_buffer;
3216 (void) key_buffer_size;
3217 (void) hash;
3218 (void) hash_length;
3219 (void) signature;
3220 (void) signature_size;
3221 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003224}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3227 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003228 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003230 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 size_t signature_size,
3232 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003233{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3235 LOCAL_INPUT_DECLARE(hash_external, hash);
3236 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3237
3238 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3239 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3240 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3241 signature_size, signature_length);
3242
David Horstmann4a48bec2024-03-13 15:42:31 +00003243#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003244exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003245#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003246 LOCAL_INPUT_FREE(hash_external, hash);
3247 LOCAL_OUTPUT_FREE(signature_external, signature);
3248
3249 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003250}
3251
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003252psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003253 const psa_key_attributes_t *attributes,
3254 const uint8_t *key_buffer, size_t key_buffer_size,
3255 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003257{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003258 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3260 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003261#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3263 return mbedtls_psa_rsa_verify_hash(
3264 attributes,
3265 key_buffer, key_buffer_size,
3266 alg, hash, hash_length,
3267 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3269 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 } else {
3271 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003272 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003273 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003275#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3277 return mbedtls_psa_ecdsa_verify_hash(
3278 attributes,
3279 key_buffer, key_buffer_size,
3280 alg, hash, hash_length,
3281 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003282#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3283 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 } else {
3285 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003286 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003287 }
Ronald Cron4501c982021-03-19 20:09:32 +01003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 (void) key_buffer;
3290 (void) key_buffer_size;
3291 (void) hash;
3292 (void) hash_length;
3293 (void) signature;
3294 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003297}
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3300 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003301 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003303 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003305{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3307 LOCAL_INPUT_DECLARE(hash_external, hash);
3308 LOCAL_INPUT_DECLARE(signature_external, signature);
3309
3310 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3311 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3312 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3313 signature_length);
3314
David Horstmann4a48bec2024-03-13 15:42:31 +00003315#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003316exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003317#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003318 LOCAL_INPUT_FREE(hash_external, hash);
3319 LOCAL_INPUT_FREE(signature_external, signature);
3320
3321 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003322}
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3325 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003326 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003328 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003330 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 size_t output_size,
3332 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003335 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003336 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003337
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003338 LOCAL_INPUT_DECLARE(input_external, input);
3339 LOCAL_INPUT_DECLARE(salt_external, salt);
3340 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341
Darryl Green5cc689a2018-07-24 15:34:10 +01003342 (void) input;
3343 (void) input_length;
3344 (void) salt;
3345 (void) output;
3346 (void) output_size;
3347
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003348 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3351 return PSA_ERROR_INVALID_ARGUMENT;
3352 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003353
Valerio Setti5bb454a2024-01-15 10:43:16 +01003354 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3356 if (status != PSA_SUCCESS) {
3357 return status;
3358 }
3359 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3360 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003361 status = PSA_ERROR_INVALID_ARGUMENT;
3362 goto exit;
3363 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003365 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3366 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3367 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003368
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003369 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003370 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003371 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003373exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003374 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003375
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003376 LOCAL_INPUT_FREE(input_external, input);
3377 LOCAL_INPUT_FREE(salt_external, salt);
3378 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003381}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3384 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003385 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003387 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003389 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 size_t output_size,
3391 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003392{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003393 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003394 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003395 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003396
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003397 LOCAL_INPUT_DECLARE(input_external, input);
3398 LOCAL_INPUT_DECLARE(salt_external, salt);
3399 LOCAL_OUTPUT_DECLARE(output_external, output);
3400
Darryl Green5cc689a2018-07-24 15:34:10 +01003401 (void) input;
3402 (void) input_length;
3403 (void) salt;
3404 (void) output;
3405 (void) output_size;
3406
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003407 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3410 return PSA_ERROR_INVALID_ARGUMENT;
3411 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003412
Valerio Setti5bb454a2024-01-15 10:43:16 +01003413 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3415 if (status != PSA_SUCCESS) {
3416 return status;
3417 }
3418 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003419 status = PSA_ERROR_INVALID_ARGUMENT;
3420 goto exit;
3421 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003422
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003423 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3424 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3425 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003426
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003427 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003428 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003429 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003431
3432exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003433 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003434
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003435 LOCAL_INPUT_FREE(input_external, input);
3436 LOCAL_INPUT_FREE(salt_external, salt);
3437 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003438
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003440}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003441
Paul Elliott9fe12f62022-11-30 19:16:02 +00003442/****************************************************************/
3443/* Asymmetric interruptible cryptography */
3444/****************************************************************/
3445
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003446static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3447
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448void psa_interruptible_set_max_ops(uint32_t max_ops)
3449{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003450 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451}
3452
3453uint32_t psa_interruptible_get_max_ops(void)
3454{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003455 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456}
3457
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458uint32_t psa_sign_hash_get_num_ops(
3459 const psa_sign_hash_interruptible_operation_t *operation)
3460{
Paul Elliott296ede92022-12-15 17:00:30 +00003461 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003462}
3463
3464uint32_t psa_verify_hash_get_num_ops(
3465 const psa_verify_hash_interruptible_operation_t *operation)
3466{
Paul Elliott296ede92022-12-15 17:00:30 +00003467 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468}
3469
Paul Elliott59ad9452022-12-18 15:09:02 +00003470static psa_status_t psa_sign_hash_abort_internal(
3471 psa_sign_hash_interruptible_operation_t *operation)
3472{
3473 if (operation->id == 0) {
3474 /* The object has (apparently) been initialized but it is not (yet)
3475 * in use. It's ok to call abort on such an object, and there's
3476 * nothing to do. */
3477 return PSA_SUCCESS;
3478 }
3479
3480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3481
3482 status = psa_driver_wrapper_sign_hash_abort(operation);
3483
3484 operation->id = 0;
3485
Paul Elliotte6145dc2023-02-07 12:51:21 +00003486 /* Do not clear either the error_occurred or num_ops elements here as they
3487 * only want to be cleared by the application calling abort, not by abort
3488 * being called at completion of an operation. */
3489
Paul Elliott59ad9452022-12-18 15:09:02 +00003490 return status;
3491}
3492
Paul Elliott9fe12f62022-11-30 19:16:02 +00003493psa_status_t psa_sign_hash_start(
3494 psa_sign_hash_interruptible_operation_t *operation,
3495 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003496 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003497{
3498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3499 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3500 psa_key_slot_t *slot;
3501
David Horstmann4a523a62024-03-11 13:32:16 +00003502 LOCAL_INPUT_DECLARE(hash_external, hash);
3503
Paul Elliottc9774412023-02-06 15:14:07 +00003504 /* Check that start has not been previously called, or operation has not
3505 * previously errored. */
3506 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507 return PSA_ERROR_BAD_STATE;
3508 }
3509
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510 status = psa_sign_verify_check_alg(0, alg);
3511 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003512 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003513 return status;
3514 }
3515
3516 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3517 PSA_KEY_USAGE_SIGN_HASH,
3518 alg);
3519
3520 if (status != PSA_SUCCESS) {
3521 goto exit;
3522 }
3523
3524 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3525 status = PSA_ERROR_INVALID_ARGUMENT;
3526 goto exit;
3527 }
3528
David Horstmann4a523a62024-03-11 13:32:16 +00003529 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3530
Paul Elliott296ede92022-12-15 17:00:30 +00003531 /* Ensure ops count gets reset, in case of operation re-use. */
3532 operation->num_ops = 0;
3533
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003534 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003535 slot->key.data,
3536 slot->key.bytes, alg,
3537 hash, hash_length);
3538exit:
3539
3540 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003541 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003542 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003543 }
3544
Ryan Everettdcc03d52024-02-14 15:44:13 +00003545 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546
Paul Elliottc9774412023-02-06 15:14:07 +00003547 if (unlock_status != PSA_SUCCESS) {
3548 operation->error_occurred = 1;
3549 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003550
David Horstmann4a523a62024-03-11 13:32:16 +00003551 LOCAL_INPUT_FREE(hash_external, hash);
3552
Paul Elliottc9774412023-02-06 15:14:07 +00003553 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554}
3555
3556
3557psa_status_t psa_sign_hash_complete(
3558 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003559 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560 size_t *signature_length)
3561{
3562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3563
David Horstmann4a523a62024-03-11 13:32:16 +00003564 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3565
Paul Elliott9fe12f62022-11-30 19:16:02 +00003566 *signature_length = 0;
3567
Paul Elliottc9774412023-02-06 15:14:07 +00003568 /* Check that start has been called first, and that operation has not
3569 * previously errored. */
3570 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003571 status = PSA_ERROR_BAD_STATE;
3572 goto exit;
3573 }
3574
Paul Elliott096abc42023-02-03 18:33:23 +00003575 /* Immediately reject a zero-length signature buffer. This guarantees that
3576 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003577 if (signature_size == 0) {
3578 status = PSA_ERROR_BUFFER_TOO_SMALL;
3579 goto exit;
3580 }
3581
David Horstmann4a523a62024-03-11 13:32:16 +00003582 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3583
Paul Elliott9fe12f62022-11-30 19:16:02 +00003584 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3585 signature_size,
3586 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587
Paul Elliott296ede92022-12-15 17:00:30 +00003588 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003589 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003590
Paul Elliottebe225c2023-02-10 14:32:53 +00003591exit:
3592
David Horstmannc5064c82024-03-11 17:02:03 +00003593 if (signature != NULL) {
3594 psa_wipe_tag_output_buffer(signature, status, signature_size,
3595 *signature_length);
3596 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003597
Paul Elliott53bb3122023-02-10 14:22:22 +00003598 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003599 if (status != PSA_SUCCESS) {
3600 operation->error_occurred = 1;
3601 }
3602
Paul Elliott59ad9452022-12-18 15:09:02 +00003603 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003604 }
3605
David Horstmann4a523a62024-03-11 13:32:16 +00003606 LOCAL_OUTPUT_FREE(signature_external, signature);
3607
Paul Elliott9fe12f62022-11-30 19:16:02 +00003608 return status;
3609}
3610
3611psa_status_t psa_sign_hash_abort(
3612 psa_sign_hash_interruptible_operation_t *operation)
3613{
Paul Elliott59ad9452022-12-18 15:09:02 +00003614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3615
3616 status = psa_sign_hash_abort_internal(operation);
3617
3618 /* We clear the number of ops done here, so that it is not cleared when
3619 * the operation fails or succeeds, only on manual abort. */
3620 operation->num_ops = 0;
3621
Paul Elliottc9774412023-02-06 15:14:07 +00003622 /* Likewise, failure state. */
3623 operation->error_occurred = 0;
3624
Paul Elliott59ad9452022-12-18 15:09:02 +00003625 return status;
3626}
3627
3628static psa_status_t psa_verify_hash_abort_internal(
3629 psa_verify_hash_interruptible_operation_t *operation)
3630{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631 if (operation->id == 0) {
3632 /* The object has (apparently) been initialized but it is not (yet)
3633 * in use. It's ok to call abort on such an object, and there's
3634 * nothing to do. */
3635 return PSA_SUCCESS;
3636 }
3637
Paul Elliott59ad9452022-12-18 15:09:02 +00003638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3639
3640 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003641
3642 operation->id = 0;
3643
Paul Elliotte6145dc2023-02-07 12:51:21 +00003644 /* Do not clear either the error_occurred or num_ops elements here as they
3645 * only want to be cleared by the application calling abort, not by abort
3646 * being called at completion of an operation. */
3647
Paul Elliott59ad9452022-12-18 15:09:02 +00003648 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003649}
3650
3651psa_status_t psa_verify_hash_start(
3652 psa_verify_hash_interruptible_operation_t *operation,
3653 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003654 const uint8_t *hash_external, size_t hash_length,
3655 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003656{
3657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3658 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3659 psa_key_slot_t *slot;
3660
David Horstmann0fea6a52024-03-11 13:41:05 +00003661 LOCAL_INPUT_DECLARE(hash_external, hash);
3662 LOCAL_INPUT_DECLARE(signature_external, signature);
3663
Paul Elliottc9774412023-02-06 15:14:07 +00003664 /* Check that start has not been previously called, or operation has not
3665 * previously errored. */
3666 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003667 return PSA_ERROR_BAD_STATE;
3668 }
3669
3670 status = psa_sign_verify_check_alg(0, alg);
3671 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003672 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003673 return status;
3674 }
3675
3676 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3677 PSA_KEY_USAGE_VERIFY_HASH,
3678 alg);
3679
3680 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003681 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003682 return status;
3683 }
3684
David Horstmann0fea6a52024-03-11 13:41:05 +00003685 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3686 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3687
Paul Elliott296ede92022-12-15 17:00:30 +00003688 /* Ensure ops count gets reset, in case of operation re-use. */
3689 operation->num_ops = 0;
3690
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003691 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003692 slot->key.data,
3693 slot->key.bytes,
3694 alg, hash, hash_length,
3695 signature, signature_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00003696#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann0fea6a52024-03-11 13:41:05 +00003697exit:
3698#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003699
3700 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003701 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003702 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003703 }
3704
Ryan Everett291267f2024-02-14 15:59:15 +00003705 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003706
Paul Elliottc9774412023-02-06 15:14:07 +00003707 if (unlock_status != PSA_SUCCESS) {
3708 operation->error_occurred = 1;
3709 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003710
David Horstmann0fea6a52024-03-11 13:41:05 +00003711 LOCAL_INPUT_FREE(hash_external, hash);
3712 LOCAL_INPUT_FREE(signature_external, signature);
3713
Paul Elliottc9774412023-02-06 15:14:07 +00003714 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003715}
3716
3717psa_status_t psa_verify_hash_complete(
3718 psa_verify_hash_interruptible_operation_t *operation)
3719{
3720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3721
Paul Elliottc9774412023-02-06 15:14:07 +00003722 /* Check that start has been called first, and that operation has not
3723 * previously errored. */
3724 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003725 status = PSA_ERROR_BAD_STATE;
3726 goto exit;
3727 }
3728
3729 status = psa_driver_wrapper_verify_hash_complete(operation);
3730
Paul Elliott296ede92022-12-15 17:00:30 +00003731 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003732 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003733 operation);
3734
Paul Elliottebe225c2023-02-10 14:32:53 +00003735exit:
3736
Paul Elliott9fe12f62022-11-30 19:16:02 +00003737 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003738 if (status != PSA_SUCCESS) {
3739 operation->error_occurred = 1;
3740 }
3741
Paul Elliott59ad9452022-12-18 15:09:02 +00003742 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003743 }
3744
3745 return status;
3746}
3747
3748psa_status_t psa_verify_hash_abort(
3749 psa_verify_hash_interruptible_operation_t *operation)
3750{
Paul Elliott59ad9452022-12-18 15:09:02 +00003751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003752
Paul Elliott59ad9452022-12-18 15:09:02 +00003753 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003754
Paul Elliott59ad9452022-12-18 15:09:02 +00003755 /* We clear the number of ops done here, so that it is not cleared when
3756 * the operation fails or succeeds, only on manual abort. */
3757 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003758
Paul Elliottc9774412023-02-06 15:14:07 +00003759 /* Likewise, failure state. */
3760 operation->error_occurred = 0;
3761
Paul Elliott59ad9452022-12-18 15:09:02 +00003762 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003763}
3764
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765/****************************************************************/
3766/* Asymmetric interruptible cryptography internal */
3767/* implementations */
3768/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003769
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3771{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003772
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3774 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3775 defined(MBEDTLS_ECP_RESTARTABLE)
3776
3777 /* Internal implementation uses zero to indicate infinite number max ops,
3778 * therefore avoid this value, and set to minimum possible. */
3779 if (max_ops == 0) {
3780 max_ops = 1;
3781 }
3782
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003784#else
3785 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3787 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3788 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3789}
3790
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003792 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793{
3794#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3795 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3796 defined(MBEDTLS_ECP_RESTARTABLE)
3797
Paul Elliott93d9ca82023-02-15 18:14:21 +00003798 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#else
3800 (void) operation;
3801 return 0;
3802#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3803 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3804 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3805}
3806
3807uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003808 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809{
3810 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3811 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3812 defined(MBEDTLS_ECP_RESTARTABLE)
3813
Paul Elliott93d9ca82023-02-15 18:14:21 +00003814 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815#else
3816 (void) operation;
3817 return 0;
3818#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3819 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3820 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3821}
3822
3823psa_status_t mbedtls_psa_sign_hash_start(
3824 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3825 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3826 size_t key_buffer_size, psa_algorithm_t alg,
3827 const uint8_t *hash, size_t hash_length)
3828{
3829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003830 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003832 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003833 return PSA_ERROR_NOT_SUPPORTED;
3834 }
3835
3836 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003837 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003838 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
3840#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003841 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3842 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843
Paul Elliotta1c94092023-02-15 16:38:04 +00003844 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3845
Paul Elliott93d9ca82023-02-15 18:14:21 +00003846 /* Ensure num_ops is zero'ed in case of context re-use. */
3847 operation->num_ops = 0;
3848
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003849 status = mbedtls_psa_ecp_load_representation(attributes->type,
3850 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003851 key_buffer,
3852 key_buffer_size,
3853 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott068fe072023-01-16 13:59:15 +00003855 if (status != PSA_SUCCESS) {
3856 return status;
3857 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858
Paul Elliott1bc59df2023-02-05 13:41:57 +00003859 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003860 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861
Paul Elliott068fe072023-01-16 13:59:15 +00003862 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003863 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003864 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865
Paul Elliott0290a762023-02-09 14:30:24 +00003866 /* We only need to store the same length of hash as the private key size
3867 * here, it would be truncated by the internal implementation anyway. */
3868 required_hash_length = (hash_length < operation->coordinate_bytes ?
3869 hash_length : operation->coordinate_bytes);
3870
Paul Elliottba70ad42023-02-15 18:23:53 +00003871 if (required_hash_length > sizeof(operation->hash)) {
3872 /* Shouldn't happen, but better safe than sorry. */
3873 return PSA_ERROR_CORRUPTION_DETECTED;
3874 }
3875
Paul Elliott0290a762023-02-09 14:30:24 +00003876 memcpy(operation->hash, hash, required_hash_length);
3877 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003878
3879 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003880
3881#else
Paul Elliott068fe072023-01-16 13:59:15 +00003882 (void) operation;
3883 (void) key_buffer;
3884 (void) key_buffer_size;
3885 (void) alg;
3886 (void) hash;
3887 (void) hash_length;
3888 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003889 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890
Paul Elliott068fe072023-01-16 13:59:15 +00003891 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3893 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3894 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003895}
3896
3897psa_status_t mbedtls_psa_sign_hash_complete(
3898 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3899 uint8_t *signature, size_t signature_size,
3900 size_t *signature_length)
3901{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3903 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3904 defined(MBEDTLS_ECP_RESTARTABLE)
3905
Paul Elliotta1c94092023-02-15 16:38:04 +00003906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003907 mbedtls_mpi r;
3908 mbedtls_mpi s;
3909
Paul Elliott46845252023-02-03 14:59:11 +00003910 mbedtls_mpi_init(&r);
3911 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003913 /* Ensure max_ops is set to the current value (or default). */
3914 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3915
Paul Elliotta1c94092023-02-15 16:38:04 +00003916 if (signature_size < 2 * operation->coordinate_bytes) {
3917 status = PSA_ERROR_BUFFER_TOO_SMALL;
3918 goto exit;
3919 }
3920
Paul Elliott588f8ed2022-12-02 18:10:26 +00003921 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003922
Paul Elliott588f8ed2022-12-02 18:10:26 +00003923#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3924 status = mbedtls_to_psa_error(
3925 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003926 &r,
3927 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928 &operation->ctx->d,
3929 operation->hash,
3930 operation->hash_length,
3931 operation->md_alg,
3932 mbedtls_psa_get_random,
3933 MBEDTLS_PSA_RANDOM_STATE,
3934 &operation->restart_ctx));
3935#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003936 status = PSA_ERROR_NOT_SUPPORTED;
3937 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3939 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 status = mbedtls_to_psa_error(
3941 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003942 &r,
3943 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 &operation->ctx->d,
3945 operation->hash,
3946 operation->hash_length,
3947 mbedtls_psa_get_random,
3948 MBEDTLS_PSA_RANDOM_STATE,
3949 mbedtls_psa_get_random,
3950 MBEDTLS_PSA_RANDOM_STATE,
3951 &operation->restart_ctx));
3952 }
3953
Paul Elliottf8e5b562023-02-19 18:43:45 +00003954 /* Hide the fact that the restart context only holds a delta of number of
3955 * ops done during the last operation, not an absolute value. */
3956 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3957
Paul Elliott724bd252023-02-08 12:35:08 +00003958 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003960 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003962 operation->coordinate_bytes)
3963 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
3965 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003966 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967 }
3968
3969 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003970 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003972 operation->coordinate_bytes,
3973 operation->coordinate_bytes)
3974 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003975
3976 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003977 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978 }
3979
Paul Elliott1bc59df2023-02-05 13:41:57 +00003980 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003981
Paul Elliott724bd252023-02-08 12:35:08 +00003982 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983 }
Paul Elliott724bd252023-02-08 12:35:08 +00003984
3985exit:
3986
3987 mbedtls_mpi_free(&r);
3988 mbedtls_mpi_free(&s);
3989 return status;
3990
Paul Elliott588f8ed2022-12-02 18:10:26 +00003991 #else
3992
3993 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003994 (void) signature;
3995 (void) signature_size;
3996 (void) signature_length;
3997
3998 return PSA_ERROR_NOT_SUPPORTED;
3999
4000#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4001 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4002 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4003}
4004
4005psa_status_t mbedtls_psa_sign_hash_abort(
4006 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
4007{
4008
4009#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4010 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4011 defined(MBEDTLS_ECP_RESTARTABLE)
4012
4013 if (operation->ctx) {
4014 mbedtls_ecdsa_free(operation->ctx);
4015 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004016 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004017 }
4018
4019 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4020
Paul Elliott93d9ca82023-02-15 18:14:21 +00004021 operation->num_ops = 0;
4022
Paul Elliott588f8ed2022-12-02 18:10:26 +00004023 return PSA_SUCCESS;
4024
4025#else
4026
4027 (void) operation;
4028
4029 return PSA_ERROR_NOT_SUPPORTED;
4030
4031#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4032 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4033 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4034}
4035
4036psa_status_t mbedtls_psa_verify_hash_start(
4037 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
4038 const psa_key_attributes_t *attributes,
4039 const uint8_t *key_buffer, size_t key_buffer_size,
4040 psa_algorithm_t alg,
4041 const uint8_t *hash, size_t hash_length,
4042 const uint8_t *signature, size_t signature_length)
4043{
4044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00004045 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00004046 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004047
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004048 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00004049 return PSA_ERROR_NOT_SUPPORTED;
4050 }
4051
4052 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004053 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004054 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055
4056#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004057 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4058 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004059
Paul Elliotta1c94092023-02-15 16:38:04 +00004060 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4061 mbedtls_mpi_init(&operation->r);
4062 mbedtls_mpi_init(&operation->s);
4063
Paul Elliott93d9ca82023-02-15 18:14:21 +00004064 /* Ensure num_ops is zero'ed in case of context re-use. */
4065 operation->num_ops = 0;
4066
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004067 status = mbedtls_psa_ecp_load_representation(attributes->type,
4068 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004069 key_buffer,
4070 key_buffer_size,
4071 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004072
Paul Elliott068fe072023-01-16 13:59:15 +00004073 if (status != PSA_SUCCESS) {
4074 return status;
4075 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004076
Paul Elliottc569fc22023-02-10 13:02:54 +00004077 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004078
Paul Elliott1bc59df2023-02-05 13:41:57 +00004079 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004080 return PSA_ERROR_INVALID_SIGNATURE;
4081 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004082
Paul Elliott068fe072023-01-16 13:59:15 +00004083 status = mbedtls_to_psa_error(
4084 mbedtls_mpi_read_binary(&operation->r,
4085 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004086 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004087
Paul Elliott068fe072023-01-16 13:59:15 +00004088 if (status != PSA_SUCCESS) {
4089 return status;
4090 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004091
Paul Elliott068fe072023-01-16 13:59:15 +00004092 status = mbedtls_to_psa_error(
4093 mbedtls_mpi_read_binary(&operation->s,
4094 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004095 coordinate_bytes,
4096 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004097
Paul Elliott068fe072023-01-16 13:59:15 +00004098 if (status != PSA_SUCCESS) {
4099 return status;
4100 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004101
Paul Elliott2c9843f2023-02-15 17:32:42 +00004102 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004103
Paul Elliott2c9843f2023-02-15 17:32:42 +00004104 if (status != PSA_SUCCESS) {
4105 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004106 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004107
Paul Elliott0290a762023-02-09 14:30:24 +00004108 /* We only need to store the same length of hash as the private key size
4109 * here, it would be truncated by the internal implementation anyway. */
4110 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4111 coordinate_bytes);
4112
Paul Elliottba70ad42023-02-15 18:23:53 +00004113 if (required_hash_length > sizeof(operation->hash)) {
4114 /* Shouldn't happen, but better safe than sorry. */
4115 return PSA_ERROR_CORRUPTION_DETECTED;
4116 }
4117
Paul Elliott0290a762023-02-09 14:30:24 +00004118 memcpy(operation->hash, hash, required_hash_length);
4119 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004120
4121 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004122#else
Paul Elliott068fe072023-01-16 13:59:15 +00004123 (void) operation;
4124 (void) key_buffer;
4125 (void) key_buffer_size;
4126 (void) alg;
4127 (void) hash;
4128 (void) hash_length;
4129 (void) signature;
4130 (void) signature_length;
4131 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004132 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004133 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004134
Paul Elliott068fe072023-01-16 13:59:15 +00004135 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4137 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4138 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004139}
4140
4141psa_status_t mbedtls_psa_verify_hash_complete(
4142 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4143{
4144
4145#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4146 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4147 defined(MBEDTLS_ECP_RESTARTABLE)
4148
Paul Elliottf8e5b562023-02-19 18:43:45 +00004149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4150
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004151 /* Ensure max_ops is set to the current value (or default). */
4152 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4153
Paul Elliottf8e5b562023-02-19 18:43:45 +00004154 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004155 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4156 operation->hash,
4157 operation->hash_length,
4158 &operation->ctx->Q,
4159 &operation->r,
4160 &operation->s,
4161 &operation->restart_ctx));
4162
Paul Elliottf8e5b562023-02-19 18:43:45 +00004163 /* Hide the fact that the restart context only holds a delta of number of
4164 * ops done during the last operation, not an absolute value. */
4165 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4166
4167 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004168#else
4169 (void) operation;
4170
4171 return PSA_ERROR_NOT_SUPPORTED;
4172
4173#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4174 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4175 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4176}
4177
4178psa_status_t mbedtls_psa_verify_hash_abort(
4179 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4180{
4181
4182#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4183 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4184 defined(MBEDTLS_ECP_RESTARTABLE)
4185
4186 if (operation->ctx) {
4187 mbedtls_ecdsa_free(operation->ctx);
4188 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004189 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004190 }
4191
4192 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4193
Paul Elliott93d9ca82023-02-15 18:14:21 +00004194 operation->num_ops = 0;
4195
Paul Elliott588f8ed2022-12-02 18:10:26 +00004196 mbedtls_mpi_free(&operation->r);
4197 mbedtls_mpi_free(&operation->s);
4198
4199 return PSA_SUCCESS;
4200
4201#else
4202 (void) operation;
4203
4204 return PSA_ERROR_NOT_SUPPORTED;
4205
4206#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4207 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4208 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4209}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004210
David Horstmann6e99bb22024-02-06 15:27:49 +00004211static psa_status_t psa_generate_random_internal(uint8_t *output,
4212 size_t output_size)
4213{
4214 GUARD_MODULE_INITIALIZED;
4215
David Horstmann6e99bb22024-02-06 15:27:49 +00004216#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4217
David Horstmanndb909142024-03-12 16:07:08 +00004218 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004219 size_t output_length = 0;
4220 status = mbedtls_psa_external_get_random(&global_data.rng,
4221 output, output_size,
4222 &output_length);
4223 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004224 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004225 }
4226 /* Breaking up a request into smaller chunks is currently not supported
4227 * for the external RNG interface. */
4228 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004229 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004230 }
David Horstmanndb909142024-03-12 16:07:08 +00004231 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004232
4233#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4234
4235 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004236 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004237 size_t request_size =
4238 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4239 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4240 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004241#if defined(MBEDTLS_CTR_DRBG_C)
4242 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4243#elif defined(MBEDTLS_HMAC_DRBG_C)
4244 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4245#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004246 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004247 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004248 }
4249 output_size -= request_size;
4250 output += request_size;
4251 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004252 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004253#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004254}
4255
4256
mohammad1603503973b2018-03-12 15:59:30 +02004257/****************************************************************/
4258/* Symmetric cryptography */
4259/****************************************************************/
4260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4262 mbedtls_svc_key_id_t key,
4263 psa_algorithm_t alg,
4264 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004265{
4266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4267 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004268 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4270 PSA_KEY_USAGE_ENCRYPT :
4271 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004272
4273 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004275 status = PSA_ERROR_BAD_STATE;
4276 goto exit;
4277 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004280 status = PSA_ERROR_INVALID_ARGUMENT;
4281 goto exit;
4282 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4285 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004288
Ronald Cron49fafa92021-03-10 08:34:23 +01004289 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004290 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004291 * so we only set it (in the driver wrapper) after resources have been
4292 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004293 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004295 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004297 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 }
4299 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004300
4301 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (cipher_operation == MBEDTLS_ENCRYPT) {
4303 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004304 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 slot->key.data,
4306 slot->key.bytes,
4307 alg);
4308 } else {
4309 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004310 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 slot->key.data,
4312 slot->key.bytes,
4313 alg);
4314 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004315
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (status != PSA_SUCCESS) {
4318 psa_cipher_abort(operation);
4319 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004320
Ryan Everettc0053cc2024-02-14 16:27:13 +00004321 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004324}
4325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4327 mbedtls_svc_key_id_t key,
4328 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004329{
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004331}
4332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4334 mbedtls_svc_key_id_t key,
4335 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004336{
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004338}
4339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004341 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 size_t iv_size,
4343 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004344{
Ronald Cron6d051732020-10-01 14:10:20 +02004345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004346 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004347
Gabor Mezei358eb212024-02-07 18:10:13 +01004348 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004351 status = PSA_ERROR_BAD_STATE;
4352 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004353 }
4354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004356 status = PSA_ERROR_BAD_STATE;
4357 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004358 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004359
Ronald Cron2fb90522021-07-05 12:31:44 +02004360 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004362 status = PSA_ERROR_BUFFER_TOO_SMALL;
4363 goto exit;
4364 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004367 status = PSA_ERROR_GENERIC_ERROR;
4368 goto exit;
4369 }
4370
Gabor Mezei358eb212024-02-07 18:10:13 +01004371 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4372
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004373 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004375 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 }
Ronald Cron5618a392021-03-26 09:52:26 +01004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004379 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004380
4381exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004383 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004384 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004386 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004388 if (iv != NULL) {
4389 mbedtls_platform_zeroize(iv, default_iv_length);
4390 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004391 }
Ronald Cron6d051732020-10-01 14:10:20 +02004392
Gabor Mezei358eb212024-02-07 18:10:13 +01004393 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004395}
4396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004398 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004400{
Ronald Cron6d051732020-10-01 14:10:20 +02004401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004402
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004403 LOCAL_INPUT_DECLARE(iv_external, iv);
4404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004406 status = PSA_ERROR_BAD_STATE;
4407 goto exit;
4408 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004411 status = PSA_ERROR_BAD_STATE;
4412 goto exit;
4413 }
Ronald Crona0d68172021-03-26 10:15:08 +01004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004416 status = PSA_ERROR_INVALID_ARGUMENT;
4417 goto exit;
4418 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004419
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004420 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 status = psa_driver_wrapper_cipher_set_iv(operation,
4423 iv,
4424 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004425
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004428 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 } else {
4430 psa_cipher_abort(operation);
4431 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004432
4433 LOCAL_INPUT_FREE(iv_external, iv);
4434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004436}
4437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004439 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004441 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 size_t output_size,
4443 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004444{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004446
Gabor Mezei212eb082024-01-24 16:56:00 +01004447 LOCAL_INPUT_DECLARE(input_external, input);
4448 LOCAL_OUTPUT_DECLARE(output_external, output);
4449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004451 status = PSA_ERROR_BAD_STATE;
4452 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004453 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004456 status = PSA_ERROR_BAD_STATE;
4457 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004458 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004459
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004460 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004461 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 status = psa_driver_wrapper_cipher_update(operation,
4464 input,
4465 input_length,
4466 output,
4467 output_size,
4468 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004469
4470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (status != PSA_SUCCESS) {
4472 psa_cipher_abort(operation);
4473 }
Ronald Cron6d051732020-10-01 14:10:20 +02004474
Gabor Mezei212eb082024-01-24 16:56:00 +01004475 LOCAL_INPUT_FREE(input_external, input);
4476 LOCAL_OUTPUT_FREE(output_external, output);
4477
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004479}
4480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004482 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 size_t output_size,
4484 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004485{
David Saadab4ecc272019-02-14 13:48:10 +02004486 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004487
Gabor Mezei212eb082024-01-24 16:56:00 +01004488 LOCAL_OUTPUT_DECLARE(output_external, output);
4489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004491 status = PSA_ERROR_BAD_STATE;
4492 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004493 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004494
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004496 status = PSA_ERROR_BAD_STATE;
4497 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004498 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004499
Gabor Mezei0b041162024-02-29 10:08:16 +00004500 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 status = psa_driver_wrapper_cipher_finish(operation,
4503 output,
4504 output_size,
4505 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004506
4507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 if (status == PSA_SUCCESS) {
Gabor Mezei212eb082024-01-24 16:56:00 +01004509 status = psa_cipher_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004511 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02004513 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004514
4515 LOCAL_OUTPUT_FREE(output_external, output);
4516
4517 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004518}
4519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004521{
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004523 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004524 * in use. It's ok to call abort on such an object, and there's
4525 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004527 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004530
Ronald Cron49fafa92021-03-10 08:34:23 +01004531 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004532 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004533 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004536}
4537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4539 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004540 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004542 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 size_t output_size,
4544 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004545{
4546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004547 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004548 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004549 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4550 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004551
David Horstmann62a56d92023-12-14 18:12:47 +00004552 LOCAL_INPUT_DECLARE(input_external, input);
4553 LOCAL_OUTPUT_DECLARE(output_external, output);
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004556 status = PSA_ERROR_INVALID_ARGUMENT;
4557 goto exit;
4558 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4561 PSA_KEY_USAGE_ENCRYPT,
4562 alg);
4563 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4568 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004569 status = PSA_ERROR_GENERIC_ERROR;
4570 goto exit;
4571 }
4572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (default_iv_length > 0) {
4574 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004575 status = PSA_ERROR_BUFFER_TOO_SMALL;
4576 goto exit;
4577 }
4578
David Horstmann6e99bb22024-02-06 15:27:49 +00004579 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004581 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004583 }
4584
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004585 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4586 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4587
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004588 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004589 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004590 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004591 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004593
4594exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004595 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004597 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004598 }
Ronald Cron23919522021-07-08 19:00:07 +02004599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (status == PSA_SUCCESS) {
4601 if (default_iv_length > 0) {
4602 memcpy(output, local_iv, default_iv_length);
4603 }
4604 *output_length += default_iv_length;
4605 } else {
4606 *output_length = 0;
4607 }
4608
David Horstmann62a56d92023-12-14 18:12:47 +00004609 LOCAL_INPUT_FREE(input_external, input);
4610 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004613}
4614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4616 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004617 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004619 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 size_t output_size,
4621 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004622{
4623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004624 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004625 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004626
Gabor Mezei212eb082024-01-24 16:56:00 +01004627 LOCAL_INPUT_DECLARE(input_external, input);
4628 LOCAL_OUTPUT_DECLARE(output_external, output);
4629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004631 status = PSA_ERROR_INVALID_ARGUMENT;
4632 goto exit;
4633 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4636 PSA_KEY_USAGE_DECRYPT,
4637 alg);
4638 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004641
Gilles Peskineb47c3b32024-06-26 13:16:33 +02004642 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004643 status = PSA_ERROR_INVALID_ARGUMENT;
4644 goto exit;
4645 }
4646
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004647 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4648 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4649
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004650 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004651 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004652 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004654
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004655exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004656 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004658 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004662 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 }
Ronald Cron23919522021-07-08 19:00:07 +02004664
Gabor Mezei212eb082024-01-24 16:56:00 +01004665 LOCAL_INPUT_FREE(input_external, input);
4666 LOCAL_OUTPUT_FREE(output_external, output);
4667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004669}
4670
4671
mohammad16035955c982018-04-26 00:53:03 +03004672/****************************************************************/
4673/* AEAD */
4674/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004675
Paul Elliottbaff51c2021-09-28 17:44:45 +01004676/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004678{
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004680}
4681
4682/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4684 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004685{
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004689#if defined(PSA_WANT_ALG_GCM)
4690 case PSA_ALG_GCM:
4691 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 * arbitrarily large nonces. Please note that we do not generally
4693 * recommend the usage of nonces of greater length than
4694 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4695 * size, which can then lead to collisions if you encrypt a very
4696 * large number of messages.*/
4697 if (nonce_length != 0) {
4698 return PSA_SUCCESS;
4699 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004700 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004701#endif /* PSA_WANT_ALG_GCM */
4702#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004703 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (nonce_length >= 7 && nonce_length <= 13) {
4705 return PSA_SUCCESS;
4706 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004707 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004708#endif /* PSA_WANT_ALG_CCM */
4709#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004710 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (nonce_length == 12) {
4712 return PSA_SUCCESS;
4713 } else if (nonce_length == 8) {
4714 return PSA_ERROR_NOT_SUPPORTED;
4715 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004716 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004717#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004718 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004719 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004721 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004724}
4725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004727{
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4729 return PSA_ERROR_INVALID_ARGUMENT;
4730 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004733}
4734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4736 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004737 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004739 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004741 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004743 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 size_t ciphertext_size,
4745 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004746{
Ronald Cron215633c2021-03-16 17:15:37 +01004747 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4748 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004749
David Horstmann9d09a022023-11-28 19:25:00 +00004750 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4751 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4752 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4753 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4754
mohammad1603f08a5502018-06-03 15:05:47 +03004755 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 status = psa_aead_check_algorithm(alg);
4758 if (status != PSA_SUCCESS) {
4759 return status;
4760 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004761
Ronald Cron9a986162021-03-26 12:40:07 +01004762 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4764 if (status != PSA_SUCCESS) {
4765 return status;
4766 }
mohammad16035955c982018-04-26 00:53:03 +03004767
David Horstmann9d09a022023-11-28 19:25:00 +00004768 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4769 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4770 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4771 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 status = psa_aead_check_nonce_length(alg, nonce_length);
4774 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004777
Ronald Cronde822812021-03-17 16:08:20 +01004778 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004779 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004780 alg,
4781 nonce, nonce_length,
4782 additional_data, additional_data_length,
4783 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4787 memset(ciphertext, 0, ciphertext_size);
4788 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004789
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004790exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004791 LOCAL_INPUT_FREE(nonce_external, nonce);
4792 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4793 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4794 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4795
Ryan Everetta103ec92024-01-31 13:59:57 +00004796 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004799}
4800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4802 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00004803 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004805 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004807 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004809 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 size_t plaintext_size,
4811 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004812{
Ronald Cron215633c2021-03-16 17:15:37 +01004813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4814 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004815
David Horstmann7f2e0402023-11-28 19:43:53 +00004816 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4817 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4818 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4819 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4820
Gilles Peskineee652a32018-06-01 19:23:52 +02004821 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 status = psa_aead_check_algorithm(alg);
4824 if (status != PSA_SUCCESS) {
4825 return status;
4826 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004827
Ronald Cron9a986162021-03-26 12:40:07 +01004828 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4830 if (status != PSA_SUCCESS) {
4831 return status;
4832 }
mohammad16035955c982018-04-26 00:53:03 +03004833
David Horstmann7f2e0402023-11-28 19:43:53 +00004834 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4835 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4836 additional_data);
4837 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4838 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 status = psa_aead_check_nonce_length(alg, nonce_length);
4841 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004844
Ronald Cronde822812021-03-17 16:08:20 +01004845 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004846 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004847 alg,
4848 nonce, nonce_length,
4849 additional_data, additional_data_length,
4850 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (status != PSA_SUCCESS && plaintext_size != 0) {
4854 memset(plaintext, 0, plaintext_size);
4855 }
mohammad160360a64d02018-06-03 17:20:42 +03004856
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004857exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00004858 LOCAL_INPUT_FREE(nonce_external, nonce);
4859 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4860 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4861 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4862
Ryan Everetta103ec92024-01-31 13:59:57 +00004863 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 return status;
mohammad16035955c982018-04-26 00:53:03 +03004866}
4867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4869{
4870 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004873#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004875 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4877 return PSA_ERROR_INVALID_ARGUMENT;
4878 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004879 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004880#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004881
Przemek Stekiel86679c72022-10-06 17:06:56 +02004882#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004884 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4886 return PSA_ERROR_INVALID_ARGUMENT;
4887 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004888 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004889#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004890
Przemek Stekiel86679c72022-10-06 17:06:56 +02004891#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004893 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if (tag_len != 16) {
4895 return PSA_ERROR_INVALID_ARGUMENT;
4896 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004897 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004898#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004899
4900 default:
4901 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004903 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004905}
4906
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004907/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4909 int is_encrypt,
4910 mbedtls_svc_key_id_t key,
4911 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004912{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4914 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4915 psa_key_slot_t *slot = NULL;
4916 psa_key_usage_t key_usage = 0;
4917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 status = psa_aead_check_algorithm(alg);
4919 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004920 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004924 status = PSA_ERROR_BAD_STATE;
4925 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004926 }
4927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (operation->nonce_set || operation->lengths_set ||
4929 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004930 status = PSA_ERROR_BAD_STATE;
4931 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004932 }
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004935 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004937 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4941 alg);
4942 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004943 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (is_encrypt) {
4951 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004952 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 slot->key.data,
4954 slot->key.bytes,
4955 alg);
4956 } else {
4957 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004958 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 slot->key.data,
4960 slot->key.bytes,
4961 alg);
4962 }
4963 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004964 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004966
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004967 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004968
4969exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004970 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004973 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004975 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 } else {
4977 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004978 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004981}
4982
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004984psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4985 mbedtls_svc_key_id_t key,
4986 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987{
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004989}
4990
4991/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004992psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4993 mbedtls_svc_key_id_t key,
4994 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004995{
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004997}
4998
David Horstmannfed23772023-12-19 17:49:20 +00004999static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
5000 const uint8_t *nonce,
5001 size_t nonce_length)
5002{
5003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5004
5005 if (operation->id == 0) {
5006 status = PSA_ERROR_BAD_STATE;
5007 goto exit;
5008 }
5009
5010 if (operation->nonce_set) {
5011 status = PSA_ERROR_BAD_STATE;
5012 goto exit;
5013 }
5014
5015 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
5016 if (status != PSA_SUCCESS) {
5017 status = PSA_ERROR_INVALID_ARGUMENT;
5018 goto exit;
5019 }
5020
5021 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
5022 nonce_length);
5023
5024exit:
5025 if (status == PSA_SUCCESS) {
5026 operation->nonce_set = 1;
5027 } else {
5028 psa_aead_abort(operation);
5029 }
5030
5031 return status;
5032}
5033
Paul Elliott302ff6b2021-04-20 18:10:30 +01005034/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01005035psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00005036 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 size_t nonce_size,
5038 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005039{
5040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01005041 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07005042 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005043
David Horstmannd3cad8b2023-12-11 14:46:04 +00005044 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
5045 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
5046
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005047 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005050 status = PSA_ERROR_BAD_STATE;
5051 goto exit;
5052 }
5053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005055 status = PSA_ERROR_BAD_STATE;
5056 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005057 }
5058
Paul Elliotte193ea82021-10-01 13:00:16 +01005059 /* For CCM, this size may not be correct according to the PSA
5060 * specification. The PSA Crypto 1.0.1 specification states:
5061 *
5062 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5063 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5064 *
5065 * However this restriction that L has to be the smallest integer is not
5066 * applied in practice, and it is not implementable here since the
5067 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5069 operation->alg);
5070 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005071 status = PSA_ERROR_BUFFER_TOO_SMALL;
5072 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005073 }
5074
David Horstmann6e99bb22024-02-06 15:27:49 +00005075 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005079
David Horstmannfed23772023-12-19 17:49:20 +00005080 status = psa_aead_set_nonce_internal(operation, local_nonce,
5081 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005082
Paul Elliott39dc6b82021-05-11 19:16:09 +01005083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (status == PSA_SUCCESS) {
5085 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005086 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 } else {
5088 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005089 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005090
David Horstmannd3cad8b2023-12-11 14:46:04 +00005091 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005094}
5095
5096/* Set the nonce for a multipart authenticated encryption or decryption
5097 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005098psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005099 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101{
David Horstmannfed23772023-12-19 17:49:20 +00005102 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005103
David Horstmann8f0ef512023-12-11 15:17:11 +00005104 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5105 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005106
David Horstmannfed23772023-12-19 17:49:20 +00005107 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005108
David Horstmann18dc0322023-12-20 16:16:43 +00005109/* Exit label is only needed for buffer copying, prevent unused warnings. */
David Horstmann4a48bec2024-03-13 15:42:31 +00005110#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005111exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005112#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005113
David Horstmann8f0ef512023-12-11 15:17:11 +00005114 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005117}
5118
5119/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5121 size_t ad_length,
5122 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005123{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005124 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005127 status = PSA_ERROR_BAD_STATE;
5128 goto exit;
5129 }
5130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 if (operation->lengths_set || operation->ad_started ||
5132 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005133 status = PSA_ERROR_BAD_STATE;
5134 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005135 }
5136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005138#if defined(PSA_WANT_ALG_GCM)
5139 case PSA_ALG_GCM:
5140 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 * bits. Without the guard this code will generate warnings on 32bit
5142 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005143#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (((uint64_t) ad_length) >> 61 != 0 ||
5145 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005146 status = PSA_ERROR_INVALID_ARGUMENT;
5147 goto exit;
5148 }
Paul Elliott325d3742021-09-27 17:56:28 +01005149#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005150 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005151#endif /* PSA_WANT_ALG_GCM */
5152#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005153 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005155 status = PSA_ERROR_INVALID_ARGUMENT;
5156 goto exit;
5157 }
5158 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005159#endif /* PSA_WANT_ALG_CCM */
5160#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005161 case PSA_ALG_CHACHA20_POLY1305:
5162 /* No length restrictions for ChaChaPoly. */
5163 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005164#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005165 default:
5166 break;
5167 }
Paul Elliott325d3742021-09-27 17:56:28 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5170 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005171
Paul Elliott39dc6b82021-05-11 19:16:09 +01005172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005174 operation->ad_remaining = ad_length;
5175 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005176 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 } else {
5178 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005179 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005182}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005183
5184/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005186 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005188{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5190
David Horstmann25dac6e2023-12-11 15:23:13 +00005191 LOCAL_INPUT_DECLARE(input_external, input);
5192 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005195 status = PSA_ERROR_BAD_STATE;
5196 goto exit;
5197 }
5198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005200 status = PSA_ERROR_BAD_STATE;
5201 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005202 }
5203
Paul Elliott304766f2024-04-26 18:30:11 +01005204 /* No input to add (zero length), nothing to do. */
5205 if (input_length == 0) {
5206 status = PSA_SUCCESS;
5207 goto exit;
5208 }
5209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (operation->lengths_set) {
5211 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005212 status = PSA_ERROR_INVALID_ARGUMENT;
5213 goto exit;
5214 }
5215
5216 operation->ad_remaining -= input_length;
5217 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005218#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005220 status = PSA_ERROR_BAD_STATE;
5221 goto exit;
5222 }
5223#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 status = psa_driver_wrapper_aead_update_ad(operation, input,
5226 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005227
Paul Elliott39dc6b82021-05-11 19:16:09 +01005228exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005230 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 } else {
5232 psa_aead_abort(operation);
5233 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005234
David Horstmann25dac6e2023-12-11 15:23:13 +00005235 LOCAL_INPUT_FREE(input_external, input);
5236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005238}
5239
5240/* Encrypt or decrypt a message fragment in an active multipart AEAD
5241 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005242psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005243 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005245 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 size_t output_size,
5247 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005248{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005250
David Horstmann2914fac2023-12-11 15:28:37 +00005251
5252 LOCAL_INPUT_DECLARE(input_external, input);
5253 LOCAL_OUTPUT_DECLARE(output_external, output);
5254
5255 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5256 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5257
Paul Elliott302ff6b2021-04-20 18:10:30 +01005258 *output_length = 0;
5259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005261 status = PSA_ERROR_BAD_STATE;
5262 goto exit;
5263 }
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005266 status = PSA_ERROR_BAD_STATE;
5267 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005268 }
5269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005271 /* Additional data length was supplied, but not all the additional
5272 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005274 status = PSA_ERROR_INVALID_ARGUMENT;
5275 goto exit;
5276 }
5277
5278 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005280 status = PSA_ERROR_INVALID_ARGUMENT;
5281 goto exit;
5282 }
5283
5284 operation->body_remaining -= input_length;
5285 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005286#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005288 status = PSA_ERROR_BAD_STATE;
5289 goto exit;
5290 }
5291#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5294 output, output_size,
5295 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005296
Paul Elliott39dc6b82021-05-11 19:16:09 +01005297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005299 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 } else {
5301 psa_aead_abort(operation);
5302 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005303
David Horstmann2914fac2023-12-11 15:28:37 +00005304 LOCAL_INPUT_FREE(input_external, input);
5305 LOCAL_OUTPUT_FREE(output_external, output);
5306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005308}
5309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005311{
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (operation->id == 0 || !operation->nonce_set) {
5313 return PSA_ERROR_BAD_STATE;
5314 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5317 operation->body_remaining != 0)) {
5318 return PSA_ERROR_INVALID_ARGUMENT;
5319 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005322}
5323
Paul Elliott302ff6b2021-04-20 18:10:30 +01005324/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005325psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005326 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 size_t ciphertext_size,
5328 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005329 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 size_t tag_size,
5331 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005332{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5334
David Horstmann6db0e732023-12-11 15:35:59 +00005335 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5336 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5337
5338 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5339 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5340
Paul Elliott302ff6b2021-04-20 18:10:30 +01005341 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005342 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 status = psa_aead_final_checks(operation);
5345 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005350 status = PSA_ERROR_BAD_STATE;
5351 goto exit;
5352 }
5353
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5355 ciphertext_size,
5356 ciphertext_length,
5357 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005358
Paul Elliott39dc6b82021-05-11 19:16:09 +01005359exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005360
5361
Paul Elliottf47b0952021-05-21 18:02:33 +01005362 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005363 * the zero tag size, make sure the tag is set to something implausible.
5364 * Even if the operation succeeds, make sure we clear the rest of the
5365 * buffer to prevent potential leakage of anything previously placed in
5366 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005367 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005370
David Horstmann6db0e732023-12-11 15:35:59 +00005371 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5372 LOCAL_OUTPUT_FREE(tag_external, tag);
5373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005375}
5376
5377/* Finish authenticating and decrypting a message in a multipart AEAD
5378 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005379psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005380 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 size_t plaintext_size,
5382 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005383 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005385{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5387
David Horstmanne000a0a2023-12-11 16:37:04 +00005388 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5389 LOCAL_INPUT_DECLARE(tag_external, tag);
5390
5391 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5392 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5393
Paul Elliott302ff6b2021-04-20 18:10:30 +01005394 *plaintext_length = 0;
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 status = psa_aead_final_checks(operation);
5397 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005398 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005402 status = PSA_ERROR_BAD_STATE;
5403 goto exit;
5404 }
5405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5407 plaintext_size,
5408 plaintext_length,
5409 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005410
5411exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005413
David Horstmanne000a0a2023-12-11 16:37:04 +00005414 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5415 LOCAL_INPUT_FREE(tag_external, tag);
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005418}
5419
5420/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005421psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005422{
5423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005426 /* The object has (apparently) been initialized but it is not (yet)
5427 * in use. It's ok to call abort on such an object, and there's
5428 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005430 }
5431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005437}
5438
Gilles Peskinee59236f2018-01-27 23:32:46 +01005439/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005440/* Generators */
5441/****************************************************************/
5442
Przemek Stekiel69c46792022-06-10 12:59:51 +02005443#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005444 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005445 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305446 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305447 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005448#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005449#endif /* At least one builtin KDF */
5450
Przemek Stekiel69c46792022-06-10 12:59:51 +02005451#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005452 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5453 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5454static psa_status_t psa_key_derivation_start_hmac(
5455 psa_mac_operation_t *operation,
5456 psa_algorithm_t hash_alg,
5457 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005459{
5460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5463 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5464 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005465
Steven Cooreman72f736a2021-05-07 14:14:37 +02005466 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 status = psa_driver_wrapper_mac_sign_setup(operation,
5470 &attributes,
5471 hmac_key, hmac_key_length,
5472 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 psa_reset_key_attributes(&attributes);
5475 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005476}
5477#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005478
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005479#define HKDF_STATE_INIT 0 /* no input yet */
5480#define HKDF_STATE_STARTED 1 /* got salt */
5481#define HKDF_STATE_KEYED 2 /* got key */
5482#define HKDF_STATE_OUTPUT 3 /* output started */
5483
Gilles Peskinecbe66502019-05-16 16:59:18 +02005484static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005486{
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5488 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5489 } else {
5490 return operation->alg;
5491 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005492}
5493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005495{
5496 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5498 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005499 /* The object has (apparently) been initialized but it is not
5500 * in use. It's ok to call abort on such an object, and there's
5501 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005503#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5505 mbedtls_free(operation->ctx.hkdf.info);
5506 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5507 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005508#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005509#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5510 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5512 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5513 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5514 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005515 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005517 }
5518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005520 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005522 }
5523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005525 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005527 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005528#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005530 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005532 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005533#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005534 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005535
5536 /* We leave the fields Ai and output_block to be erased safely by the
5537 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005539#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5540 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005541#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5543 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5544 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5545 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305547#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305548 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305549 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005550 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305551 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305552 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305553
5554 status = PSA_SUCCESS;
5555 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305556#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005557 {
5558 status = PSA_ERROR_BAD_STATE;
5559 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 mbedtls_platform_zeroize(operation, sizeof(*operation));
5561 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562}
5563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005564psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005566{
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005570 }
5571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005574}
5575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5577 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005578{
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 if (operation->alg == 0) {
5580 return PSA_ERROR_BAD_STATE;
5581 }
5582 if (capacity > operation->capacity) {
5583 return PSA_ERROR_INVALID_ARGUMENT;
5584 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005587}
5588
Przemek Stekiel69c46792022-06-10 12:59:51 +02005589#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005590/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005591static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5592 psa_algorithm_t kdf_alg,
5593 uint8_t *output,
5594 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005595{
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5597 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005598 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005599 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005600#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005602#else
5603 const uint8_t last_block = 0xff;
5604#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 if (hkdf->state < HKDF_STATE_KEYED ||
5607 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005608#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005610#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 )) {
5612 return PSA_ERROR_BAD_STATE;
5613 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005614 hkdf->state = HKDF_STATE_OUTPUT;
5615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005617 /* Copy what remains of the current block */
5618 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005620 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 }
5622 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005623 output += n;
5624 output_length -= n;
5625 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005627 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005629 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005630 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005632 * object was corrupted or if this function is called directly
5633 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if (hkdf->block_number == last_block) {
5635 return PSA_ERROR_BAD_STATE;
5636 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005637
5638 /* We need a new block */
5639 ++hkdf->block_number;
5640 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5643 hash_alg,
5644 hkdf->prk,
5645 hash_length);
5646 if (status != PSA_SUCCESS) {
5647 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005649
5650 if (hkdf->block_number != 1) {
5651 status = psa_mac_update(&hkdf->hmac,
5652 hkdf->output_block,
5653 hash_length);
5654 if (status != PSA_SUCCESS) {
5655 return status;
5656 }
5657 }
5658 status = psa_mac_update(&hkdf->hmac,
5659 hkdf->info,
5660 hkdf->info_length);
5661 if (status != PSA_SUCCESS) {
5662 return status;
5663 }
5664 status = psa_mac_update(&hkdf->hmac,
5665 &hkdf->block_number, 1);
5666 if (status != PSA_SUCCESS) {
5667 return status;
5668 }
5669 status = psa_mac_sign_finish(&hkdf->hmac,
5670 hkdf->output_block,
5671 sizeof(hkdf->output_block),
5672 &hmac_output_length);
5673 if (status != PSA_SUCCESS) {
5674 return status;
5675 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005676 }
5677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005679}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005680#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005681
John Durkop07cc04a2020-11-16 22:08:34 -08005682#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5683 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005684static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5685 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005687{
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5689 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005690 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5691 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005692 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005693
Janos Follath7742fee2019-06-17 12:58:10 +01005694 /* We can't be wanting more output after block 0xff, otherwise
5695 * the capacity check in psa_key_derivation_output_bytes() would have
5696 * prevented this call. It could happen only if the operation
5697 * object was corrupted or if this function is called directly
5698 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 if (tls12_prf->block_number == 0xff) {
5700 return PSA_ERROR_CORRUPTION_DETECTED;
5701 }
Janos Follath7742fee2019-06-17 12:58:10 +01005702
5703 /* We need a new block */
5704 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005705 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005706
5707 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5708 *
5709 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5710 *
5711 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5712 * HMAC_hash(secret, A(2) + seed) +
5713 * HMAC_hash(secret, A(3) + seed) + ...
5714 *
5715 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005716 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005717 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005718 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005719 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005720 * is currently extracted as `output_block` and where i is
5721 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005722 */
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 status = psa_key_derivation_start_hmac(&hmac,
5725 hash_alg,
5726 tls12_prf->secret,
5727 tls12_prf->secret_length);
5728 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005729 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005731
5732 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005734 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5735 * the variable seed and in this instance means it in the context of the
5736 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 status = psa_mac_update(&hmac,
5738 tls12_prf->label,
5739 tls12_prf->label_length);
5740 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005741 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 }
5743 status = psa_mac_update(&hmac,
5744 tls12_prf->seed,
5745 tls12_prf->seed_length);
5746 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005747 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 }
5749 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005750 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5752 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005753 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005755 }
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 status = psa_mac_sign_finish(&hmac,
5758 tls12_prf->Ai, hash_length,
5759 &hmac_output_length);
5760 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005761 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 }
5763 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005764 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005766
5767 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 status = psa_key_derivation_start_hmac(&hmac,
5769 hash_alg,
5770 tls12_prf->secret,
5771 tls12_prf->secret_length);
5772 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005773 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 }
5775 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5776 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005777 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 }
5779 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5780 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005781 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 }
5783 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5784 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005785 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 }
5787 status = psa_mac_sign_finish(&hmac,
5788 tls12_prf->output_block, hash_length,
5789 &hmac_output_length);
5790 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005791 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005793
Janos Follath7742fee2019-06-17 12:58:10 +01005794
5795cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 cleanup_status = psa_mac_abort(&hmac);
5797 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005798 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005802}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005803
Janos Follath844eb0e2019-06-19 12:10:49 +01005804static psa_status_t psa_key_derivation_tls12_prf_read(
5805 psa_tls12_prf_key_derivation_t *tls12_prf,
5806 psa_algorithm_t alg,
5807 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005809{
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5811 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005812 psa_status_t status;
5813 uint8_t offset, length;
5814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005816 case PSA_TLS12_PRF_STATE_LABEL_SET:
5817 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5818 break;
5819 case PSA_TLS12_PRF_STATE_OUTPUT:
5820 break;
5821 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005823 }
5824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005826 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 if (tls12_prf->left_in_block == 0) {
5828 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5829 alg);
5830 if (status != PSA_SUCCESS) {
5831 return status;
5832 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005833
5834 continue;
5835 }
5836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005838 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005840 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005842
5843 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005845 output += length;
5846 output_length -= length;
5847 tls12_prf->left_in_block -= length;
5848 }
5849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005851}
John Durkop07cc04a2020-11-16 22:08:34 -08005852#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5853 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005854
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005855#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5856static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5857 psa_tls12_ecjpake_to_pms_t *ecjpake,
5858 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005860{
5861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005862 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (output_length != 32) {
5865 return PSA_ERROR_INVALID_ARGUMENT;
5866 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5869 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5870 &output_size);
5871 if (status != PSA_SUCCESS) {
5872 return status;
5873 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (output_size != output_length) {
5876 return PSA_ERROR_GENERIC_ERROR;
5877 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005880}
5881#endif
5882
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305883#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305884static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5885 psa_pbkdf2_key_derivation_t *pbkdf2,
5886 psa_algorithm_t prf_alg,
5887 uint8_t prf_output_length,
5888 psa_key_attributes_t *attributes)
5889{
5890 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305891 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305892 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305893 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305894 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305895 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305896 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305897
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305898 mac_operation.is_sign = 1;
5899 mac_operation.mac_size = prf_output_length;
5900 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305901
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305902 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5903 attributes,
5904 pbkdf2->password,
5905 pbkdf2->password_length,
5906 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305907 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305908 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305909 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305910 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5911 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305912 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305913 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305914 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305915 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305916 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305917 }
5918 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5919 &mac_output_length);
5920 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305921 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305922 }
5923
5924 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305925 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305926 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305927 }
5928
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305929 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305930
5931 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305932 /* We are passing prf_output_length as mac_size because the driver
5933 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305934 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305935 status = psa_driver_wrapper_mac_compute(attributes,
5936 pbkdf2->password,
5937 pbkdf2->password_length,
5938 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305939 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305940 &mac_output_length);
5941 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305942 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305943 }
5944
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305945 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305946 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305947
5948cleanup:
5949 /* Zeroise buffers to clear sensitive data from memory. */
5950 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5951 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305952}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305953
5954static psa_status_t psa_key_derivation_pbkdf2_read(
5955 psa_pbkdf2_key_derivation_t *pbkdf2,
5956 psa_algorithm_t kdf_alg,
5957 uint8_t *output,
5958 size_t output_length)
5959{
5960 psa_status_t status;
5961 psa_algorithm_t prf_alg;
5962 uint8_t prf_output_length;
5963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5964 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5965 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5966
5967 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5968 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5969 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5970 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305971 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5972 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305973 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305974 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305975 } else {
5976 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305977 }
5978
5979 switch (pbkdf2->state) {
5980 case PSA_PBKDF2_STATE_PASSWORD_SET:
5981 /* Initially we need a new block so bytes_used is equal to block size*/
5982 pbkdf2->bytes_used = prf_output_length;
5983 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5984 break;
5985 case PSA_PBKDF2_STATE_OUTPUT:
5986 break;
5987 default:
5988 return PSA_ERROR_BAD_STATE;
5989 }
5990
5991 while (output_length != 0) {
5992 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5993 if (n > output_length) {
5994 n = (uint8_t) output_length;
5995 }
5996 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5997 output += n;
5998 output_length -= n;
5999 pbkdf2->bytes_used += n;
6000
6001 if (output_length == 0) {
6002 break;
6003 }
6004
6005 /* We need a new block */
6006 pbkdf2->bytes_used = 0;
6007 pbkdf2->block_number++;
6008
6009 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
6010 prf_output_length,
6011 &attributes);
6012 if (status != PSA_SUCCESS) {
6013 return status;
6014 }
6015 }
6016
6017 return PSA_SUCCESS;
6018}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306019#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05306020
Janos Follath6c6c8fc2019-06-17 12:38:20 +01006021psa_status_t psa_key_derivation_output_bytes(
6022 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00006023 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006025{
6026 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00006027 LOCAL_OUTPUT_DECLARE(output_external, output);
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02006030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006032 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006034 }
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006037 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006038 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02006039 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
6040 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006041 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02006042 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006044 }
Ryan Everettf943e222024-01-19 14:46:39 +00006045
6046 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00006047 if (output_length > operation->capacity) {
6048 operation->capacity = 0;
6049 /* Go through the error path to wipe all confidential data now
6050 * that the operation object is useless. */
6051 status = PSA_ERROR_INSUFFICIENT_DATA;
6052 goto exit;
6053 }
6054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006055 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006056
Przemek Stekiel69c46792022-06-10 12:59:51 +02006057#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6059 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6060 output, output_length);
6061 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006062#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006063#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6064 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6066 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6067 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6068 kdf_alg, output,
6069 output_length);
6070 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006071#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6072 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006073#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006075 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6077 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006078#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306079#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306080 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306081 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6082 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306083 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306084#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006085
Gilles Peskineeab56e42018-07-12 17:12:33 +02006086 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006087 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006088 status = PSA_ERROR_BAD_STATE;
6089 LOCAL_OUTPUT_FREE(output_external, output);
6090
6091 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006092 }
6093
6094exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006096 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006097 * This allows us to differentiate between exhausted operations and
6098 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6099 * operations. */
6100 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006102 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006103 if (output != NULL) {
6104 memset(output, '!', output_length);
6105 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006106 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006107
6108 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006110}
6111
David Brown7807bf72021-02-09 16:28:23 -07006112#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006113static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006114{
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (data_size >= 8) {
6116 mbedtls_des_key_set_parity(data);
6117 }
6118 if (data_size >= 16) {
6119 mbedtls_des_key_set_parity(data + 8);
6120 }
6121 if (data_size >= 24) {
6122 mbedtls_des_key_set_parity(data + 16);
6123 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006124}
David Brown7807bf72021-02-09 16:28:23 -07006125#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006126
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006127/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 * ECC keys on a Weierstrass elliptic curve require the generation
6129 * of a private key which is an integer
6130 * in the range [1, N - 1], where N is the boundary of the private key domain:
6131 * N is the prime p for Diffie-Hellman, or the order of the
6132 * curve’s base point for ECC.
6133 *
6134 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6135 * This function generates the private key using the following process:
6136 *
6137 * 1. Draw a byte string of length ceiling(m/8) bytes.
6138 * 2. If m is not a multiple of 8, set the most significant
6139 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6140 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6141 * 4. If k > N - 2, discard the result and return to step 1.
6142 * 5. Output k + 1 as the private key.
6143 *
6144 * This method allows compliance to NIST standards, specifically the methods titled
6145 * Key-Pair Generation by Testing Candidates in the following publications:
6146 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6147 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6148 * Diffie-Hellman keys.
6149 *
6150 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6151 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6152 *
6153 * Note: Function allocates memory for *data buffer, so given *data should be
6154 * always NULL.
6155 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006156#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6157#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006158static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006159 psa_key_slot_t *slot,
6160 size_t bits,
6161 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006162 uint8_t **data
6163 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006164{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006165 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006166 mbedtls_mpi k;
6167 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006170 size_t m;
6171 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 mbedtls_mpi_init(&k);
6174 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006175
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006176 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006178 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006179 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006182 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006183 goto cleanup;
6184 }
6185
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006186 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006190
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006191 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006192 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006193 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006194
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006195 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006196
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006197 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006199
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006200 /* Note: This function is always called with *data == NULL and it
6201 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 *data = mbedtls_calloc(1, m_bytes);
6203 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006204 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006205 goto cleanup;
6206 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006209 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006211 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006213
6214 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006216 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006217 * (8 * ceiling(m/8) - m) bits of the first byte in
6218 * the string to zero.
6219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006221 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006222 }
6223
6224 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 * big-endian byte string.
6226 */
6227 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006228
6229 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 * Result of comparison is returned. When it indicates error
6231 * then this function is called again.
6232 */
6233 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006234 }
6235
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006236 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6238 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006239cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 if (ret != 0) {
6241 status = mbedtls_to_psa_error(ret);
6242 }
6243 if (status != PSA_SUCCESS) {
6244 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006245 *data = NULL;
6246 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 mbedtls_mpi_free(&k);
6248 mbedtls_mpi_free(&diff_N_2);
6249 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006250}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006251
6252/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6253 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6254 *
6255 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6256 * draw a 32-byte string and process it as specified in
6257 * Elliptic Curves for Security [RFC7748] §5.
6258 *
6259 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6260 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6261 *
6262 * Note: Function allocates memory for *data buffer, so given *data should be
6263 * always NULL.
6264 */
6265
6266static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6267 size_t bits,
6268 psa_key_derivation_operation_t *operation,
6269 uint8_t **data
6270 )
6271{
6272 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006276 case 255:
6277 output_length = 32;
6278 break;
6279 case 448:
6280 output_length = 56;
6281 break;
6282 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006284 break;
6285 }
6286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (*data == NULL) {
6290 return PSA_ERROR_INSUFFICIENT_MEMORY;
6291 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006296 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006300 case 255:
6301 (*data)[0] &= 248;
6302 (*data)[31] &= 127;
6303 (*data)[31] |= 64;
6304 break;
6305 case 448:
6306 (*data)[0] &= 252;
6307 (*data)[55] |= 128;
6308 break;
6309 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006311 break;
6312 }
6313
6314 return status;
6315}
Valerio Setti86587ab2023-06-27 13:09:30 +02006316#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6317static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6318 psa_key_slot_t *slot, size_t bits,
6319 psa_key_derivation_operation_t *operation, uint8_t **data)
6320{
6321 (void) slot;
6322 (void) bits;
6323 (void) operation;
6324 (void) data;
6325 return PSA_ERROR_NOT_SUPPORTED;
6326}
6327
6328static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6329 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6330{
6331 (void) bits;
6332 (void) operation;
6333 (void) data;
6334 return PSA_ERROR_NOT_SUPPORTED;
6335}
6336#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6337#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006338
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006339static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006340 psa_key_slot_t *slot,
6341 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006343{
6344 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306346 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6350 return PSA_ERROR_INVALID_ARGUMENT;
6351 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006352
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006353#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006354 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6356 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6357 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006358 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6360 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006361 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 }
6363 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006364 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6366 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006367 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006369 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006370 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006371#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006372 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (key_type_is_raw_bytes(slot->attr.type)) {
6374 if (bits % 8 != 0) {
6375 return PSA_ERROR_INVALID_ARGUMENT;
6376 }
6377 data = mbedtls_calloc(1, bytes);
6378 if (data == NULL) {
6379 return PSA_ERROR_INSUFFICIENT_MEMORY;
6380 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 status = psa_key_derivation_output_bytes(operation, data, bytes);
6383 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006384 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 }
David Brown12ca5032021-02-11 11:02:00 -07006386#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6388 psa_des_set_key_parity(data, bytes);
6389 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006390#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 } else {
6392 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006393 }
Ronald Crondd04d422020-11-28 15:14:42 +01006394
Ronald Cronbf33c932020-11-28 18:06:53 +01006395 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006396
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006397 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006398 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 &storage_size);
6400 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306401 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 }
Archanad8a83dc2021-06-14 10:04:16 +05306403 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 status = psa_allocate_buffer_to_slot(slot, storage_size);
6405 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 }
Archanad8a83dc2021-06-14 10:04:16 +05306408
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006409 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 data, bytes,
6411 slot->key.data,
6412 slot->key.bytes,
6413 &slot->key.bytes, &bits);
6414 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006415 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006417
6418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 mbedtls_free(data);
6420 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006421}
6422
Gilles Peskine092ce512024-02-20 12:31:24 +01006423static const psa_key_production_parameters_t default_production_parameters =
6424 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006425
Gilles Peskine092ce512024-02-20 12:31:24 +01006426int psa_key_production_parameters_are_default(
6427 const psa_key_production_parameters_t *params,
6428 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006429{
Gilles Peskine092ce512024-02-20 12:31:24 +01006430 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006431 return 0;
6432 }
Gilles Peskine092ce512024-02-20 12:31:24 +01006433 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006434 return 0;
6435 }
6436 return 1;
6437}
6438
6439psa_status_t psa_key_derivation_output_key_ext(
6440 const psa_key_attributes_t *attributes,
6441 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006442 const psa_key_production_parameters_t *params,
6443 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006444 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006445{
6446 psa_status_t status;
6447 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006448 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006449
Ronald Cron81709fc2020-11-14 12:10:32 +01006450 *key = MBEDTLS_SVC_KEY_ID_INIT;
6451
Gilles Peskine0f84d622019-09-12 19:03:13 +02006452 /* Reject any attempt to create a zero-length key so that we don't
6453 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (psa_get_key_bits(attributes) == 0) {
6455 return PSA_ERROR_INVALID_ARGUMENT;
6456 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006457
Gilles Peskine092ce512024-02-20 12:31:24 +01006458 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006459 return PSA_ERROR_INVALID_ARGUMENT;
6460 }
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 if (operation->alg == PSA_ALG_NONE) {
6463 return PSA_ERROR_BAD_STATE;
6464 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 if (!operation->can_output_key) {
6467 return PSA_ERROR_NOT_PERMITTED;
6468 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6471 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006472#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006474 /* Deriving a key in a secure element is not implemented yet. */
6475 status = PSA_ERROR_NOT_SUPPORTED;
6476 }
6477#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 if (status == PSA_SUCCESS) {
6479 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006480 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (status == PSA_SUCCESS) {
6484 status = psa_finish_key_creation(slot, driver, key);
6485 }
6486 if (status != PSA_SUCCESS) {
6487 psa_fail_key_creation(slot, driver);
6488 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006491}
6492
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006493psa_status_t psa_key_derivation_output_key(
6494 const psa_key_attributes_t *attributes,
6495 psa_key_derivation_operation_t *operation,
6496 mbedtls_svc_key_id_t *key)
6497{
Gilles Peskinec81393b2024-02-14 20:51:28 +01006498 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006499 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01006500 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006501}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006502
6503
6504/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006505/* Key derivation */
6506/****************************************************************/
6507
Steven Cooreman932ffb72021-02-15 12:14:32 +01006508#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006509static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006510{
6511#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6513 return 1;
6514 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006515#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006516#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6518 return 1;
6519 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006520#endif
6521#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6523 return 1;
6524 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006525#endif
6526#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6528 return 1;
6529 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006530#endif
6531#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6533 return 1;
6534 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006535#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006536#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6538 return 1;
6539 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006540#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306541#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6542 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6543 return 1;
6544 }
6545#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306546#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6547 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6548 return 1;
6549 }
6550#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006552}
6553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006555{
6556 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 psa_status_t status = psa_hash_setup(&operation, alg);
6558 psa_hash_abort(&operation);
6559 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006560}
6561
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306562static psa_status_t psa_key_derivation_set_maximum_capacity(
6563 psa_key_derivation_operation_t *operation,
6564 psa_algorithm_t kdf_alg)
6565{
6566#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6567 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6568 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6569 return PSA_SUCCESS;
6570 }
6571#endif
6572#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6573 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6574#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306575 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306576 PSA_KEY_TYPE_AES,
6577 128U,
6578 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306579#else
6580 operation->capacity = SIZE_MAX;
6581#endif
6582 return PSA_SUCCESS;
6583 }
6584#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6585
6586 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6587 * invalid or meaningless but it does not affect this function */
6588 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6589 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306590 if (hash_size == 0) {
6591 return PSA_ERROR_NOT_SUPPORTED;
6592 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306593
6594 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6595 * we might fail later, which is somewhat unfriendly and potentially
6596 * risk-prone. */
6597 psa_status_t status = psa_hash_try_support(hash_alg);
6598 if (status != PSA_SUCCESS) {
6599 return status;
6600 }
6601
6602#if defined(PSA_WANT_ALG_HKDF)
6603 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6604 operation->capacity = 255 * hash_size;
6605 } else
6606#endif
6607#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6608 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6609 operation->capacity = hash_size;
6610 } else
6611#endif
6612#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6613 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6614 operation->capacity = 255 * hash_size;
6615 } else
6616#endif
6617#if defined(PSA_WANT_ALG_TLS12_PRF)
6618 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6619 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6620 operation->capacity = SIZE_MAX;
6621 } else
6622#endif
6623#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6624 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6625 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6626 /* Master Secret is always 48 bytes
6627 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6628 operation->capacity = 48U;
6629 } else
6630#endif
6631#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6632 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6633#if (SIZE_MAX > UINT32_MAX)
6634 operation->capacity = UINT32_MAX * hash_size;
6635#else
6636 operation->capacity = SIZE_MAX;
6637#endif
6638 } else
6639#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6640 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306641 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306642 status = PSA_ERROR_NOT_SUPPORTED;
6643 }
6644 return status;
6645}
6646
Gilles Peskine969c5d62019-01-16 15:53:06 +01006647static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006648 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006650{
Janos Follath5fe19732019-06-20 15:09:30 +01006651 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6652 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006654
Gilles Peskine969c5d62019-01-16 15:53:06 +01006655 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 if (!is_kdf_alg_supported(kdf_alg)) {
6657 return PSA_ERROR_NOT_SUPPORTED;
6658 }
John Durkop07cc04a2020-11-16 22:08:34 -08006659
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306660 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6661 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306662 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006663}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006666{
6667#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 if (alg == PSA_ALG_ECDH) {
6669 return PSA_SUCCESS;
6670 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006671#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006672#if defined(PSA_WANT_ALG_FFDH)
6673 if (alg == PSA_ALG_FFDH) {
6674 return PSA_SUCCESS;
6675 }
6676#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006677 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006679}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006680
6681static int psa_key_derivation_allows_free_form_secret_input(
6682 psa_algorithm_t kdf_alg)
6683{
6684#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6685 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6686 return 0;
6687 }
6688#endif
6689 (void) kdf_alg;
6690 return 1;
6691}
John Durkop07cc04a2020-11-16 22:08:34 -08006692#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006693
Gilles Peskine449bd832023-01-11 14:50:10 +01006694psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6695 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006696{
6697 psa_status_t status;
6698
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (operation->alg != 0) {
6700 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006701 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6704 return PSA_ERROR_INVALID_ARGUMENT;
6705 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6706#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6707 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6708 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6709 status = psa_key_agreement_try_support(ka_alg);
6710 if (status != PSA_SUCCESS) {
6711 return status;
6712 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006713 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6714 return PSA_ERROR_INVALID_ARGUMENT;
6715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6717#else
6718 return PSA_ERROR_NOT_SUPPORTED;
6719#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6720 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6721#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6722 status = psa_key_derivation_setup_kdf(operation, alg);
6723#else
6724 return PSA_ERROR_NOT_SUPPORTED;
6725#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6726 } else {
6727 return PSA_ERROR_INVALID_ARGUMENT;
6728 }
6729
6730 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006731 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 }
6733 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006734}
6735
Przemek Stekiel69c46792022-06-10 12:59:51 +02006736#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006737static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6738 psa_algorithm_t kdf_alg,
6739 psa_key_derivation_step_t step,
6740 const uint8_t *data,
6741 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006742{
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006744 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006746 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006747#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6749 return PSA_ERROR_INVALID_ARGUMENT;
6750 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006751#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 if (hkdf->state != HKDF_STATE_INIT) {
6753 return PSA_ERROR_BAD_STATE;
6754 } else {
6755 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6756 hash_alg,
6757 data, data_length);
6758 if (status != PSA_SUCCESS) {
6759 return status;
6760 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006761 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006763 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006764 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006765#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006767 /* We shouldn't be in different state as HKDF_EXPAND only allows
6768 * two inputs: SECRET (this case) and INFO which does not modify
6769 * the state. It could happen only if the hkdf
6770 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 if (hkdf->state != HKDF_STATE_INIT) {
6772 return PSA_ERROR_BAD_STATE;
6773 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006774
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006775 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6777 return PSA_ERROR_INVALID_ARGUMENT;
6778 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006779
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 memcpy(hkdf->prk, data, data_length);
6781 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006782#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006783 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006784 /* HKDF: If no salt was provided, use an empty salt.
6785 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006787#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6789 return PSA_ERROR_BAD_STATE;
6790 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006791#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6793 hash_alg,
6794 NULL, 0);
6795 if (status != PSA_SUCCESS) {
6796 return status;
6797 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006798 hkdf->state = HKDF_STATE_STARTED;
6799 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 if (hkdf->state != HKDF_STATE_STARTED) {
6801 return PSA_ERROR_BAD_STATE;
6802 }
6803 status = psa_mac_update(&hkdf->hmac,
6804 data, data_length);
6805 if (status != PSA_SUCCESS) {
6806 return status;
6807 }
6808 status = psa_mac_sign_finish(&hkdf->hmac,
6809 hkdf->prk,
6810 sizeof(hkdf->prk),
6811 &data_length);
6812 if (status != PSA_SUCCESS) {
6813 return status;
6814 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006815 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006816
Gilles Peskine2b522db2019-04-12 15:11:49 +02006817 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006818 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006819#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006821 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006823 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006825#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006826 {
6827 /* Block 0 is empty, and the next block will be
6828 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006830 }
6831
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006833 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006834#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6836 return PSA_ERROR_INVALID_ARGUMENT;
6837 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006838#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006839#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6841 hkdf->state == HKDF_STATE_INIT) {
6842 return PSA_ERROR_BAD_STATE;
6843 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006844#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 if (hkdf->state == HKDF_STATE_OUTPUT) {
6846 return PSA_ERROR_BAD_STATE;
6847 }
6848 if (hkdf->info_set) {
6849 return PSA_ERROR_BAD_STATE;
6850 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006851 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 if (data_length != 0) {
6853 hkdf->info = mbedtls_calloc(1, data_length);
6854 if (hkdf->info == NULL) {
6855 return PSA_ERROR_INSUFFICIENT_MEMORY;
6856 }
6857 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006858 }
6859 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006861 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006863 }
6864}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006865#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006866
John Durkop07cc04a2020-11-16 22:08:34 -08006867#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6868 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006869static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6870 const uint8_t *data,
6871 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006872{
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6874 return PSA_ERROR_BAD_STATE;
6875 }
Janos Follathf08e2652019-06-13 09:05:41 +01006876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 if (data_length != 0) {
6878 prf->seed = mbedtls_calloc(1, data_length);
6879 if (prf->seed == NULL) {
6880 return PSA_ERROR_INSUFFICIENT_MEMORY;
6881 }
Janos Follathf08e2652019-06-13 09:05:41 +01006882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006884 prf->seed_length = data_length;
6885 }
Janos Follathf08e2652019-06-13 09:05:41 +01006886
Gilles Peskine43f958b2020-12-13 14:55:14 +01006887 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006890}
6891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6893 const uint8_t *data,
6894 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006895{
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6897 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6898 return PSA_ERROR_BAD_STATE;
6899 }
Janos Follath81550542019-06-13 14:26:34 +01006900
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 if (data_length != 0) {
6902 prf->secret = mbedtls_calloc(1, data_length);
6903 if (prf->secret == NULL) {
6904 return PSA_ERROR_INSUFFICIENT_MEMORY;
6905 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006906
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006908 prf->secret_length = data_length;
6909 }
Janos Follath81550542019-06-13 14:26:34 +01006910
Gilles Peskine43f958b2020-12-13 14:55:14 +01006911 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006914}
6915
Gilles Peskine449bd832023-01-11 14:50:10 +01006916static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6917 const uint8_t *data,
6918 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006919{
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6921 return PSA_ERROR_BAD_STATE;
6922 }
Janos Follath63028dd2019-06-13 09:15:47 +01006923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 if (data_length != 0) {
6925 prf->label = mbedtls_calloc(1, data_length);
6926 if (prf->label == NULL) {
6927 return PSA_ERROR_INSUFFICIENT_MEMORY;
6928 }
Janos Follath63028dd2019-06-13 09:15:47 +01006929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006931 prf->label_length = data_length;
6932 }
Janos Follath63028dd2019-06-13 09:15:47 +01006933
Gilles Peskine43f958b2020-12-13 14:55:14 +01006934 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006937}
6938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6940 psa_key_derivation_step_t step,
6941 const uint8_t *data,
6942 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006943{
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006945 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006947 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006949 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006951 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006953 }
6954}
John Durkop07cc04a2020-11-16 22:08:34 -08006955#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6956 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6957
6958#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6959static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6960 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006961 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006963{
6964 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6966 4 + data_length + prf->other_secret_length :
6967 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6970 return PSA_ERROR_INVALID_ARGUMENT;
6971 }
John Durkop07cc04a2020-11-16 22:08:34 -08006972
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 uint8_t *pms = mbedtls_calloc(1, pms_len);
6974 if (pms == NULL) {
6975 return PSA_ERROR_INSUFFICIENT_MEMORY;
6976 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006977 uint8_t *cur = pms;
6978
6979 /* pure-PSK:
6980 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006981 *
6982 * The premaster secret is formed as follows: if the PSK is N octets
6983 * long, concatenate a uint16 with the value N, N zero octets, a second
6984 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006985 *
6986 * mixed-PSK:
6987 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6988 * follows: concatenate a uint16 with the length of the other secret,
6989 * the other secret itself, uint16 with the length of PSK, and the
6990 * PSK itself.
6991 * For details please check:
6992 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6993 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6994 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006995 */
6996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6998 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6999 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
7000 if (prf->other_secret_length != 0) {
7001 memcpy(cur, prf->other_secret, prf->other_secret_length);
7002 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02007003 cur += prf->other_secret_length;
7004 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 } else {
7006 *cur++ = MBEDTLS_BYTE_1(data_length);
7007 *cur++ = MBEDTLS_BYTE_0(data_length);
7008 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02007009 cur += data_length;
7010 }
7011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 *cur++ = MBEDTLS_BYTE_1(data_length);
7013 *cur++ = MBEDTLS_BYTE_0(data_length);
7014 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08007015 cur += data_length;
7016
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007017 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08007018
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007019 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08007021}
Janos Follath6660f0e2019-06-17 08:44:03 +01007022
Przemek Stekiele47201b2022-04-19 13:53:28 +02007023static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
7024 psa_tls12_prf_key_derivation_t *prf,
7025 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02007027{
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
7029 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007031
7032 if (data_length != 0) {
7033 prf->other_secret = mbedtls_calloc(1, data_length);
7034 if (prf->other_secret == NULL) {
7035 return PSA_ERROR_INSUFFICIENT_MEMORY;
7036 }
7037
7038 memcpy(prf->other_secret, data, data_length);
7039 prf->other_secret_length = data_length;
7040 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007041 prf->other_secret_length = 0;
7042 }
7043
7044 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
7045
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02007047}
7048
Janos Follath6660f0e2019-06-17 08:44:03 +01007049static psa_status_t psa_tls12_prf_psk_to_ms_input(
7050 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01007051 psa_key_derivation_step_t step,
7052 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01007054{
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02007056 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 return psa_tls12_prf_psk_to_ms_set_key(prf,
7058 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007059 break;
7060 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7062 data,
7063 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007064 break;
7065 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007067 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007068
Przemek Stekiele47201b2022-04-19 13:53:28 +02007069 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007070}
John Durkop07cc04a2020-11-16 22:08:34 -08007071#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007072
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007073#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7074static psa_status_t psa_tls12_ecjpake_to_pms_input(
7075 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007076 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007077 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007079{
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7081 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7082 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007083 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007084
7085 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 if (data[0] != 0x04) {
7087 return PSA_ERROR_INVALID_ARGUMENT;
7088 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007089
7090 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007094}
7095#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307096
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307097#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307098static psa_status_t psa_pbkdf2_set_input_cost(
7099 psa_pbkdf2_key_derivation_t *pbkdf2,
7100 psa_key_derivation_step_t step,
7101 uint64_t data)
7102{
7103 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7104 return PSA_ERROR_INVALID_ARGUMENT;
7105 }
7106
7107 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7108 return PSA_ERROR_BAD_STATE;
7109 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307110
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307111 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307112 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307113 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307114
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307115 if (data == 0) {
7116 return PSA_ERROR_INVALID_ARGUMENT;
7117 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307118
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307119 pbkdf2->input_cost = data;
7120 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7121
7122 return PSA_SUCCESS;
7123}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307124
7125static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7126 const uint8_t *data,
7127 size_t data_length)
7128{
Gilles Peskineead17662023-08-20 22:02:51 +02007129 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7130 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7131 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7132 /* Appending to existing salt. No state change. */
7133 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307134 return PSA_ERROR_BAD_STATE;
7135 }
7136
Gilles Peskineca57d782023-07-20 19:08:06 +02007137 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007138 /* Appending an empty string, nothing to do. */
7139 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307140 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307141
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307142 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7143 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307144 return PSA_ERROR_INSUFFICIENT_MEMORY;
7145 }
7146
Gilles Peskineead17662023-08-20 22:02:51 +02007147 if (pbkdf2->salt_length != 0) {
7148 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7149 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307150 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307151 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307152 mbedtls_free(pbkdf2->salt);
7153 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307154 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307155 return PSA_SUCCESS;
7156}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307157
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307158#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307159static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307160 const uint8_t *input,
7161 size_t input_len,
7162 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307163 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307164{
7165 psa_status_t status = PSA_SUCCESS;
7166 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007167 return psa_hash_compute(hash_alg, input, input_len, output,
7168 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7169 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307170 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307171 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007172 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307173 return status;
7174}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307175#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307176
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307177#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307178static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7179 size_t input_len,
7180 uint8_t *output,
7181 size_t *output_len)
7182{
7183 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307184 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307186 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307187 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7188 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7189 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307190 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307191 * mac_size as the driver function sets mac_output_length = mac_size
7192 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307193 status = psa_driver_wrapper_mac_compute(&attributes,
7194 zeros, sizeof(zeros),
7195 PSA_ALG_CMAC, input, input_len,
7196 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307197 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7198 128U,
7199 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307200 output_len);
7201 } else {
7202 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307203 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307204 }
7205 return status;
7206}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307207#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307208
7209static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307210 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307211 const uint8_t *data,
7212 size_t data_length)
7213{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307214 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307215 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7216 return PSA_ERROR_BAD_STATE;
7217 }
7218
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307219#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307220 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7221 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7222 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7223 pbkdf2->password,
7224 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307225 } else
7226#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7227#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7228 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307229 status = psa_pbkdf2_cmac_set_password(data, data_length,
7230 pbkdf2->password,
7231 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307232 } else
7233#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7234 {
7235 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307236 }
7237
7238 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7239
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307240 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307241}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307242
7243static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307244 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307245 psa_key_derivation_step_t step,
7246 const uint8_t *data,
7247 size_t data_length)
7248{
7249 switch (step) {
7250 case PSA_KEY_DERIVATION_INPUT_SALT:
7251 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7252 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307253 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307254 default:
7255 return PSA_ERROR_INVALID_ARGUMENT;
7256 }
7257}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307258#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307259
Gilles Peskineb8965192019-09-24 16:21:10 +02007260/** Check whether the given key type is acceptable for the given
7261 * input step of a key derivation.
7262 *
7263 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7264 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7265 * Both secret and non-secret inputs can alternatively have the type
7266 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7267 * that the input was passed as a buffer rather than via a key object.
7268 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007269static int psa_key_derivation_check_input_type(
7270 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007272{
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007274 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 if (key_type == PSA_KEY_TYPE_DERIVE) {
7276 return PSA_SUCCESS;
7277 }
7278 if (key_type == PSA_KEY_TYPE_NONE) {
7279 return PSA_SUCCESS;
7280 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007281 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007282 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (key_type == PSA_KEY_TYPE_DERIVE) {
7284 return PSA_SUCCESS;
7285 }
7286 if (key_type == PSA_KEY_TYPE_NONE) {
7287 return PSA_SUCCESS;
7288 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007289 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007290 case PSA_KEY_DERIVATION_INPUT_LABEL:
7291 case PSA_KEY_DERIVATION_INPUT_SALT:
7292 case PSA_KEY_DERIVATION_INPUT_INFO:
7293 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7295 return PSA_SUCCESS;
7296 }
7297 if (key_type == PSA_KEY_TYPE_NONE) {
7298 return PSA_SUCCESS;
7299 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007300 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307301 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7302 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7303 return PSA_SUCCESS;
7304 }
7305 if (key_type == PSA_KEY_TYPE_DERIVE) {
7306 return PSA_SUCCESS;
7307 }
7308 if (key_type == PSA_KEY_TYPE_NONE) {
7309 return PSA_SUCCESS;
7310 }
7311 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007312 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007314}
7315
Janos Follathb80a94e2019-06-12 15:54:46 +01007316static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007317 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007318 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007319 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007320 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007322{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007323 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 status = psa_key_derivation_check_input_type(step, key_type);
7327 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007330
Przemek Stekiel69c46792022-06-10 12:59:51 +02007331#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7333 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7334 step, data, data_length);
7335 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007336#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007337#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7339 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7340 step, data, data_length);
7341 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007342#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7343#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7345 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7346 step, data, data_length);
7347 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007348#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007349#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007351 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7353 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007354#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307355#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307356 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307357 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7358 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307359 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307360#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007361 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007362 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007363 (void) data;
7364 (void) data_length;
7365 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007367 }
7368
Gilles Peskine224b0d62019-09-23 18:13:17 +02007369exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 if (status != PSA_SUCCESS) {
7371 psa_key_derivation_abort(operation);
7372 }
7373 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007374}
7375
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307376static psa_status_t psa_key_derivation_input_integer_internal(
7377 psa_key_derivation_operation_t *operation,
7378 psa_key_derivation_step_t step,
7379 uint64_t value)
7380{
7381 psa_status_t status;
7382 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7383
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307384#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307385 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307386 status = psa_pbkdf2_set_input_cost(
7387 &operation->ctx.pbkdf2, step, value);
7388 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307389#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307390 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307391 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307392 (void) value;
7393 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307394 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307395 }
7396
7397 if (status != PSA_SUCCESS) {
7398 psa_key_derivation_abort(operation);
7399 }
7400 return status;
7401}
7402
Janos Follath51f4a0f2019-06-14 11:35:55 +01007403psa_status_t psa_key_derivation_input_bytes(
7404 psa_key_derivation_operation_t *operation,
7405 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007406 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007408{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7410 LOCAL_INPUT_DECLARE(data_external, data);
7411
7412 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7413
7414 status = psa_key_derivation_input_internal(operation, step,
7415 PSA_KEY_TYPE_NONE,
7416 data, data_length);
David Horstmann4a48bec2024-03-13 15:42:31 +00007417#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007418exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007419#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007420 LOCAL_INPUT_FREE(data_external, data);
7421 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007422}
7423
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307424psa_status_t psa_key_derivation_input_integer(
7425 psa_key_derivation_operation_t *operation,
7426 psa_key_derivation_step_t step,
7427 uint64_t value)
7428{
7429 return psa_key_derivation_input_integer_internal(operation, step, value);
7430}
7431
Janos Follath51f4a0f2019-06-14 11:35:55 +01007432psa_status_t psa_key_derivation_input_key(
7433 psa_key_derivation_operation_t *operation,
7434 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007436{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007438 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007439 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007440
Ronald Cron5c522922020-11-14 16:35:34 +01007441 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7443 if (status != PSA_SUCCESS) {
7444 psa_key_derivation_abort(operation);
7445 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007446 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007447
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307448 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7449 * permission to output to a key object. */
7450 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7451 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007452 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 status = psa_key_derivation_input_internal(operation,
7456 step, slot->attr.type,
7457 slot->key.data,
7458 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007459
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007460 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007463}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007464
7465
7466
7467/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007468/* Key agreement */
7469/****************************************************************/
7470
Gilles Peskine449bd832023-01-11 14:50:10 +01007471psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7472 const uint8_t *key_buffer,
7473 size_t key_buffer_size,
7474 psa_algorithm_t alg,
7475 const uint8_t *peer_key,
7476 size_t peer_key_length,
7477 uint8_t *shared_secret,
7478 size_t shared_secret_size,
7479 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007480{
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007482#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007483 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7485 key_buffer_size, alg,
7486 peer_key, peer_key_length,
7487 shared_secret,
7488 shared_secret_size,
7489 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007490#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007491
7492#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7493 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007494 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007495 peer_key,
7496 peer_key_length,
7497 key_buffer,
7498 key_buffer_size,
7499 shared_secret,
7500 shared_secret_size,
7501 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007502#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7503
Gilles Peskine01d718c2018-09-18 12:01:02 +02007504 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007505 (void) attributes;
7506 (void) key_buffer;
7507 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007508 (void) peer_key;
7509 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007510 (void) shared_secret;
7511 (void) shared_secret_size;
7512 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007514 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007515}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007516
Aditya Deshpande17845b82022-10-13 17:21:01 +01007517/** Internal function for raw key agreement
7518 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007519 * to the driver's implementation if a driver is present.
7520 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007521 * (psa_key_agreement_raw_builtin).
7522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007523static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7524 psa_key_slot_t *private_key,
7525 const uint8_t *peer_key,
7526 size_t peer_key_length,
7527 uint8_t *shared_secret,
7528 size_t shared_secret_size,
7529 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007530{
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7532 return PSA_ERROR_NOT_SUPPORTED;
7533 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007534
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007535 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 private_key->key.data,
7537 private_key->key.bytes, alg,
7538 peer_key, peer_key_length,
7539 shared_secret,
7540 shared_secret_size,
7541 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007542}
7543
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007544/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007545 * to potentially free embedded data structures and wipe confidential data.
7546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007547static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7548 psa_key_derivation_step_t step,
7549 psa_key_slot_t *private_key,
7550 const uint8_t *peer_key,
7551 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007552{
7553 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007554 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007555 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007557
7558 /* Step 1: run the secret agreement algorithm to generate the shared
7559 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 status = psa_key_agreement_raw_internal(ka_alg,
7561 private_key,
7562 peer_key, peer_key_length,
7563 shared_secret,
7564 sizeof(shared_secret),
7565 &shared_secret_length);
7566 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007567 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007569
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007570 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007571 * the shared secret. A shared secret is permitted wherever a key
7572 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 status = psa_key_derivation_input_internal(operation, step,
7574 PSA_KEY_TYPE_DERIVE,
7575 shared_secret,
7576 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7579 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007580}
7581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7583 psa_key_derivation_step_t step,
7584 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007585 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007587{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007590 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007591 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7594 return PSA_ERROR_INVALID_ARGUMENT;
7595 }
Ronald Cron5c522922020-11-14 16:35:34 +01007596 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7598 if (status != PSA_SUCCESS) {
7599 return status;
7600 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007601
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007602 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 status = psa_key_agreement_internal(operation, step,
7604 slot,
7605 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007606
David Horstmann4a48bec2024-03-13 15:42:31 +00007607#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007608exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007609#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (status != PSA_SUCCESS) {
7611 psa_key_derivation_abort(operation);
7612 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007613 /* If a private key has been added as SECRET, we allow the derived
7614 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007616 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007618 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007619
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007620 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007621 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007624}
7625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7627 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007628 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007630 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 size_t output_size,
7632 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007633{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007635 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007636 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007637 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007638 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7639 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007640 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007643 status = PSA_ERROR_INVALID_ARGUMENT;
7644 goto exit;
7645 }
Ronald Cron5c522922020-11-14 16:35:34 +01007646 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7648 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007651
Gilles Peskine3e561302022-04-14 00:17:15 +02007652 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7653 * for the output size. The PSA specification only guarantees that this
7654 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7655 * but it might be nice to allow smaller buffers if the output fits.
7656 * At the time of writing this comment, with only ECDH implemented,
7657 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7658 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7659 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007660 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7662 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007663 status = PSA_ERROR_BUFFER_TOO_SMALL;
7664 goto exit;
7665 }
7666
Thomas Daubney81899ab2024-02-15 12:57:26 +00007667 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 status = psa_key_agreement_raw_internal(alg, slot,
7669 peer_key, peer_key_length,
7670 output, output_size,
7671 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007672
7673exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007674 /* Check for successful allocation of output,
7675 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007676 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007677 /* If an error happens and is not handled properly, the output
7678 * may be used as a key to protect sensitive data. Arrange for such
7679 * a key to be random, which is likely to result in decryption or
7680 * verification errors. This is better than filling the buffer with
7681 * some constant data such as zeros, which would result in the data
7682 * being protected with a reproducible, easily knowable key.
7683 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007684 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007685 *output_length = output_size;
7686 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007687
Thomas Daubney5390aca2024-02-22 11:06:04 +00007688 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007689 /* output allocation failed. */
7690 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007691 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007692
Ryan Everetta103ec92024-01-31 13:59:57 +00007693 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007694
Thomas Daubney81899ab2024-02-15 12:57:26 +00007695 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7696 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007698}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007699
7700
7701/****************************************************************/
7702/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007703/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007704
Gilles Peskine672a7712023-04-28 21:00:28 +02007705#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7706#include "entropy_poll.h"
7707#endif
7708
Gilles Peskine30524eb2020-11-13 17:02:26 +01007709/** Initialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007710 *
7711 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7712 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007714static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007715{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007716#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007718#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7719
Gilles Peskine30524eb2020-11-13 17:02:26 +01007720 /* Set default configuration if
7721 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007723 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 }
7725 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007726 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007728
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007730#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7731 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7732 /* The PSA entropy injection feature depends on using NV seed as an entropy
7733 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 mbedtls_entropy_add_source(&rng->entropy,
7735 mbedtls_nv_seed_poll, NULL,
7736 MBEDTLS_ENTROPY_BLOCK_SIZE,
7737 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007738#endif
7739
Valerio Setti061d4e42024-02-26 12:52:44 +01007740 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007741#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007742}
7743
7744/** Deinitialize the PSA random generator.
Paul Elliottd35dce62024-03-15 13:57:16 +00007745 *
7746 * Note: the mbedtls_threading_psa_rngdata_mutex should be held when calling
7747 * this function if mutexes are enabled.
Gilles Peskine30524eb2020-11-13 17:02:26 +01007748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007749static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007750{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007751#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007753#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007754 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007756#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007757}
7758
7759/** Seed the PSA random generator.
7760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007761static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007762{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007763#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7764 /* Do nothing: the external RNG seeds itself. */
7765 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007767#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007768 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007769 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 drbg_seed, sizeof(drbg_seed) - 1);
7771 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007772#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007773}
7774
David Horstmann6e99bb22024-02-06 15:27:49 +00007775psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007776 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007777{
David Horstmann6e99bb22024-02-06 15:27:49 +00007778 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007779
David Horstmann6e99bb22024-02-06 15:27:49 +00007780 LOCAL_OUTPUT_DECLARE(output_external, output);
7781 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007782
David Horstmann6e99bb22024-02-06 15:27:49 +00007783 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007784
David Horstmann4a48bec2024-03-13 15:42:31 +00007785#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00007786exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00007787#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00007788 LOCAL_OUTPUT_FREE(output_external, output);
7789 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007790}
7791
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007792#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007793psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7794 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007795{
Paul Elliott600472b2024-02-23 17:26:58 +00007796 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007797 return PSA_ERROR_NOT_PERMITTED;
7798 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007799
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7801 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7802 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7803 return PSA_ERROR_INVALID_ARGUMENT;
7804 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007805
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007807}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007808#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007809
Ronald Cron3772afe2021-02-08 16:10:05 +01007810/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007811 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007812 * \param type The key type
7813 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007814 *
7815 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007816 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007817 * \retval #PSA_ERROR_INVALID_ARGUMENT
7818 * The size in bits of the key is not valid.
7819 * \retval #PSA_ERROR_NOT_SUPPORTED
7820 * The type and/or the size in bits of the key or the combination of
7821 * the two is not supported.
7822 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007823static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007824 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007825{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007827
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 if (key_type_is_raw_bytes(type)) {
7829 status = psa_validate_unstructured_key_bit_size(type, bits);
7830 if (status != PSA_SUCCESS) {
7831 return status;
7832 }
7833 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007834#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7836 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7837 return PSA_ERROR_NOT_SUPPORTED;
7838 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007839 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007840 return PSA_ERROR_NOT_SUPPORTED;
7841 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007842
Ronald Cron01b2aba2020-10-05 09:42:02 +02007843 /* Accept only byte-aligned keys, for the same reasons as
7844 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 if (bits % 8 != 0) {
7846 return PSA_ERROR_NOT_SUPPORTED;
7847 }
7848 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007849#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007850
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007851#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007853 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 return PSA_SUCCESS;
7855 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007856#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007857
Valerio Settia55f0422023-07-10 15:34:41 +02007858#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007859 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007860 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007861 return PSA_ERROR_NOT_SUPPORTED;
7862 }
7863 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007864#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007865 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007867 }
7868
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007870}
7871
Ronald Cron55ed0592020-10-05 10:30:40 +02007872psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007873 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007874 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007876{
7877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007878 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007879
Gilles Peskine7a18f962024-02-12 16:48:11 +01007880 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007881 (void) params;
7882 (void) params_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00007885 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007886 if (status != PSA_SUCCESS) {
7887 return status;
7888 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007889
David Brown12ca5032021-02-11 11:02:00 -07007890#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 if (type == PSA_KEY_TYPE_DES) {
7892 psa_des_set_key_parity(key_buffer, key_buffer_size);
7893 }
David Brown8107e312021-02-12 08:08:46 -07007894#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007896
Valerio Setti76df8c12023-07-11 14:11:28 +02007897#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7899 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007900 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 key_buffer,
7902 key_buffer_size,
7903 key_buffer_length);
7904 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007905#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007906
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007907#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7909 return mbedtls_psa_ecp_generate_key(attributes,
7910 key_buffer,
7911 key_buffer_size,
7912 key_buffer_length);
7913 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007914#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007915
Valerio Settia55f0422023-07-10 15:34:41 +02007916#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007917 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7918 return mbedtls_psa_ffdh_generate_key(attributes,
7919 key_buffer,
7920 key_buffer_size,
7921 key_buffer_length);
7922 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007923#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007924 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 (void) key_buffer_length;
7926 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007927 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007928
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007930}
Darryl Green0c6575a2018-11-07 16:05:30 +00007931
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007932psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007933 const psa_key_production_parameters_t *params,
7934 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007935 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007936{
7937 psa_status_t status;
7938 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007939 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007940 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007941
Ronald Cron81709fc2020-11-14 12:10:32 +01007942 *key = MBEDTLS_SVC_KEY_ID_INIT;
7943
Gilles Peskine0f84d622019-09-12 19:03:13 +02007944 /* Reject any attempt to create a zero-length key so that we don't
7945 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 if (psa_get_key_bits(attributes) == 0) {
7947 return PSA_ERROR_INVALID_ARGUMENT;
7948 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007949
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007950 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007951 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 return PSA_ERROR_INVALID_ARGUMENT;
7953 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007954
Gilles Peskine7a18f962024-02-12 16:48:11 +01007955#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007956 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007957 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007958 return PSA_ERROR_INVALID_ARGUMENT;
7959 }
7960 } else
7961#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007962 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01007963 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02007964 }
7965
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7967 &slot, &driver);
7968 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007969 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007970 }
Gilles Peskine11792082019-08-06 18:36:36 +02007971
Ronald Cron977c2472020-10-13 08:32:21 +02007972 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307973 * storage ( thus not in the case of generating a key in a secure element
7974 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7975 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007977 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007979 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007980 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007984
7985 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007986 attributes->type,
7987 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007989 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 attributes, &key_buffer_size);
7991 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 }
Ronald Cron977c2472020-10-13 08:32:21 +02007994 }
Ronald Cron977c2472020-10-13 08:32:21 +02007995
Gilles Peskine449bd832023-01-11 14:50:10 +01007996 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7997 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007998 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 }
Ronald Cron977c2472020-10-13 08:32:21 +02008000 }
8001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01008003 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01008004 slot->key.data, slot->key.bytes,
8005 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 if (status != PSA_SUCCESS) {
8007 psa_remove_key_data_from_memory(slot);
8008 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02008009
Gilles Peskine11792082019-08-06 18:36:36 +02008010exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 if (status == PSA_SUCCESS) {
8012 status = psa_finish_key_creation(slot, driver, key);
8013 }
8014 if (status != PSA_SUCCESS) {
8015 psa_fail_key_creation(slot, driver);
8016 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02008017
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008019}
8020
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008021psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
8022 mbedtls_svc_key_id_t *key)
8023{
8024 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01008025 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01008026 key);
8027}
8028
Gilles Peskinee59236f2018-01-27 23:32:46 +01008029/****************************************************************/
8030/* Module setup */
8031/****************************************************************/
8032
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008033#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01008034psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 void (* entropy_init)(mbedtls_entropy_context *ctx),
8036 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01008037{
Paul Elliott8e151532024-02-23 17:35:29 +00008038 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8039
8040#if defined(MBEDTLS_THREADING_C)
8041 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8042#endif /* defined(MBEDTLS_THREADING_C) */
8043
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Paul Elliott8e151532024-02-23 17:35:29 +00008045 status = PSA_ERROR_BAD_STATE;
8046 } else {
8047 global_data.rng.entropy_init = entropy_init;
8048 global_data.rng.entropy_free = entropy_free;
8049 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 }
Paul Elliott8e151532024-02-23 17:35:29 +00008051
8052#if defined(MBEDTLS_THREADING_C)
8053 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8054#endif /* defined(MBEDTLS_THREADING_C) */
8055
8056 return status;
Gilles Peskine5e769522018-11-20 21:59:56 +01008057}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01008058#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01008059
Gilles Peskine449bd832023-01-11 14:50:10 +01008060void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008061{
Paul Elliott47cee8e2024-03-08 21:38:02 +00008062
Paul Elliott600472b2024-02-23 17:26:58 +00008063#if defined(MBEDTLS_THREADING_C)
8064 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8065#endif /* defined(MBEDTLS_THREADING_C) */
8066
Paul Elliott47cee8e2024-03-08 21:38:02 +00008067 /* Nothing to do to free transaction. */
8068 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) {
8069 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
Valerio Setti83e0de82023-11-24 12:13:05 +01008070 }
Paul Elliott600472b2024-02-23 17:26:58 +00008071
Paul Elliott47cee8e2024-03-08 21:38:02 +00008072 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED) {
8073 psa_wipe_all_key_slots();
8074 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8075 }
Ronald Cron9ba76912021-04-10 16:57:30 +02008076
Paul Elliott600472b2024-02-23 17:26:58 +00008077#if defined(MBEDTLS_THREADING_C)
8078 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8079#endif /* defined(MBEDTLS_THREADING_C) */
8080
Paul Elliott47cee8e2024-03-08 21:38:02 +00008081#if defined(MBEDTLS_THREADING_C)
8082 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
8083#endif /* defined(MBEDTLS_THREADING_C) */
8084
Gilles Peskinec6b69072018-11-20 21:42:52 +01008085 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01008086 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008087 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008088 global_data.rng_state = RNG_NOT_INITIALIZED;
8089 mbedtls_platform_zeroize(&global_data.rng, sizeof(global_data.rng));
8090
8091#if defined(MBEDTLS_THREADING_C)
8092 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
8093#endif /* defined(MBEDTLS_THREADING_C) */
8094
8095#if defined(MBEDTLS_THREADING_C)
8096 mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
8097#endif /* defined(MBEDTLS_THREADING_C) */
Ronald Cron9ba76912021-04-10 16:57:30 +02008098
8099 /* Terminate drivers */
Paul Elliott47cee8e2024-03-08 21:38:02 +00008100 if (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) {
8101 psa_driver_wrapper_free();
8102 global_data.initialized &= ~PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8103 }
8104
8105#if defined(MBEDTLS_THREADING_C)
8106 mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
8107#endif /* defined(MBEDTLS_THREADING_C) */
8108
Gilles Peskinee59236f2018-01-27 23:32:46 +01008109}
8110
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008111#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8112/** Recover a transaction that was interrupted by a power failure.
8113 *
8114 * This function is called during initialization, before psa_crypto_init()
8115 * returns. If this function returns a failure status, the initialization
8116 * fails.
8117 */
8118static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008120{
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008122 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8123 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 /* TODO - fall through to the failure case until this
8125 * is implemented.
8126 * https://github.com/ARMmbed/mbed-crypto/issues/218
8127 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008128 default:
8129 /* We found an unsupported transaction in the storage.
8130 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008132 }
8133}
8134#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8135
Paul Elliott47cee8e2024-03-08 21:38:02 +00008136static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
8137{
8138 psa_status_t status = PSA_SUCCESS;
8139 uint8_t driver_wrappers_initialized = 0;
8140
8141 switch (subsystem) {
8142 case PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS:
8143
8144#if defined(MBEDTLS_THREADING_C)
8145 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8146#endif /* defined(MBEDTLS_THREADING_C) */
8147
8148 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED)) {
8149 /* Init drivers */
8150 status = psa_driver_wrapper_init();
8151
8152 /* Drivers need shutdown regardless of startup errors. */
8153 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED;
8154
8155
8156 }
8157#if defined(MBEDTLS_THREADING_C)
8158 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8159 &mbedtls_threading_psa_globaldata_mutex));
8160#endif /* defined(MBEDTLS_THREADING_C) */
8161
8162 break;
8163
8164 case PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS:
8165
8166#if defined(MBEDTLS_THREADING_C)
8167 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8168#endif /* defined(MBEDTLS_THREADING_C) */
8169
8170 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED)) {
8171 status = psa_initialize_key_slots();
8172
8173 /* Need to wipe keys even if initialization fails. */
8174 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED;
8175
8176 }
8177#if defined(MBEDTLS_THREADING_C)
8178 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8179 &mbedtls_threading_psa_globaldata_mutex));
8180#endif /* defined(MBEDTLS_THREADING_C) */
8181
8182 break;
8183
8184 case PSA_CRYPTO_SUBSYSTEM_RNG:
8185
8186#if defined(MBEDTLS_THREADING_C)
8187 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8188#endif /* defined(MBEDTLS_THREADING_C) */
8189
8190 driver_wrappers_initialized =
8191 (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED);
8192
8193#if defined(MBEDTLS_THREADING_C)
8194 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8195 &mbedtls_threading_psa_globaldata_mutex));
8196#endif /* defined(MBEDTLS_THREADING_C) */
8197
8198 /* Need to use separate mutex here, as initialisation can require
8199 * testing of init flags, which requires locking the global data
8200 * mutex. */
8201#if defined(MBEDTLS_THREADING_C)
8202 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex));
8203#endif /* defined(MBEDTLS_THREADING_C) */
8204
8205 /* Initialize and seed the random generator. */
8206 if (global_data.rng_state == RNG_NOT_INITIALIZED && driver_wrappers_initialized) {
8207 mbedtls_psa_random_init(&global_data.rng);
8208 global_data.rng_state = RNG_INITIALIZED;
8209
8210 status = mbedtls_psa_random_seed(&global_data.rng);
8211 if (status == PSA_SUCCESS) {
8212 global_data.rng_state = RNG_SEEDED;
8213 }
8214 }
8215
8216#if defined(MBEDTLS_THREADING_C)
8217 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8218 &mbedtls_threading_psa_rngdata_mutex));
8219#endif /* defined(MBEDTLS_THREADING_C) */
8220
Paul Elliott47cee8e2024-03-08 21:38:02 +00008221 break;
8222
8223 case PSA_CRYPTO_SUBSYSTEM_TRANSACTION:
8224
8225#if defined(MBEDTLS_THREADING_C)
8226 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex));
8227#endif /* defined(MBEDTLS_THREADING_C) */
8228
8229 if (!(global_data.initialized & PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED)) {
8230#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
8231 status = psa_crypto_load_transaction();
8232 if (status == PSA_SUCCESS) {
8233 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8234 if (status == PSA_SUCCESS) {
8235 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8236 }
8237 status = psa_crypto_stop_transaction();
8238 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
8239 /* There's no transaction to complete. It's all good. */
8240 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8241 status = PSA_SUCCESS;
8242 }
8243#else /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8244 global_data.initialized |= PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED;
8245 status = PSA_SUCCESS;
8246#endif /* defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) */
8247 }
8248
8249#if defined(MBEDTLS_THREADING_C)
8250 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_unlock(
8251 &mbedtls_threading_psa_globaldata_mutex));
8252#endif /* defined(MBEDTLS_THREADING_C) */
8253
8254 break;
8255
8256 default:
8257 status = PSA_ERROR_CORRUPTION_DETECTED;
8258 }
8259
8260 /* Exit label only required when using threading macros. */
8261#if defined(MBEDTLS_THREADING_C)
8262exit:
8263#endif /* defined(MBEDTLS_THREADING_C) */
8264
8265 return status;
8266}
8267
Gilles Peskine449bd832023-01-11 14:50:10 +01008268psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008269{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008270 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008271
Paul Elliott47cee8e2024-03-08 21:38:02 +00008272 /* Double initialization is explicitly allowed. Early out if everything is
8273 * done. */
8274 if (psa_get_initialized()) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 return PSA_SUCCESS;
8276 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008277
Paul Elliott47cee8e2024-03-08 21:38:02 +00008278 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS);
Valerio Setti402cfba2023-11-13 10:24:32 +01008279 if (status != PSA_SUCCESS) {
8280 goto exit;
8281 }
8282
Paul Elliott47cee8e2024-03-08 21:38:02 +00008283 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS);
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008287
Paul Elliott47cee8e2024-03-08 21:38:02 +00008288 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_RNG);
8289 if (status != PSA_SUCCESS) {
8290 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02008291 }
Gilles Peskinefc762652019-07-22 19:30:34 +02008292
Paul Elliott47cee8e2024-03-08 21:38:02 +00008293 status = mbedtls_psa_crypto_init_subsystem(PSA_CRYPTO_SUBSYSTEM_TRANSACTION);
Gilles Peskinee59236f2018-01-27 23:32:46 +01008294
8295exit:
Paul Elliott600472b2024-02-23 17:26:58 +00008296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 if (status != PSA_SUCCESS) {
8298 mbedtls_psa_crypto_free();
8299 }
Paul Elliott47cee8e2024-03-08 21:38:02 +00008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008302}
8303
Yanray Wangffc3c482023-07-11 12:01:04 +08008304#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008305psa_status_t psa_crypto_driver_pake_get_password_len(
8306 const psa_crypto_driver_pake_inputs_t *inputs,
8307 size_t *password_len)
8308{
8309 if (inputs->password_len == 0) {
8310 return PSA_ERROR_BAD_STATE;
8311 }
8312
8313 *password_len = inputs->password_len;
8314
8315 return PSA_SUCCESS;
8316}
8317
8318psa_status_t psa_crypto_driver_pake_get_password(
8319 const psa_crypto_driver_pake_inputs_t *inputs,
8320 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8321{
8322 if (inputs->password_len == 0) {
8323 return PSA_ERROR_BAD_STATE;
8324 }
8325
8326 if (buffer_size < inputs->password_len) {
8327 return PSA_ERROR_BUFFER_TOO_SMALL;
8328 }
8329
8330 memcpy(buffer, inputs->password, inputs->password_len);
8331 *buffer_length = inputs->password_len;
8332
8333 return PSA_SUCCESS;
8334}
8335
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008336psa_status_t psa_crypto_driver_pake_get_user_len(
8337 const psa_crypto_driver_pake_inputs_t *inputs,
8338 size_t *user_len)
8339{
8340 if (inputs->user_len == 0) {
8341 return PSA_ERROR_BAD_STATE;
8342 }
8343
8344 *user_len = inputs->user_len;
8345
8346 return PSA_SUCCESS;
8347}
8348
8349psa_status_t psa_crypto_driver_pake_get_user(
8350 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008351 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008352{
8353 if (inputs->user_len == 0) {
8354 return PSA_ERROR_BAD_STATE;
8355 }
8356
Przemek Stekielc0e62502023-03-14 11:49:36 +01008357 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008358 return PSA_ERROR_BUFFER_TOO_SMALL;
8359 }
8360
Przemek Stekielc0e62502023-03-14 11:49:36 +01008361 memcpy(user_id, inputs->user, inputs->user_len);
8362 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008363
8364 return PSA_SUCCESS;
8365}
8366
8367psa_status_t psa_crypto_driver_pake_get_peer_len(
8368 const psa_crypto_driver_pake_inputs_t *inputs,
8369 size_t *peer_len)
8370{
8371 if (inputs->peer_len == 0) {
8372 return PSA_ERROR_BAD_STATE;
8373 }
8374
8375 *peer_len = inputs->peer_len;
8376
8377 return PSA_SUCCESS;
8378}
8379
8380psa_status_t psa_crypto_driver_pake_get_peer(
8381 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008382 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008383{
8384 if (inputs->peer_len == 0) {
8385 return PSA_ERROR_BAD_STATE;
8386 }
8387
Przemek Stekielc0e62502023-03-14 11:49:36 +01008388 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008389 return PSA_ERROR_BUFFER_TOO_SMALL;
8390 }
8391
Przemek Stekielc0e62502023-03-14 11:49:36 +01008392 memcpy(peer_id, inputs->peer, inputs->peer_len);
8393 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008394
8395 return PSA_SUCCESS;
8396}
8397
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008398psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8399 const psa_crypto_driver_pake_inputs_t *inputs,
8400 psa_pake_cipher_suite_t *cipher_suite)
8401{
8402 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8403 return PSA_ERROR_BAD_STATE;
8404 }
8405
8406 *cipher_suite = inputs->cipher_suite;
8407
8408 return PSA_SUCCESS;
8409}
8410
Neil Armstronga7d08c32022-06-01 18:21:20 +02008411psa_status_t psa_pake_setup(
8412 psa_pake_operation_t *operation,
8413 const psa_pake_cipher_suite_t *cipher_suite)
8414{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008416
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008417 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008418 status = PSA_ERROR_BAD_STATE;
8419 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008420 }
8421
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008422 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008423 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008424 status = PSA_ERROR_INVALID_ARGUMENT;
8425 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008426 }
8427
Przemek Stekiel51eac532022-12-07 11:04:51 +01008428 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8429
Przemek Stekiele12ed362022-12-21 12:54:46 +01008430 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008431 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8432 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008433 operation->data.inputs.cipher_suite = *cipher_suite;
8434
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008435#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008436 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008437 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008438 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008439
David Horstmann16f01512023-06-14 17:21:07 +01008440 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008441 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008442 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008443#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008444 {
8445 status = PSA_ERROR_NOT_SUPPORTED;
8446 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008447 }
8448
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008449 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8450
Przemek Stekiel51eac532022-12-07 11:04:51 +01008451 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008452exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008453 psa_pake_abort(operation);
8454 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008455}
8456
8457psa_status_t psa_pake_set_password_key(
8458 psa_pake_operation_t *operation,
8459 mbedtls_svc_key_id_t password)
8460{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008462 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8463 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008464 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008465
Przemek Stekiel51eac532022-12-07 11:04:51 +01008466 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008467 status = PSA_ERROR_BAD_STATE;
8468 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008469 }
8470
Przemek Stekiel6c764412022-11-22 14:05:12 +01008471 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8472 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008473 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008474 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008475 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008476 }
8477
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008478 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008479
Przemek Stekiel51eac532022-12-07 11:04:51 +01008480 if (type != PSA_KEY_TYPE_PASSWORD &&
8481 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8482 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008483 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008484 }
8485
Przemek Stekiel51eac532022-12-07 11:04:51 +01008486 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8487 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008488 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008489 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008490 }
8491
8492 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8493 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008494 operation->data.inputs.attributes = slot->attr;
8495
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008496exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008497 if (status != PSA_SUCCESS) {
8498 psa_pake_abort(operation);
8499 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008500 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008501 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008502}
8503
8504psa_status_t psa_pake_set_user(
8505 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008506 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008507 size_t user_id_len)
8508{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008510 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008511
8512 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008513 status = PSA_ERROR_BAD_STATE;
8514 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008515 }
8516
Przemek Stekiel51eac532022-12-07 11:04:51 +01008517 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008518 status = PSA_ERROR_INVALID_ARGUMENT;
8519 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008520 }
8521
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008522 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008523 status = PSA_ERROR_BAD_STATE;
8524 goto exit;
8525 }
8526
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008527 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8528 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008529 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8530 goto exit;
8531 }
8532
David Horstmann4f534ae2024-01-22 17:35:59 +00008533 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8534
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008535 memcpy(operation->data.inputs.user, user_id, user_id_len);
8536 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008537
David Horstmann4f534ae2024-01-22 17:35:59 +00008538 status = PSA_SUCCESS;
8539
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008540exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008541 LOCAL_INPUT_FREE(user_id_external, user_id);
8542 if (status != PSA_SUCCESS) {
8543 psa_pake_abort(operation);
8544 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008545 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008546}
8547
8548psa_status_t psa_pake_set_peer(
8549 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008550 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008551 size_t peer_id_len)
8552{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008554 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008555
8556 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008557 status = PSA_ERROR_BAD_STATE;
8558 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008559 }
8560
Przemek Stekiel51eac532022-12-07 11:04:51 +01008561 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008562 status = PSA_ERROR_INVALID_ARGUMENT;
8563 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008564 }
8565
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008566 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008567 status = PSA_ERROR_BAD_STATE;
8568 goto exit;
8569 }
8570
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008571 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8572 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008573 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8574 goto exit;
8575 }
8576
David Horstmann4f534ae2024-01-22 17:35:59 +00008577 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8578
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008579 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8580 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008581
David Horstmann4f534ae2024-01-22 17:35:59 +00008582 status = PSA_SUCCESS;
8583
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008584exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008585 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8586 if (status != PSA_SUCCESS) {
8587 psa_pake_abort(operation);
8588 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008589 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008590}
8591
8592psa_status_t psa_pake_set_role(
8593 psa_pake_operation_t *operation,
8594 psa_pake_role_t role)
8595{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008597
Przemek Stekiel51eac532022-12-07 11:04:51 +01008598 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008599 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008600 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008601 }
8602
Przemek Stekiel09104b82023-03-06 14:11:51 +01008603 switch (operation->alg) {
8604#if defined(PSA_WANT_ALG_JPAKE)
8605 case PSA_ALG_JPAKE:
8606 if (role == PSA_PAKE_ROLE_NONE) {
8607 return PSA_SUCCESS;
8608 }
8609 status = PSA_ERROR_INVALID_ARGUMENT;
8610 break;
8611#endif
8612 default:
8613 (void) role;
8614 status = PSA_ERROR_NOT_SUPPORTED;
8615 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008616 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008617exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008618 psa_pake_abort(operation);
8619 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008620}
8621
David Horstmanne7f21e62023-05-12 18:17:21 +01008622/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008623#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008624static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008625 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008626{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008627 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008628 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008629 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008630
David Horstmann024e5c52023-06-14 15:48:21 +01008631 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008632 is_x1 = (stage->outputs < 1);
8633 } else {
8634 is_x1 = (stage->inputs < 1);
8635 }
8636
David Horstmann74a3d8c2023-06-14 18:28:19 +01008637 key_share_step = is_x1 ?
8638 PSA_JPAKE_X1_STEP_KEY_SHARE :
8639 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008640 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008641 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8642 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8643 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8644 } else {
8645 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008646 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008647 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008648}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008649#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008650
Przemek Stekiel2797d372022-12-22 11:19:22 +01008651static psa_status_t psa_pake_complete_inputs(
8652 psa_pake_operation_t *operation)
8653{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008655 /* Create copy of the inputs on stack as inputs share memory
8656 with the driver context which will be setup by the driver. */
8657 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008658
Przemek Stekielfde11282023-03-13 16:06:09 +01008659 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008660 return PSA_ERROR_BAD_STATE;
8661 }
8662
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008663 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008664 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8665 return PSA_ERROR_BAD_STATE;
8666 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008667 }
8668
Przemek Stekiel18620a32023-01-17 16:34:52 +01008669 /* Clear driver context */
8670 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8671
8672 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008673
8674 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008675 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008676
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008677 /* User and peer are translated to role. */
8678 mbedtls_free(inputs.user);
8679 mbedtls_free(inputs.peer);
8680
Przemek Stekiel2797d372022-12-22 11:19:22 +01008681 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008682#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008683 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008684 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008685 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008686#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008687 {
8688 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008689 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008690 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008691 return status;
8692}
8693
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008694#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008695static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008696 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008697 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008698 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008699{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008700 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8701 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8702 step != PSA_PAKE_STEP_ZK_PROOF) {
8703 return PSA_ERROR_INVALID_ARGUMENT;
8704 }
8705
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008706 psa_jpake_computation_stage_t *computation_stage =
8707 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008708
David Horstmann5da95602023-06-08 15:37:12 +01008709 if (computation_stage->round != PSA_JPAKE_FIRST &&
8710 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008711 return PSA_ERROR_BAD_STATE;
8712 }
8713
David Horstmanne7f21e62023-05-12 18:17:21 +01008714 /* Check that the step we are given is the one we were expecting */
8715 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008716 return PSA_ERROR_BAD_STATE;
8717 }
8718
David Horstmanne7f21e62023-05-12 18:17:21 +01008719 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8720 computation_stage->inputs == 0 &&
8721 computation_stage->outputs == 0) {
8722 /* Start of the round, so function decides whether we are inputting
8723 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008724 computation_stage->io_mode = io_mode;
8725 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008726 /* Middle of the round so the mode we are in must match the function
8727 * called by the user */
8728 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008729 }
8730
8731 return PSA_SUCCESS;
8732}
8733
David Horstmanne7f21e62023-05-12 18:17:21 +01008734static psa_status_t psa_jpake_epilogue(
8735 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008736 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008737{
David Horstmanne7f21e62023-05-12 18:17:21 +01008738 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008739 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008740
David Horstmanne7f21e62023-05-12 18:17:21 +01008741 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8742 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008743 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008744 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008745 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008746 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008747 }
8748 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008749 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008750 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008751 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008752 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008753 }
8754 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008755 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8756 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008757 /* End of a round, move to the next round */
8758 stage->inputs = 0;
8759 stage->outputs = 0;
8760 stage->round++;
8761 }
8762 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008763 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008764 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008765 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008766 return PSA_SUCCESS;
8767}
David Horstmanne7f21e62023-05-12 18:17:21 +01008768
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008769#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008770
Neil Armstronga7d08c32022-06-01 18:21:20 +02008771psa_status_t psa_pake_output(
8772 psa_pake_operation_t *operation,
8773 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008774 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008775 size_t output_size,
8776 size_t *output_length)
8777{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008779 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008780 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008781 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008782
8783 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008784 status = psa_pake_complete_inputs(operation);
8785 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008786 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008787 }
8788 }
8789
8790 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008791 status = PSA_ERROR_BAD_STATE;
8792 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008793 }
8794
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008795 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008796 status = PSA_ERROR_INVALID_ARGUMENT;
8797 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008798 }
8799
Przemek Stekiele12ed362022-12-21 12:54:46 +01008800 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008801#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008802 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008803 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008804 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008805 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008806 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008807 driver_step = convert_jpake_computation_stage_to_driver_step(
8808 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008809 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008810#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008811 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008812 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008813 status = PSA_ERROR_NOT_SUPPORTED;
8814 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008815 }
8816
David Horstmannc75639d2024-01-22 17:56:39 +00008817 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
8818
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008819 status = psa_driver_wrapper_pake_output(operation, driver_step,
8820 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008821
8822 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008823 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008824 }
8825
8826 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008827#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008828 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008829 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008830 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008831 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008832 }
8833 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008834#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008835 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008836 status = PSA_ERROR_NOT_SUPPORTED;
8837 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008838 }
8839
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008840exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008841 LOCAL_OUTPUT_FREE(output_external, output);
8842 if (status != PSA_SUCCESS) {
8843 psa_pake_abort(operation);
8844 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008845 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008846}
8847
8848psa_status_t psa_pake_input(
8849 psa_pake_operation_t *operation,
8850 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008851 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008852 size_t input_length)
8853{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008855 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008856 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8857 operation->primitive,
8858 step);
David Horstmannc75639d2024-01-22 17:56:39 +00008859 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008860
8861 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008862 status = psa_pake_complete_inputs(operation);
8863 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008864 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008865 }
8866 }
8867
8868 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008869 status = PSA_ERROR_BAD_STATE;
8870 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008871 }
8872
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008873 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008874 status = PSA_ERROR_INVALID_ARGUMENT;
8875 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008876 }
8877
Przemek Stekiele12ed362022-12-21 12:54:46 +01008878 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008879#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008880 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008881 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008882 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008883 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008884 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008885 driver_step = convert_jpake_computation_stage_to_driver_step(
8886 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008887 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008888#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008889 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008890 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008891 status = PSA_ERROR_NOT_SUPPORTED;
8892 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008893 }
8894
David Horstmannc75639d2024-01-22 17:56:39 +00008895 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008896 status = psa_driver_wrapper_pake_input(operation, driver_step,
8897 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008898
8899 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008900 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008901 }
8902
8903 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008904#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008905 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008906 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008907 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008908 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008909 }
8910 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008911#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008912 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008913 status = PSA_ERROR_NOT_SUPPORTED;
8914 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008915 }
8916
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008917exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008918 LOCAL_INPUT_FREE(input_external, input);
8919 if (status != PSA_SUCCESS) {
8920 psa_pake_abort(operation);
8921 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008922 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008923}
8924
8925psa_status_t psa_pake_get_implicit_key(
8926 psa_pake_operation_t *operation,
8927 psa_key_derivation_operation_t *output)
8928{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008930 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008931 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008932 size_t shared_key_len = 0;
8933
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008934 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008935 status = PSA_ERROR_BAD_STATE;
8936 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008937 }
8938
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008939#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008940 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008941 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008942 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008943 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008944 status = PSA_ERROR_BAD_STATE;
8945 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008946 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008947 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008948#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008949 {
8950 status = PSA_ERROR_NOT_SUPPORTED;
8951 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008952 }
8953
Przemek Stekiel0c781802022-11-29 14:53:13 +01008954 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8955 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008956 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008957 &shared_key_len);
8958
8959 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008960 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008961 }
8962
8963 status = psa_key_derivation_input_bytes(output,
8964 PSA_KEY_DERIVATION_INPUT_SECRET,
8965 shared_key,
8966 shared_key_len);
8967
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008968 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008969exit:
8970 abort_status = psa_pake_abort(operation);
8971 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008972}
8973
8974psa_status_t psa_pake_abort(
8975 psa_pake_operation_t *operation)
8976{
Przemek Stekield69dca92023-01-31 19:59:20 +01008977 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008978
Przemek Stekield69dca92023-01-31 19:59:20 +01008979 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008980 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008981 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008982
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008983 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8984 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008985 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008986 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008987 }
8988 if (operation->data.inputs.user != NULL) {
8989 mbedtls_free(operation->data.inputs.user);
8990 }
8991 if (operation->data.inputs.peer != NULL) {
8992 mbedtls_free(operation->data.inputs.peer);
8993 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008994 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008995 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008996
Przemek Stekield69dca92023-01-31 19:59:20 +01008997 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008998}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008999#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02009000
David Horstmann00d7a042023-12-07 18:39:17 +00009001/* Memory copying test hooks. These are called before input copy, after input
9002 * copy, before output copy and after output copy, respectively.
9003 * They are used by memory-poisoning tests to temporarily unpoison buffers
9004 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00009005#if defined(MBEDTLS_TEST_HOOKS)
9006void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9007void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
9008void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9009void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
9010#endif
David Horstmannfde97392023-10-30 20:29:43 +00009011
David Horstmann1b7279a2023-11-15 15:18:30 +00009012/** Copy from an input buffer to a local copy.
9013 *
9014 * \param[in] input Pointer to input buffer.
9015 * \param[in] input_len Length of the input buffer.
9016 * \param[out] input_copy Pointer to a local copy in which to store the input data.
9017 * \param[out] input_copy_len Length of the local copy buffer.
9018 * \return #PSA_SUCCESS, if the buffer was successfully
9019 * copied.
9020 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
9021 * copy is too small to hold contents of the
9022 * input buffer.
9023 */
9024MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00009025psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
9026 uint8_t *input_copy, size_t input_copy_len)
9027{
9028 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00009029 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00009030 }
9031
David Horstmann372b8bb2023-11-24 16:21:04 +00009032#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009033 if (psa_input_pre_copy_hook != NULL) {
9034 psa_input_pre_copy_hook(input, input_len);
9035 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009036#endif
9037
David Horstmann777e7412023-11-15 17:33:47 +00009038 if (input_len > 0) {
9039 memcpy(input_copy, input, input_len);
9040 }
David Horstmannfde97392023-10-30 20:29:43 +00009041
David Horstmann372b8bb2023-11-24 16:21:04 +00009042#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009043 if (psa_input_post_copy_hook != NULL) {
9044 psa_input_post_copy_hook(input, input_len);
9045 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009046#endif
9047
David Horstmannfde97392023-10-30 20:29:43 +00009048 return PSA_SUCCESS;
9049}
9050
David Horstmann1b7279a2023-11-15 15:18:30 +00009051/** Copy from a local output buffer into a user-supplied one.
9052 *
9053 * \param[in] output_copy Pointer to a local buffer containing the output.
9054 * \param[in] output_copy_len Length of the local buffer.
9055 * \param[out] output Pointer to user-supplied output buffer.
9056 * \param[out] output_len Length of the user-supplied output buffer.
9057 * \return #PSA_SUCCESS, if the buffer was successfully
9058 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00009059 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00009060 * user-supplied output buffer is too small to
9061 * hold the contents of the local buffer.
9062 */
9063MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00009064psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
9065 uint8_t *output, size_t output_len)
9066{
9067 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00009068 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00009069 }
David Horstmann777e7412023-11-15 17:33:47 +00009070
David Horstmann372b8bb2023-11-24 16:21:04 +00009071#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009072 if (psa_output_pre_copy_hook != NULL) {
9073 psa_output_pre_copy_hook(output, output_len);
9074 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009075#endif
9076
David Horstmann777e7412023-11-15 17:33:47 +00009077 if (output_copy_len > 0) {
9078 memcpy(output, output_copy, output_copy_len);
9079 }
9080
David Horstmann372b8bb2023-11-24 16:21:04 +00009081#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00009082 if (psa_output_post_copy_hook != NULL) {
9083 psa_output_post_copy_hook(output, output_len);
9084 }
David Horstmann372b8bb2023-11-24 16:21:04 +00009085#endif
9086
David Horstmann8978f5c2023-10-30 20:37:12 +00009087 return PSA_SUCCESS;
9088}
9089
David Horstmannf1734052023-11-20 17:15:32 +00009090psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
9091 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00009092{
9093 psa_status_t status;
9094
David Horstmann9db14482023-11-23 15:50:37 +00009095 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00009096
David Horstmannc5cc1c32023-11-15 18:11:26 +00009097 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00009098 return PSA_SUCCESS;
9099 }
9100
David Horstmannf1734052023-11-20 17:15:32 +00009101 local_input->buffer = mbedtls_calloc(input_len, 1);
9102 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00009103 /* Since we dealt with the zero-length case above, we know that
9104 * a NULL return value means a failure of allocation. */
9105 return PSA_ERROR_INSUFFICIENT_MEMORY;
9106 }
David Horstmannf1734052023-11-20 17:15:32 +00009107 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00009108
David Horstmannf1734052023-11-20 17:15:32 +00009109 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00009110
9111 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00009112 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00009113 if (status != PSA_SUCCESS) {
9114 goto error;
9115 }
9116
9117 return PSA_SUCCESS;
9118
9119error:
David Horstmannf1734052023-11-20 17:15:32 +00009120 mbedtls_free(local_input->buffer);
9121 local_input->buffer = NULL;
9122 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00009123 return status;
9124}
9125
David Horstmannf1734052023-11-20 17:15:32 +00009126void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00009127{
David Horstmannf1734052023-11-20 17:15:32 +00009128 mbedtls_free(local_input->buffer);
9129 local_input->buffer = NULL;
9130 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00009131}
9132
David Horstmann89875a42023-11-20 12:54:09 +00009133psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
9134 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00009135{
David Horstmann9db14482023-11-23 15:50:37 +00009136 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00009137
David Horstmannc5cc1c32023-11-15 18:11:26 +00009138 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009139 return PSA_SUCCESS;
9140 }
David Horstmann89875a42023-11-20 12:54:09 +00009141 local_output->buffer = mbedtls_calloc(output_len, 1);
9142 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00009143 /* Since we dealt with the zero-length case above, we know that
9144 * a NULL return value means a failure of allocation. */
9145 return PSA_ERROR_INSUFFICIENT_MEMORY;
9146 }
David Horstmann89875a42023-11-20 12:54:09 +00009147 local_output->length = output_len;
9148 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00009149
9150 return PSA_SUCCESS;
9151}
9152
David Horstmann89875a42023-11-20 12:54:09 +00009153psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00009154{
David Horstmannc335a4e2023-11-15 15:57:32 +00009155 psa_status_t status;
9156
David Horstmann89875a42023-11-20 12:54:09 +00009157 if (local_output->buffer == NULL) {
9158 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009159 return PSA_SUCCESS;
9160 }
David Horstmann89875a42023-11-20 12:54:09 +00009161 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00009162 /* We have an internal copy but nothing to copy back to. */
9163 return PSA_ERROR_CORRUPTION_DETECTED;
9164 }
9165
David Horstmann89875a42023-11-20 12:54:09 +00009166 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
9167 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00009168 if (status != PSA_SUCCESS) {
9169 return status;
9170 }
David Horstmann9467ea32023-11-08 17:44:18 +00009171
David Horstmann89875a42023-11-20 12:54:09 +00009172 mbedtls_free(local_output->buffer);
9173 local_output->buffer = NULL;
9174 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00009175
9176 return PSA_SUCCESS;
9177}
9178
Gilles Peskinee59236f2018-01-27 23:32:46 +01009179#endif /* MBEDTLS_PSA_CRYPTO_C */