blob: 9b22348c789bb0a01281b6f3bd6e5e721636aab0 [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
itayzafrir0adf0fc2018-09-06 16:24:41 +0300104#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (global_data.initialized == 0) \
106 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300107
David Horstmann513101b2023-11-29 17:24:08 +0000108#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann36df4b22023-12-13 15:55:25 +0000109
David Horstmann62a56d92023-12-14 18:12:47 +0000110/* Declare a local copy of an input buffer and a variable that will be used
111 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000112 *
113 * Note: This macro must be called before any operations which may jump to
114 * the exit label, so that the local input copy object is safe to be freed.
115 *
116 * Assumptions:
117 * - input is the name of a pointer to the buffer to be copied
118 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000119 * - input_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000120 */
David Horstmann62a56d92023-12-14 18:12:47 +0000121#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
122 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
123 const uint8_t *input_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000124
David Horstmann62a56d92023-12-14 18:12:47 +0000125/* Allocate a copy of the buffer input and set the pointer input_copy to
126 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000127 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000128 * Assumptions:
129 * - psa_status_t status exists
130 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000131 * - input is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000132 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000133 */
David Horstmann62a56d92023-12-14 18:12:47 +0000134#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000135 status = psa_crypto_local_input_alloc(input, length, \
136 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000137 if (status != PSA_SUCCESS) { \
138 goto exit; \
139 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000140 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000141
David Horstmann36df4b22023-12-13 15:55:25 +0000142/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
143 *
David Horstmann3e72db42023-12-08 14:08:18 +0000144 * Assumptions:
David Horstmann62a56d92023-12-14 18:12:47 +0000145 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann36df4b22023-12-13 15:55:25 +0000146 * - input is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000147 */
David Horstmann62a56d92023-12-14 18:12:47 +0000148#define LOCAL_INPUT_FREE(input, input_copy) \
149 input_copy = NULL; \
David Horstmann36df4b22023-12-13 15:55:25 +0000150 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmanne9a88ab2023-11-29 17:07:13 +0000151
David Horstmann62a56d92023-12-14 18:12:47 +0000152/* Declare a local copy of an output buffer and a variable that will be used
153 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000154 *
155 * Note: This macro must be called before any operations which may jump to
156 * the exit label, so that the local output copy object is safe to be freed.
157 *
158 * Assumptions:
159 * - output is the name of a pointer to the buffer to be copied
160 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000161 * - output_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000162 */
David Horstmann62a56d92023-12-14 18:12:47 +0000163#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
164 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
165 uint8_t *output_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000166
David Horstmann62a56d92023-12-14 18:12:47 +0000167/* Allocate a copy of the buffer output and set the pointer output_copy to
168 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000169 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000170 * Assumptions:
171 * - psa_status_t status exists
172 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000173 * - output is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000174 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000175 */
David Horstmann62a56d92023-12-14 18:12:47 +0000176#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000177 status = psa_crypto_local_output_alloc(output, length, \
178 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000179 if (status != PSA_SUCCESS) { \
180 goto exit; \
181 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000182 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000183
Gabor Mezei1882c512024-01-24 13:07:17 +0100184/* Allocate a copy of the buffer output and set the pointer output_copy to
185 * point to the start of the copy.
186 *
187 * Assumptions:
188 * - psa_status_t status exists
189 * - An exit label is declared
190 * - output is the name of a pointer to the buffer to be copied
191 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
192 */
193#define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \
194 status = psa_crypto_local_output_alloc_with_copy(output, length, \
195 &LOCAL_OUTPUT_COPY_OF_##output); \
196 if (status != PSA_SUCCESS) { \
197 goto exit; \
198 } \
199 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
200
David Horstmann62a56d92023-12-14 18:12:47 +0000201/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann3e72db42023-12-08 14:08:18 +0000202 * after first copying back its contents to the original buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000203 *
David Horstmann3e72db42023-12-08 14:08:18 +0000204 * Assumptions:
David Horstmann3e72db42023-12-08 14:08:18 +0000205 * - psa_status_t status exists
David Horstmann62a56d92023-12-14 18:12:47 +0000206 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
207 * - output is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000208 */
David Horstmann62a56d92023-12-14 18:12:47 +0000209#define LOCAL_OUTPUT_FREE(output, output_copy) \
210 output_copy = NULL; \
David Horstmann5a945f52023-12-13 14:09:08 +0000211 do { \
212 psa_status_t local_output_status; \
David Horstmann36df4b22023-12-13 15:55:25 +0000213 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann5a945f52023-12-13 14:09:08 +0000214 if (local_output_status != PSA_SUCCESS) { \
215 /* Since this error case is an internal error, it's more serious than \
216 * any existing error code and so it's fine to overwrite the existing \
217 * status. */ \
218 status = local_output_status; \
219 } \
220 } while (0)
David Horstmann513101b2023-11-29 17:24:08 +0000221#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann62a56d92023-12-14 18:12:47 +0000222#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
223 const uint8_t *input_copy_name = NULL;
224#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
225 input_copy = input;
226#define LOCAL_INPUT_FREE(input, input_copy) \
227 input_copy = NULL;
228#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
229 uint8_t *output_copy_name = NULL;
230#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
231 output_copy = output;
Gabor Mezei4892d752024-01-29 17:27:44 +0100232#define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \
233 output_copy = output;
David Horstmann62a56d92023-12-14 18:12:47 +0000234#define LOCAL_OUTPUT_FREE(output, output_copy) \
235 output_copy = NULL;
David Horstmann513101b2023-11-29 17:24:08 +0000236#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000237
238
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100239int psa_can_do_hash(psa_algorithm_t hash_alg)
240{
241 (void) hash_alg;
242 return global_data.drivers_initialized;
243}
Valerio Settic6f004f2023-12-12 11:27:36 +0100244
Valerio Setti1fff4f22023-12-28 14:19:34 +0100245int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100246{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100247 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100248 (void) cipher_alg;
249 return global_data.drivers_initialized;
250}
251
252
Valerio Settia55f0422023-07-10 15:34:41 +0200253#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200254 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200255 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200256static int psa_is_dh_key_size_valid(size_t bits)
257{
Valerio Setti504a1022024-01-17 15:19:30 +0100258 switch (bits) {
259#if defined(PSA_WANT_DH_RFC7919_2048)
260 case 2048:
261 return 1;
262#endif /* PSA_WANT_DH_RFC7919_2048 */
263#if defined(PSA_WANT_DH_RFC7919_3072)
264 case 3072:
265 return 1;
266#endif /* PSA_WANT_DH_RFC7919_3072 */
267#if defined(PSA_WANT_DH_RFC7919_4096)
268 case 4096:
269 return 1;
270#endif /* PSA_WANT_DH_RFC7919_4096 */
271#if defined(PSA_WANT_DH_RFC7919_6144)
272 case 6144:
273 return 1;
274#endif /* PSA_WANT_DH_RFC7919_6144 */
275#if defined(PSA_WANT_DH_RFC7919_8192)
276 case 8192:
277 return 1;
278#endif /* PSA_WANT_DH_RFC7919_8192 */
279 default:
280 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200281 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200282}
Valerio Settia55f0422023-07-10 15:34:41 +0200283#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200284 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200285 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100288{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100289 /* Mbed TLS error codes can combine a high-level error code and a
290 * low-level error code. The low-level error usually reflects the
291 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 int low_level_ret = -(-ret & 0x007f);
293 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100294 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100297#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
299 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100301 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
302 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100303#endif
304
305#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200306 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
307 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
308 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
309 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
310 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200312 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200314 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100316#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200317
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100318#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000319 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100323
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100324#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100329#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100330
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100331#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200332 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100334#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200335
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100336#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200337 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200339 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100341#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200342
Dave Rodgman509b5672023-08-16 19:26:23 +0100343#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100358#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
361 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100362 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
363 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100364 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100366 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
367 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100371#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100372
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100373#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100374 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100376#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100377
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
379 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
380 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100382
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100383#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100384 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200386 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100388 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100390#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100391
Gilles Peskine82e57d12020-11-13 21:31:17 +0100392#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100394 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
395 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100396 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100398 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
399 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100401 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100403#endif
404
Dave Rodgmandea266f2023-08-31 11:52:43 +0100405#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100406 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100408 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100410 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100412#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100413 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100415#endif
416#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100417
Dave Rodgman509b5672023-08-16 19:26:23 +0100418#if defined(MBEDTLS_BIGNUM_C)
419#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100420 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100422#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100423 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100425 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100427 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100429 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100431 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100433 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100435 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100437#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100438
Dave Rodgman509b5672023-08-16 19:26:23 +0100439#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
443 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100445#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
446 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100449#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100450 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
451 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
456 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100458 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 case MBEDTLS_ERR_PK_INVALID_ALG:
461 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
462 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200466 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100468#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100469
Gilles Peskineff2d2002019-05-06 15:26:23 +0200470 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200472 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200474
Dave Rodgman509b5672023-08-16 19:26:23 +0100475#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100476 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100478 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100480 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100482 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100484 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
485 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100487 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100489 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100491 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100493#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200494
Dave Rodgman1dab4452023-09-01 09:59:51 +0100495#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300496 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300497 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300499 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300501 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300503 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
504 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300506 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100508 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100510
Dave Rodgman509b5672023-08-16 19:26:23 +0100511#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000512 case MBEDTLS_ERR_ECP_IN_PROGRESS:
513 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100514#endif
515#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000516
Janos Follatha13b9052019-11-22 12:48:59 +0000517 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300519
Gilles Peskinee59236f2018-01-27 23:32:46 +0100520 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100522 }
523}
524
Paul Elliottdc42ca82023-02-24 18:11:59 +0000525/**
526 * \brief For output buffers which contain "tags"
527 * (outputs that may be checked for validity like
528 * hashes, MACs and signatures), fill the unused
529 * part of the output buffer (the whole buffer on
530 * error, the trailing part on success) with
531 * something that isn't a valid tag (barring an
532 * attack on the tag and deliberately-crafted
533 * input), in case the caller doesn't check the
534 * return status properly.
535 *
536 * \param output_buffer Pointer to buffer to wipe. May not be NULL
537 * unless \p output_buffer_size is zero.
538 * \param status Status of function called to generate
539 * output_buffer originally
540 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
541 * could be NULL.
542 * \param output_buffer_length Length of data written to output_buffer, must be
543 * less than \p output_buffer_size
544 */
545static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000546 size_t output_buffer_size, size_t output_buffer_length)
547{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000548 size_t offset = 0;
549
550 if (output_buffer_size == 0) {
551 /* If output_buffer_size is 0 then we have nothing to do. We must not
552 call memset because output_buffer may be NULL in this case */
553 return;
554 }
555
556 if (status == PSA_SUCCESS) {
557 offset = output_buffer_length;
558 }
559
560 memset(output_buffer + offset, '!', output_buffer_size - offset);
561}
562
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
565 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200566{
567 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200569 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200570 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200571 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200572 case PSA_KEY_TYPE_PASSWORD:
573 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200574 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100575#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200576 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (bits != 128 && bits != 192 && bits != 256) {
578 return PSA_ERROR_INVALID_ARGUMENT;
579 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200580 break;
581#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200582#if defined(PSA_WANT_KEY_TYPE_ARIA)
583 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (bits != 128 && bits != 192 && bits != 256) {
585 return PSA_ERROR_INVALID_ARGUMENT;
586 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200587 break;
588#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100589#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200590 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (bits != 128 && bits != 192 && bits != 256) {
592 return PSA_ERROR_INVALID_ARGUMENT;
593 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200594 break;
595#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100596#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200597 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (bits != 64 && bits != 128 && bits != 192) {
599 return PSA_ERROR_INVALID_ARGUMENT;
600 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200601 break;
602#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100603#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200604 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (bits != 256) {
606 return PSA_ERROR_INVALID_ARGUMENT;
607 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200608 break;
609#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (bits % 8 != 0) {
614 return PSA_ERROR_INVALID_ARGUMENT;
615 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200618}
619
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100620/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100621 *
Steven Cooremancd640932021-03-08 12:00:27 +0100622 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
623 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100624 *
625 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100626 * \param[in] key_type The key type of the key to be used with the
627 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628 *
629 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100630 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100631 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100632 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100633 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100634MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100635 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100637{
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (PSA_ALG_IS_HMAC(algorithm)) {
639 if (key_type == PSA_KEY_TYPE_HMAC) {
640 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100641 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100642 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
645 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
646 * key. */
647 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
648 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
649 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
650 * the block length (larger than 1) for block ciphers. */
651 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
652 return PSA_SUCCESS;
653 }
654 }
655 }
656
657 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100658}
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
661 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200662{
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (slot->key.data != NULL) {
664 return PSA_ERROR_ALREADY_EXISTS;
665 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 slot->key.data = mbedtls_calloc(1, buffer_length);
668 if (slot->key.data == NULL) {
669 return PSA_ERROR_INSUFFICIENT_MEMORY;
670 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200671
Ronald Cronea0f8a62020-11-25 17:52:23 +0100672 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200674}
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
677 const uint8_t *data,
678 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200679{
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 psa_status_t status = psa_allocate_buffer_to_slot(slot,
681 data_length);
682 if (status != PSA_SUCCESS) {
683 return status;
684 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 memcpy(slot->key.data, data, data_length);
687 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200688}
689
Ronald Crona0fe59f2020-11-29 11:14:53 +0100690psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100691 const psa_key_attributes_t *attributes,
692 const uint8_t *data, size_t data_length,
693 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100697 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200699 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (data_length == 0) {
701 return PSA_ERROR_NOT_SUPPORTED;
702 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (key_type_is_raw_bytes(type)) {
705 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200706
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100707 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 *bits);
709 if (status != PSA_SUCCESS) {
710 return status;
711 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200712
Ronald Crondd04d422020-11-28 15:14:42 +0100713 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100715 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return PSA_SUCCESS;
719 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200720#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200721 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100722 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200723 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100724 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100725 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200726 return mbedtls_psa_ffdh_import_key(attributes,
727 data, data_length,
728 key_buffer, key_buffer_size,
729 key_buffer_length,
730 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100731 }
Valerio Settia55f0422023-07-10 15:34:41 +0200732#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200733 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200734#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
736 if (PSA_KEY_TYPE_IS_ECC(type)) {
737 return mbedtls_psa_ecp_import_key(attributes,
738 data, data_length,
739 key_buffer, key_buffer_size,
740 key_buffer_length,
741 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100742 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200743#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800744 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200745#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
746 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
748 if (PSA_KEY_TYPE_IS_RSA(type)) {
749 return mbedtls_psa_rsa_import_key(attributes,
750 data, data_length,
751 key_buffer, key_buffer_size,
752 key_buffer_length,
753 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200754 }
Valerio Settife478902023-07-25 12:27:19 +0200755#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
756 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800757 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100758 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100761}
762
Gilles Peskinef603c712019-01-19 13:40:11 +0100763/** Calculate the intersection of two algorithm usage policies.
764 *
765 * Return 0 (which allows no operation) on incompatibility.
766 */
767static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100768 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100769 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100771{
Gilles Peskine549ea862019-05-22 11:45:59 +0200772 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (alg1 == alg2) {
774 return alg1;
775 }
Steven Cooreman03488022021-02-18 12:07:20 +0100776 /* If the policies are from the same hash-and-sign family, check
777 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
779 PSA_ALG_IS_SIGN_HASH(alg2) &&
780 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
781 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
782 return alg2;
783 }
784 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
785 return alg1;
786 }
Steven Cooreman03488022021-02-18 12:07:20 +0100787 }
788 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100789 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100790 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
792 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
793 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
794 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
795 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100796 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100797
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100798 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
800 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
801 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
802 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100803 }
804 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
806 (alg1_len <= alg2_len)) {
807 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100808 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
810 (alg2_len <= alg1_len)) {
811 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100812 }
813 }
814 /* If the policies are from the same MAC family, check whether one
815 * of them is a minimum-MAC-length policy. Calculate the most
816 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
818 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
819 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100820 /* Validate the combination of key type and algorithm. Since the base
821 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
823 return 0;
824 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100825
Steven Cooreman31a876d2021-03-03 20:47:40 +0100826 /* Get the (exact or at-least) output lengths for both sides of the
827 * requested intersection. None of the currently supported algorithms
828 * have an output length dependent on the actual key size, so setting it
829 * to a bogus value of 0 is currently OK.
830 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831 * Note that for at-least-this-length wildcard algorithms, the output
832 * length is set to the shortest allowed length, which allows us to
833 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
835 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100836 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837
Steven Cooremana1d83222021-02-25 10:20:29 +0100838 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
840 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
841 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100842 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100843
844 /* If only one is an at-least-this-length policy, the intersection would
845 * be the other (fixed-length) policy as long as said fixed length is
846 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
848 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100849 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
851 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100852 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100853
Steven Cooremancd640932021-03-08 12:00:27 +0100854 /* If none of them are wildcards, check whether they define the same tag
855 * length. This is still possible here when one is default-length and
856 * the other specific-length. Ensure to always return the
857 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (alg1_len == alg2_len) {
859 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
860 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100861 }
862 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100864}
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866static int psa_key_algorithm_permits(psa_key_type_t key_type,
867 psa_algorithm_t policy_alg,
868 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200869{
Gilles Peskine549ea862019-05-22 11:45:59 +0200870 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (requested_alg == policy_alg) {
872 return 1;
873 }
Steven Cooreman03488022021-02-18 12:07:20 +0100874 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
875 * and requested_alg is the same hash-and-sign family with any hash,
876 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
878 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
879 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
880 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100881 }
882 /* If policy_alg is a wildcard AEAD algorithm of the same base as
883 * the requested algorithm, check the requested tag length to be
884 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (PSA_ALG_IS_AEAD(policy_alg) &&
886 PSA_ALG_IS_AEAD(requested_alg) &&
887 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
888 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
889 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
890 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
891 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100892 }
Steven Cooremancd640932021-03-08 12:00:27 +0100893 /* If policy_alg is a MAC algorithm of the same base as the requested
894 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (PSA_ALG_IS_MAC(policy_alg) &&
896 PSA_ALG_IS_MAC(requested_alg) &&
897 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
898 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100899 /* Validate the combination of key type and algorithm. Since the policy
900 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
902 return 0;
903 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100904
Steven Cooreman31a876d2021-03-03 20:47:40 +0100905 /* Get both the requested output length for the algorithm which is to be
906 * verified, and the default output length for the base algorithm.
907 * Note that none of the currently supported algorithms have an output
908 * length dependent on actual key size, so setting it to a bogus value
909 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100910 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100912 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 key_type, 0,
914 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100915
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100916 /* If the policy is default-length, only allow an algorithm with
917 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
919 return requested_output_length == default_output_length;
920 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100921
922 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100923 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
925 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
926 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100927 }
928
Steven Cooremancd640932021-03-08 12:00:27 +0100929 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
930 * check for the requested MAC length to be equal to or longer than the
931 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
933 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
934 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100935 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200936 }
Steven Cooremance48e852020-10-05 16:02:45 +0200937 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200938 * a key derivation with that key agreement should also be allowed. This
939 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
941 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
942 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
943 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200944 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200947}
948
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100949/** Test whether a policy permits an algorithm.
950 *
951 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100952 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100953 * \note This function requires providing the key type for which the policy is
954 * being validated, since some algorithm policy definitions (e.g. MAC)
955 * have different properties depending on what kind of cipher it is
956 * combined with.
957 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100958 * \retval PSA_SUCCESS When \p alg is a specific algorithm
959 * allowed by the \p policy.
960 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
961 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
962 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
965 psa_key_type_t key_type,
966 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100967{
Steven Cooremand788fab2021-02-25 11:29:17 +0100968 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (alg == 0) {
970 return PSA_ERROR_INVALID_ARGUMENT;
971 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100972
Steven Cooreman7e39f052021-02-23 14:18:32 +0100973 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (PSA_ALG_IS_WILDCARD(alg)) {
975 return PSA_ERROR_INVALID_ARGUMENT;
976 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
979 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
980 return PSA_SUCCESS;
981 } else {
982 return PSA_ERROR_NOT_PERMITTED;
983 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984}
985
Gilles Peskinef603c712019-01-19 13:40:11 +0100986/** Restrict a key policy based on a constraint.
987 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100988 * \note This function requires providing the key type for which the policy is
989 * being restricted, since some algorithm policy definitions (e.g. MAC)
990 * have different properties depending on what kind of cipher it is
991 * combined with.
992 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100993 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100994 * \param[in,out] policy The policy to restrict.
995 * \param[in] constraint The policy constraint to apply.
996 *
997 * \retval #PSA_SUCCESS
998 * \c *policy contains the intersection of the original value of
999 * \c *policy and \c *constraint.
1000 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001001 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001002 * \c *policy is unchanged.
1003 */
1004static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001005 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001006 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001008{
1009 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1011 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001012 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1014 constraint->alg2);
1015 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1016 return PSA_ERROR_INVALID_ARGUMENT;
1017 }
1018 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1019 return PSA_ERROR_INVALID_ARGUMENT;
1020 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001021 policy->usage &= constraint->usage;
1022 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001023 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001025}
1026
Przemek Stekiel061f6942022-11-30 10:51:35 +01001027/** Get the description of a key given its identifier and policy constraints
1028 * and lock it.
1029 *
1030 * The key must have allow all the usage flags set in \p usage. If \p alg is
1031 * nonzero, the key must allow operations with this algorithm. If \p alg is
1032 * zero, the algorithm is not checked.
1033 *
1034 * In case of a persistent key, the function loads the description of the key
1035 * into a key slot if not already done.
1036 *
Ryan Everett098c6652024-01-03 13:03:36 +00001037 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001038 * It is the responsibility of the caller to then unregister
1039 * once they have finished reading the contents of the slot.
1040 * The caller unregisters by calling psa_unregister_read() or
1041 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1042 * if and only if the caller already holds the global key slot mutex
1043 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1044 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +01001045 */
1046static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001047 mbedtls_svc_key_id_t key,
1048 psa_key_slot_t **p_slot,
1049 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001051{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001053 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 status = psa_get_and_lock_key_slot(key, p_slot);
1056 if (status != PSA_SUCCESS) {
1057 return status;
1058 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001059 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001060
1061 /* Enforce that usage policy for the key slot contains all the flags
1062 * required by the usage parameter. There is one exception: public
1063 * keys can always be exported, so we treat public key objects as
1064 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001066 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001070 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001071 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001072 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001073
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001074 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (alg != 0) {
1076 status = psa_key_policy_permits(&slot->attr.policy,
1077 slot->attr.type,
1078 alg);
1079 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001080 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001082 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001085
1086error:
1087 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001088 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001091}
Darryl Green940d72c2018-07-13 13:18:51 +01001092
Ronald Cron5c522922020-11-14 16:35:34 +01001093/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001094 *
1095 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001096 * available, as opposed to a key in a secure element and/or to be used
1097 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001098 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001099 * This is a temporary function that may be used instead of
1100 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1101 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001102 *
Ryan Everett098c6652024-01-03 13:03:36 +00001103 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001104 * It is the responsibility of the caller to then unregister
1105 * once they have finished reading the contents of the slot.
1106 * The caller unregisters by calling psa_unregister_read() or
1107 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1108 * if and only if the caller already holds the global key slot mutex
1109 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1110 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001111 */
Ronald Cron5c522922020-11-14 16:35:34 +01001112static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1113 mbedtls_svc_key_id_t key,
1114 psa_key_slot_t **p_slot,
1115 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001117{
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1119 usage, alg);
1120 if (status != PSA_SUCCESS) {
1121 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001125 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 *p_slot = NULL;
1127 return PSA_ERROR_NOT_SUPPORTED;
1128 }
1129
1130 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001131}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001134{
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001136 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001138
Ronald Cronea0f8a62020-11-25 17:52:23 +01001139 slot->key.data = NULL;
1140 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001143}
1144
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001145/** Completely wipe a slot in memory, including its policy.
1146 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001148{
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 /*
1152 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001153 * do our best to report an unexpected amount of registered readers or
1154 * an unexpected state.
1155 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1156 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1158 * function is called as part of the execution of a test suite, the
1159 * execution of the test suite is stopped in error if the assertion fails.
1160 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001161 switch (slot->state) {
1162 case PSA_SLOT_FULL:
1163 /* In this state psa_wipe_key_slot() must only be called if the
1164 * caller is the last reader. */
1165 case PSA_SLOT_PENDING_DELETION:
1166 /* In this state psa_wipe_key_slot() must only be called if the
1167 * caller is the last reader. */
1168 if (slot->registered_readers != 1) {
1169 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1170 status = PSA_ERROR_CORRUPTION_DETECTED;
1171 }
1172 break;
1173 case PSA_SLOT_FILLING:
1174 /* In this state registered_readers must be 0. */
1175 if (slot->registered_readers != 0) {
1176 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1177 status = PSA_ERROR_CORRUPTION_DETECTED;
1178 }
1179 break;
1180 case PSA_SLOT_EMPTY:
1181 /* The slot is already empty, it cannot be wiped. */
1182 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1183 status = PSA_ERROR_CORRUPTION_DETECTED;
1184 break;
1185 default:
1186 /* The slot's state is invalid. */
1187 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001188 }
1189
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001190 /* Multipart operations may still be using the key. This is safe
1191 * because all multipart operation objects are independent from
1192 * the key slot: if they need to access the key after the setup
1193 * phase, they have a copy of the key. Note that this means that
1194 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001195 /* At this point, key material and other type-specific content has
1196 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001197 * zeroize because the metadata is not particularly sensitive.
1198 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 memset(slot, 0, sizeof(*slot));
1200 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001201}
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001205 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001206 psa_status_t status; /* status of the last operation */
1207 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001208#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1209 psa_se_drv_table_entry_t *driver;
1210#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (mbedtls_svc_key_id_is_null(key)) {
1213 return PSA_SUCCESS;
1214 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001215
Ronald Cronf2911112020-10-29 17:51:10 +01001216 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001217 * Get the description of the key in a key slot, and register to read it.
1218 * In the case of a persistent key, this will load the key description
1219 * from persistent memory if not done yet.
1220 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001221 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001222 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 status = psa_get_and_lock_key_slot(key, &slot);
1224 if (status != PSA_SUCCESS) {
1225 return status;
1226 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001227
Ryan Everettc053d962024-01-25 17:56:32 +00001228#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001229 /* We cannot unlock between setting the state to PENDING_DELETION
1230 * and destroying the key in storage, as otherwise another thread
1231 * could load the key into a new slot and the key will not be
1232 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001233 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1234 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001235
1236 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1237 /* Another thread has destroyed the key between us locking the slot
1238 * and us gaining the mutex. Unregister from the slot,
1239 * and report that the key does not exist. */
1240 status = psa_unregister_read(slot);
1241
1242 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1243 &mbedtls_threading_key_slot_mutex));
1244 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1245 }
Ryan Everettc053d962024-01-25 17:56:32 +00001246#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001247 /* Set the key slot containing the key description's state to
1248 * PENDING_DELETION. This stops new operations from registering
1249 * to read the slot. Current readers can safely continue to access
1250 * the key within the slot; the last registered reader will
1251 * automatically wipe the slot when they call psa_unregister_read().
1252 * If the key is persistent, we can now delete the copy of the key
1253 * from memory. If the key is opaque, we require the driver to
1254 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001255 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1256 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001257
Ryan Everettd868b742024-03-08 18:35:09 +00001258 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001259 goto exit;
1260 }
Ronald Cronf2911112020-10-29 17:51:10 +01001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001263 /* Refuse the destruction of a read-only key (which may or may not work
1264 * if we attempt it, depending on whether the key is merely read-only
1265 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001266 * Just do the best we can, which is to wipe the copy in memory
1267 * (done in this function's cleanup code). */
1268 overall_status = PSA_ERROR_NOT_PERMITTED;
1269 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001270 }
1271
Gilles Peskine354f7672019-07-12 23:46:38 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1274 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001275 /* For a key in a secure element, we need to do three things:
1276 * remove the key file in internal storage, destroy the
1277 * key inside the secure element, and update the driver's
1278 * persistent data. Start a transaction that will encompass these
1279 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001283 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 status = psa_crypto_save_transaction();
1285 if (status != PSA_SUCCESS) {
1286 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287 /* We should still try to destroy the key in the secure
1288 * element and the key metadata in storage. This is especially
1289 * important if the error is that the storage is full.
1290 * But how to do it exactly without risking an inconsistent
1291 * state after a reset?
1292 * https://github.com/ARMmbed/mbed-crypto/issues/215
1293 */
1294 overall_status = status;
1295 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001296 }
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 status = psa_destroy_se_key(driver,
1299 psa_key_slot_get_slot_number(slot));
1300 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001303 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001304#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1305
Darryl Greend49a4992018-06-18 17:27:26 +01001306#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001308 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001309 * The slot will still hold a copy of the key until the last reader
1310 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 status = psa_destroy_persistent_key(slot->attr.id);
1312 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001313 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 }
Darryl Greend49a4992018-06-18 17:27:26 +01001315 }
1316#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001317
Gilles Peskinefc762652019-07-22 19:30:34 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (driver != NULL) {
1320 status = psa_save_se_persistent_data(driver);
1321 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001322 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 }
1324 status = psa_crypto_stop_transaction();
1325 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001326 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001328 }
1329#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1330
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001331exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001332 /* Unregister from reading the slot. If we are the last active reader
1333 * then this will wipe the slot. */
1334 status = psa_unregister_read(slot);
1335 /* Prioritize CORRUPTION_DETECTED from unregistering over
1336 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001338 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 }
Ryan Everettc053d962024-01-25 17:56:32 +00001340
1341#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001342 /* Don't overwrite existing errors if the unlock fails. */
1343 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001344 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1345 &mbedtls_threading_key_slot_mutex));
1346#endif
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349}
1350
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001351/** Retrieve all the publicly-accessible attributes of a key.
1352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1354 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001357 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1362 if (status != PSA_SUCCESS) {
1363 return status;
1364 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001365
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001366 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001367
1368#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1370 psa_set_key_slot_number(attributes,
1371 psa_key_slot_get_slot_number(slot));
1372 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001374
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001375 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001376}
1377
Gilles Peskinec8000c02019-08-02 20:15:51 +02001378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1379psa_status_t psa_get_key_slot_number(
1380 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382{
Gilles Peskine972539c2024-02-28 01:49:45 +01001383 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001384 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return PSA_SUCCESS;
1386 } else {
1387 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001388 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001389}
1390#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1393 size_t key_buffer_size,
1394 uint8_t *data,
1395 size_t data_size,
1396 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001397{
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (key_buffer_size > data_size) {
1399 return PSA_ERROR_BUFFER_TOO_SMALL;
1400 }
1401 memcpy(data, key_buffer, key_buffer_size);
1402 memset(data + key_buffer_size, 0,
1403 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001404 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001406}
1407
Ronald Cron7285cda2020-11-26 14:46:37 +01001408psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 const psa_key_attributes_t *attributes,
1410 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001412{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001413 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (key_type_is_raw_bytes(type) ||
1416 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001417 PSA_KEY_TYPE_IS_ECC(type) ||
1418 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return psa_export_key_buffer_internal(
1420 key_buffer, key_buffer_size,
1421 data, data_size, data_length);
1422 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001423 /* This shouldn't happen in the reference implementation, but
1424 it is valid for a special-purpose implementation to omit
1425 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001427 }
1428}
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001431 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 size_t data_size,
1433 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001434{
1435 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1436 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1437 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001438 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439
Ronald Crone907e552021-01-18 13:22:38 +01001440 /* Reject a zero-length output buffer now, since this can never be a
1441 * valid key representation. This way we know that data must be a valid
1442 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (data_size == 0) {
1444 return PSA_ERROR_BUFFER_TOO_SMALL;
1445 }
Ronald Crone907e552021-01-18 13:22:38 +01001446
Ronald Cron9486f9d2020-11-26 13:36:23 +01001447 /* Set the key to empty now, so that even when there are errors, we always
1448 * set data_length to a value between 0 and data_size. On error, setting
1449 * the key to empty is a good choice because an empty key representation is
1450 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001451 *data_length = 0;
1452
Ronald Cron9486f9d2020-11-26 13:36:23 +01001453 /* Export requires the EXPORT flag. There is an exception for public keys,
1454 * which don't require any flag, but
1455 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1456 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1458 PSA_KEY_USAGE_EXPORT, 0);
1459 if (status != PSA_SUCCESS) {
1460 return status;
1461 }
mohammad160306e79202018-03-28 13:17:44 +03001462
Ryan Everett45ac5262024-01-08 17:15:19 +00001463 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1464
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001465 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 slot->key.data, slot->key.bytes,
1467 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001468
Ryan Everett35f68532024-01-25 12:02:03 +00001469#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001470exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001471#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001472 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001473
Ryan Everett45ac5262024-01-08 17:15:19 +00001474 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476}
1477
Ronald Cron7285cda2020-11-26 14:46:37 +01001478psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001479 const psa_key_attributes_t *attributes,
1480 const uint8_t *key_buffer,
1481 size_t key_buffer_size,
1482 uint8_t *data,
1483 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001485{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001486 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001487
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001488 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001489 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1490 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001491 /* Exporting public -> public */
1492 return psa_export_key_buffer_internal(
1493 key_buffer, key_buffer_size,
1494 data, data_size, data_length);
1495 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001496#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001497 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1498 return mbedtls_psa_rsa_export_public_key(attributes,
1499 key_buffer,
1500 key_buffer_size,
1501 data,
1502 data_size,
1503 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001504#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001505 /* We don't know how to convert a private RSA key to public. */
1506 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001507#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001508 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001509 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001511 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1512 return mbedtls_psa_ecp_export_public_key(attributes,
1513 key_buffer,
1514 key_buffer_size,
1515 data,
1516 data_size,
1517 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001518#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001519 /* We don't know how to convert a private ECC key to public */
1520 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001521#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001522 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001523 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001524#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001525 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001526 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001527 key_buffer,
1528 key_buffer_size,
1529 data, data_size,
1530 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001531#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001532 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001533#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001534 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001536 (void) key_buffer;
1537 (void) key_buffer_size;
1538 (void) data;
1539 (void) data_size;
1540 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001542 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543}
Ronald Crond18b5f82020-11-25 16:46:05 +01001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001546 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 size_t data_size,
1548 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001549{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001550 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001551 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001552 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001553
Ryan Everettb1d2c672024-01-08 17:19:30 +00001554 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001555
Ronald Crone907e552021-01-18 13:22:38 +01001556 /* Reject a zero-length output buffer now, since this can never be a
1557 * valid key representation. This way we know that data must be a valid
1558 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (data_size == 0) {
1560 return PSA_ERROR_BUFFER_TOO_SMALL;
1561 }
Ronald Crone907e552021-01-18 13:22:38 +01001562
Darryl Greendd8fb772018-11-07 16:00:44 +00001563 /* Set the key to empty now, so that even when there are errors, we always
1564 * set data_length to a value between 0 and data_size. On error, setting
1565 * the key to empty is a good choice because an empty key representation is
1566 * unlikely to be accepted anywhere. */
1567 *data_length = 0;
1568
1569 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1571 if (status != PSA_SUCCESS) {
1572 return status;
1573 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001574
Ryan Everettb1d2c672024-01-08 17:19:30 +00001575 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1578 status = PSA_ERROR_INVALID_ARGUMENT;
1579 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001580 }
1581
Ronald Cron67227982020-11-26 15:16:05 +01001582 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001583 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001585
1586exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001587 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001588
Ryan Everettb1d2c672024-01-08 17:19:30 +00001589 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001591}
1592
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001593/** Validate that a key policy is internally well-formed.
1594 *
1595 * This function only rejects invalid policies. It does not validate the
1596 * consistency of the policy with respect to other attributes of the key
1597 * such as the key type.
1598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001599static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001600{
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1602 PSA_KEY_USAGE_COPY |
1603 PSA_KEY_USAGE_ENCRYPT |
1604 PSA_KEY_USAGE_DECRYPT |
1605 PSA_KEY_USAGE_SIGN_MESSAGE |
1606 PSA_KEY_USAGE_VERIFY_MESSAGE |
1607 PSA_KEY_USAGE_SIGN_HASH |
1608 PSA_KEY_USAGE_VERIFY_HASH |
1609 PSA_KEY_USAGE_VERIFY_DERIVATION |
1610 PSA_KEY_USAGE_DERIVE)) != 0) {
1611 return PSA_ERROR_INVALID_ARGUMENT;
1612 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001615}
1616
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001617/** Validate the internal consistency of key attributes.
1618 *
1619 * This function only rejects invalid attribute values. If does not
1620 * validate the consistency of the attributes with any key data that may
1621 * be involved in the creation of the key.
1622 *
1623 * Call this function early in the key creation process.
1624 *
1625 * \param[in] attributes Key attributes for the new key.
1626 * \param[out] p_drv On any return, the driver for the key, if any.
1627 * NULL for a transparent key.
1628 *
1629 */
1630static psa_status_t psa_validate_key_attributes(
1631 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001633{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001634 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1636 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 status = psa_validate_key_location(lifetime, p_drv);
1639 if (status != PSA_SUCCESS) {
1640 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001641 }
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 status = psa_validate_key_persistence(lifetime);
1644 if (status != PSA_SUCCESS) {
1645 return status;
1646 }
1647
1648 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1649 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1650 return PSA_ERROR_INVALID_ARGUMENT;
1651 }
1652 } else {
1653 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1654 return PSA_ERROR_INVALID_ARGUMENT;
1655 }
1656 }
1657
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001658 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (status != PSA_SUCCESS) {
1660 return status;
1661 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001662
1663 /* Refuse to create overly large keys.
1664 * Note that this doesn't trigger on import if the attributes don't
1665 * explicitly specify a size (so psa_get_key_bits returns 0), so
1666 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1668 return PSA_ERROR_NOT_SUPPORTED;
1669 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001672}
1673
Gilles Peskine4747d192019-04-17 15:05:45 +02001674/** Prepare a key slot to receive key material.
1675 *
1676 * This function allocates a key slot and sets its metadata.
1677 *
1678 * If this function fails, call psa_fail_key_creation().
1679 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001680 * This function is intended to be used as follows:
1681 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001682 * it with the specified attributes, and in case of a volatile key assign it
1683 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001684 * -# Populate the slot with the key material.
1685 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1686 * In case of failure at any step, stop the sequence and call
1687 * psa_fail_key_creation().
1688 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001689 * On success, the key slot's state is PSA_SLOT_FILLING.
1690 * It is the responsibility of the caller to change the slot's state to
1691 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001692 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001693 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 * \param[out] p_slot On success, a pointer to the prepared slot.
1696 * \param[out] p_drv On any return, the driver for the key, if any.
1697 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001698 *
1699 * \retval #PSA_SUCCESS
1700 * The key slot is ready to receive key material.
1701 * \return If this function fails, the key slot is an invalid state.
1702 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 */
1704static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001705 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001707 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001709{
1710 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001711 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 psa_key_slot_t *slot;
1713
Gilles Peskinedf179142019-07-15 22:02:14 +02001714 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001715 *p_drv = NULL;
1716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 status = psa_validate_key_attributes(attributes, p_drv);
1718 if (status != PSA_SUCCESS) {
1719 return status;
1720 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721
Ryan Everett3d8118d2024-01-30 16:58:47 +00001722#if defined(MBEDTLS_THREADING_C)
1723 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1724 &mbedtls_threading_key_slot_mutex));
1725#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001726 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001727#if defined(MBEDTLS_THREADING_C)
1728 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1729 &mbedtls_threading_key_slot_mutex));
1730#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (status != PSA_SUCCESS) {
1732 return status;
1733 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 slot = *p_slot;
1735
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001736 /* We're storing the declared bit-size of the key. It's up to each
1737 * creation mechanism to verify that this information is correct.
1738 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001739 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001740 * is optional (import, copy). In case of a volatile key, assign it the
1741 * volatile key identifier associated to the slot returned to contain its
1742 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001743
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001744 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001746#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1747 slot->attr.id = volatile_key_id;
1748#else
1749 slot->attr.id.key_id = volatile_key_id;
1750#endif
1751 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001752
Gilles Peskinecbaff462019-07-12 23:46:04 +02001753#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001754 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001755 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001756 * create the key file in internal storage, create the
1757 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001758 * persistent data. This is done by starting a transaction that will
1759 * encompass these three actions.
1760 * For registering a volatile key, we just need to find an appropriate
1761 * slot number inside the SE. Since the key is designated volatile, creating
1762 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001763 /* The first thing to do is to find a slot number for the new key.
1764 * We save the slot number in persistent storage as part of the
1765 * transaction data. It will be needed to recover if the power
1766 * fails during the key creation process, to clean up on the secure
1767 * element side after restarting. Obtaining a slot number from the
1768 * secure element driver updates its persistent state, but we do not yet
1769 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001770 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001772 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1774 &slot_number);
1775 if (status != PSA_SUCCESS) {
1776 return status;
1777 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001778
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001779 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001781 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001782 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001783 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 status = psa_crypto_save_transaction();
1785 if (status != PSA_SUCCESS) {
1786 (void) psa_crypto_stop_transaction();
1787 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001788 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001789 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001790
1791 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001793 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001796 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001798 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001799#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001802}
Gilles Peskine4747d192019-04-17 15:05:45 +02001803
1804/** Finalize the creation of a key once its key material has been set.
1805 *
1806 * This entails writing the key to persistent storage.
1807 *
1808 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001809 * See the documentation of psa_start_key_creation() for the intended use
1810 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001812 * If the finalization succeeds, the function sets the key slot's state to
1813 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1814 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001815 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 * \param[in,out] slot Pointer to the slot with key material.
1817 * \param[in] driver The secure element driver for the key,
1818 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001819 * \param[out] key On success, identifier of the key. Note that the
1820 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001821 *
1822 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001823 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1825 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1826 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1827 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1828 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1829 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001830 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001831 * \return If this function fails, the key slot is an invalid state.
1832 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001833 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001834static psa_status_t psa_finish_key_creation(
1835 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001836 psa_se_drv_table_entry_t *driver,
1837 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001838{
1839 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001840 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001841 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001842
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001843#if defined(MBEDTLS_THREADING_C)
1844 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1845 &mbedtls_threading_key_slot_mutex));
1846#endif
1847
Gilles Peskine4747d192019-04-17 15:05:45 +02001848#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001852 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001853 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001855
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001856 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1857 sizeof(data.slot_number),
1858 "Slot number size does not match psa_se_key_data_storage_t");
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1861 status = psa_save_persistent_key(&slot->attr,
1862 (uint8_t *) &data,
1863 sizeof(data));
1864 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001865#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1866 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001867 /* Key material is saved in export representation in the slot, so
1868 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 status = psa_save_persistent_key(&slot->attr,
1870 slot->key.data,
1871 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001872 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001873 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001874#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1875
Gilles Peskinecbaff462019-07-12 23:46:04 +02001876#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001877 /* Finish the transaction for a key creation. This does not
1878 * happen when registering an existing key. Detect this case
1879 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001880 * creation of a persistent key in a secure element requires a transaction,
1881 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (driver != NULL &&
1883 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1884 status = psa_save_se_persistent_data(driver);
1885 if (status != PSA_SUCCESS) {
1886 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001887
1888#if defined(MBEDTLS_THREADING_C)
1889 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1890 &mbedtls_threading_key_slot_mutex));
1891#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001893 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001895 }
1896#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001899 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001900 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1901 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001903 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001905 }
Ronald Cron50972942020-11-14 11:28:25 +01001906
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001907#if defined(MBEDTLS_THREADING_C)
1908 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1909 &mbedtls_threading_key_slot_mutex));
1910#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001912}
1913
1914/** Abort the creation of a key.
1915 *
1916 * You may call this function after calling psa_start_key_creation(),
1917 * or after psa_finish_key_creation() fails. In other circumstances, this
1918 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001919 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001920 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001922 * \param[in,out] slot Pointer to the slot with key material.
1923 * \param[in] driver The secure element driver for the key,
1924 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926static void psa_fail_key_creation(psa_key_slot_t *slot,
1927 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001928{
Gilles Peskine011e4282019-06-26 18:34:38 +02001929 (void) driver;
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001934
Ryan Everettb7101442024-01-23 20:09:49 +00001935#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001936 /* If the lock operation fails we still wipe the slot.
1937 * Operations will no longer work after a failed lock,
1938 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001939 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1940#endif
1941
Gilles Peskine011e4282019-06-26 18:34:38 +02001942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001943 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001944 * element, and the failure happened later (when saving metadata
1945 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001946 * element.
1947 * https://github.com/ARMmbed/mbed-crypto/issues/217
1948 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001949
Gilles Peskined7729582019-08-05 15:55:54 +02001950 /* Abort the ongoing transaction if any (there may not be one if
1951 * the creation process failed before starting one, or if the
1952 * key creation is a registration of a key in a secure element).
1953 * Earlier functions must already have done what it takes to undo any
1954 * partial creation. All that's left is to update the transaction data
1955 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001957#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001960
1961#if defined(MBEDTLS_THREADING_C)
1962 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1963#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001964}
1965
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001966/** Validate optional attributes during key creation.
1967 *
1968 * Some key attributes are optional during key creation. If they are
1969 * specified in the attributes structure, check that they are consistent
1970 * with the data in the slot.
1971 *
1972 * This function should be called near the end of key creation, after
1973 * the slot in memory is fully populated but before saving persistent data.
1974 */
1975static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001978{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001979 if (attributes->type != 0) {
1980 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 return PSA_ERROR_INVALID_ARGUMENT;
1982 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 }
1984
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001985 if (attributes->bits != 0) {
1986 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 return PSA_ERROR_INVALID_ARGUMENT;
1988 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 }
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992}
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00001995 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 size_t data_length,
1997 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001998{
1999 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00002000 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002001 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002002 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302004 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005
Ronald Cron81709fc2020-11-14 12:10:32 +01002006 *key = MBEDTLS_SVC_KEY_ID_INIT;
2007
Gilles Peskine0f84d622019-09-12 19:03:13 +02002008 /* Reject zero-length symmetric keys (including raw data key objects).
2009 * This also rejects any key which might be encoded as an empty string,
2010 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (data_length == 0) {
2012 return PSA_ERROR_INVALID_ARGUMENT;
2013 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002014
Archana449608b2021-09-08 15:36:05 +05302015 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (data_length > SIZE_MAX / 8) {
2017 return PSA_ERROR_NOT_SUPPORTED;
2018 }
Archana6ed4bda2021-08-04 10:47:15 +05302019
Ryan Everettf028fe12024-01-08 17:14:44 +00002020 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2023 &slot, &driver);
2024 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302029 * storage ( thus not in the case of importing a key in a secure element
2030 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2031 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002033 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302034 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 attributes, data, data_length, &storage_size);
2036 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302037 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 }
Archanad8a83dc2021-06-14 10:04:16 +05302039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 status = psa_allocate_buffer_to_slot(slot, storage_size);
2041 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002044 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002045
2046 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 status = psa_driver_wrapper_import_key(attributes,
2048 data, data_length,
2049 slot->key.data,
2050 slot->key.bytes,
2051 &slot->key.bytes, &bits);
2052 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002057 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002059 status = PSA_ERROR_INVALID_ARGUMENT;
2060 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002061 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002062
Archana6ed4bda2021-08-04 10:47:15 +05302063 /* Enforce a size limit, and in particular ensure that the bit
2064 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302066 status = PSA_ERROR_NOT_SUPPORTED;
2067 goto exit;
2068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 status = psa_validate_optional_attributes(slot, attributes);
2070 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002076 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (status != PSA_SUCCESS) {
2078 psa_fail_key_creation(slot, driver);
2079 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002082}
2083
Gilles Peskined7729582019-08-05 15:55:54 +02002084#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2085psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002087{
2088 psa_status_t status;
2089 psa_key_slot_t *slot = NULL;
2090 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002092
2093 /* Leaving attributes unspecified is not currently supported.
2094 * It could make sense to query the key type and size from the
2095 * secure element, but not all secure elements support this
2096 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2098 return PSA_ERROR_NOT_SUPPORTED;
2099 }
2100 if (psa_get_key_bits(attributes) == 0) {
2101 return PSA_ERROR_NOT_SUPPORTED;
2102 }
Gilles Peskined7729582019-08-05 15:55:54 +02002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2105 &slot, &driver);
2106 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Gilles Peskined7729582019-08-05 15:55:54 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002111
2112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (status != PSA_SUCCESS) {
2114 psa_fail_key_creation(slot, driver);
2115 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002116
Gilles Peskined7729582019-08-05 15:55:54 +02002117 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 psa_close_key(key);
2119 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002120}
2121#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2124 const psa_key_attributes_t *specified_attributes,
2125 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002126{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002128 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002129 psa_key_slot_t *source_slot = NULL;
2130 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002132 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302133 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002134
Ronald Cron81709fc2020-11-14 12:10:32 +01002135 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2136
Archana8a180362021-07-05 02:18:48 +05302137 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2139 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_validate_optional_attributes(source_slot,
2144 specified_attributes);
2145 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148
Archana9d17bf42021-09-10 06:22:44 +05302149 /* The target key type and number of bits have been validated by
2150 * psa_validate_optional_attributes() to be either equal to zero or
2151 * equal to the ones of the source key. So it is safe to inherit
2152 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302153 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002154 actual_attributes.bits = source_slot->attr.bits;
2155 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302156
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002159 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 &source_slot->attr.policy);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2166 &target_slot, &driver);
2167 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 }
2170 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2171 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302172 /*
Archana9d17bf42021-09-10 06:22:44 +05302173 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302174 * the source key would need to be exported as plaintext and re-imported
2175 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302176 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302177 * appropriate API invocations from the application, if needed.
2178 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002179 status = PSA_ERROR_NOT_SUPPORTED;
2180 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002181 }
Archana8a180362021-07-05 02:18:48 +05302182 /*
2183 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302184 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302185 * - For opaque keys this translates to an invocation of the drivers'
2186 * copy_key entry point through the dispatch layer.
2187 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002188 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2190 &storage_size);
2191 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 }
Archana374fe5b2021-09-08 15:50:28 +05302194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2196 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 }
Archana374fe5b2021-09-08 15:50:28 +05302199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 status = psa_driver_wrapper_copy_key(&actual_attributes,
2201 source_slot->key.data,
2202 source_slot->key.bytes,
2203 target_slot->key.data,
2204 target_slot->key.bytes,
2205 &target_slot->key.bytes);
2206 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 }
2209 } else {
2210 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302211 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 source_slot->key.bytes);
2213 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Archana8a180362021-07-05 02:18:48 +05302216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (status != PSA_SUCCESS) {
2220 psa_fail_key_creation(target_slot, driver);
2221 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002222
Ryan Everetta103ec92024-01-31 13:59:57 +00002223 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002226}
2227
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002228
2229
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002230/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002231/* Message digests */
2232/****************************************************************/
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002236 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (operation->id == 0) {
2238 return PSA_SUCCESS;
2239 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002242 operation->id = 0;
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245}
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2248 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002251
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002252 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002254 status = PSA_ERROR_BAD_STATE;
2255 goto exit;
2256 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002259 status = PSA_ERROR_INVALID_ARGUMENT;
2260 goto exit;
2261 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002262
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002263 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2264 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002268
2269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (status != PSA_SUCCESS) {
2271 psa_hash_abort(operation);
2272 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273
2274 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275}
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002278 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002282 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002285 status = PSA_ERROR_BAD_STATE;
2286 goto exit;
2287 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002288
Steven Cooremanc8288352021-03-04 14:02:19 +01002289 /* Don't require hash implementations to behave correctly on a
2290 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (input_length == 0) {
2292 return PSA_SUCCESS;
2293 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002294
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002295 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002297
2298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (status != PSA_SUCCESS) {
2300 psa_hash_abort(operation);
2301 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002302
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002303 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305}
2306
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002307static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002308 uint8_t *hash,
2309 size_t hash_size,
2310 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2313
Steven Cooremanfbe09282021-03-08 18:41:12 +01002314 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (operation->id == 0) {
2316 return PSA_ERROR_BAD_STATE;
2317 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002319 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 operation, hash, hash_size, hash_length);
2321 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002322
2323 return status;
2324}
2325
2326psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2327 uint8_t *hash_external,
2328 size_t hash_size,
2329 size_t *hash_length)
2330{
2331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2332 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2333
2334 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2335 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2336
Thomas Daubneydedd1002024-01-25 16:52:50 +00002337#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002338exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002339#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002340 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342}
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002345 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347{
Ronald Cron69a63422021-10-18 09:47:58 +02002348 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2351 LOCAL_INPUT_DECLARE(hash_external, hash);
2352
2353 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 operation,
2355 actual_hash, sizeof(actual_hash),
2356 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002363 status = PSA_ERROR_INVALID_SIGNATURE;
2364 goto exit;
2365 }
2366
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002367 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002368 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002371
2372exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2374 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002375 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002377 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379}
2380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002382 const uint8_t *input_external, size_t input_length,
2383 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002385{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2387 LOCAL_INPUT_DECLARE(input_external, input);
2388 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2389
Steven Cooremanfbe09282021-03-08 18:41:12 +01002390 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (!PSA_ALG_IS_HASH(alg)) {
2392 return PSA_ERROR_INVALID_ARGUMENT;
2393 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002394
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002395 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2396 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2397 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002398 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002399
Thomas Daubneydedd1002024-01-25 16:52:50 +00002400#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002401exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002402#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002403 LOCAL_INPUT_FREE(input_external, input);
2404 LOCAL_OUTPUT_FREE(hash_external, hash);
2405 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002406}
2407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002409 const uint8_t *input_external, size_t input_length,
2410 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002411{
Ronald Cron69a63422021-10-18 09:47:58 +02002412 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002413 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2415
2416 LOCAL_INPUT_DECLARE(input_external, input);
2417 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002420 status = PSA_ERROR_INVALID_ARGUMENT;
2421 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002423
Thomas Daubney51ffac92024-01-18 15:46:26 +00002424 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2425 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 alg, input, input_length,
2427 actual_hash, sizeof(actual_hash),
2428 &actual_hash_length);
2429 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 }
2432 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002433 status = PSA_ERROR_INVALID_SIGNATURE;
2434 goto exit;
2435 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002436
2437 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002438 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002439 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002441
2442exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002444
2445 LOCAL_INPUT_FREE(input_external, input);
2446 LOCAL_INPUT_FREE(hash_external, hash);
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002449}
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2452 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002453{
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (source_operation->id == 0 ||
2455 target_operation->id != 0) {
2456 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002457 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2460 target_operation);
2461 if (status != PSA_SUCCESS) {
2462 psa_hash_abort(target_operation);
2463 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002466}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467
2468
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470/* MAC */
2471/****************************************************************/
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474{
Steven Cooremane6804192021-03-19 18:28:56 +01002475 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (operation->id == 0) {
2477 return PSA_SUCCESS;
2478 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002481 operation->mac_size = 0;
2482 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002483 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002486}
2487
Ronald Cron2dff3b22021-06-17 16:33:22 +02002488static psa_status_t psa_mac_finalize_alg_and_key_validation(
2489 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002490 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 psa_key_type_t key_type = psa_get_key_type(attributes);
2495 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (!PSA_ALG_IS_MAC(alg)) {
2498 return PSA_ERROR_INVALID_ARGUMENT;
2499 }
Steven Cooremane6804192021-03-19 18:28:56 +01002500
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002501 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 status = psa_mac_key_can_do(alg, key_type);
2503 if (status != PSA_SUCCESS) {
2504 return status;
2505 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002506
Steven Cooremand1a68f12021-05-10 11:13:41 +02002507 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002511 /* A very short MAC is too short for security since it can be
2512 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2513 * so we make this our minimum, even though 32 bits is still
2514 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002516 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2519 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002520 /* It's impossible to "truncate" to a larger length than the full length
2521 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002523 }
2524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002526 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2527 * that is disabled in the compile-time configuration. The result can
2528 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2529 * configuration into account. In this case, force a return of
2530 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2531 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2532 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2533 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2534 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002536 }
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002539}
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2542 mbedtls_svc_key_id_t key,
2543 psa_algorithm_t alg,
2544 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002545{
2546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2547 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002548 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549
2550 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002552 status = PSA_ERROR_BAD_STATE;
2553 goto exit;
2554 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555
2556 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 key,
2558 &slot,
2559 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2560 alg);
2561 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002562 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002565 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 &operation->mac_size);
2567 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570
2571 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002572 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (is_sign) {
2574 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002575 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 slot->key.data,
2577 slot->key.bytes,
2578 alg);
2579 } else {
2580 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002581 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 slot->key.data,
2583 slot->key.bytes,
2584 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586
Gilles Peskinefbfac682018-07-08 20:51:54 +02002587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (status != PSA_SUCCESS) {
2589 psa_mac_abort(operation);
2590 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002591
Ryan Everettfb9857f2024-02-14 12:16:41 +00002592 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595}
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2598 mbedtls_svc_key_id_t key,
2599 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002600{
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002602}
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2605 mbedtls_svc_key_id_t key,
2606 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002607{
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002609}
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002612 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2616 LOCAL_INPUT_DECLARE(input_external, input);
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002619 status = PSA_ERROR_BAD_STATE;
2620 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002623 /* Don't require hash implementations to behave correctly on a
2624 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002626 status = PSA_SUCCESS;
2627 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002629
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002630 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2631 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (status != PSA_SUCCESS) {
2634 psa_mac_abort(operation);
2635 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002636
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002637#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002638exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002639#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002640 LOCAL_INPUT_FREE(input_external, input);
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643}
2644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002646 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 size_t mac_size,
2648 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002649{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2651 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002652 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002653 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002656 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002657 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002658 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002661 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002662 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002663 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002664
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002665 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2666 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002668 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002669 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002670 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002673 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002674 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002676
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 status = psa_driver_wrapper_mac_sign_finish(operation,
2679 mac, operation->mac_size,
2680 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002681
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002683 /* In case of success, set the potential excess room in the output buffer
2684 * to an invalid value, to avoid potentially leaking a longer MAC.
2685 * In case of error, set the output length and content to a safe default,
2686 * such that in case the caller misses an error check, the output would be
2687 * an unachievable MAC.
2688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002690 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002691 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002692 }
mohammad16036df908f2018-04-02 08:34:15 -07002693
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002694 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002695 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2696 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002699 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002702}
2703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002705 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2709 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002710 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002713 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002714 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002715 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002718 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002719 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002720 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002723 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002724 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002725 }
2726
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002727 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 status = psa_driver_wrapper_mac_verify_finish(operation,
2729 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002730
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002731exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002733 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736}
2737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2739 psa_algorithm_t alg,
2740 const uint8_t *input,
2741 size_t input_length,
2742 uint8_t *mac,
2743 size_t mac_size,
2744 size_t *mac_length,
2745 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002746{
2747 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002748 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2749 psa_key_slot_t *slot;
2750 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751
Ronald Cron51131b52021-06-17 17:17:20 +02002752 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 key,
2754 &slot,
2755 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2756 alg);
2757 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002758 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002760
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002761 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 &operation_mac_size);
2763 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002768 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002769 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002770 }
2771
2772 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002773 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 slot->key.data, slot->key.bytes,
2775 alg,
2776 input, input_length,
2777 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778
2779exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002780 /* In case of success, set the potential excess room in the output buffer
2781 * to an invalid value, to avoid potentially leaking a longer MAC.
2782 * In case of error, set the output length and content to a safe default,
2783 * such that in case the caller misses an error check, the output would be
2784 * an unachievable MAC.
2785 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002787 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002788 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002789 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002790
2791 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792
Ryan Everetta103ec92024-01-31 13:59:57 +00002793 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002796}
2797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002799 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002800 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002801 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002802 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 size_t mac_size,
2804 size_t *mac_length)
2805{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002806 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2807 LOCAL_INPUT_DECLARE(input_external, input);
2808 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2809
2810 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2811 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2812 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002813 input, input_length,
2814 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002815
2816#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002817exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002818#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002819 LOCAL_INPUT_FREE(input_external, input);
2820 LOCAL_OUTPUT_FREE(mac_external, mac);
2821
2822 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002823}
2824
2825psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2826 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002827 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002829 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002831{
2832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002833 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2834 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002835 LOCAL_INPUT_DECLARE(input_external, input);
2836 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002837
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002838 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 status = psa_mac_compute_internal(key, alg,
2840 input, input_length,
2841 actual_mac, sizeof(actual_mac),
2842 &actual_mac_length, 0);
2843 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002848 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002850 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002851
2852 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01002853 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002854 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002856 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002857
2858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002860 LOCAL_INPUT_FREE(input_external, input);
2861 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002865
Gilles Peskinee59236f2018-01-27 23:32:46 +01002866/****************************************************************/
2867/* Asymmetric cryptography */
2868/****************************************************************/
2869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2871 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872{
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (input_is_message) {
2874 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2875 return PSA_ERROR_INVALID_ARGUMENT;
2876 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2879 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2880 return PSA_ERROR_INVALID_ARGUMENT;
2881 }
2882 }
2883 } else {
2884 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2885 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002886 }
2887 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2893 int input_is_message,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 uint8_t *signature,
2898 size_t signature_size,
2899 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002900{
2901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904
2905 *signature_length = 0;
2906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 status = psa_sign_verify_check_alg(input_is_message, alg);
2908 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002909 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002913 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914 * buffer can in principle be empty since it doesn't actually have
2915 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (signature_size == 0) {
2917 return PSA_ERROR_BUFFER_TOO_SMALL;
2918 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002920 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 key, &slot,
2922 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2923 PSA_KEY_USAGE_SIGN_HASH,
2924 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931 status = PSA_ERROR_INVALID_ARGUMENT;
2932 goto exit;
2933 }
2934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002936 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002937 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002938 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 signature, signature_size, signature_length);
2940 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002941
2942 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002943 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946 }
2947
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002948
2949exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002950 psa_wipe_tag_output_buffer(signature, status, signature_size,
2951 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002952
Ryan Everetta103ec92024-01-31 13:59:57 +00002953 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002956}
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2959 int input_is_message,
2960 psa_algorithm_t alg,
2961 const uint8_t *input,
2962 size_t input_length,
2963 const uint8_t *signature,
2964 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002965{
2966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2967 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2968 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 status = psa_sign_verify_check_alg(input_is_message, alg);
2971 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002972 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002975 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 key, &slot,
2977 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2978 PSA_KEY_USAGE_VERIFY_HASH,
2979 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 if (status != PSA_SUCCESS) {
2982 return status;
2983 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002986 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002987 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002988 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 signature, signature_length);
2990 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002991 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002992 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002996
Ryan Everetta103ec92024-01-31 13:59:57 +00002997 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000
3001}
3002
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003003psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003004 const psa_key_attributes_t *attributes,
3005 const uint8_t *key_buffer,
3006 size_t key_buffer_size,
3007 psa_algorithm_t alg,
3008 const uint8_t *input,
3009 size_t input_length,
3010 uint8_t *signature,
3011 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003013{
3014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003017 size_t hash_length;
3018 uint8_t hash[PSA_HASH_MAX_SIZE];
3019
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003020 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 PSA_ALG_SIGN_GET_HASH(alg),
3022 input, input_length,
3023 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003026 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003028
3029 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 attributes, key_buffer, key_buffer_size,
3031 alg, hash, hash_length,
3032 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003033 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003036}
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3039 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003040 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003042 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 size_t signature_size,
3044 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003045{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3047 LOCAL_INPUT_DECLARE(input_external, input);
3048 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3049
3050 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3051 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3052 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3053 signature_size, signature_length);
3054
Thomas Daubney3e65f522024-01-30 12:37:25 +00003055#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003056exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003057#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003058 LOCAL_INPUT_FREE(input_external, input);
3059 LOCAL_OUTPUT_FREE(signature_external, signature);
3060 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003061}
3062
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003063psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003064 const psa_key_attributes_t *attributes,
3065 const uint8_t *key_buffer,
3066 size_t key_buffer_size,
3067 psa_algorithm_t alg,
3068 const uint8_t *input,
3069 size_t input_length,
3070 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003072{
3073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003076 size_t hash_length;
3077 uint8_t hash[PSA_HASH_MAX_SIZE];
3078
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003079 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ALG_SIGN_GET_HASH(alg),
3081 input, input_length,
3082 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003085 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003087
3088 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 attributes, key_buffer, key_buffer_size,
3090 alg, hash, hash_length,
3091 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003092 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003095}
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3098 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003099 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003101 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003103{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3105 LOCAL_INPUT_DECLARE(input_external, input);
3106 LOCAL_INPUT_DECLARE(signature_external, signature);
3107
3108 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3109 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3110 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3111 signature_length);
3112
Thomas Daubney3e65f522024-01-30 12:37:25 +00003113#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003114exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003115#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003116 LOCAL_INPUT_FREE(input_external, input);
3117 LOCAL_INPUT_FREE(signature_external, signature);
3118
3119 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003120}
3121
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003122psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003123 const psa_key_attributes_t *attributes,
3124 const uint8_t *key_buffer, size_t key_buffer_size,
3125 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003127{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003128 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3130 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3133 return mbedtls_psa_rsa_sign_hash(
3134 attributes,
3135 key_buffer, key_buffer_size,
3136 alg, hash, hash_length,
3137 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3139 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 } else {
3141 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003142 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003143 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003145#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3147 return mbedtls_psa_ecdsa_sign_hash(
3148 attributes,
3149 key_buffer, key_buffer_size,
3150 alg, hash, hash_length,
3151 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003152#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3153 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 } else {
3155 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003156 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003157 }
Ronald Cron4501c982021-03-19 20:09:32 +01003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 (void) key_buffer;
3160 (void) key_buffer_size;
3161 (void) hash;
3162 (void) hash_length;
3163 (void) signature;
3164 (void) signature_size;
3165 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003168}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3171 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003172 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003174 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 size_t signature_size,
3176 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003177{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3179 LOCAL_INPUT_DECLARE(hash_external, hash);
3180 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3181
3182 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3183 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3184 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3185 signature_size, signature_length);
3186
Thomas Daubney3e65f522024-01-30 12:37:25 +00003187#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003188exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003189#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003190 LOCAL_INPUT_FREE(hash_external, hash);
3191 LOCAL_OUTPUT_FREE(signature_external, signature);
3192
3193 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003194}
3195
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003196psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003197 const psa_key_attributes_t *attributes,
3198 const uint8_t *key_buffer, size_t key_buffer_size,
3199 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003201{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003202 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3204 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003205#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3207 return mbedtls_psa_rsa_verify_hash(
3208 attributes,
3209 key_buffer, key_buffer_size,
3210 alg, hash, hash_length,
3211 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003212#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3213 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 } else {
3215 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003216 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003217 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3221 return mbedtls_psa_ecdsa_verify_hash(
3222 attributes,
3223 key_buffer, key_buffer_size,
3224 alg, hash, hash_length,
3225 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003226#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3227 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 } else {
3229 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003230 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003231 }
Ronald Cron4501c982021-03-19 20:09:32 +01003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 (void) key_buffer;
3234 (void) key_buffer_size;
3235 (void) hash;
3236 (void) hash_length;
3237 (void) signature;
3238 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003241}
3242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3244 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003245 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003247 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003249{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3251 LOCAL_INPUT_DECLARE(hash_external, hash);
3252 LOCAL_INPUT_DECLARE(signature_external, signature);
3253
3254 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3255 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3256 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3257 signature_length);
3258
Thomas Daubney3e65f522024-01-30 12:37:25 +00003259#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003260exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003261#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003262 LOCAL_INPUT_FREE(hash_external, hash);
3263 LOCAL_INPUT_FREE(signature_external, signature);
3264
3265 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003266}
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3269 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003270 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003272 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003274 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 size_t output_size,
3276 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003280 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003282 LOCAL_INPUT_DECLARE(input_external, input);
3283 LOCAL_INPUT_DECLARE(salt_external, salt);
3284 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Darryl Green5cc689a2018-07-24 15:34:10 +01003286 (void) input;
3287 (void) input_length;
3288 (void) salt;
3289 (void) output;
3290 (void) output_size;
3291
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003292 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3295 return PSA_ERROR_INVALID_ARGUMENT;
3296 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003297
Valerio Setti5bb454a2024-01-15 10:43:16 +01003298 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3300 if (status != PSA_SUCCESS) {
3301 return status;
3302 }
3303 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3304 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003305 status = PSA_ERROR_INVALID_ARGUMENT;
3306 goto exit;
3307 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003309 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3310 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3311 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003312
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003313 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003314 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003315 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003317exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003318 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003319
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003320 LOCAL_INPUT_FREE(input_external, input);
3321 LOCAL_INPUT_FREE(salt_external, salt);
3322 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003325}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3328 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003329 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003331 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003333 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 size_t output_size,
3335 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003338 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003339 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003340
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003341 LOCAL_INPUT_DECLARE(input_external, input);
3342 LOCAL_INPUT_DECLARE(salt_external, salt);
3343 LOCAL_OUTPUT_DECLARE(output_external, output);
3344
Darryl Green5cc689a2018-07-24 15:34:10 +01003345 (void) input;
3346 (void) input_length;
3347 (void) salt;
3348 (void) output;
3349 (void) output_size;
3350
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003351 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3354 return PSA_ERROR_INVALID_ARGUMENT;
3355 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003356
Valerio Setti5bb454a2024-01-15 10:43:16 +01003357 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3359 if (status != PSA_SUCCESS) {
3360 return status;
3361 }
3362 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003363 status = PSA_ERROR_INVALID_ARGUMENT;
3364 goto exit;
3365 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003366
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003367 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3368 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3369 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003370
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003371 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003372 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003373 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003375
3376exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003377 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003378
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003379 LOCAL_INPUT_FREE(input_external, input);
3380 LOCAL_INPUT_FREE(salt_external, salt);
3381 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003384}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003385
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386/****************************************************************/
3387/* Asymmetric interruptible cryptography */
3388/****************************************************************/
3389
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003390static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3391
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392void psa_interruptible_set_max_ops(uint32_t max_ops)
3393{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003394 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395}
3396
3397uint32_t psa_interruptible_get_max_ops(void)
3398{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003399 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400}
3401
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402uint32_t psa_sign_hash_get_num_ops(
3403 const psa_sign_hash_interruptible_operation_t *operation)
3404{
Paul Elliott296ede92022-12-15 17:00:30 +00003405 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003406}
3407
3408uint32_t psa_verify_hash_get_num_ops(
3409 const psa_verify_hash_interruptible_operation_t *operation)
3410{
Paul Elliott296ede92022-12-15 17:00:30 +00003411 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003412}
3413
Paul Elliott59ad9452022-12-18 15:09:02 +00003414static psa_status_t psa_sign_hash_abort_internal(
3415 psa_sign_hash_interruptible_operation_t *operation)
3416{
3417 if (operation->id == 0) {
3418 /* The object has (apparently) been initialized but it is not (yet)
3419 * in use. It's ok to call abort on such an object, and there's
3420 * nothing to do. */
3421 return PSA_SUCCESS;
3422 }
3423
3424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3425
3426 status = psa_driver_wrapper_sign_hash_abort(operation);
3427
3428 operation->id = 0;
3429
Paul Elliotte6145dc2023-02-07 12:51:21 +00003430 /* Do not clear either the error_occurred or num_ops elements here as they
3431 * only want to be cleared by the application calling abort, not by abort
3432 * being called at completion of an operation. */
3433
Paul Elliott59ad9452022-12-18 15:09:02 +00003434 return status;
3435}
3436
Paul Elliott9fe12f62022-11-30 19:16:02 +00003437psa_status_t psa_sign_hash_start(
3438 psa_sign_hash_interruptible_operation_t *operation,
3439 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003440 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441{
3442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3443 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3444 psa_key_slot_t *slot;
3445
David Horstmann4a523a62024-03-11 13:32:16 +00003446 LOCAL_INPUT_DECLARE(hash_external, hash);
3447
Paul Elliottc9774412023-02-06 15:14:07 +00003448 /* Check that start has not been previously called, or operation has not
3449 * previously errored. */
3450 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003451 return PSA_ERROR_BAD_STATE;
3452 }
3453
Paul Elliott9fe12f62022-11-30 19:16:02 +00003454 status = psa_sign_verify_check_alg(0, alg);
3455 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003456 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457 return status;
3458 }
3459
3460 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3461 PSA_KEY_USAGE_SIGN_HASH,
3462 alg);
3463
3464 if (status != PSA_SUCCESS) {
3465 goto exit;
3466 }
3467
3468 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3469 status = PSA_ERROR_INVALID_ARGUMENT;
3470 goto exit;
3471 }
3472
David Horstmann4a523a62024-03-11 13:32:16 +00003473 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3474
Paul Elliott296ede92022-12-15 17:00:30 +00003475 /* Ensure ops count gets reset, in case of operation re-use. */
3476 operation->num_ops = 0;
3477
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003478 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003479 slot->key.data,
3480 slot->key.bytes, alg,
3481 hash, hash_length);
3482exit:
3483
3484 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003485 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003486 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 }
3488
Ryan Everettdcc03d52024-02-14 15:44:13 +00003489 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490
Paul Elliottc9774412023-02-06 15:14:07 +00003491 if (unlock_status != PSA_SUCCESS) {
3492 operation->error_occurred = 1;
3493 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003494
David Horstmann4a523a62024-03-11 13:32:16 +00003495 LOCAL_INPUT_FREE(hash_external, hash);
3496
Paul Elliottc9774412023-02-06 15:14:07 +00003497 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498}
3499
3500
3501psa_status_t psa_sign_hash_complete(
3502 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003503 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504 size_t *signature_length)
3505{
3506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3507
David Horstmann4a523a62024-03-11 13:32:16 +00003508 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3509
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510 *signature_length = 0;
3511
Paul Elliottc9774412023-02-06 15:14:07 +00003512 /* Check that start has been called first, and that operation has not
3513 * previously errored. */
3514 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003515 status = PSA_ERROR_BAD_STATE;
3516 goto exit;
3517 }
3518
Paul Elliott096abc42023-02-03 18:33:23 +00003519 /* Immediately reject a zero-length signature buffer. This guarantees that
3520 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003521 if (signature_size == 0) {
3522 status = PSA_ERROR_BUFFER_TOO_SMALL;
3523 goto exit;
3524 }
3525
David Horstmann4a523a62024-03-11 13:32:16 +00003526 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3527
Paul Elliott9fe12f62022-11-30 19:16:02 +00003528 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3529 signature_size,
3530 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003531
Paul Elliott296ede92022-12-15 17:00:30 +00003532 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003533 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003534
Paul Elliottebe225c2023-02-10 14:32:53 +00003535exit:
3536
David Horstmannc5064c82024-03-11 17:02:03 +00003537 if (signature != NULL) {
3538 psa_wipe_tag_output_buffer(signature, status, signature_size,
3539 *signature_length);
3540 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003541
Paul Elliott53bb3122023-02-10 14:22:22 +00003542 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003543 if (status != PSA_SUCCESS) {
3544 operation->error_occurred = 1;
3545 }
3546
Paul Elliott59ad9452022-12-18 15:09:02 +00003547 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 }
3549
David Horstmann4a523a62024-03-11 13:32:16 +00003550 LOCAL_OUTPUT_FREE(signature_external, signature);
3551
Paul Elliott9fe12f62022-11-30 19:16:02 +00003552 return status;
3553}
3554
3555psa_status_t psa_sign_hash_abort(
3556 psa_sign_hash_interruptible_operation_t *operation)
3557{
Paul Elliott59ad9452022-12-18 15:09:02 +00003558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3559
3560 status = psa_sign_hash_abort_internal(operation);
3561
3562 /* We clear the number of ops done here, so that it is not cleared when
3563 * the operation fails or succeeds, only on manual abort. */
3564 operation->num_ops = 0;
3565
Paul Elliottc9774412023-02-06 15:14:07 +00003566 /* Likewise, failure state. */
3567 operation->error_occurred = 0;
3568
Paul Elliott59ad9452022-12-18 15:09:02 +00003569 return status;
3570}
3571
3572static psa_status_t psa_verify_hash_abort_internal(
3573 psa_verify_hash_interruptible_operation_t *operation)
3574{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003575 if (operation->id == 0) {
3576 /* The object has (apparently) been initialized but it is not (yet)
3577 * in use. It's ok to call abort on such an object, and there's
3578 * nothing to do. */
3579 return PSA_SUCCESS;
3580 }
3581
Paul Elliott59ad9452022-12-18 15:09:02 +00003582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3583
3584 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585
3586 operation->id = 0;
3587
Paul Elliotte6145dc2023-02-07 12:51:21 +00003588 /* Do not clear either the error_occurred or num_ops elements here as they
3589 * only want to be cleared by the application calling abort, not by abort
3590 * being called at completion of an operation. */
3591
Paul Elliott59ad9452022-12-18 15:09:02 +00003592 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003593}
3594
3595psa_status_t psa_verify_hash_start(
3596 psa_verify_hash_interruptible_operation_t *operation,
3597 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003598 const uint8_t *hash_external, size_t hash_length,
3599 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003600{
3601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3602 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3603 psa_key_slot_t *slot;
3604
David Horstmann0fea6a52024-03-11 13:41:05 +00003605 LOCAL_INPUT_DECLARE(hash_external, hash);
3606 LOCAL_INPUT_DECLARE(signature_external, signature);
3607
Paul Elliottc9774412023-02-06 15:14:07 +00003608 /* Check that start has not been previously called, or operation has not
3609 * previously errored. */
3610 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003611 return PSA_ERROR_BAD_STATE;
3612 }
3613
3614 status = psa_sign_verify_check_alg(0, alg);
3615 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003616 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003617 return status;
3618 }
3619
3620 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3621 PSA_KEY_USAGE_VERIFY_HASH,
3622 alg);
3623
3624 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003625 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003626 return status;
3627 }
3628
David Horstmann0fea6a52024-03-11 13:41:05 +00003629 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3630 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3631
Paul Elliott296ede92022-12-15 17:00:30 +00003632 /* Ensure ops count gets reset, in case of operation re-use. */
3633 operation->num_ops = 0;
3634
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003635 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003636 slot->key.data,
3637 slot->key.bytes,
3638 alg, hash, hash_length,
3639 signature, signature_length);
David Horstmann0fea6a52024-03-11 13:41:05 +00003640#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
3641exit:
3642#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003643
3644 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003645 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003646 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003647 }
3648
Ryan Everett291267f2024-02-14 15:59:15 +00003649 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003650
Paul Elliottc9774412023-02-06 15:14:07 +00003651 if (unlock_status != PSA_SUCCESS) {
3652 operation->error_occurred = 1;
3653 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003654
David Horstmann0fea6a52024-03-11 13:41:05 +00003655 LOCAL_INPUT_FREE(hash_external, hash);
3656 LOCAL_INPUT_FREE(signature_external, signature);
3657
Paul Elliottc9774412023-02-06 15:14:07 +00003658 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003659}
3660
3661psa_status_t psa_verify_hash_complete(
3662 psa_verify_hash_interruptible_operation_t *operation)
3663{
3664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3665
Paul Elliottc9774412023-02-06 15:14:07 +00003666 /* Check that start has been called first, and that operation has not
3667 * previously errored. */
3668 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003669 status = PSA_ERROR_BAD_STATE;
3670 goto exit;
3671 }
3672
3673 status = psa_driver_wrapper_verify_hash_complete(operation);
3674
Paul Elliott296ede92022-12-15 17:00:30 +00003675 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003676 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003677 operation);
3678
Paul Elliottebe225c2023-02-10 14:32:53 +00003679exit:
3680
Paul Elliott9fe12f62022-11-30 19:16:02 +00003681 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003682 if (status != PSA_SUCCESS) {
3683 operation->error_occurred = 1;
3684 }
3685
Paul Elliott59ad9452022-12-18 15:09:02 +00003686 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003687 }
3688
3689 return status;
3690}
3691
3692psa_status_t psa_verify_hash_abort(
3693 psa_verify_hash_interruptible_operation_t *operation)
3694{
Paul Elliott59ad9452022-12-18 15:09:02 +00003695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003696
Paul Elliott59ad9452022-12-18 15:09:02 +00003697 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003698
Paul Elliott59ad9452022-12-18 15:09:02 +00003699 /* We clear the number of ops done here, so that it is not cleared when
3700 * the operation fails or succeeds, only on manual abort. */
3701 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003702
Paul Elliottc9774412023-02-06 15:14:07 +00003703 /* Likewise, failure state. */
3704 operation->error_occurred = 0;
3705
Paul Elliott59ad9452022-12-18 15:09:02 +00003706 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003707}
3708
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709/****************************************************************/
3710/* Asymmetric interruptible cryptography internal */
3711/* implementations */
3712/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003713
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3715{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003716
Paul Elliott588f8ed2022-12-02 18:10:26 +00003717#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3718 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3719 defined(MBEDTLS_ECP_RESTARTABLE)
3720
3721 /* Internal implementation uses zero to indicate infinite number max ops,
3722 * therefore avoid this value, and set to minimum possible. */
3723 if (max_ops == 0) {
3724 max_ops = 1;
3725 }
3726
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003728#else
3729 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3731 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3732 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3733}
3734
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003736 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737{
3738#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3739 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3740 defined(MBEDTLS_ECP_RESTARTABLE)
3741
Paul Elliott93d9ca82023-02-15 18:14:21 +00003742 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743#else
3744 (void) operation;
3745 return 0;
3746#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3747 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3748 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3749}
3750
3751uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003752 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753{
3754 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3755 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3756 defined(MBEDTLS_ECP_RESTARTABLE)
3757
Paul Elliott93d9ca82023-02-15 18:14:21 +00003758 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759#else
3760 (void) operation;
3761 return 0;
3762#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3763 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3764 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3765}
3766
3767psa_status_t mbedtls_psa_sign_hash_start(
3768 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3769 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3770 size_t key_buffer_size, psa_algorithm_t alg,
3771 const uint8_t *hash, size_t hash_length)
3772{
3773 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003774 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003776 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003777 return PSA_ERROR_NOT_SUPPORTED;
3778 }
3779
3780 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003781 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003782 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
3784#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003785 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3786 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliotta1c94092023-02-15 16:38:04 +00003788 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3789
Paul Elliott93d9ca82023-02-15 18:14:21 +00003790 /* Ensure num_ops is zero'ed in case of context re-use. */
3791 operation->num_ops = 0;
3792
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003793 status = mbedtls_psa_ecp_load_representation(attributes->type,
3794 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003795 key_buffer,
3796 key_buffer_size,
3797 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798
Paul Elliott068fe072023-01-16 13:59:15 +00003799 if (status != PSA_SUCCESS) {
3800 return status;
3801 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802
Paul Elliott1bc59df2023-02-05 13:41:57 +00003803 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003804 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
Paul Elliott068fe072023-01-16 13:59:15 +00003806 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003807 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003808 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809
Paul Elliott0290a762023-02-09 14:30:24 +00003810 /* We only need to store the same length of hash as the private key size
3811 * here, it would be truncated by the internal implementation anyway. */
3812 required_hash_length = (hash_length < operation->coordinate_bytes ?
3813 hash_length : operation->coordinate_bytes);
3814
Paul Elliottba70ad42023-02-15 18:23:53 +00003815 if (required_hash_length > sizeof(operation->hash)) {
3816 /* Shouldn't happen, but better safe than sorry. */
3817 return PSA_ERROR_CORRUPTION_DETECTED;
3818 }
3819
Paul Elliott0290a762023-02-09 14:30:24 +00003820 memcpy(operation->hash, hash, required_hash_length);
3821 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003822
3823 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824
3825#else
Paul Elliott068fe072023-01-16 13:59:15 +00003826 (void) operation;
3827 (void) key_buffer;
3828 (void) key_buffer_size;
3829 (void) alg;
3830 (void) hash;
3831 (void) hash_length;
3832 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003833 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834
Paul Elliott068fe072023-01-16 13:59:15 +00003835 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3837 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3838 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839}
3840
3841psa_status_t mbedtls_psa_sign_hash_complete(
3842 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3843 uint8_t *signature, size_t signature_size,
3844 size_t *signature_length)
3845{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3847 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3848 defined(MBEDTLS_ECP_RESTARTABLE)
3849
Paul Elliotta1c94092023-02-15 16:38:04 +00003850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003851 mbedtls_mpi r;
3852 mbedtls_mpi s;
3853
Paul Elliott46845252023-02-03 14:59:11 +00003854 mbedtls_mpi_init(&r);
3855 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003857 /* Ensure max_ops is set to the current value (or default). */
3858 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3859
Paul Elliotta1c94092023-02-15 16:38:04 +00003860 if (signature_size < 2 * operation->coordinate_bytes) {
3861 status = PSA_ERROR_BUFFER_TOO_SMALL;
3862 goto exit;
3863 }
3864
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003866
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3868 status = mbedtls_to_psa_error(
3869 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003870 &r,
3871 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003872 &operation->ctx->d,
3873 operation->hash,
3874 operation->hash_length,
3875 operation->md_alg,
3876 mbedtls_psa_get_random,
3877 MBEDTLS_PSA_RANDOM_STATE,
3878 &operation->restart_ctx));
3879#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003880 status = PSA_ERROR_NOT_SUPPORTED;
3881 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3883 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 status = mbedtls_to_psa_error(
3885 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003886 &r,
3887 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888 &operation->ctx->d,
3889 operation->hash,
3890 operation->hash_length,
3891 mbedtls_psa_get_random,
3892 MBEDTLS_PSA_RANDOM_STATE,
3893 mbedtls_psa_get_random,
3894 MBEDTLS_PSA_RANDOM_STATE,
3895 &operation->restart_ctx));
3896 }
3897
Paul Elliottf8e5b562023-02-19 18:43:45 +00003898 /* Hide the fact that the restart context only holds a delta of number of
3899 * ops done during the last operation, not an absolute value. */
3900 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3901
Paul Elliott724bd252023-02-08 12:35:08 +00003902 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003904 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003905 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003906 operation->coordinate_bytes)
3907 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908
3909 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003910 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003911 }
3912
3913 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003914 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003915 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003916 operation->coordinate_bytes,
3917 operation->coordinate_bytes)
3918 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919
3920 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003921 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003922 }
3923
Paul Elliott1bc59df2023-02-05 13:41:57 +00003924 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003925
Paul Elliott724bd252023-02-08 12:35:08 +00003926 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003927 }
Paul Elliott724bd252023-02-08 12:35:08 +00003928
3929exit:
3930
3931 mbedtls_mpi_free(&r);
3932 mbedtls_mpi_free(&s);
3933 return status;
3934
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935 #else
3936
3937 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938 (void) signature;
3939 (void) signature_size;
3940 (void) signature_length;
3941
3942 return PSA_ERROR_NOT_SUPPORTED;
3943
3944#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3945 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3946 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3947}
3948
3949psa_status_t mbedtls_psa_sign_hash_abort(
3950 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3951{
3952
3953#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3954 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3955 defined(MBEDTLS_ECP_RESTARTABLE)
3956
3957 if (operation->ctx) {
3958 mbedtls_ecdsa_free(operation->ctx);
3959 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003960 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961 }
3962
3963 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3964
Paul Elliott93d9ca82023-02-15 18:14:21 +00003965 operation->num_ops = 0;
3966
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967 return PSA_SUCCESS;
3968
3969#else
3970
3971 (void) operation;
3972
3973 return PSA_ERROR_NOT_SUPPORTED;
3974
3975#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3976 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3977 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3978}
3979
3980psa_status_t mbedtls_psa_verify_hash_start(
3981 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3982 const psa_key_attributes_t *attributes,
3983 const uint8_t *key_buffer, size_t key_buffer_size,
3984 psa_algorithm_t alg,
3985 const uint8_t *hash, size_t hash_length,
3986 const uint8_t *signature, size_t signature_length)
3987{
3988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003989 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003990 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003991
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003992 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003993 return PSA_ERROR_NOT_SUPPORTED;
3994 }
3995
3996 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003997 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003998 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999
4000#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004001 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4002 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004003
Paul Elliotta1c94092023-02-15 16:38:04 +00004004 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4005 mbedtls_mpi_init(&operation->r);
4006 mbedtls_mpi_init(&operation->s);
4007
Paul Elliott93d9ca82023-02-15 18:14:21 +00004008 /* Ensure num_ops is zero'ed in case of context re-use. */
4009 operation->num_ops = 0;
4010
Gilles Peskine2f107ae2024-02-28 01:26:46 +01004011 status = mbedtls_psa_ecp_load_representation(attributes->type,
4012 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00004013 key_buffer,
4014 key_buffer_size,
4015 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004016
Paul Elliott068fe072023-01-16 13:59:15 +00004017 if (status != PSA_SUCCESS) {
4018 return status;
4019 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004020
Paul Elliottc569fc22023-02-10 13:02:54 +00004021 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022
Paul Elliott1bc59df2023-02-05 13:41:57 +00004023 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004024 return PSA_ERROR_INVALID_SIGNATURE;
4025 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004026
Paul Elliott068fe072023-01-16 13:59:15 +00004027 status = mbedtls_to_psa_error(
4028 mbedtls_mpi_read_binary(&operation->r,
4029 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004030 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004031
Paul Elliott068fe072023-01-16 13:59:15 +00004032 if (status != PSA_SUCCESS) {
4033 return status;
4034 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004035
Paul Elliott068fe072023-01-16 13:59:15 +00004036 status = mbedtls_to_psa_error(
4037 mbedtls_mpi_read_binary(&operation->s,
4038 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004039 coordinate_bytes,
4040 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004041
Paul Elliott068fe072023-01-16 13:59:15 +00004042 if (status != PSA_SUCCESS) {
4043 return status;
4044 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004045
Paul Elliott2c9843f2023-02-15 17:32:42 +00004046 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004047
Paul Elliott2c9843f2023-02-15 17:32:42 +00004048 if (status != PSA_SUCCESS) {
4049 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004050 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004051
Paul Elliott0290a762023-02-09 14:30:24 +00004052 /* We only need to store the same length of hash as the private key size
4053 * here, it would be truncated by the internal implementation anyway. */
4054 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4055 coordinate_bytes);
4056
Paul Elliottba70ad42023-02-15 18:23:53 +00004057 if (required_hash_length > sizeof(operation->hash)) {
4058 /* Shouldn't happen, but better safe than sorry. */
4059 return PSA_ERROR_CORRUPTION_DETECTED;
4060 }
4061
Paul Elliott0290a762023-02-09 14:30:24 +00004062 memcpy(operation->hash, hash, required_hash_length);
4063 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004064
4065 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004066#else
Paul Elliott068fe072023-01-16 13:59:15 +00004067 (void) operation;
4068 (void) key_buffer;
4069 (void) key_buffer_size;
4070 (void) alg;
4071 (void) hash;
4072 (void) hash_length;
4073 (void) signature;
4074 (void) signature_length;
4075 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004076 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004077 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004078
Paul Elliott068fe072023-01-16 13:59:15 +00004079 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4081 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4082 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004083}
4084
4085psa_status_t mbedtls_psa_verify_hash_complete(
4086 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4087{
4088
4089#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4090 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4091 defined(MBEDTLS_ECP_RESTARTABLE)
4092
Paul Elliottf8e5b562023-02-19 18:43:45 +00004093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4094
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004095 /* Ensure max_ops is set to the current value (or default). */
4096 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4097
Paul Elliottf8e5b562023-02-19 18:43:45 +00004098 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004099 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4100 operation->hash,
4101 operation->hash_length,
4102 &operation->ctx->Q,
4103 &operation->r,
4104 &operation->s,
4105 &operation->restart_ctx));
4106
Paul Elliottf8e5b562023-02-19 18:43:45 +00004107 /* Hide the fact that the restart context only holds a delta of number of
4108 * ops done during the last operation, not an absolute value. */
4109 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4110
4111 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004112#else
4113 (void) operation;
4114
4115 return PSA_ERROR_NOT_SUPPORTED;
4116
4117#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4118 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4119 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4120}
4121
4122psa_status_t mbedtls_psa_verify_hash_abort(
4123 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4124{
4125
4126#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4127 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4128 defined(MBEDTLS_ECP_RESTARTABLE)
4129
4130 if (operation->ctx) {
4131 mbedtls_ecdsa_free(operation->ctx);
4132 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004133 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004134 }
4135
4136 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4137
Paul Elliott93d9ca82023-02-15 18:14:21 +00004138 operation->num_ops = 0;
4139
Paul Elliott588f8ed2022-12-02 18:10:26 +00004140 mbedtls_mpi_free(&operation->r);
4141 mbedtls_mpi_free(&operation->s);
4142
4143 return PSA_SUCCESS;
4144
4145#else
4146 (void) operation;
4147
4148 return PSA_ERROR_NOT_SUPPORTED;
4149
4150#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4151 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4152 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4153}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004154
David Horstmann6e99bb22024-02-06 15:27:49 +00004155static psa_status_t psa_generate_random_internal(uint8_t *output,
4156 size_t output_size)
4157{
4158 GUARD_MODULE_INITIALIZED;
4159
David Horstmann6e99bb22024-02-06 15:27:49 +00004160#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4161
David Horstmanndb909142024-03-12 16:07:08 +00004162 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004163 size_t output_length = 0;
4164 status = mbedtls_psa_external_get_random(&global_data.rng,
4165 output, output_size,
4166 &output_length);
4167 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004168 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004169 }
4170 /* Breaking up a request into smaller chunks is currently not supported
4171 * for the external RNG interface. */
4172 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004173 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004174 }
David Horstmanndb909142024-03-12 16:07:08 +00004175 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004176
4177#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4178
4179 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004180 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004181 size_t request_size =
4182 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4183 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4184 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004185#if defined(MBEDTLS_CTR_DRBG_C)
4186 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4187#elif defined(MBEDTLS_HMAC_DRBG_C)
4188 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4189#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004190 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004191 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004192 }
4193 output_size -= request_size;
4194 output += request_size;
4195 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004196 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004197#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004198}
4199
4200
mohammad1603503973b2018-03-12 15:59:30 +02004201/****************************************************************/
4202/* Symmetric cryptography */
4203/****************************************************************/
4204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4206 mbedtls_svc_key_id_t key,
4207 psa_algorithm_t alg,
4208 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004209{
4210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4211 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004212 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4214 PSA_KEY_USAGE_ENCRYPT :
4215 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004216
4217 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004219 status = PSA_ERROR_BAD_STATE;
4220 goto exit;
4221 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004224 status = PSA_ERROR_INVALID_ARGUMENT;
4225 goto exit;
4226 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4229 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004232
Ronald Cron49fafa92021-03-10 08:34:23 +01004233 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004234 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004235 * so we only set it (in the driver wrapper) after resources have been
4236 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004237 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004239 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004241 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 }
4243 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004244
4245 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 if (cipher_operation == MBEDTLS_ENCRYPT) {
4247 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004248 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 slot->key.data,
4250 slot->key.bytes,
4251 alg);
4252 } else {
4253 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004254 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 slot->key.data,
4256 slot->key.bytes,
4257 alg);
4258 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004259
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (status != PSA_SUCCESS) {
4262 psa_cipher_abort(operation);
4263 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004264
Ryan Everettc0053cc2024-02-14 16:27:13 +00004265 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004268}
4269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4271 mbedtls_svc_key_id_t key,
4272 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004273{
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004275}
4276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4278 mbedtls_svc_key_id_t key,
4279 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004280{
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004282}
4283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004285 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 size_t iv_size,
4287 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004288{
Ronald Cron6d051732020-10-01 14:10:20 +02004289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004290 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004291
Gabor Mezei358eb212024-02-07 18:10:13 +01004292 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004295 status = PSA_ERROR_BAD_STATE;
4296 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004297 }
4298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004300 status = PSA_ERROR_BAD_STATE;
4301 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004302 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004303
Ronald Cron2fb90522021-07-05 12:31:44 +02004304 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004306 status = PSA_ERROR_BUFFER_TOO_SMALL;
4307 goto exit;
4308 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004311 status = PSA_ERROR_GENERIC_ERROR;
4312 goto exit;
4313 }
4314
Gabor Mezei358eb212024-02-07 18:10:13 +01004315 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4316
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004317 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 }
Ronald Cron5618a392021-03-26 09:52:26 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004323 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004324
4325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004327 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004328 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004330 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004332 if (iv != NULL) {
4333 mbedtls_platform_zeroize(iv, default_iv_length);
4334 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004335 }
Ronald Cron6d051732020-10-01 14:10:20 +02004336
Gabor Mezei358eb212024-02-07 18:10:13 +01004337 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004339}
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004342 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 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;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004346
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004347 LOCAL_INPUT_DECLARE(iv_external, iv);
4348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004350 status = PSA_ERROR_BAD_STATE;
4351 goto exit;
4352 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004355 status = PSA_ERROR_BAD_STATE;
4356 goto exit;
4357 }
Ronald Crona0d68172021-03-26 10:15:08 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004360 status = PSA_ERROR_INVALID_ARGUMENT;
4361 goto exit;
4362 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004363
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004364 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 status = psa_driver_wrapper_cipher_set_iv(operation,
4367 iv,
4368 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004369
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004370exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004372 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 } else {
4374 psa_cipher_abort(operation);
4375 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004376
4377 LOCAL_INPUT_FREE(iv_external, iv);
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004380}
4381
Gilles Peskine449bd832023-01-11 14:50:10 +01004382psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004383 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004385 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 size_t output_size,
4387 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004388{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004390
Gabor Mezei212eb082024-01-24 16:56:00 +01004391 LOCAL_INPUT_DECLARE(input_external, input);
4392 LOCAL_OUTPUT_DECLARE(output_external, output);
4393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004395 status = PSA_ERROR_BAD_STATE;
4396 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004397 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004400 status = PSA_ERROR_BAD_STATE;
4401 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004402 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004403
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004404 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004405 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 status = psa_driver_wrapper_cipher_update(operation,
4408 input,
4409 input_length,
4410 output,
4411 output_size,
4412 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004413
4414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (status != PSA_SUCCESS) {
4416 psa_cipher_abort(operation);
4417 }
Ronald Cron6d051732020-10-01 14:10:20 +02004418
Gabor Mezei212eb082024-01-24 16:56:00 +01004419 LOCAL_INPUT_FREE(input_external, input);
4420 LOCAL_OUTPUT_FREE(output_external, output);
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004423}
4424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004426 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 size_t output_size,
4428 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004429{
David Saadab4ecc272019-02-14 13:48:10 +02004430 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004431
Gabor Mezei212eb082024-01-24 16:56:00 +01004432 LOCAL_OUTPUT_DECLARE(output_external, output);
4433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004435 status = PSA_ERROR_BAD_STATE;
4436 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004437 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004440 status = PSA_ERROR_BAD_STATE;
4441 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004442 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004443
Gabor Mezei0b041162024-02-29 10:08:16 +00004444 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 status = psa_driver_wrapper_cipher_finish(operation,
4447 output,
4448 output_size,
4449 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004450
4451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 if (status == PSA_SUCCESS) {
Gabor Mezei212eb082024-01-24 16:56:00 +01004453 status = psa_cipher_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004455 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02004457 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004458
4459 LOCAL_OUTPUT_FREE(output_external, output);
4460
4461 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004462}
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004465{
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004467 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004468 * in use. It's ok to call abort on such an object, and there's
4469 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004471 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004474
Ronald Cron49fafa92021-03-10 08:34:23 +01004475 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004476 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004477 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004480}
4481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4483 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004484 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004486 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 size_t output_size,
4488 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004489{
4490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004491 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004492 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004493 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4494 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004495
David Horstmann62a56d92023-12-14 18:12:47 +00004496 LOCAL_INPUT_DECLARE(input_external, input);
4497 LOCAL_OUTPUT_DECLARE(output_external, output);
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004500 status = PSA_ERROR_INVALID_ARGUMENT;
4501 goto exit;
4502 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4505 PSA_KEY_USAGE_ENCRYPT,
4506 alg);
4507 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004508 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4512 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004513 status = PSA_ERROR_GENERIC_ERROR;
4514 goto exit;
4515 }
4516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (default_iv_length > 0) {
4518 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004519 status = PSA_ERROR_BUFFER_TOO_SMALL;
4520 goto exit;
4521 }
4522
David Horstmann6e99bb22024-02-06 15:27:49 +00004523 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004525 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004527 }
4528
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004529 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4530 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4531
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004532 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004533 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004534 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004535 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004537
4538exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004539 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004541 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004542 }
Ronald Cron23919522021-07-08 19:00:07 +02004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (status == PSA_SUCCESS) {
4545 if (default_iv_length > 0) {
4546 memcpy(output, local_iv, default_iv_length);
4547 }
4548 *output_length += default_iv_length;
4549 } else {
4550 *output_length = 0;
4551 }
4552
David Horstmann62a56d92023-12-14 18:12:47 +00004553 LOCAL_INPUT_FREE(input_external, input);
4554 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004557}
4558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4560 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004561 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004563 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 size_t output_size,
4565 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004566{
4567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004569 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004570
Gabor Mezei212eb082024-01-24 16:56:00 +01004571 LOCAL_INPUT_DECLARE(input_external, input);
4572 LOCAL_OUTPUT_DECLARE(output_external, output);
4573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004575 status = PSA_ERROR_INVALID_ARGUMENT;
4576 goto exit;
4577 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4580 PSA_KEY_USAGE_DECRYPT,
4581 alg);
4582 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4587 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004588 status = PSA_ERROR_INVALID_ARGUMENT;
4589 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004591 status = PSA_ERROR_INVALID_ARGUMENT;
4592 goto exit;
4593 }
4594
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004595 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4596 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4597
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004598 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004599 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004600 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004602
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004603exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004604 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004606 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004610 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 }
Ronald Cron23919522021-07-08 19:00:07 +02004612
Gabor Mezei212eb082024-01-24 16:56:00 +01004613 LOCAL_INPUT_FREE(input_external, input);
4614 LOCAL_OUTPUT_FREE(output_external, output);
4615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004617}
4618
4619
mohammad16035955c982018-04-26 00:53:03 +03004620/****************************************************************/
4621/* AEAD */
4622/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004623
Paul Elliottbaff51c2021-09-28 17:44:45 +01004624/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004625static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004626{
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004628}
4629
4630/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004631static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4632 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004633{
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004637#if defined(PSA_WANT_ALG_GCM)
4638 case PSA_ALG_GCM:
4639 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 * arbitrarily large nonces. Please note that we do not generally
4641 * recommend the usage of nonces of greater length than
4642 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4643 * size, which can then lead to collisions if you encrypt a very
4644 * large number of messages.*/
4645 if (nonce_length != 0) {
4646 return PSA_SUCCESS;
4647 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004648 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004649#endif /* PSA_WANT_ALG_GCM */
4650#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004651 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (nonce_length >= 7 && nonce_length <= 13) {
4653 return PSA_SUCCESS;
4654 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004655 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004656#endif /* PSA_WANT_ALG_CCM */
4657#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004658 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (nonce_length == 12) {
4660 return PSA_SUCCESS;
4661 } else if (nonce_length == 8) {
4662 return PSA_ERROR_NOT_SUPPORTED;
4663 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004664 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004665#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004666 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004667 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004669 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004672}
4673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004675{
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4677 return PSA_ERROR_INVALID_ARGUMENT;
4678 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004681}
4682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4684 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004685 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004687 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004689 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004691 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 size_t ciphertext_size,
4693 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004694{
Ronald Cron215633c2021-03-16 17:15:37 +01004695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4696 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004697
David Horstmann9d09a022023-11-28 19:25:00 +00004698 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4699 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4700 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4701 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4702
mohammad1603f08a5502018-06-03 15:05:47 +03004703 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 status = psa_aead_check_algorithm(alg);
4706 if (status != PSA_SUCCESS) {
4707 return status;
4708 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004709
Ronald Cron9a986162021-03-26 12:40:07 +01004710 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4712 if (status != PSA_SUCCESS) {
4713 return status;
4714 }
mohammad16035955c982018-04-26 00:53:03 +03004715
David Horstmann9d09a022023-11-28 19:25:00 +00004716 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4717 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4718 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4719 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 status = psa_aead_check_nonce_length(alg, nonce_length);
4722 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004725
Ronald Cronde822812021-03-17 16:08:20 +01004726 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004727 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004728 alg,
4729 nonce, nonce_length,
4730 additional_data, additional_data_length,
4731 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4735 memset(ciphertext, 0, ciphertext_size);
4736 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004737
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004738exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004739 LOCAL_INPUT_FREE(nonce_external, nonce);
4740 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4741 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4742 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4743
Ryan Everetta103ec92024-01-31 13:59:57 +00004744 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004747}
4748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4750 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00004751 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004753 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004755 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004757 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 size_t plaintext_size,
4759 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004760{
Ronald Cron215633c2021-03-16 17:15:37 +01004761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4762 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004763
David Horstmann7f2e0402023-11-28 19:43:53 +00004764 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4765 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4766 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4767 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4768
Gilles Peskineee652a32018-06-01 19:23:52 +02004769 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 status = psa_aead_check_algorithm(alg);
4772 if (status != PSA_SUCCESS) {
4773 return status;
4774 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004775
Ronald Cron9a986162021-03-26 12:40:07 +01004776 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4778 if (status != PSA_SUCCESS) {
4779 return status;
4780 }
mohammad16035955c982018-04-26 00:53:03 +03004781
David Horstmann7f2e0402023-11-28 19:43:53 +00004782 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4783 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4784 additional_data);
4785 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4786 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 status = psa_aead_check_nonce_length(alg, nonce_length);
4789 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004790 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004792
Ronald Cronde822812021-03-17 16:08:20 +01004793 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004794 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004795 alg,
4796 nonce, nonce_length,
4797 additional_data, additional_data_length,
4798 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (status != PSA_SUCCESS && plaintext_size != 0) {
4802 memset(plaintext, 0, plaintext_size);
4803 }
mohammad160360a64d02018-06-03 17:20:42 +03004804
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004805exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00004806 LOCAL_INPUT_FREE(nonce_external, nonce);
4807 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4808 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4809 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4810
Ryan Everetta103ec92024-01-31 13:59:57 +00004811 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 return status;
mohammad16035955c982018-04-26 00:53:03 +03004814}
4815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4817{
4818 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004821#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004823 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4825 return PSA_ERROR_INVALID_ARGUMENT;
4826 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004827 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004828#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004829
Przemek Stekiel86679c72022-10-06 17:06:56 +02004830#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004832 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4834 return PSA_ERROR_INVALID_ARGUMENT;
4835 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004836 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004837#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004838
Przemek Stekiel86679c72022-10-06 17:06:56 +02004839#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004841 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (tag_len != 16) {
4843 return PSA_ERROR_INVALID_ARGUMENT;
4844 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004845 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004846#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004847
4848 default:
4849 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004851 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004853}
4854
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004855/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4857 int is_encrypt,
4858 mbedtls_svc_key_id_t key,
4859 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004860{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4862 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4863 psa_key_slot_t *slot = NULL;
4864 psa_key_usage_t key_usage = 0;
4865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 status = psa_aead_check_algorithm(alg);
4867 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004868 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004872 status = PSA_ERROR_BAD_STATE;
4873 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004874 }
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if (operation->nonce_set || operation->lengths_set ||
4877 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004878 status = PSA_ERROR_BAD_STATE;
4879 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004883 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004885 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4889 alg);
4890 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004891 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004895 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 if (is_encrypt) {
4899 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004900 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 slot->key.data,
4902 slot->key.bytes,
4903 alg);
4904 } else {
4905 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004906 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 slot->key.data,
4908 slot->key.bytes,
4909 alg);
4910 }
4911 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004912 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004914
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004915 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004916
4917exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004918 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004919
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004921 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004923 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 } else {
4925 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004926 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004929}
4930
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004932psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4933 mbedtls_svc_key_id_t key,
4934 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935{
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937}
4938
4939/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004940psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4941 mbedtls_svc_key_id_t key,
4942 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943{
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945}
4946
David Horstmannfed23772023-12-19 17:49:20 +00004947static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
4948 const uint8_t *nonce,
4949 size_t nonce_length)
4950{
4951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4952
4953 if (operation->id == 0) {
4954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
4956 }
4957
4958 if (operation->nonce_set) {
4959 status = PSA_ERROR_BAD_STATE;
4960 goto exit;
4961 }
4962
4963 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4964 if (status != PSA_SUCCESS) {
4965 status = PSA_ERROR_INVALID_ARGUMENT;
4966 goto exit;
4967 }
4968
4969 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4970 nonce_length);
4971
4972exit:
4973 if (status == PSA_SUCCESS) {
4974 operation->nonce_set = 1;
4975 } else {
4976 psa_aead_abort(operation);
4977 }
4978
4979 return status;
4980}
4981
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004983psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00004984 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 size_t nonce_size,
4986 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987{
4988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004989 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004990 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991
David Horstmannd3cad8b2023-12-11 14:46:04 +00004992 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
4993 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
4994
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004995 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004998 status = PSA_ERROR_BAD_STATE;
4999 goto exit;
5000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003 status = PSA_ERROR_BAD_STATE;
5004 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005 }
5006
Paul Elliotte193ea82021-10-01 13:00:16 +01005007 /* For CCM, this size may not be correct according to the PSA
5008 * specification. The PSA Crypto 1.0.1 specification states:
5009 *
5010 * CCM encodes the plaintext length pLen in L octets, with L the smallest
5011 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
5012 *
5013 * However this restriction that L has to be the smallest integer is not
5014 * applied in practice, and it is not implementable here since the
5015 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
5017 operation->alg);
5018 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005019 status = PSA_ERROR_BUFFER_TOO_SMALL;
5020 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005021 }
5022
David Horstmann6e99bb22024-02-06 15:27:49 +00005023 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005025 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005027
David Horstmannfed23772023-12-19 17:49:20 +00005028 status = psa_aead_set_nonce_internal(operation, local_nonce,
5029 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030
Paul Elliott39dc6b82021-05-11 19:16:09 +01005031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (status == PSA_SUCCESS) {
5033 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 } else {
5036 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005037 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005038
David Horstmannd3cad8b2023-12-11 14:46:04 +00005039 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005042}
5043
5044/* Set the nonce for a multipart authenticated encryption or decryption
5045 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005046psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005047 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005049{
David Horstmannfed23772023-12-19 17:49:20 +00005050 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005051
David Horstmann8f0ef512023-12-11 15:17:11 +00005052 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5053 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005054
David Horstmannfed23772023-12-19 17:49:20 +00005055 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005056
David Horstmann18dc0322023-12-20 16:16:43 +00005057/* Exit label is only needed for buffer copying, prevent unused warnings. */
5058#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005059exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005060#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005061
David Horstmann8f0ef512023-12-11 15:17:11 +00005062 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005065}
5066
5067/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5069 size_t ad_length,
5070 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005075 status = PSA_ERROR_BAD_STATE;
5076 goto exit;
5077 }
5078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (operation->lengths_set || operation->ad_started ||
5080 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005081 status = PSA_ERROR_BAD_STATE;
5082 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005083 }
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005086#if defined(PSA_WANT_ALG_GCM)
5087 case PSA_ALG_GCM:
5088 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 * bits. Without the guard this code will generate warnings on 32bit
5090 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005091#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (((uint64_t) ad_length) >> 61 != 0 ||
5093 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005094 status = PSA_ERROR_INVALID_ARGUMENT;
5095 goto exit;
5096 }
Paul Elliott325d3742021-09-27 17:56:28 +01005097#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005098 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005099#endif /* PSA_WANT_ALG_GCM */
5100#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005101 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005103 status = PSA_ERROR_INVALID_ARGUMENT;
5104 goto exit;
5105 }
5106 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005107#endif /* PSA_WANT_ALG_CCM */
5108#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005109 case PSA_ALG_CHACHA20_POLY1305:
5110 /* No length restrictions for ChaChaPoly. */
5111 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005112#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005113 default:
5114 break;
5115 }
Paul Elliott325d3742021-09-27 17:56:28 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5118 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005119
Paul Elliott39dc6b82021-05-11 19:16:09 +01005120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005122 operation->ad_remaining = ad_length;
5123 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005124 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 } else {
5126 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005127 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005130}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005131
5132/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005134 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5138
David Horstmann25dac6e2023-12-11 15:23:13 +00005139 LOCAL_INPUT_DECLARE(input_external, input);
5140 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005143 status = PSA_ERROR_BAD_STATE;
5144 goto exit;
5145 }
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005148 status = PSA_ERROR_BAD_STATE;
5149 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005150 }
5151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (operation->lengths_set) {
5153 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005154 status = PSA_ERROR_INVALID_ARGUMENT;
5155 goto exit;
5156 }
5157
5158 operation->ad_remaining -= input_length;
5159 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005160#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005162 status = PSA_ERROR_BAD_STATE;
5163 goto exit;
5164 }
5165#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 status = psa_driver_wrapper_aead_update_ad(operation, input,
5168 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005169
Paul Elliott39dc6b82021-05-11 19:16:09 +01005170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005172 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 } else {
5174 psa_aead_abort(operation);
5175 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005176
David Horstmann25dac6e2023-12-11 15:23:13 +00005177 LOCAL_INPUT_FREE(input_external, input);
5178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005180}
5181
5182/* Encrypt or decrypt a message fragment in an active multipart AEAD
5183 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005184psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005185 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005187 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 size_t output_size,
5189 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005190{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005192
David Horstmann2914fac2023-12-11 15:28:37 +00005193
5194 LOCAL_INPUT_DECLARE(input_external, input);
5195 LOCAL_OUTPUT_DECLARE(output_external, output);
5196
5197 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5198 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5199
Paul Elliott302ff6b2021-04-20 18:10:30 +01005200 *output_length = 0;
5201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005203 status = PSA_ERROR_BAD_STATE;
5204 goto exit;
5205 }
5206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005208 status = PSA_ERROR_BAD_STATE;
5209 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005210 }
5211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005213 /* Additional data length was supplied, but not all the additional
5214 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005216 status = PSA_ERROR_INVALID_ARGUMENT;
5217 goto exit;
5218 }
5219
5220 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005222 status = PSA_ERROR_INVALID_ARGUMENT;
5223 goto exit;
5224 }
5225
5226 operation->body_remaining -= input_length;
5227 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005228#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005230 status = PSA_ERROR_BAD_STATE;
5231 goto exit;
5232 }
5233#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5236 output, output_size,
5237 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005238
Paul Elliott39dc6b82021-05-11 19:16:09 +01005239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005241 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 } else {
5243 psa_aead_abort(operation);
5244 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005245
David Horstmann2914fac2023-12-11 15:28:37 +00005246 LOCAL_INPUT_FREE(input_external, input);
5247 LOCAL_OUTPUT_FREE(output_external, output);
5248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005250}
5251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005253{
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (operation->id == 0 || !operation->nonce_set) {
5255 return PSA_ERROR_BAD_STATE;
5256 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5259 operation->body_remaining != 0)) {
5260 return PSA_ERROR_INVALID_ARGUMENT;
5261 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005264}
5265
Paul Elliott302ff6b2021-04-20 18:10:30 +01005266/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005268 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 size_t ciphertext_size,
5270 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005271 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 size_t tag_size,
5273 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005274{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005275 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5276
David Horstmann6db0e732023-12-11 15:35:59 +00005277 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5278 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5279
5280 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5281 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5282
Paul Elliott302ff6b2021-04-20 18:10:30 +01005283 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005284 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005285
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 status = psa_aead_final_checks(operation);
5287 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005292 status = PSA_ERROR_BAD_STATE;
5293 goto exit;
5294 }
5295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5297 ciphertext_size,
5298 ciphertext_length,
5299 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005300
Paul Elliott39dc6b82021-05-11 19:16:09 +01005301exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005302
5303
Paul Elliottf47b0952021-05-21 18:02:33 +01005304 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005305 * the zero tag size, make sure the tag is set to something implausible.
5306 * Even if the operation succeeds, make sure we clear the rest of the
5307 * buffer to prevent potential leakage of anything previously placed in
5308 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005309 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005312
David Horstmann6db0e732023-12-11 15:35:59 +00005313 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5314 LOCAL_OUTPUT_FREE(tag_external, tag);
5315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005317}
5318
5319/* Finish authenticating and decrypting a message in a multipart AEAD
5320 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005321psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005322 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 size_t plaintext_size,
5324 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005325 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005327{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5329
David Horstmanne000a0a2023-12-11 16:37:04 +00005330 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5331 LOCAL_INPUT_DECLARE(tag_external, tag);
5332
5333 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5334 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5335
Paul Elliott302ff6b2021-04-20 18:10:30 +01005336 *plaintext_length = 0;
5337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 status = psa_aead_final_checks(operation);
5339 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005342
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005344 status = PSA_ERROR_BAD_STATE;
5345 goto exit;
5346 }
5347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5349 plaintext_size,
5350 plaintext_length,
5351 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005352
5353exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005355
David Horstmanne000a0a2023-12-11 16:37:04 +00005356 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5357 LOCAL_INPUT_FREE(tag_external, tag);
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005360}
5361
5362/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005364{
5365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005368 /* The object has (apparently) been initialized but it is not (yet)
5369 * in use. It's ok to call abort on such an object, and there's
5370 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005372 }
5373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005379}
5380
Gilles Peskinee59236f2018-01-27 23:32:46 +01005381/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005382/* Generators */
5383/****************************************************************/
5384
Przemek Stekiel69c46792022-06-10 12:59:51 +02005385#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005386 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005387 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305388 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305389 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005390#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005391#endif /* At least one builtin KDF */
5392
Przemek Stekiel69c46792022-06-10 12:59:51 +02005393#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005394 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5395 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5396static psa_status_t psa_key_derivation_start_hmac(
5397 psa_mac_operation_t *operation,
5398 psa_algorithm_t hash_alg,
5399 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005401{
5402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5405 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5406 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005407
Steven Cooreman72f736a2021-05-07 14:14:37 +02005408 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 status = psa_driver_wrapper_mac_sign_setup(operation,
5412 &attributes,
5413 hmac_key, hmac_key_length,
5414 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 psa_reset_key_attributes(&attributes);
5417 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005418}
5419#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005420
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005421#define HKDF_STATE_INIT 0 /* no input yet */
5422#define HKDF_STATE_STARTED 1 /* got salt */
5423#define HKDF_STATE_KEYED 2 /* got key */
5424#define HKDF_STATE_OUTPUT 3 /* output started */
5425
Gilles Peskinecbe66502019-05-16 16:59:18 +02005426static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005428{
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5430 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5431 } else {
5432 return operation->alg;
5433 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434}
5435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005437{
5438 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5440 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 /* The object has (apparently) been initialized but it is not
5442 * in use. It's ok to call abort on such an object, and there's
5443 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005445#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5447 mbedtls_free(operation->ctx.hkdf.info);
5448 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5449 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005450#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005451#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5452 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5454 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5455 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5456 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005457 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005459 }
5460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005462 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005464 }
5465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005467 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005469 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005470#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005472 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005474 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005475#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005476 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005477
5478 /* We leave the fields Ai and output_block to be erased safely by the
5479 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005481#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5482 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005483#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5485 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5486 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5487 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005488#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305489#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305490 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305491 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005492 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305493 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305494 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305495
5496 status = PSA_SUCCESS;
5497 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305498#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005499 {
5500 status = PSA_ERROR_BAD_STATE;
5501 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 mbedtls_platform_zeroize(operation, sizeof(*operation));
5503 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005504}
5505
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508{
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005512 }
5513
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516}
5517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5519 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005520{
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (operation->alg == 0) {
5522 return PSA_ERROR_BAD_STATE;
5523 }
5524 if (capacity > operation->capacity) {
5525 return PSA_ERROR_INVALID_ARGUMENT;
5526 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005529}
5530
Przemek Stekiel69c46792022-06-10 12:59:51 +02005531#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005532/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005533static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5534 psa_algorithm_t kdf_alg,
5535 uint8_t *output,
5536 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005537{
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5539 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005540 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005541 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005542#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005544#else
5545 const uint8_t last_block = 0xff;
5546#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (hkdf->state < HKDF_STATE_KEYED ||
5549 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005550#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 )) {
5554 return PSA_ERROR_BAD_STATE;
5555 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005556 hkdf->state = HKDF_STATE_OUTPUT;
5557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005559 /* Copy what remains of the current block */
5560 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005562 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 }
5564 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005565 output += n;
5566 output_length -= n;
5567 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005569 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005571 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005572 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005574 * object was corrupted or if this function is called directly
5575 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 if (hkdf->block_number == last_block) {
5577 return PSA_ERROR_BAD_STATE;
5578 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005579
5580 /* We need a new block */
5581 ++hkdf->block_number;
5582 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5585 hash_alg,
5586 hkdf->prk,
5587 hash_length);
5588 if (status != PSA_SUCCESS) {
5589 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005590 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005591
5592 if (hkdf->block_number != 1) {
5593 status = psa_mac_update(&hkdf->hmac,
5594 hkdf->output_block,
5595 hash_length);
5596 if (status != PSA_SUCCESS) {
5597 return status;
5598 }
5599 }
5600 status = psa_mac_update(&hkdf->hmac,
5601 hkdf->info,
5602 hkdf->info_length);
5603 if (status != PSA_SUCCESS) {
5604 return status;
5605 }
5606 status = psa_mac_update(&hkdf->hmac,
5607 &hkdf->block_number, 1);
5608 if (status != PSA_SUCCESS) {
5609 return status;
5610 }
5611 status = psa_mac_sign_finish(&hkdf->hmac,
5612 hkdf->output_block,
5613 sizeof(hkdf->output_block),
5614 &hmac_output_length);
5615 if (status != PSA_SUCCESS) {
5616 return status;
5617 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005618 }
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005621}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005622#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005623
John Durkop07cc04a2020-11-16 22:08:34 -08005624#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5625 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005626static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5627 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005629{
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5631 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005632 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5633 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005634 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005635
Janos Follath7742fee2019-06-17 12:58:10 +01005636 /* We can't be wanting more output after block 0xff, otherwise
5637 * the capacity check in psa_key_derivation_output_bytes() would have
5638 * prevented this call. It could happen only if the operation
5639 * object was corrupted or if this function is called directly
5640 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (tls12_prf->block_number == 0xff) {
5642 return PSA_ERROR_CORRUPTION_DETECTED;
5643 }
Janos Follath7742fee2019-06-17 12:58:10 +01005644
5645 /* We need a new block */
5646 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005647 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005648
5649 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5650 *
5651 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5652 *
5653 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5654 * HMAC_hash(secret, A(2) + seed) +
5655 * HMAC_hash(secret, A(3) + seed) + ...
5656 *
5657 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005658 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005659 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005660 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005661 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005662 * is currently extracted as `output_block` and where i is
5663 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005664 */
5665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 status = psa_key_derivation_start_hmac(&hmac,
5667 hash_alg,
5668 tls12_prf->secret,
5669 tls12_prf->secret_length);
5670 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005671 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005673
5674 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005676 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5677 * the variable seed and in this instance means it in the context of the
5678 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 status = psa_mac_update(&hmac,
5680 tls12_prf->label,
5681 tls12_prf->label_length);
5682 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005683 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 }
5685 status = psa_mac_update(&hmac,
5686 tls12_prf->seed,
5687 tls12_prf->seed_length);
5688 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005689 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 }
5691 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005692 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5694 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005695 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005697 }
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 status = psa_mac_sign_finish(&hmac,
5700 tls12_prf->Ai, hash_length,
5701 &hmac_output_length);
5702 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005703 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 }
5705 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005706 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005708
5709 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 status = psa_key_derivation_start_hmac(&hmac,
5711 hash_alg,
5712 tls12_prf->secret,
5713 tls12_prf->secret_length);
5714 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005715 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 }
5717 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5718 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005719 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 }
5721 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5722 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005723 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 }
5725 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5726 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005727 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 }
5729 status = psa_mac_sign_finish(&hmac,
5730 tls12_prf->output_block, hash_length,
5731 &hmac_output_length);
5732 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005733 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005735
Janos Follath7742fee2019-06-17 12:58:10 +01005736
5737cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 cleanup_status = psa_mac_abort(&hmac);
5739 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005740 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005744}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005745
Janos Follath844eb0e2019-06-19 12:10:49 +01005746static psa_status_t psa_key_derivation_tls12_prf_read(
5747 psa_tls12_prf_key_derivation_t *tls12_prf,
5748 psa_algorithm_t alg,
5749 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005751{
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5753 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005754 psa_status_t status;
5755 uint8_t offset, length;
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005758 case PSA_TLS12_PRF_STATE_LABEL_SET:
5759 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5760 break;
5761 case PSA_TLS12_PRF_STATE_OUTPUT:
5762 break;
5763 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005765 }
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005768 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 if (tls12_prf->left_in_block == 0) {
5770 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5771 alg);
5772 if (status != PSA_SUCCESS) {
5773 return status;
5774 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005775
5776 continue;
5777 }
5778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005780 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005782 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005784
5785 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005787 output += length;
5788 output_length -= length;
5789 tls12_prf->left_in_block -= length;
5790 }
5791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005793}
John Durkop07cc04a2020-11-16 22:08:34 -08005794#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5795 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005796
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005797#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5798static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5799 psa_tls12_ecjpake_to_pms_t *ecjpake,
5800 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005802{
5803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005804 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 if (output_length != 32) {
5807 return PSA_ERROR_INVALID_ARGUMENT;
5808 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5811 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5812 &output_size);
5813 if (status != PSA_SUCCESS) {
5814 return status;
5815 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 if (output_size != output_length) {
5818 return PSA_ERROR_GENERIC_ERROR;
5819 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005822}
5823#endif
5824
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305825#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305826static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5827 psa_pbkdf2_key_derivation_t *pbkdf2,
5828 psa_algorithm_t prf_alg,
5829 uint8_t prf_output_length,
5830 psa_key_attributes_t *attributes)
5831{
5832 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305833 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305834 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305835 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305836 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305837 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305838 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305839
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305840 mac_operation.is_sign = 1;
5841 mac_operation.mac_size = prf_output_length;
5842 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305843
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305844 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5845 attributes,
5846 pbkdf2->password,
5847 pbkdf2->password_length,
5848 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305849 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305850 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305851 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305852 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5853 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305854 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305855 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305856 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305857 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305858 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305859 }
5860 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5861 &mac_output_length);
5862 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305863 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305864 }
5865
5866 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305867 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305868 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305869 }
5870
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305871 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305872
5873 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305874 /* We are passing prf_output_length as mac_size because the driver
5875 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305876 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305877 status = psa_driver_wrapper_mac_compute(attributes,
5878 pbkdf2->password,
5879 pbkdf2->password_length,
5880 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305881 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305882 &mac_output_length);
5883 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305884 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305885 }
5886
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305887 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305888 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305889
5890cleanup:
5891 /* Zeroise buffers to clear sensitive data from memory. */
5892 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5893 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305894}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305895
5896static psa_status_t psa_key_derivation_pbkdf2_read(
5897 psa_pbkdf2_key_derivation_t *pbkdf2,
5898 psa_algorithm_t kdf_alg,
5899 uint8_t *output,
5900 size_t output_length)
5901{
5902 psa_status_t status;
5903 psa_algorithm_t prf_alg;
5904 uint8_t prf_output_length;
5905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5906 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5907 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5908
5909 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5910 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5911 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5912 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305913 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5914 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305915 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305916 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305917 } else {
5918 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305919 }
5920
5921 switch (pbkdf2->state) {
5922 case PSA_PBKDF2_STATE_PASSWORD_SET:
5923 /* Initially we need a new block so bytes_used is equal to block size*/
5924 pbkdf2->bytes_used = prf_output_length;
5925 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5926 break;
5927 case PSA_PBKDF2_STATE_OUTPUT:
5928 break;
5929 default:
5930 return PSA_ERROR_BAD_STATE;
5931 }
5932
5933 while (output_length != 0) {
5934 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5935 if (n > output_length) {
5936 n = (uint8_t) output_length;
5937 }
5938 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5939 output += n;
5940 output_length -= n;
5941 pbkdf2->bytes_used += n;
5942
5943 if (output_length == 0) {
5944 break;
5945 }
5946
5947 /* We need a new block */
5948 pbkdf2->bytes_used = 0;
5949 pbkdf2->block_number++;
5950
5951 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5952 prf_output_length,
5953 &attributes);
5954 if (status != PSA_SUCCESS) {
5955 return status;
5956 }
5957 }
5958
5959 return PSA_SUCCESS;
5960}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305961#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305962
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005963psa_status_t psa_key_derivation_output_bytes(
5964 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00005965 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005967{
5968 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00005969 LOCAL_OUTPUT_DECLARE(output_external, output);
5970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005974 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005976 }
5977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005979 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005980 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005981 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5982 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005983 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005984 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005986 }
Ryan Everettf943e222024-01-19 14:46:39 +00005987
5988 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00005989 if (output_length > operation->capacity) {
5990 operation->capacity = 0;
5991 /* Go through the error path to wipe all confidential data now
5992 * that the operation object is useless. */
5993 status = PSA_ERROR_INSUFFICIENT_DATA;
5994 goto exit;
5995 }
5996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005997 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005998
Przemek Stekiel69c46792022-06-10 12:59:51 +02005999#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6001 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
6002 output, output_length);
6003 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006004#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006005#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6006 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6008 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6009 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
6010 kdf_alg, output,
6011 output_length);
6012 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006013#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
6014 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006015#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006017 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6019 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006020#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306021#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306022 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306023 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6024 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306025 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306026#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006027
Gilles Peskineeab56e42018-07-12 17:12:33 +02006028 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006029 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006030 status = PSA_ERROR_BAD_STATE;
6031 LOCAL_OUTPUT_FREE(output_external, output);
6032
6033 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006034 }
6035
6036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006038 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006039 * This allows us to differentiate between exhausted operations and
6040 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6041 * operations. */
6042 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006044 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006045 if (output != NULL) {
6046 memset(output, '!', output_length);
6047 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006048 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006049
6050 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006052}
6053
David Brown7807bf72021-02-09 16:28:23 -07006054#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006055static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006056{
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (data_size >= 8) {
6058 mbedtls_des_key_set_parity(data);
6059 }
6060 if (data_size >= 16) {
6061 mbedtls_des_key_set_parity(data + 8);
6062 }
6063 if (data_size >= 24) {
6064 mbedtls_des_key_set_parity(data + 16);
6065 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006066}
David Brown7807bf72021-02-09 16:28:23 -07006067#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006068
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006069/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 * ECC keys on a Weierstrass elliptic curve require the generation
6071 * of a private key which is an integer
6072 * in the range [1, N - 1], where N is the boundary of the private key domain:
6073 * N is the prime p for Diffie-Hellman, or the order of the
6074 * curve’s base point for ECC.
6075 *
6076 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6077 * This function generates the private key using the following process:
6078 *
6079 * 1. Draw a byte string of length ceiling(m/8) bytes.
6080 * 2. If m is not a multiple of 8, set the most significant
6081 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6082 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6083 * 4. If k > N - 2, discard the result and return to step 1.
6084 * 5. Output k + 1 as the private key.
6085 *
6086 * This method allows compliance to NIST standards, specifically the methods titled
6087 * Key-Pair Generation by Testing Candidates in the following publications:
6088 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6089 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6090 * Diffie-Hellman keys.
6091 *
6092 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6093 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6094 *
6095 * Note: Function allocates memory for *data buffer, so given *data should be
6096 * always NULL.
6097 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006098#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6099#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006100static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006101 psa_key_slot_t *slot,
6102 size_t bits,
6103 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006104 uint8_t **data
6105 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006106{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006107 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006108 mbedtls_mpi k;
6109 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006112 size_t m;
6113 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 mbedtls_mpi_init(&k);
6116 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006117
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006118 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006120 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006121 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006124 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006125 goto cleanup;
6126 }
6127
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006128 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006132
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006133 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006134 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006135 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006136
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006137 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006138
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006139 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006141
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006142 /* Note: This function is always called with *data == NULL and it
6143 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 *data = mbedtls_calloc(1, m_bytes);
6145 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006146 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006147 goto cleanup;
6148 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006151 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006153 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006155
6156 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006158 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006159 * (8 * ceiling(m/8) - m) bits of the first byte in
6160 * the string to zero.
6161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006163 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006164 }
6165
6166 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 * big-endian byte string.
6168 */
6169 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006170
6171 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 * Result of comparison is returned. When it indicates error
6173 * then this function is called again.
6174 */
6175 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006176 }
6177
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006178 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6180 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006181cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (ret != 0) {
6183 status = mbedtls_to_psa_error(ret);
6184 }
6185 if (status != PSA_SUCCESS) {
6186 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006187 *data = NULL;
6188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 mbedtls_mpi_free(&k);
6190 mbedtls_mpi_free(&diff_N_2);
6191 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006192}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006193
6194/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6195 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6196 *
6197 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6198 * draw a 32-byte string and process it as specified in
6199 * Elliptic Curves for Security [RFC7748] §5.
6200 *
6201 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6202 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6203 *
6204 * Note: Function allocates memory for *data buffer, so given *data should be
6205 * always NULL.
6206 */
6207
6208static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6209 size_t bits,
6210 psa_key_derivation_operation_t *operation,
6211 uint8_t **data
6212 )
6213{
6214 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006218 case 255:
6219 output_length = 32;
6220 break;
6221 case 448:
6222 output_length = 56;
6223 break;
6224 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006226 break;
6227 }
6228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 if (*data == NULL) {
6232 return PSA_ERROR_INSUFFICIENT_MEMORY;
6233 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006238 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006242 case 255:
6243 (*data)[0] &= 248;
6244 (*data)[31] &= 127;
6245 (*data)[31] |= 64;
6246 break;
6247 case 448:
6248 (*data)[0] &= 252;
6249 (*data)[55] |= 128;
6250 break;
6251 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006253 break;
6254 }
6255
6256 return status;
6257}
Valerio Setti86587ab2023-06-27 13:09:30 +02006258#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6259static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6260 psa_key_slot_t *slot, size_t bits,
6261 psa_key_derivation_operation_t *operation, uint8_t **data)
6262{
6263 (void) slot;
6264 (void) bits;
6265 (void) operation;
6266 (void) data;
6267 return PSA_ERROR_NOT_SUPPORTED;
6268}
6269
6270static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6271 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6272{
6273 (void) bits;
6274 (void) operation;
6275 (void) data;
6276 return PSA_ERROR_NOT_SUPPORTED;
6277}
6278#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6279#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006280
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006281static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006282 psa_key_slot_t *slot,
6283 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006285{
6286 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306288 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6292 return PSA_ERROR_INVALID_ARGUMENT;
6293 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006294
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006295#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006296 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6298 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6299 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006300 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6302 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 }
6305 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006306 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6308 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006311 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006312 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006313#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006314 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 if (key_type_is_raw_bytes(slot->attr.type)) {
6316 if (bits % 8 != 0) {
6317 return PSA_ERROR_INVALID_ARGUMENT;
6318 }
6319 data = mbedtls_calloc(1, bytes);
6320 if (data == NULL) {
6321 return PSA_ERROR_INSUFFICIENT_MEMORY;
6322 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 status = psa_key_derivation_output_bytes(operation, data, bytes);
6325 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006326 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 }
David Brown12ca5032021-02-11 11:02:00 -07006328#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6330 psa_des_set_key_parity(data, bytes);
6331 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006332#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 } else {
6334 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006335 }
Ronald Crondd04d422020-11-28 15:14:42 +01006336
Ronald Cronbf33c932020-11-28 18:06:53 +01006337 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006338
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006339 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006340 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 &storage_size);
6342 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306343 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 }
Archanad8a83dc2021-06-14 10:04:16 +05306345 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 status = psa_allocate_buffer_to_slot(slot, storage_size);
6347 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 }
Archanad8a83dc2021-06-14 10:04:16 +05306350
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006351 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 data, bytes,
6353 slot->key.data,
6354 slot->key.bytes,
6355 &slot->key.bytes, &bits);
6356 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006357 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006359
6360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 mbedtls_free(data);
6362 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006363}
6364
Gilles Peskine092ce512024-02-20 12:31:24 +01006365static const psa_key_production_parameters_t default_production_parameters =
6366 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006367
Gilles Peskine092ce512024-02-20 12:31:24 +01006368int psa_key_production_parameters_are_default(
6369 const psa_key_production_parameters_t *params,
6370 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006371{
Gilles Peskine092ce512024-02-20 12:31:24 +01006372 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006373 return 0;
6374 }
Gilles Peskine092ce512024-02-20 12:31:24 +01006375 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006376 return 0;
6377 }
6378 return 1;
6379}
6380
6381psa_status_t psa_key_derivation_output_key_ext(
6382 const psa_key_attributes_t *attributes,
6383 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006384 const psa_key_production_parameters_t *params,
6385 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006386 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006387{
6388 psa_status_t status;
6389 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006390 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006391
Ronald Cron81709fc2020-11-14 12:10:32 +01006392 *key = MBEDTLS_SVC_KEY_ID_INIT;
6393
Gilles Peskine0f84d622019-09-12 19:03:13 +02006394 /* Reject any attempt to create a zero-length key so that we don't
6395 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (psa_get_key_bits(attributes) == 0) {
6397 return PSA_ERROR_INVALID_ARGUMENT;
6398 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006399
Gilles Peskine092ce512024-02-20 12:31:24 +01006400 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006401 return PSA_ERROR_INVALID_ARGUMENT;
6402 }
6403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 if (operation->alg == PSA_ALG_NONE) {
6405 return PSA_ERROR_BAD_STATE;
6406 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (!operation->can_output_key) {
6409 return PSA_ERROR_NOT_PERMITTED;
6410 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6413 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006414#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006416 /* Deriving a key in a secure element is not implemented yet. */
6417 status = PSA_ERROR_NOT_SUPPORTED;
6418 }
6419#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (status == PSA_SUCCESS) {
6421 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006422 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006424 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 if (status == PSA_SUCCESS) {
6426 status = psa_finish_key_creation(slot, driver, key);
6427 }
6428 if (status != PSA_SUCCESS) {
6429 psa_fail_key_creation(slot, driver);
6430 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006433}
6434
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006435psa_status_t psa_key_derivation_output_key(
6436 const psa_key_attributes_t *attributes,
6437 psa_key_derivation_operation_t *operation,
6438 mbedtls_svc_key_id_t *key)
6439{
Gilles Peskinec81393b2024-02-14 20:51:28 +01006440 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006441 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01006442 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006443}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006444
6445
6446/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006447/* Key derivation */
6448/****************************************************************/
6449
Steven Cooreman932ffb72021-02-15 12:14:32 +01006450#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006452{
6453#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6455 return 1;
6456 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006457#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006458#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6460 return 1;
6461 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006462#endif
6463#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6465 return 1;
6466 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006467#endif
6468#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6470 return 1;
6471 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006472#endif
6473#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6475 return 1;
6476 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006477#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6480 return 1;
6481 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006482#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306483#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6484 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6485 return 1;
6486 }
6487#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306488#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6489 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6490 return 1;
6491 }
6492#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006494}
6495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006497{
6498 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 psa_status_t status = psa_hash_setup(&operation, alg);
6500 psa_hash_abort(&operation);
6501 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006502}
6503
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306504static psa_status_t psa_key_derivation_set_maximum_capacity(
6505 psa_key_derivation_operation_t *operation,
6506 psa_algorithm_t kdf_alg)
6507{
6508#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6509 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6510 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6511 return PSA_SUCCESS;
6512 }
6513#endif
6514#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6515 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6516#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306517 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306518 PSA_KEY_TYPE_AES,
6519 128U,
6520 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306521#else
6522 operation->capacity = SIZE_MAX;
6523#endif
6524 return PSA_SUCCESS;
6525 }
6526#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6527
6528 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6529 * invalid or meaningless but it does not affect this function */
6530 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6531 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306532 if (hash_size == 0) {
6533 return PSA_ERROR_NOT_SUPPORTED;
6534 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306535
6536 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6537 * we might fail later, which is somewhat unfriendly and potentially
6538 * risk-prone. */
6539 psa_status_t status = psa_hash_try_support(hash_alg);
6540 if (status != PSA_SUCCESS) {
6541 return status;
6542 }
6543
6544#if defined(PSA_WANT_ALG_HKDF)
6545 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6546 operation->capacity = 255 * hash_size;
6547 } else
6548#endif
6549#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6550 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6551 operation->capacity = hash_size;
6552 } else
6553#endif
6554#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6555 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6556 operation->capacity = 255 * hash_size;
6557 } else
6558#endif
6559#if defined(PSA_WANT_ALG_TLS12_PRF)
6560 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6561 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6562 operation->capacity = SIZE_MAX;
6563 } else
6564#endif
6565#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6566 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6567 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6568 /* Master Secret is always 48 bytes
6569 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6570 operation->capacity = 48U;
6571 } else
6572#endif
6573#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6574 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6575#if (SIZE_MAX > UINT32_MAX)
6576 operation->capacity = UINT32_MAX * hash_size;
6577#else
6578 operation->capacity = SIZE_MAX;
6579#endif
6580 } else
6581#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6582 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306583 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306584 status = PSA_ERROR_NOT_SUPPORTED;
6585 }
6586 return status;
6587}
6588
Gilles Peskine969c5d62019-01-16 15:53:06 +01006589static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006590 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006592{
Janos Follath5fe19732019-06-20 15:09:30 +01006593 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6594 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006596
Gilles Peskine969c5d62019-01-16 15:53:06 +01006597 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 if (!is_kdf_alg_supported(kdf_alg)) {
6599 return PSA_ERROR_NOT_SUPPORTED;
6600 }
John Durkop07cc04a2020-11-16 22:08:34 -08006601
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306602 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6603 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306604 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006605}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006608{
6609#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 if (alg == PSA_ALG_ECDH) {
6611 return PSA_SUCCESS;
6612 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006613#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006614#if defined(PSA_WANT_ALG_FFDH)
6615 if (alg == PSA_ALG_FFDH) {
6616 return PSA_SUCCESS;
6617 }
6618#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006619 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006621}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006622
6623static int psa_key_derivation_allows_free_form_secret_input(
6624 psa_algorithm_t kdf_alg)
6625{
6626#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6627 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6628 return 0;
6629 }
6630#endif
6631 (void) kdf_alg;
6632 return 1;
6633}
John Durkop07cc04a2020-11-16 22:08:34 -08006634#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006635
Gilles Peskine449bd832023-01-11 14:50:10 +01006636psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6637 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006638{
6639 psa_status_t status;
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 if (operation->alg != 0) {
6642 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006643 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6646 return PSA_ERROR_INVALID_ARGUMENT;
6647 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6648#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6649 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6650 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6651 status = psa_key_agreement_try_support(ka_alg);
6652 if (status != PSA_SUCCESS) {
6653 return status;
6654 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006655 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6656 return PSA_ERROR_INVALID_ARGUMENT;
6657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6659#else
6660 return PSA_ERROR_NOT_SUPPORTED;
6661#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6662 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6663#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6664 status = psa_key_derivation_setup_kdf(operation, alg);
6665#else
6666 return PSA_ERROR_NOT_SUPPORTED;
6667#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6668 } else {
6669 return PSA_ERROR_INVALID_ARGUMENT;
6670 }
6671
6672 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006673 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 }
6675 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006676}
6677
Przemek Stekiel69c46792022-06-10 12:59:51 +02006678#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006679static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6680 psa_algorithm_t kdf_alg,
6681 psa_key_derivation_step_t step,
6682 const uint8_t *data,
6683 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006684{
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006686 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006688 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006689#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6691 return PSA_ERROR_INVALID_ARGUMENT;
6692 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006693#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if (hkdf->state != HKDF_STATE_INIT) {
6695 return PSA_ERROR_BAD_STATE;
6696 } else {
6697 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6698 hash_alg,
6699 data, data_length);
6700 if (status != PSA_SUCCESS) {
6701 return status;
6702 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006703 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006705 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006706 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006707#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006709 /* We shouldn't be in different state as HKDF_EXPAND only allows
6710 * two inputs: SECRET (this case) and INFO which does not modify
6711 * the state. It could happen only if the hkdf
6712 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (hkdf->state != HKDF_STATE_INIT) {
6714 return PSA_ERROR_BAD_STATE;
6715 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006716
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006717 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6719 return PSA_ERROR_INVALID_ARGUMENT;
6720 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006721
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 memcpy(hkdf->prk, data, data_length);
6723 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006724#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006725 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006726 /* HKDF: If no salt was provided, use an empty salt.
6727 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006729#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6731 return PSA_ERROR_BAD_STATE;
6732 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006733#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6735 hash_alg,
6736 NULL, 0);
6737 if (status != PSA_SUCCESS) {
6738 return status;
6739 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006740 hkdf->state = HKDF_STATE_STARTED;
6741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 if (hkdf->state != HKDF_STATE_STARTED) {
6743 return PSA_ERROR_BAD_STATE;
6744 }
6745 status = psa_mac_update(&hkdf->hmac,
6746 data, data_length);
6747 if (status != PSA_SUCCESS) {
6748 return status;
6749 }
6750 status = psa_mac_sign_finish(&hkdf->hmac,
6751 hkdf->prk,
6752 sizeof(hkdf->prk),
6753 &data_length);
6754 if (status != PSA_SUCCESS) {
6755 return status;
6756 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006757 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006758
Gilles Peskine2b522db2019-04-12 15:11:49 +02006759 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006760 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006761#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006763 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006765 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006767#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006768 {
6769 /* Block 0 is empty, and the next block will be
6770 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006772 }
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006775 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006776#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6778 return PSA_ERROR_INVALID_ARGUMENT;
6779 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006780#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006781#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6783 hkdf->state == HKDF_STATE_INIT) {
6784 return PSA_ERROR_BAD_STATE;
6785 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006786#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 if (hkdf->state == HKDF_STATE_OUTPUT) {
6788 return PSA_ERROR_BAD_STATE;
6789 }
6790 if (hkdf->info_set) {
6791 return PSA_ERROR_BAD_STATE;
6792 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006793 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (data_length != 0) {
6795 hkdf->info = mbedtls_calloc(1, data_length);
6796 if (hkdf->info == NULL) {
6797 return PSA_ERROR_INSUFFICIENT_MEMORY;
6798 }
6799 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006800 }
6801 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006803 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006805 }
6806}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006807#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006808
John Durkop07cc04a2020-11-16 22:08:34 -08006809#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6810 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006811static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6812 const uint8_t *data,
6813 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006814{
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6816 return PSA_ERROR_BAD_STATE;
6817 }
Janos Follathf08e2652019-06-13 09:05:41 +01006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 if (data_length != 0) {
6820 prf->seed = mbedtls_calloc(1, data_length);
6821 if (prf->seed == NULL) {
6822 return PSA_ERROR_INSUFFICIENT_MEMORY;
6823 }
Janos Follathf08e2652019-06-13 09:05:41 +01006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006826 prf->seed_length = data_length;
6827 }
Janos Follathf08e2652019-06-13 09:05:41 +01006828
Gilles Peskine43f958b2020-12-13 14:55:14 +01006829 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006832}
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6835 const uint8_t *data,
6836 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006837{
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6839 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6840 return PSA_ERROR_BAD_STATE;
6841 }
Janos Follath81550542019-06-13 14:26:34 +01006842
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 if (data_length != 0) {
6844 prf->secret = mbedtls_calloc(1, data_length);
6845 if (prf->secret == NULL) {
6846 return PSA_ERROR_INSUFFICIENT_MEMORY;
6847 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006850 prf->secret_length = data_length;
6851 }
Janos Follath81550542019-06-13 14:26:34 +01006852
Gilles Peskine43f958b2020-12-13 14:55:14 +01006853 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006856}
6857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6859 const uint8_t *data,
6860 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006861{
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6863 return PSA_ERROR_BAD_STATE;
6864 }
Janos Follath63028dd2019-06-13 09:15:47 +01006865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if (data_length != 0) {
6867 prf->label = mbedtls_calloc(1, data_length);
6868 if (prf->label == NULL) {
6869 return PSA_ERROR_INSUFFICIENT_MEMORY;
6870 }
Janos Follath63028dd2019-06-13 09:15:47 +01006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006873 prf->label_length = data_length;
6874 }
Janos Follath63028dd2019-06-13 09:15:47 +01006875
Gilles Peskine43f958b2020-12-13 14:55:14 +01006876 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006877
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006879}
6880
Gilles Peskine449bd832023-01-11 14:50:10 +01006881static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6882 psa_key_derivation_step_t step,
6883 const uint8_t *data,
6884 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006885{
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006887 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006889 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006891 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006893 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006895 }
6896}
John Durkop07cc04a2020-11-16 22:08:34 -08006897#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6898 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6899
6900#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6901static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6902 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006903 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006905{
6906 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6908 4 + data_length + prf->other_secret_length :
6909 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6912 return PSA_ERROR_INVALID_ARGUMENT;
6913 }
John Durkop07cc04a2020-11-16 22:08:34 -08006914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 uint8_t *pms = mbedtls_calloc(1, pms_len);
6916 if (pms == NULL) {
6917 return PSA_ERROR_INSUFFICIENT_MEMORY;
6918 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006919 uint8_t *cur = pms;
6920
6921 /* pure-PSK:
6922 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006923 *
6924 * The premaster secret is formed as follows: if the PSK is N octets
6925 * long, concatenate a uint16 with the value N, N zero octets, a second
6926 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006927 *
6928 * mixed-PSK:
6929 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6930 * follows: concatenate a uint16 with the length of the other secret,
6931 * the other secret itself, uint16 with the length of PSK, and the
6932 * PSK itself.
6933 * For details please check:
6934 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6935 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6936 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006937 */
6938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6940 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6941 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6942 if (prf->other_secret_length != 0) {
6943 memcpy(cur, prf->other_secret, prf->other_secret_length);
6944 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006945 cur += prf->other_secret_length;
6946 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 } else {
6948 *cur++ = MBEDTLS_BYTE_1(data_length);
6949 *cur++ = MBEDTLS_BYTE_0(data_length);
6950 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006951 cur += data_length;
6952 }
6953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 *cur++ = MBEDTLS_BYTE_1(data_length);
6955 *cur++ = MBEDTLS_BYTE_0(data_length);
6956 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006957 cur += data_length;
6958
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006959 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006960
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006961 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006963}
Janos Follath6660f0e2019-06-17 08:44:03 +01006964
Przemek Stekiele47201b2022-04-19 13:53:28 +02006965static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6966 psa_tls12_prf_key_derivation_t *prf,
6967 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006969{
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6971 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006972 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006973
6974 if (data_length != 0) {
6975 prf->other_secret = mbedtls_calloc(1, data_length);
6976 if (prf->other_secret == NULL) {
6977 return PSA_ERROR_INSUFFICIENT_MEMORY;
6978 }
6979
6980 memcpy(prf->other_secret, data, data_length);
6981 prf->other_secret_length = data_length;
6982 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006983 prf->other_secret_length = 0;
6984 }
6985
6986 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006989}
6990
Janos Follath6660f0e2019-06-17 08:44:03 +01006991static psa_status_t psa_tls12_prf_psk_to_ms_input(
6992 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006993 psa_key_derivation_step_t step,
6994 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006996{
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006998 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return psa_tls12_prf_psk_to_ms_set_key(prf,
7000 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007001 break;
7002 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
7004 data,
7005 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007006 break;
7007 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02007009 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01007010
Przemek Stekiele47201b2022-04-19 13:53:28 +02007011 }
Janos Follath6660f0e2019-06-17 08:44:03 +01007012}
John Durkop07cc04a2020-11-16 22:08:34 -08007013#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007014
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007015#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
7016static psa_status_t psa_tls12_ecjpake_to_pms_input(
7017 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04007018 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007019 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007021{
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7023 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7024 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007025 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007026
7027 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (data[0] != 0x04) {
7029 return PSA_ERROR_INVALID_ARGUMENT;
7030 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007031
7032 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007036}
7037#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307038
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307039#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307040static psa_status_t psa_pbkdf2_set_input_cost(
7041 psa_pbkdf2_key_derivation_t *pbkdf2,
7042 psa_key_derivation_step_t step,
7043 uint64_t data)
7044{
7045 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7046 return PSA_ERROR_INVALID_ARGUMENT;
7047 }
7048
7049 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7050 return PSA_ERROR_BAD_STATE;
7051 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307052
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307053 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307054 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307055 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307056
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307057 if (data == 0) {
7058 return PSA_ERROR_INVALID_ARGUMENT;
7059 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307060
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307061 pbkdf2->input_cost = data;
7062 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7063
7064 return PSA_SUCCESS;
7065}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307066
7067static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7068 const uint8_t *data,
7069 size_t data_length)
7070{
Gilles Peskineead17662023-08-20 22:02:51 +02007071 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7072 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7073 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7074 /* Appending to existing salt. No state change. */
7075 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307076 return PSA_ERROR_BAD_STATE;
7077 }
7078
Gilles Peskineca57d782023-07-20 19:08:06 +02007079 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007080 /* Appending an empty string, nothing to do. */
7081 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307082 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307083
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307084 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7085 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307086 return PSA_ERROR_INSUFFICIENT_MEMORY;
7087 }
7088
Gilles Peskineead17662023-08-20 22:02:51 +02007089 if (pbkdf2->salt_length != 0) {
7090 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7091 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307092 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307093 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307094 mbedtls_free(pbkdf2->salt);
7095 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307096 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307097 return PSA_SUCCESS;
7098}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307099
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307100#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307101static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307102 const uint8_t *input,
7103 size_t input_len,
7104 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307105 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307106{
7107 psa_status_t status = PSA_SUCCESS;
7108 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007109 return psa_hash_compute(hash_alg, input, input_len, output,
7110 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7111 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307112 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307113 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007114 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307115 return status;
7116}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307117#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307118
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307119#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307120static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7121 size_t input_len,
7122 uint8_t *output,
7123 size_t *output_len)
7124{
7125 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307126 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307128 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307129 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7130 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7131 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307132 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307133 * mac_size as the driver function sets mac_output_length = mac_size
7134 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307135 status = psa_driver_wrapper_mac_compute(&attributes,
7136 zeros, sizeof(zeros),
7137 PSA_ALG_CMAC, input, input_len,
7138 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307139 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7140 128U,
7141 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307142 output_len);
7143 } else {
7144 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307145 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307146 }
7147 return status;
7148}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307149#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307150
7151static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307152 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307153 const uint8_t *data,
7154 size_t data_length)
7155{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307156 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307157 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7158 return PSA_ERROR_BAD_STATE;
7159 }
7160
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307161#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307162 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7163 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7164 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7165 pbkdf2->password,
7166 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307167 } else
7168#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7169#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7170 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307171 status = psa_pbkdf2_cmac_set_password(data, data_length,
7172 pbkdf2->password,
7173 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307174 } else
7175#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7176 {
7177 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307178 }
7179
7180 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7181
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307182 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307183}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307184
7185static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307186 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307187 psa_key_derivation_step_t step,
7188 const uint8_t *data,
7189 size_t data_length)
7190{
7191 switch (step) {
7192 case PSA_KEY_DERIVATION_INPUT_SALT:
7193 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7194 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307195 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307196 default:
7197 return PSA_ERROR_INVALID_ARGUMENT;
7198 }
7199}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307200#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307201
Gilles Peskineb8965192019-09-24 16:21:10 +02007202/** Check whether the given key type is acceptable for the given
7203 * input step of a key derivation.
7204 *
7205 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7206 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7207 * Both secret and non-secret inputs can alternatively have the type
7208 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7209 * that the input was passed as a buffer rather than via a key object.
7210 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007211static int psa_key_derivation_check_input_type(
7212 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007214{
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007216 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 if (key_type == PSA_KEY_TYPE_DERIVE) {
7218 return PSA_SUCCESS;
7219 }
7220 if (key_type == PSA_KEY_TYPE_NONE) {
7221 return PSA_SUCCESS;
7222 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007223 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007224 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 if (key_type == PSA_KEY_TYPE_DERIVE) {
7226 return PSA_SUCCESS;
7227 }
7228 if (key_type == PSA_KEY_TYPE_NONE) {
7229 return PSA_SUCCESS;
7230 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007231 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007232 case PSA_KEY_DERIVATION_INPUT_LABEL:
7233 case PSA_KEY_DERIVATION_INPUT_SALT:
7234 case PSA_KEY_DERIVATION_INPUT_INFO:
7235 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7237 return PSA_SUCCESS;
7238 }
7239 if (key_type == PSA_KEY_TYPE_NONE) {
7240 return PSA_SUCCESS;
7241 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007242 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307243 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7244 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7245 return PSA_SUCCESS;
7246 }
7247 if (key_type == PSA_KEY_TYPE_DERIVE) {
7248 return PSA_SUCCESS;
7249 }
7250 if (key_type == PSA_KEY_TYPE_NONE) {
7251 return PSA_SUCCESS;
7252 }
7253 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007254 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007256}
7257
Janos Follathb80a94e2019-06-12 15:54:46 +01007258static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007259 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007260 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007261 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007262 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007264{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007265 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 status = psa_key_derivation_check_input_type(step, key_type);
7269 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007270 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007272
Przemek Stekiel69c46792022-06-10 12:59:51 +02007273#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7275 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7276 step, data, data_length);
7277 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007278#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007279#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7281 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7282 step, data, data_length);
7283 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007284#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7285#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007286 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7287 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7288 step, data, data_length);
7289 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007290#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007291#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007293 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7295 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007296#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307297#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307298 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307299 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7300 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307301 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307302#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007303 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007304 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007305 (void) data;
7306 (void) data_length;
7307 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007309 }
7310
Gilles Peskine224b0d62019-09-23 18:13:17 +02007311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 if (status != PSA_SUCCESS) {
7313 psa_key_derivation_abort(operation);
7314 }
7315 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007316}
7317
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307318static psa_status_t psa_key_derivation_input_integer_internal(
7319 psa_key_derivation_operation_t *operation,
7320 psa_key_derivation_step_t step,
7321 uint64_t value)
7322{
7323 psa_status_t status;
7324 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7325
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307326#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307327 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307328 status = psa_pbkdf2_set_input_cost(
7329 &operation->ctx.pbkdf2, step, value);
7330 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307331#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307332 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307333 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307334 (void) value;
7335 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307336 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307337 }
7338
7339 if (status != PSA_SUCCESS) {
7340 psa_key_derivation_abort(operation);
7341 }
7342 return status;
7343}
7344
Janos Follath51f4a0f2019-06-14 11:35:55 +01007345psa_status_t psa_key_derivation_input_bytes(
7346 psa_key_derivation_operation_t *operation,
7347 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007348 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007350{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7352 LOCAL_INPUT_DECLARE(data_external, data);
7353
7354 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7355
7356 status = psa_key_derivation_input_internal(operation, step,
7357 PSA_KEY_TYPE_NONE,
7358 data, data_length);
Ryan Everettb41c3c92024-01-25 11:56:35 +00007359#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007360exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007361#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007362 LOCAL_INPUT_FREE(data_external, data);
7363 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007364}
7365
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307366psa_status_t psa_key_derivation_input_integer(
7367 psa_key_derivation_operation_t *operation,
7368 psa_key_derivation_step_t step,
7369 uint64_t value)
7370{
7371 return psa_key_derivation_input_integer_internal(operation, step, value);
7372}
7373
Janos Follath51f4a0f2019-06-14 11:35:55 +01007374psa_status_t psa_key_derivation_input_key(
7375 psa_key_derivation_operation_t *operation,
7376 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007378{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007380 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007381 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007382
Ronald Cron5c522922020-11-14 16:35:34 +01007383 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7385 if (status != PSA_SUCCESS) {
7386 psa_key_derivation_abort(operation);
7387 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007388 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007389
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307390 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7391 * permission to output to a key object. */
7392 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7393 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007394 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 status = psa_key_derivation_input_internal(operation,
7398 step, slot->attr.type,
7399 slot->key.data,
7400 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007401
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007402 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007405}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007406
7407
7408
7409/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007410/* Key agreement */
7411/****************************************************************/
7412
Gilles Peskine449bd832023-01-11 14:50:10 +01007413psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7414 const uint8_t *key_buffer,
7415 size_t key_buffer_size,
7416 psa_algorithm_t alg,
7417 const uint8_t *peer_key,
7418 size_t peer_key_length,
7419 uint8_t *shared_secret,
7420 size_t shared_secret_size,
7421 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007422{
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007424#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007425 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7427 key_buffer_size, alg,
7428 peer_key, peer_key_length,
7429 shared_secret,
7430 shared_secret_size,
7431 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007432#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007433
7434#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7435 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007436 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007437 peer_key,
7438 peer_key_length,
7439 key_buffer,
7440 key_buffer_size,
7441 shared_secret,
7442 shared_secret_size,
7443 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007444#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7445
Gilles Peskine01d718c2018-09-18 12:01:02 +02007446 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007447 (void) attributes;
7448 (void) key_buffer;
7449 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007450 (void) peer_key;
7451 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007452 (void) shared_secret;
7453 (void) shared_secret_size;
7454 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007456 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007457}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007458
Aditya Deshpande17845b82022-10-13 17:21:01 +01007459/** Internal function for raw key agreement
7460 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007461 * to the driver's implementation if a driver is present.
7462 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007463 * (psa_key_agreement_raw_builtin).
7464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007465static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7466 psa_key_slot_t *private_key,
7467 const uint8_t *peer_key,
7468 size_t peer_key_length,
7469 uint8_t *shared_secret,
7470 size_t shared_secret_size,
7471 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007472{
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7474 return PSA_ERROR_NOT_SUPPORTED;
7475 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007476
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007477 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 private_key->key.data,
7479 private_key->key.bytes, alg,
7480 peer_key, peer_key_length,
7481 shared_secret,
7482 shared_secret_size,
7483 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007484}
7485
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007486/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007487 * to potentially free embedded data structures and wipe confidential data.
7488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007489static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7490 psa_key_derivation_step_t step,
7491 psa_key_slot_t *private_key,
7492 const uint8_t *peer_key,
7493 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007494{
7495 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007496 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007497 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007499
7500 /* Step 1: run the secret agreement algorithm to generate the shared
7501 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 status = psa_key_agreement_raw_internal(ka_alg,
7503 private_key,
7504 peer_key, peer_key_length,
7505 shared_secret,
7506 sizeof(shared_secret),
7507 &shared_secret_length);
7508 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007511
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007512 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007513 * the shared secret. A shared secret is permitted wherever a key
7514 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 status = psa_key_derivation_input_internal(operation, step,
7516 PSA_KEY_TYPE_DERIVE,
7517 shared_secret,
7518 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7521 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007522}
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7525 psa_key_derivation_step_t step,
7526 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007527 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007529{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007530 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007531 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007532 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007533 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007534
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7536 return PSA_ERROR_INVALID_ARGUMENT;
7537 }
Ronald Cron5c522922020-11-14 16:35:34 +01007538 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7540 if (status != PSA_SUCCESS) {
7541 return status;
7542 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007543
7544 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key)
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 status = psa_key_agreement_internal(operation, step,
7546 slot,
7547 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007548
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007549#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007550exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007551#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 if (status != PSA_SUCCESS) {
7553 psa_key_derivation_abort(operation);
7554 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007555 /* If a private key has been added as SECRET, we allow the derived
7556 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007558 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007560 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007561
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007562 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007563 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007566}
7567
Gilles Peskine449bd832023-01-11 14:50:10 +01007568psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7569 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007570 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007572 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 size_t output_size,
7574 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007575{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007577 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007578 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007579 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007580 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7581 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007582 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007585 status = PSA_ERROR_INVALID_ARGUMENT;
7586 goto exit;
7587 }
Ronald Cron5c522922020-11-14 16:35:34 +01007588 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7590 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007591 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007593
Gilles Peskine3e561302022-04-14 00:17:15 +02007594 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7595 * for the output size. The PSA specification only guarantees that this
7596 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7597 * but it might be nice to allow smaller buffers if the output fits.
7598 * At the time of writing this comment, with only ECDH implemented,
7599 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7600 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7601 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007602 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7604 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007605 status = PSA_ERROR_BUFFER_TOO_SMALL;
7606 goto exit;
7607 }
7608
Thomas Daubney81899ab2024-02-15 12:57:26 +00007609 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 status = psa_key_agreement_raw_internal(alg, slot,
7611 peer_key, peer_key_length,
7612 output, output_size,
7613 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007614
7615exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007616 /* Check for successful allocation of output,
7617 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007618 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007619 /* If an error happens and is not handled properly, the output
7620 * may be used as a key to protect sensitive data. Arrange for such
7621 * a key to be random, which is likely to result in decryption or
7622 * verification errors. This is better than filling the buffer with
7623 * some constant data such as zeros, which would result in the data
7624 * being protected with a reproducible, easily knowable key.
7625 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007626 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007627 *output_length = output_size;
7628 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007629
Thomas Daubney5390aca2024-02-22 11:06:04 +00007630 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007631 /* output allocation failed. */
7632 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007633 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007634
Ryan Everetta103ec92024-01-31 13:59:57 +00007635 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007636
Thomas Daubney81899ab2024-02-15 12:57:26 +00007637 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7638 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007640}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007641
7642
7643/****************************************************************/
7644/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007645/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007646
Gilles Peskine672a7712023-04-28 21:00:28 +02007647#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7648#include "entropy_poll.h"
7649#endif
7650
Gilles Peskine30524eb2020-11-13 17:02:26 +01007651/** Initialize the PSA random generator.
7652 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007653static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007654{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007655#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007657#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7658
Gilles Peskine30524eb2020-11-13 17:02:26 +01007659 /* Set default configuration if
7660 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007662 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 }
7664 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007665 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007669#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7670 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7671 /* The PSA entropy injection feature depends on using NV seed as an entropy
7672 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 mbedtls_entropy_add_source(&rng->entropy,
7674 mbedtls_nv_seed_poll, NULL,
7675 MBEDTLS_ENTROPY_BLOCK_SIZE,
7676 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007677#endif
7678
Valerio Setti061d4e42024-02-26 12:52:44 +01007679 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007680#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007681}
7682
7683/** Deinitialize the PSA random generator.
7684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007685static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007686{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007687#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007689#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007690 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007692#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007693}
7694
7695/** Seed the PSA random generator.
7696 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007697static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007698{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007699#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7700 /* Do nothing: the external RNG seeds itself. */
7701 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007703#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007704 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007705 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 drbg_seed, sizeof(drbg_seed) - 1);
7707 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007708#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007709}
7710
David Horstmann6e99bb22024-02-06 15:27:49 +00007711psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007713{
David Horstmann6e99bb22024-02-06 15:27:49 +00007714 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007715
David Horstmann6e99bb22024-02-06 15:27:49 +00007716 LOCAL_OUTPUT_DECLARE(output_external, output);
7717 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007718
David Horstmann6e99bb22024-02-06 15:27:49 +00007719 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007720
David Horstmanne097bbd2024-02-28 14:17:10 +00007721#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00007722exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00007723#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00007724 LOCAL_OUTPUT_FREE(output_external, output);
7725 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007726}
7727
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007728#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007729psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7730 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007731{
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 if (global_data.initialized) {
7733 return PSA_ERROR_NOT_PERMITTED;
7734 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007735
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7737 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7738 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7739 return PSA_ERROR_INVALID_ARGUMENT;
7740 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007741
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007743}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007744#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007745
Ronald Cron3772afe2021-02-08 16:10:05 +01007746/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007747 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007748 * \param type The key type
7749 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007750 *
7751 * \retval #PSA_SUCCESS
David Horstmann93fa4e12024-03-12 15:02:28 +00007752 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007753 * \retval #PSA_ERROR_INVALID_ARGUMENT
7754 * The size in bits of the key is not valid.
7755 * \retval #PSA_ERROR_NOT_SUPPORTED
7756 * The type and/or the size in bits of the key or the combination of
7757 * the two is not supported.
7758 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007759static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007761{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007763
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 if (key_type_is_raw_bytes(type)) {
7765 status = psa_validate_unstructured_key_bit_size(type, bits);
7766 if (status != PSA_SUCCESS) {
7767 return status;
7768 }
7769 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007770#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7772 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7773 return PSA_ERROR_NOT_SUPPORTED;
7774 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007775 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007776 return PSA_ERROR_NOT_SUPPORTED;
7777 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007778
Ronald Cron01b2aba2020-10-05 09:42:02 +02007779 /* Accept only byte-aligned keys, for the same reasons as
7780 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 if (bits % 8 != 0) {
7782 return PSA_ERROR_NOT_SUPPORTED;
7783 }
7784 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007785#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007786
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007787#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007789 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 return PSA_SUCCESS;
7791 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007792#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007793
Valerio Settia55f0422023-07-10 15:34:41 +02007794#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007795 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007796 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007797 return PSA_ERROR_NOT_SUPPORTED;
7798 }
7799 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007800#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007801 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007803 }
7804
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007806}
7807
Ronald Cron55ed0592020-10-05 10:30:40 +02007808psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007809 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007810 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007811 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007812{
7813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007814 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007815
Gilles Peskine7a18f962024-02-12 16:48:11 +01007816 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007817 (void) params;
7818 (void) params_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007819
Gilles Peskine449bd832023-01-11 14:50:10 +01007820 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00007821 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 if (status != PSA_SUCCESS) {
7823 return status;
7824 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007825
David Brown12ca5032021-02-11 11:02:00 -07007826#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007827 if (type == PSA_KEY_TYPE_DES) {
7828 psa_des_set_key_parity(key_buffer, key_buffer_size);
7829 }
David Brown8107e312021-02-12 08:08:46 -07007830#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007831 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007832
Valerio Setti76df8c12023-07-11 14:11:28 +02007833#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7835 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007836 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 key_buffer,
7838 key_buffer_size,
7839 key_buffer_length);
7840 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007841#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007842
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007843#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7845 return mbedtls_psa_ecp_generate_key(attributes,
7846 key_buffer,
7847 key_buffer_size,
7848 key_buffer_length);
7849 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007850#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007851
Valerio Settia55f0422023-07-10 15:34:41 +02007852#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007853 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7854 return mbedtls_psa_ffdh_generate_key(attributes,
7855 key_buffer,
7856 key_buffer_size,
7857 key_buffer_length);
7858 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007859#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007860 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 (void) key_buffer_length;
7862 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007863 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007864
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007866}
Darryl Green0c6575a2018-11-07 16:05:30 +00007867
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007868psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007869 const psa_key_production_parameters_t *params,
7870 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007871 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007872{
7873 psa_status_t status;
7874 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007875 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007876 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007877
Ronald Cron81709fc2020-11-14 12:10:32 +01007878 *key = MBEDTLS_SVC_KEY_ID_INIT;
7879
Gilles Peskine0f84d622019-09-12 19:03:13 +02007880 /* Reject any attempt to create a zero-length key so that we don't
7881 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 if (psa_get_key_bits(attributes) == 0) {
7883 return PSA_ERROR_INVALID_ARGUMENT;
7884 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007885
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007886 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007887 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 return PSA_ERROR_INVALID_ARGUMENT;
7889 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007890
Gilles Peskine7a18f962024-02-12 16:48:11 +01007891#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007892 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007893 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007894 return PSA_ERROR_INVALID_ARGUMENT;
7895 }
7896 } else
7897#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007898 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01007899 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02007900 }
7901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7903 &slot, &driver);
7904 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007905 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 }
Gilles Peskine11792082019-08-06 18:36:36 +02007907
Ronald Cron977c2472020-10-13 08:32:21 +02007908 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307909 * storage ( thus not in the case of generating a key in a secure element
7910 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7911 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007913 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007915 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007916 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007918 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007920
7921 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007922 attributes->type,
7923 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007925 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 attributes, &key_buffer_size);
7927 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007928 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 }
Ronald Cron977c2472020-10-13 08:32:21 +02007930 }
Ronald Cron977c2472020-10-13 08:32:21 +02007931
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7933 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007934 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 }
Ronald Cron977c2472020-10-13 08:32:21 +02007936 }
7937
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007939 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007940 slot->key.data, slot->key.bytes,
7941 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 if (status != PSA_SUCCESS) {
7943 psa_remove_key_data_from_memory(slot);
7944 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007945
Gilles Peskine11792082019-08-06 18:36:36 +02007946exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 if (status == PSA_SUCCESS) {
7948 status = psa_finish_key_creation(slot, driver, key);
7949 }
7950 if (status != PSA_SUCCESS) {
7951 psa_fail_key_creation(slot, driver);
7952 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007953
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007955}
7956
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007957psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7958 mbedtls_svc_key_id_t *key)
7959{
7960 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007961 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007962 key);
7963}
7964
Gilles Peskinee59236f2018-01-27 23:32:46 +01007965/****************************************************************/
7966/* Module setup */
7967/****************************************************************/
7968
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007969#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007970psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 void (* entropy_init)(mbedtls_entropy_context *ctx),
7972 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007973{
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7975 return PSA_ERROR_BAD_STATE;
7976 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007977 global_data.rng.entropy_init = entropy_init;
7978 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007980}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007981#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007982
Gilles Peskine449bd832023-01-11 14:50:10 +01007983void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007984{
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 psa_wipe_all_key_slots();
7986 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7987 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007988 }
7989 /* Wipe all remaining data, including configuration.
7990 * In particular, this sets all state indicator to the value
7991 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007993
7994 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007996}
7997
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007998#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7999/** Recover a transaction that was interrupted by a power failure.
8000 *
8001 * This function is called during initialization, before psa_crypto_init()
8002 * returns. If this function returns a failure status, the initialization
8003 * fails.
8004 */
8005static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008007{
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008009 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
8010 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 /* TODO - fall through to the failure case until this
8012 * is implemented.
8013 * https://github.com/ARMmbed/mbed-crypto/issues/218
8014 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008015 default:
8016 /* We found an unsupported transaction in the storage.
8017 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008019 }
8020}
8021#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008024{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008025 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008026
Gilles Peskinec6b69072018-11-20 21:42:52 +01008027 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 if (global_data.initialized != 0) {
8029 return PSA_SUCCESS;
8030 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008031
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01008032 /* Init drivers */
8033 status = psa_driver_wrapper_init();
8034 if (status != PSA_SUCCESS) {
8035 goto exit;
8036 }
8037 global_data.drivers_initialized = 1;
8038
Valerio Setti402cfba2023-11-13 10:24:32 +01008039 status = psa_initialize_key_slots();
8040 if (status != PSA_SUCCESS) {
8041 goto exit;
8042 }
8043
Gilles Peskine30524eb2020-11-13 17:02:26 +01008044 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008046 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 status = mbedtls_psa_random_seed(&global_data.rng);
8048 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01008051 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008052
Gilles Peskine4b734222019-07-24 15:56:31 +02008053#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 status = psa_crypto_load_transaction();
8055 if (status == PSA_SUCCESS) {
8056 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8057 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008058 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 }
8060 status = psa_crypto_stop_transaction();
8061 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02008062 /* There's no transaction to complete. It's all good. */
8063 status = PSA_SUCCESS;
8064 }
Gilles Peskine4b734222019-07-24 15:56:31 +02008065#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02008066
Gilles Peskinec6b69072018-11-20 21:42:52 +01008067 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01008068 global_data.initialized = 1;
8069
8070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008071 if (status != PSA_SUCCESS) {
8072 mbedtls_psa_crypto_free();
8073 }
8074 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008075}
8076
Yanray Wangffc3c482023-07-11 12:01:04 +08008077#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008078psa_status_t psa_crypto_driver_pake_get_password_len(
8079 const psa_crypto_driver_pake_inputs_t *inputs,
8080 size_t *password_len)
8081{
8082 if (inputs->password_len == 0) {
8083 return PSA_ERROR_BAD_STATE;
8084 }
8085
8086 *password_len = inputs->password_len;
8087
8088 return PSA_SUCCESS;
8089}
8090
8091psa_status_t psa_crypto_driver_pake_get_password(
8092 const psa_crypto_driver_pake_inputs_t *inputs,
8093 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8094{
8095 if (inputs->password_len == 0) {
8096 return PSA_ERROR_BAD_STATE;
8097 }
8098
8099 if (buffer_size < inputs->password_len) {
8100 return PSA_ERROR_BUFFER_TOO_SMALL;
8101 }
8102
8103 memcpy(buffer, inputs->password, inputs->password_len);
8104 *buffer_length = inputs->password_len;
8105
8106 return PSA_SUCCESS;
8107}
8108
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008109psa_status_t psa_crypto_driver_pake_get_user_len(
8110 const psa_crypto_driver_pake_inputs_t *inputs,
8111 size_t *user_len)
8112{
8113 if (inputs->user_len == 0) {
8114 return PSA_ERROR_BAD_STATE;
8115 }
8116
8117 *user_len = inputs->user_len;
8118
8119 return PSA_SUCCESS;
8120}
8121
8122psa_status_t psa_crypto_driver_pake_get_user(
8123 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008124 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008125{
8126 if (inputs->user_len == 0) {
8127 return PSA_ERROR_BAD_STATE;
8128 }
8129
Przemek Stekielc0e62502023-03-14 11:49:36 +01008130 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008131 return PSA_ERROR_BUFFER_TOO_SMALL;
8132 }
8133
Przemek Stekielc0e62502023-03-14 11:49:36 +01008134 memcpy(user_id, inputs->user, inputs->user_len);
8135 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008136
8137 return PSA_SUCCESS;
8138}
8139
8140psa_status_t psa_crypto_driver_pake_get_peer_len(
8141 const psa_crypto_driver_pake_inputs_t *inputs,
8142 size_t *peer_len)
8143{
8144 if (inputs->peer_len == 0) {
8145 return PSA_ERROR_BAD_STATE;
8146 }
8147
8148 *peer_len = inputs->peer_len;
8149
8150 return PSA_SUCCESS;
8151}
8152
8153psa_status_t psa_crypto_driver_pake_get_peer(
8154 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008155 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008156{
8157 if (inputs->peer_len == 0) {
8158 return PSA_ERROR_BAD_STATE;
8159 }
8160
Przemek Stekielc0e62502023-03-14 11:49:36 +01008161 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008162 return PSA_ERROR_BUFFER_TOO_SMALL;
8163 }
8164
Przemek Stekielc0e62502023-03-14 11:49:36 +01008165 memcpy(peer_id, inputs->peer, inputs->peer_len);
8166 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008167
8168 return PSA_SUCCESS;
8169}
8170
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008171psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8172 const psa_crypto_driver_pake_inputs_t *inputs,
8173 psa_pake_cipher_suite_t *cipher_suite)
8174{
8175 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8176 return PSA_ERROR_BAD_STATE;
8177 }
8178
8179 *cipher_suite = inputs->cipher_suite;
8180
8181 return PSA_SUCCESS;
8182}
8183
Neil Armstronga7d08c32022-06-01 18:21:20 +02008184psa_status_t psa_pake_setup(
8185 psa_pake_operation_t *operation,
8186 const psa_pake_cipher_suite_t *cipher_suite)
8187{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008190 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 status = PSA_ERROR_BAD_STATE;
8192 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008193 }
8194
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008195 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008196 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008197 status = PSA_ERROR_INVALID_ARGUMENT;
8198 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008199 }
8200
Przemek Stekiel51eac532022-12-07 11:04:51 +01008201 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8202
Przemek Stekiele12ed362022-12-21 12:54:46 +01008203 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008204 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8205 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008206 operation->data.inputs.cipher_suite = *cipher_suite;
8207
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008208#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008210 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008211 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008212
David Horstmann16f01512023-06-14 17:21:07 +01008213 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008214 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008215 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008216#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008217 {
8218 status = PSA_ERROR_NOT_SUPPORTED;
8219 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
8221
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008222 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8223
Przemek Stekiel51eac532022-12-07 11:04:51 +01008224 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008225exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008226 psa_pake_abort(operation);
8227 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008228}
8229
8230psa_status_t psa_pake_set_password_key(
8231 psa_pake_operation_t *operation,
8232 mbedtls_svc_key_id_t password)
8233{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008234 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008235 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8236 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008237 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008238
Przemek Stekiel51eac532022-12-07 11:04:51 +01008239 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 status = PSA_ERROR_BAD_STATE;
8241 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008242 }
8243
Przemek Stekiel6c764412022-11-22 14:05:12 +01008244 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8245 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008246 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008247 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008248 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008249 }
8250
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008251 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008252
Przemek Stekiel51eac532022-12-07 11:04:51 +01008253 if (type != PSA_KEY_TYPE_PASSWORD &&
8254 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8255 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008257 }
8258
Przemek Stekiel51eac532022-12-07 11:04:51 +01008259 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8260 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008263 }
8264
8265 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8266 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008267 operation->data.inputs.attributes = slot->attr;
8268
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008269exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008270 if (status != PSA_SUCCESS) {
8271 psa_pake_abort(operation);
8272 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008273 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008274 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008275}
8276
8277psa_status_t psa_pake_set_user(
8278 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008279 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008280 size_t user_id_len)
8281{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008283 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008284
8285 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008286 status = PSA_ERROR_BAD_STATE;
8287 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008288 }
8289
Przemek Stekiel51eac532022-12-07 11:04:51 +01008290 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008291 status = PSA_ERROR_INVALID_ARGUMENT;
8292 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008293 }
8294
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008295 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008296 status = PSA_ERROR_BAD_STATE;
8297 goto exit;
8298 }
8299
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008300 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8301 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008302 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8303 goto exit;
8304 }
8305
David Horstmann4f534ae2024-01-22 17:35:59 +00008306 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8307
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008308 memcpy(operation->data.inputs.user, user_id, user_id_len);
8309 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008310
David Horstmann4f534ae2024-01-22 17:35:59 +00008311 status = PSA_SUCCESS;
8312
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008314 LOCAL_INPUT_FREE(user_id_external, user_id);
8315 if (status != PSA_SUCCESS) {
8316 psa_pake_abort(operation);
8317 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008318 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008319}
8320
8321psa_status_t psa_pake_set_peer(
8322 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008323 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008324 size_t peer_id_len)
8325{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008327 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008328
8329 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008330 status = PSA_ERROR_BAD_STATE;
8331 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008332 }
8333
Przemek Stekiel51eac532022-12-07 11:04:51 +01008334 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008335 status = PSA_ERROR_INVALID_ARGUMENT;
8336 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008337 }
8338
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008339 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008340 status = PSA_ERROR_BAD_STATE;
8341 goto exit;
8342 }
8343
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008344 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8345 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008346 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8347 goto exit;
8348 }
8349
David Horstmann4f534ae2024-01-22 17:35:59 +00008350 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8351
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008352 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8353 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008354
David Horstmann4f534ae2024-01-22 17:35:59 +00008355 status = PSA_SUCCESS;
8356
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008357exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008358 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8359 if (status != PSA_SUCCESS) {
8360 psa_pake_abort(operation);
8361 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008362 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008363}
8364
8365psa_status_t psa_pake_set_role(
8366 psa_pake_operation_t *operation,
8367 psa_pake_role_t role)
8368{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008370
Przemek Stekiel51eac532022-12-07 11:04:51 +01008371 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008372 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008373 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008374 }
8375
Przemek Stekiel09104b82023-03-06 14:11:51 +01008376 switch (operation->alg) {
8377#if defined(PSA_WANT_ALG_JPAKE)
8378 case PSA_ALG_JPAKE:
8379 if (role == PSA_PAKE_ROLE_NONE) {
8380 return PSA_SUCCESS;
8381 }
8382 status = PSA_ERROR_INVALID_ARGUMENT;
8383 break;
8384#endif
8385 default:
8386 (void) role;
8387 status = PSA_ERROR_NOT_SUPPORTED;
8388 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008389 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008391 psa_pake_abort(operation);
8392 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008393}
8394
David Horstmanne7f21e62023-05-12 18:17:21 +01008395/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008396#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008397static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008398 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008399{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008400 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008401 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008402 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008403
David Horstmann024e5c52023-06-14 15:48:21 +01008404 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008405 is_x1 = (stage->outputs < 1);
8406 } else {
8407 is_x1 = (stage->inputs < 1);
8408 }
8409
David Horstmann74a3d8c2023-06-14 18:28:19 +01008410 key_share_step = is_x1 ?
8411 PSA_JPAKE_X1_STEP_KEY_SHARE :
8412 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008413 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008414 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8415 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8416 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8417 } else {
8418 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008419 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008420 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008421}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008422#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008423
Przemek Stekiel2797d372022-12-22 11:19:22 +01008424static psa_status_t psa_pake_complete_inputs(
8425 psa_pake_operation_t *operation)
8426{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008428 /* Create copy of the inputs on stack as inputs share memory
8429 with the driver context which will be setup by the driver. */
8430 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008431
Przemek Stekielfde11282023-03-13 16:06:09 +01008432 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008433 return PSA_ERROR_BAD_STATE;
8434 }
8435
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008436 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008437 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8438 return PSA_ERROR_BAD_STATE;
8439 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008440 }
8441
Przemek Stekiel18620a32023-01-17 16:34:52 +01008442 /* Clear driver context */
8443 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8444
8445 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008446
8447 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008448 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008449
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008450 /* User and peer are translated to role. */
8451 mbedtls_free(inputs.user);
8452 mbedtls_free(inputs.peer);
8453
Przemek Stekiel2797d372022-12-22 11:19:22 +01008454 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008455#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008456 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008457 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008458 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008459#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008460 {
8461 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008462 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008463 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008464 return status;
8465}
8466
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008467#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008468static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008469 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008470 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008471 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008472{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008473 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8474 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8475 step != PSA_PAKE_STEP_ZK_PROOF) {
8476 return PSA_ERROR_INVALID_ARGUMENT;
8477 }
8478
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008479 psa_jpake_computation_stage_t *computation_stage =
8480 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008481
David Horstmann5da95602023-06-08 15:37:12 +01008482 if (computation_stage->round != PSA_JPAKE_FIRST &&
8483 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008484 return PSA_ERROR_BAD_STATE;
8485 }
8486
David Horstmanne7f21e62023-05-12 18:17:21 +01008487 /* Check that the step we are given is the one we were expecting */
8488 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008489 return PSA_ERROR_BAD_STATE;
8490 }
8491
David Horstmanne7f21e62023-05-12 18:17:21 +01008492 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8493 computation_stage->inputs == 0 &&
8494 computation_stage->outputs == 0) {
8495 /* Start of the round, so function decides whether we are inputting
8496 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008497 computation_stage->io_mode = io_mode;
8498 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008499 /* Middle of the round so the mode we are in must match the function
8500 * called by the user */
8501 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008502 }
8503
8504 return PSA_SUCCESS;
8505}
8506
David Horstmanne7f21e62023-05-12 18:17:21 +01008507static psa_status_t psa_jpake_epilogue(
8508 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008509 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008510{
David Horstmanne7f21e62023-05-12 18:17:21 +01008511 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008512 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008513
David Horstmanne7f21e62023-05-12 18:17:21 +01008514 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8515 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008516 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008517 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008518 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008519 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008520 }
8521 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008522 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008523 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008524 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008525 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008526 }
8527 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008528 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8529 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008530 /* End of a round, move to the next round */
8531 stage->inputs = 0;
8532 stage->outputs = 0;
8533 stage->round++;
8534 }
8535 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008536 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008537 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008538 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008539 return PSA_SUCCESS;
8540}
David Horstmanne7f21e62023-05-12 18:17:21 +01008541
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008542#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008543
Neil Armstronga7d08c32022-06-01 18:21:20 +02008544psa_status_t psa_pake_output(
8545 psa_pake_operation_t *operation,
8546 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008547 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008548 size_t output_size,
8549 size_t *output_length)
8550{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008552 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008553 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008554 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008555
8556 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008557 status = psa_pake_complete_inputs(operation);
8558 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008559 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008560 }
8561 }
8562
8563 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008564 status = PSA_ERROR_BAD_STATE;
8565 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008566 }
8567
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008568 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008569 status = PSA_ERROR_INVALID_ARGUMENT;
8570 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008571 }
8572
Przemek Stekiele12ed362022-12-21 12:54:46 +01008573 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008574#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008575 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008576 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008577 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008578 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008579 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008580 driver_step = convert_jpake_computation_stage_to_driver_step(
8581 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008582 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008583#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008584 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008585 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008586 status = PSA_ERROR_NOT_SUPPORTED;
8587 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008588 }
8589
David Horstmannc75639d2024-01-22 17:56:39 +00008590 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
8591
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008592 status = psa_driver_wrapper_pake_output(operation, driver_step,
8593 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008594
8595 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008596 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008597 }
8598
8599 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008600#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008601 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008602 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008603 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008604 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008605 }
8606 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008607#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008608 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008609 status = PSA_ERROR_NOT_SUPPORTED;
8610 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008611 }
8612
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008613exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008614 LOCAL_OUTPUT_FREE(output_external, output);
8615 if (status != PSA_SUCCESS) {
8616 psa_pake_abort(operation);
8617 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008618 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008619}
8620
8621psa_status_t psa_pake_input(
8622 psa_pake_operation_t *operation,
8623 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008624 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008625 size_t input_length)
8626{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008628 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008629 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8630 operation->primitive,
8631 step);
David Horstmannc75639d2024-01-22 17:56:39 +00008632 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008633
8634 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008635 status = psa_pake_complete_inputs(operation);
8636 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008637 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008638 }
8639 }
8640
8641 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008642 status = PSA_ERROR_BAD_STATE;
8643 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008644 }
8645
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008646 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008647 status = PSA_ERROR_INVALID_ARGUMENT;
8648 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008649 }
8650
Przemek Stekiele12ed362022-12-21 12:54:46 +01008651 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008652#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008653 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008654 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008655 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008656 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008657 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008658 driver_step = convert_jpake_computation_stage_to_driver_step(
8659 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008660 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008661#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008662 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008663 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008664 status = PSA_ERROR_NOT_SUPPORTED;
8665 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008666 }
8667
David Horstmannc75639d2024-01-22 17:56:39 +00008668 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008669 status = psa_driver_wrapper_pake_input(operation, driver_step,
8670 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008671
8672 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008673 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008674 }
8675
8676 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008677#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008678 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008679 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008680 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008681 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008682 }
8683 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008684#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008685 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008686 status = PSA_ERROR_NOT_SUPPORTED;
8687 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008688 }
8689
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008690exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008691 LOCAL_INPUT_FREE(input_external, input);
8692 if (status != PSA_SUCCESS) {
8693 psa_pake_abort(operation);
8694 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008695 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008696}
8697
8698psa_status_t psa_pake_get_implicit_key(
8699 psa_pake_operation_t *operation,
8700 psa_key_derivation_operation_t *output)
8701{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008703 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008704 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008705 size_t shared_key_len = 0;
8706
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008707 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008708 status = PSA_ERROR_BAD_STATE;
8709 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008710 }
8711
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008712#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008713 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008714 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008715 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008716 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008717 status = PSA_ERROR_BAD_STATE;
8718 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008719 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008720 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008721#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008722 {
8723 status = PSA_ERROR_NOT_SUPPORTED;
8724 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008725 }
8726
Przemek Stekiel0c781802022-11-29 14:53:13 +01008727 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8728 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008729 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008730 &shared_key_len);
8731
8732 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008733 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008734 }
8735
8736 status = psa_key_derivation_input_bytes(output,
8737 PSA_KEY_DERIVATION_INPUT_SECRET,
8738 shared_key,
8739 shared_key_len);
8740
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008741 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008742exit:
8743 abort_status = psa_pake_abort(operation);
8744 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008745}
8746
8747psa_status_t psa_pake_abort(
8748 psa_pake_operation_t *operation)
8749{
Przemek Stekield69dca92023-01-31 19:59:20 +01008750 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008751
Przemek Stekield69dca92023-01-31 19:59:20 +01008752 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008753 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008754 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008755
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008756 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8757 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008758 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008759 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008760 }
8761 if (operation->data.inputs.user != NULL) {
8762 mbedtls_free(operation->data.inputs.user);
8763 }
8764 if (operation->data.inputs.peer != NULL) {
8765 mbedtls_free(operation->data.inputs.peer);
8766 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008767 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008768 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008769
Przemek Stekield69dca92023-01-31 19:59:20 +01008770 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008771}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008772#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008773
David Horstmann00d7a042023-12-07 18:39:17 +00008774/* Memory copying test hooks. These are called before input copy, after input
8775 * copy, before output copy and after output copy, respectively.
8776 * They are used by memory-poisoning tests to temporarily unpoison buffers
8777 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00008778#if defined(MBEDTLS_TEST_HOOKS)
8779void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8780void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8781void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8782void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8783#endif
David Horstmannfde97392023-10-30 20:29:43 +00008784
David Horstmann1b7279a2023-11-15 15:18:30 +00008785/** Copy from an input buffer to a local copy.
8786 *
8787 * \param[in] input Pointer to input buffer.
8788 * \param[in] input_len Length of the input buffer.
8789 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8790 * \param[out] input_copy_len Length of the local copy buffer.
8791 * \return #PSA_SUCCESS, if the buffer was successfully
8792 * copied.
8793 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8794 * copy is too small to hold contents of the
8795 * input buffer.
8796 */
8797MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008798psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8799 uint8_t *input_copy, size_t input_copy_len)
8800{
8801 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008802 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008803 }
8804
David Horstmann372b8bb2023-11-24 16:21:04 +00008805#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008806 if (psa_input_pre_copy_hook != NULL) {
8807 psa_input_pre_copy_hook(input, input_len);
8808 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008809#endif
8810
David Horstmann777e7412023-11-15 17:33:47 +00008811 if (input_len > 0) {
8812 memcpy(input_copy, input, input_len);
8813 }
David Horstmannfde97392023-10-30 20:29:43 +00008814
David Horstmann372b8bb2023-11-24 16:21:04 +00008815#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008816 if (psa_input_post_copy_hook != NULL) {
8817 psa_input_post_copy_hook(input, input_len);
8818 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008819#endif
8820
David Horstmannfde97392023-10-30 20:29:43 +00008821 return PSA_SUCCESS;
8822}
8823
David Horstmann1b7279a2023-11-15 15:18:30 +00008824/** Copy from a local output buffer into a user-supplied one.
8825 *
8826 * \param[in] output_copy Pointer to a local buffer containing the output.
8827 * \param[in] output_copy_len Length of the local buffer.
8828 * \param[out] output Pointer to user-supplied output buffer.
8829 * \param[out] output_len Length of the user-supplied output buffer.
8830 * \return #PSA_SUCCESS, if the buffer was successfully
8831 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008832 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008833 * user-supplied output buffer is too small to
8834 * hold the contents of the local buffer.
8835 */
8836MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008837psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8838 uint8_t *output, size_t output_len)
8839{
8840 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008841 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008842 }
David Horstmann777e7412023-11-15 17:33:47 +00008843
David Horstmann372b8bb2023-11-24 16:21:04 +00008844#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008845 if (psa_output_pre_copy_hook != NULL) {
8846 psa_output_pre_copy_hook(output, output_len);
8847 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008848#endif
8849
David Horstmann777e7412023-11-15 17:33:47 +00008850 if (output_copy_len > 0) {
8851 memcpy(output, output_copy, output_copy_len);
8852 }
8853
David Horstmann372b8bb2023-11-24 16:21:04 +00008854#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008855 if (psa_output_post_copy_hook != NULL) {
8856 psa_output_post_copy_hook(output, output_len);
8857 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008858#endif
8859
David Horstmann8978f5c2023-10-30 20:37:12 +00008860 return PSA_SUCCESS;
8861}
8862
David Horstmannf1734052023-11-20 17:15:32 +00008863psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8864 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008865{
8866 psa_status_t status;
8867
David Horstmann9db14482023-11-23 15:50:37 +00008868 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008869
David Horstmannc5cc1c32023-11-15 18:11:26 +00008870 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008871 return PSA_SUCCESS;
8872 }
8873
David Horstmannf1734052023-11-20 17:15:32 +00008874 local_input->buffer = mbedtls_calloc(input_len, 1);
8875 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008876 /* Since we dealt with the zero-length case above, we know that
8877 * a NULL return value means a failure of allocation. */
8878 return PSA_ERROR_INSUFFICIENT_MEMORY;
8879 }
David Horstmannf1734052023-11-20 17:15:32 +00008880 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008881
David Horstmannf1734052023-11-20 17:15:32 +00008882 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008883
8884 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008885 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008886 if (status != PSA_SUCCESS) {
8887 goto error;
8888 }
8889
8890 return PSA_SUCCESS;
8891
8892error:
David Horstmannf1734052023-11-20 17:15:32 +00008893 mbedtls_free(local_input->buffer);
8894 local_input->buffer = NULL;
8895 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008896 return status;
8897}
8898
David Horstmannf1734052023-11-20 17:15:32 +00008899void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008900{
David Horstmannf1734052023-11-20 17:15:32 +00008901 mbedtls_free(local_input->buffer);
8902 local_input->buffer = NULL;
8903 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008904}
8905
David Horstmann89875a42023-11-20 12:54:09 +00008906psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8907 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008908{
David Horstmann9db14482023-11-23 15:50:37 +00008909 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008910
David Horstmannc5cc1c32023-11-15 18:11:26 +00008911 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008912 return PSA_SUCCESS;
8913 }
David Horstmann89875a42023-11-20 12:54:09 +00008914 local_output->buffer = mbedtls_calloc(output_len, 1);
8915 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008916 /* Since we dealt with the zero-length case above, we know that
8917 * a NULL return value means a failure of allocation. */
8918 return PSA_ERROR_INSUFFICIENT_MEMORY;
8919 }
David Horstmann89875a42023-11-20 12:54:09 +00008920 local_output->length = output_len;
8921 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008922
8923 return PSA_SUCCESS;
8924}
8925
Gabor Mezei1882c512024-01-24 13:07:17 +01008926psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len,
8927 psa_crypto_local_output_t *local_output)
8928{
8929 psa_status_t status;
8930 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
8931
8932 if (output_len == 0) {
8933 return PSA_SUCCESS;
8934 }
8935 local_output->buffer = mbedtls_calloc(output_len, 1);
8936 if (local_output->buffer == NULL) {
8937 /* Since we dealt with the zero-length case above, we know that
8938 * a NULL return value means a failure of allocation. */
8939 return PSA_ERROR_INSUFFICIENT_MEMORY;
8940 }
8941 local_output->length = output_len;
8942 local_output->original = output;
8943
8944 status = psa_crypto_copy_input(output, output_len,
8945 local_output->buffer, local_output->length);
8946 if (status != PSA_SUCCESS) {
8947 goto error;
8948 }
8949
8950 return PSA_SUCCESS;
8951
8952error:
8953 mbedtls_free(local_output->buffer);
8954 local_output->buffer = NULL;
8955 local_output->length = 0;
8956 return status;
8957}
8958
David Horstmann89875a42023-11-20 12:54:09 +00008959psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008960{
David Horstmannc335a4e2023-11-15 15:57:32 +00008961 psa_status_t status;
8962
David Horstmann89875a42023-11-20 12:54:09 +00008963 if (local_output->buffer == NULL) {
8964 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008965 return PSA_SUCCESS;
8966 }
David Horstmann89875a42023-11-20 12:54:09 +00008967 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008968 /* We have an internal copy but nothing to copy back to. */
8969 return PSA_ERROR_CORRUPTION_DETECTED;
8970 }
8971
David Horstmann89875a42023-11-20 12:54:09 +00008972 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8973 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008974 if (status != PSA_SUCCESS) {
8975 return status;
8976 }
David Horstmann9467ea32023-11-08 17:44:18 +00008977
David Horstmann89875a42023-11-20 12:54:09 +00008978 mbedtls_free(local_output->buffer);
8979 local_output->buffer = NULL;
8980 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008981
8982 return PSA_SUCCESS;
8983}
8984
Gilles Peskinee59236f2018-01-27 23:32:46 +01008985#endif /* MBEDTLS_PSA_CRYPTO_C */