blob: 5ba94c42021215227d538631c4ebe0c83a20c414 [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
David Horstmann62a56d92023-12-14 18:12:47 +0000184/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann3e72db42023-12-08 14:08:18 +0000185 * after first copying back its contents to the original buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000186 *
David Horstmann3e72db42023-12-08 14:08:18 +0000187 * Assumptions:
David Horstmann3e72db42023-12-08 14:08:18 +0000188 * - psa_status_t status exists
David Horstmann62a56d92023-12-14 18:12:47 +0000189 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
190 * - output is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000191 */
David Horstmann62a56d92023-12-14 18:12:47 +0000192#define LOCAL_OUTPUT_FREE(output, output_copy) \
193 output_copy = NULL; \
David Horstmann5a945f52023-12-13 14:09:08 +0000194 do { \
195 psa_status_t local_output_status; \
David Horstmann36df4b22023-12-13 15:55:25 +0000196 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann5a945f52023-12-13 14:09:08 +0000197 if (local_output_status != PSA_SUCCESS) { \
198 /* Since this error case is an internal error, it's more serious than \
199 * any existing error code and so it's fine to overwrite the existing \
200 * status. */ \
201 status = local_output_status; \
202 } \
203 } while (0)
David Horstmann513101b2023-11-29 17:24:08 +0000204#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann62a56d92023-12-14 18:12:47 +0000205#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
206 const uint8_t *input_copy_name = NULL;
207#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
208 input_copy = input;
209#define LOCAL_INPUT_FREE(input, input_copy) \
210 input_copy = NULL;
211#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
212 uint8_t *output_copy_name = NULL;
213#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
214 output_copy = output;
215#define LOCAL_OUTPUT_FREE(output, output_copy) \
216 output_copy = NULL;
David Horstmann513101b2023-11-29 17:24:08 +0000217#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000218
219
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100220int psa_can_do_hash(psa_algorithm_t hash_alg)
221{
222 (void) hash_alg;
223 return global_data.drivers_initialized;
224}
Valerio Settic6f004f2023-12-12 11:27:36 +0100225
Valerio Setti1fff4f22023-12-28 14:19:34 +0100226int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
Valerio Settic6f004f2023-12-12 11:27:36 +0100227{
Valerio Setti1fff4f22023-12-28 14:19:34 +0100228 (void) key_type;
Valerio Settic6f004f2023-12-12 11:27:36 +0100229 (void) cipher_alg;
230 return global_data.drivers_initialized;
231}
232
233
Valerio Settia55f0422023-07-10 15:34:41 +0200234#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200235 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200236 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200237static int psa_is_dh_key_size_valid(size_t bits)
238{
Valerio Setti504a1022024-01-17 15:19:30 +0100239 switch (bits) {
240#if defined(PSA_WANT_DH_RFC7919_2048)
241 case 2048:
242 return 1;
243#endif /* PSA_WANT_DH_RFC7919_2048 */
244#if defined(PSA_WANT_DH_RFC7919_3072)
245 case 3072:
246 return 1;
247#endif /* PSA_WANT_DH_RFC7919_3072 */
248#if defined(PSA_WANT_DH_RFC7919_4096)
249 case 4096:
250 return 1;
251#endif /* PSA_WANT_DH_RFC7919_4096 */
252#if defined(PSA_WANT_DH_RFC7919_6144)
253 case 6144:
254 return 1;
255#endif /* PSA_WANT_DH_RFC7919_6144 */
256#if defined(PSA_WANT_DH_RFC7919_8192)
257 case 8192:
258 return 1;
259#endif /* PSA_WANT_DH_RFC7919_8192 */
260 default:
261 return 0;
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200262 }
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200263}
Valerio Settia55f0422023-07-10 15:34:41 +0200264#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200265 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200266 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100269{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100270 /* Mbed TLS error codes can combine a high-level error code and a
271 * low-level error code. The low-level error usually reflects the
272 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 int low_level_ret = -(-ret & 0x007f);
274 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100278#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
280 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100282 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
283 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100284#endif
285
286#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200287 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
288 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
289 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
290 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
291 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200293 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200295 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100297#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200298
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100299#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000300 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100303#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100304
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100305#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100310#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100311
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100312#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200313 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100315#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200316
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100317#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200318 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200320 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100322#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200323
Dave Rodgman509b5672023-08-16 19:26:23 +0100324#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100339#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
342 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100343 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
344 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
348 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100352#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100353
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100354#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100355 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100357#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100358
Gilles Peskinee59236f2018-01-27 23:32:46 +0100359 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
360 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
361 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100363
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100364#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200367 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100371#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100372
Gilles Peskine82e57d12020-11-13 21:31:17 +0100373#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100375 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
376 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100377 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100379 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
380 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100382 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100384#endif
385
Dave Rodgmandea266f2023-08-31 11:52:43 +0100386#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100387 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100389 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100391 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100393#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100394 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100396#endif
397#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100398
Dave Rodgman509b5672023-08-16 19:26:23 +0100399#if defined(MBEDTLS_BIGNUM_C)
400#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100401 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100403#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100404 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100406 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100408 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100410 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100412 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100414 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100416 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100418#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100419
Dave Rodgman509b5672023-08-16 19:26:23 +0100420#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
424 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100426#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
427 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100430#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
432 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100434 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
437 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100439 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441 case MBEDTLS_ERR_PK_INVALID_ALG:
442 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
443 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200447 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100449#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100450
Gilles Peskineff2d2002019-05-06 15:26:23 +0200451 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200453 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200455
Dave Rodgman509b5672023-08-16 19:26:23 +0100456#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100457 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100459 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100461 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100463 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100465 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
466 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100468 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100470 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100472 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100474#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200475
Dave Rodgman1dab4452023-09-01 09:59:51 +0100476#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300477 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300478 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300480 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300482 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300484 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
485 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300487 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100489 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100491
Dave Rodgman509b5672023-08-16 19:26:23 +0100492#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000493 case MBEDTLS_ERR_ECP_IN_PROGRESS:
494 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100495#endif
496#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000497
Janos Follatha13b9052019-11-22 12:48:59 +0000498 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300500
Gilles Peskinee59236f2018-01-27 23:32:46 +0100501 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100503 }
504}
505
Paul Elliottdc42ca82023-02-24 18:11:59 +0000506/**
507 * \brief For output buffers which contain "tags"
508 * (outputs that may be checked for validity like
509 * hashes, MACs and signatures), fill the unused
510 * part of the output buffer (the whole buffer on
511 * error, the trailing part on success) with
512 * something that isn't a valid tag (barring an
513 * attack on the tag and deliberately-crafted
514 * input), in case the caller doesn't check the
515 * return status properly.
516 *
517 * \param output_buffer Pointer to buffer to wipe. May not be NULL
518 * unless \p output_buffer_size is zero.
519 * \param status Status of function called to generate
520 * output_buffer originally
521 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
522 * could be NULL.
523 * \param output_buffer_length Length of data written to output_buffer, must be
524 * less than \p output_buffer_size
525 */
526static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000527 size_t output_buffer_size, size_t output_buffer_length)
528{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000529 size_t offset = 0;
530
531 if (output_buffer_size == 0) {
532 /* If output_buffer_size is 0 then we have nothing to do. We must not
533 call memset because output_buffer may be NULL in this case */
534 return;
535 }
536
537 if (status == PSA_SUCCESS) {
538 offset = output_buffer_length;
539 }
540
541 memset(output_buffer + offset, '!', output_buffer_size - offset);
542}
543
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
546 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200547{
548 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200551 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200552 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200553 case PSA_KEY_TYPE_PASSWORD:
554 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200555 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100556#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200557 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (bits != 128 && bits != 192 && bits != 256) {
559 return PSA_ERROR_INVALID_ARGUMENT;
560 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200561 break;
562#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200563#if defined(PSA_WANT_KEY_TYPE_ARIA)
564 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (bits != 128 && bits != 192 && bits != 256) {
566 return PSA_ERROR_INVALID_ARGUMENT;
567 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200568 break;
569#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100570#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (bits != 128 && bits != 192 && bits != 256) {
573 return PSA_ERROR_INVALID_ARGUMENT;
574 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200575 break;
576#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100577#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200578 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (bits != 64 && bits != 128 && bits != 192) {
580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 break;
583#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100584#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200585 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (bits != 256) {
587 return PSA_ERROR_INVALID_ARGUMENT;
588 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200589 break;
590#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200591 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200593 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (bits % 8 != 0) {
595 return PSA_ERROR_INVALID_ARGUMENT;
596 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200599}
600
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100601/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100602 *
Steven Cooremancd640932021-03-08 12:00:27 +0100603 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
604 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100605 *
606 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100607 * \param[in] key_type The key type of the key to be used with the
608 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100609 *
610 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100611 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100614 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100615MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100616 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100618{
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (PSA_ALG_IS_HMAC(algorithm)) {
620 if (key_type == PSA_KEY_TYPE_HMAC) {
621 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100622 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100623 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
626 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
627 * key. */
628 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
629 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
630 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
631 * the block length (larger than 1) for block ciphers. */
632 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
633 return PSA_SUCCESS;
634 }
635 }
636 }
637
638 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100639}
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
642 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200643{
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (slot->key.data != NULL) {
645 return PSA_ERROR_ALREADY_EXISTS;
646 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 slot->key.data = mbedtls_calloc(1, buffer_length);
649 if (slot->key.data == NULL) {
650 return PSA_ERROR_INSUFFICIENT_MEMORY;
651 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200652
Ronald Cronea0f8a62020-11-25 17:52:23 +0100653 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200655}
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
658 const uint8_t *data,
659 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200660{
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 psa_status_t status = psa_allocate_buffer_to_slot(slot,
662 data_length);
663 if (status != PSA_SUCCESS) {
664 return status;
665 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 memcpy(slot->key.data, data, data_length);
668 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200669}
670
Ronald Crona0fe59f2020-11-29 11:14:53 +0100671psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100672 const psa_key_attributes_t *attributes,
673 const uint8_t *data, size_t data_length,
674 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100678 psa_key_type_t type = attributes->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200680 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (data_length == 0) {
682 return PSA_ERROR_NOT_SUPPORTED;
683 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (key_type_is_raw_bytes(type)) {
686 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100688 status = psa_validate_unstructured_key_bit_size(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 *bits);
690 if (status != PSA_SUCCESS) {
691 return status;
692 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200693
Ronald Crondd04d422020-11-28 15:14:42 +0100694 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100696 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return PSA_SUCCESS;
700 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200701#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200702 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100703 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200704 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Valerio Setti504a1022024-01-17 15:19:30 +0100705 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100706 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200707 return mbedtls_psa_ffdh_import_key(attributes,
708 data, data_length,
709 key_buffer, key_buffer_size,
710 key_buffer_length,
711 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100712 }
Valerio Settia55f0422023-07-10 15:34:41 +0200713#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200714 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200715#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
717 if (PSA_KEY_TYPE_IS_ECC(type)) {
718 return mbedtls_psa_ecp_import_key(attributes,
719 data, data_length,
720 key_buffer, key_buffer_size,
721 key_buffer_length,
722 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100723 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200724#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800725 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200726#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
727 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
729 if (PSA_KEY_TYPE_IS_RSA(type)) {
730 return mbedtls_psa_rsa_import_key(attributes,
731 data, data_length,
732 key_buffer, key_buffer_size,
733 key_buffer_length,
734 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200735 }
Valerio Settife478902023-07-25 12:27:19 +0200736#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
737 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800738 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100739 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100742}
743
Gilles Peskinef603c712019-01-19 13:40:11 +0100744/** Calculate the intersection of two algorithm usage policies.
745 *
746 * Return 0 (which allows no operation) on incompatibility.
747 */
748static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100749 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100750 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100752{
Gilles Peskine549ea862019-05-22 11:45:59 +0200753 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (alg1 == alg2) {
755 return alg1;
756 }
Steven Cooreman03488022021-02-18 12:07:20 +0100757 /* If the policies are from the same hash-and-sign family, check
758 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
760 PSA_ALG_IS_SIGN_HASH(alg2) &&
761 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
762 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
763 return alg2;
764 }
765 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
766 return alg1;
767 }
Steven Cooreman03488022021-02-18 12:07:20 +0100768 }
769 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100770 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100771 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
773 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
774 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
775 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
776 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100777 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100778
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100779 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
781 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
782 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
783 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100784 }
785 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
787 (alg1_len <= alg2_len)) {
788 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100789 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
791 (alg2_len <= alg1_len)) {
792 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100793 }
794 }
795 /* If the policies are from the same MAC family, check whether one
796 * of them is a minimum-MAC-length policy. Calculate the most
797 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
799 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
800 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 /* Validate the combination of key type and algorithm. Since the base
802 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
804 return 0;
805 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100806
Steven Cooreman31a876d2021-03-03 20:47:40 +0100807 /* Get the (exact or at-least) output lengths for both sides of the
808 * requested intersection. None of the currently supported algorithms
809 * have an output length dependent on the actual key size, so setting it
810 * to a bogus value of 0 is currently OK.
811 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100812 * Note that for at-least-this-length wildcard algorithms, the output
813 * length is set to the shortest allowed length, which allows us to
814 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
816 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100817 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100818
Steven Cooremana1d83222021-02-25 10:20:29 +0100819 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
821 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
822 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100823 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100824
825 /* If only one is an at-least-this-length policy, the intersection would
826 * be the other (fixed-length) policy as long as said fixed length is
827 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
829 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100830 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
832 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100834
Steven Cooremancd640932021-03-08 12:00:27 +0100835 /* If none of them are wildcards, check whether they define the same tag
836 * length. This is still possible here when one is default-length and
837 * the other specific-length. Ensure to always return the
838 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (alg1_len == alg2_len) {
840 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
841 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100842 }
843 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100845}
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847static int psa_key_algorithm_permits(psa_key_type_t key_type,
848 psa_algorithm_t policy_alg,
849 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200850{
Gilles Peskine549ea862019-05-22 11:45:59 +0200851 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (requested_alg == policy_alg) {
853 return 1;
854 }
Steven Cooreman03488022021-02-18 12:07:20 +0100855 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
856 * and requested_alg is the same hash-and-sign family with any hash,
857 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
859 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
860 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
861 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100862 }
863 /* If policy_alg is a wildcard AEAD algorithm of the same base as
864 * the requested algorithm, check the requested tag length to be
865 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (PSA_ALG_IS_AEAD(policy_alg) &&
867 PSA_ALG_IS_AEAD(requested_alg) &&
868 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
869 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
870 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
871 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
872 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100873 }
Steven Cooremancd640932021-03-08 12:00:27 +0100874 /* If policy_alg is a MAC algorithm of the same base as the requested
875 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (PSA_ALG_IS_MAC(policy_alg) &&
877 PSA_ALG_IS_MAC(requested_alg) &&
878 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
879 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100880 /* Validate the combination of key type and algorithm. Since the policy
881 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
883 return 0;
884 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100885
Steven Cooreman31a876d2021-03-03 20:47:40 +0100886 /* Get both the requested output length for the algorithm which is to be
887 * verified, and the default output length for the base algorithm.
888 * Note that none of the currently supported algorithms have an output
889 * length dependent on actual key size, so setting it to a bogus value
890 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100891 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100893 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 key_type, 0,
895 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100896
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100897 /* If the policy is default-length, only allow an algorithm with
898 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
900 return requested_output_length == default_output_length;
901 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100902
903 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100904 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
906 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
907 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100908 }
909
Steven Cooremancd640932021-03-08 12:00:27 +0100910 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
911 * check for the requested MAC length to be equal to or longer than the
912 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
914 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
915 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100916 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200917 }
Steven Cooremance48e852020-10-05 16:02:45 +0200918 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200919 * a key derivation with that key agreement should also be allowed. This
920 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
922 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
923 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
924 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200925 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100926 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200928}
929
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100930/** Test whether a policy permits an algorithm.
931 *
932 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100934 * \note This function requires providing the key type for which the policy is
935 * being validated, since some algorithm policy definitions (e.g. MAC)
936 * have different properties depending on what kind of cipher it is
937 * combined with.
938 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100939 * \retval PSA_SUCCESS When \p alg is a specific algorithm
940 * allowed by the \p policy.
941 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
942 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
943 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
946 psa_key_type_t key_type,
947 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100948{
Steven Cooremand788fab2021-02-25 11:29:17 +0100949 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (alg == 0) {
951 return PSA_ERROR_INVALID_ARGUMENT;
952 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100953
Steven Cooreman7e39f052021-02-23 14:18:32 +0100954 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (PSA_ALG_IS_WILDCARD(alg)) {
956 return PSA_ERROR_INVALID_ARGUMENT;
957 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
960 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
961 return PSA_SUCCESS;
962 } else {
963 return PSA_ERROR_NOT_PERMITTED;
964 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100965}
966
Gilles Peskinef603c712019-01-19 13:40:11 +0100967/** Restrict a key policy based on a constraint.
968 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100969 * \note This function requires providing the key type for which the policy is
970 * being restricted, since some algorithm policy definitions (e.g. MAC)
971 * have different properties depending on what kind of cipher it is
972 * combined with.
973 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100974 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100975 * \param[in,out] policy The policy to restrict.
976 * \param[in] constraint The policy constraint to apply.
977 *
978 * \retval #PSA_SUCCESS
979 * \c *policy contains the intersection of the original value of
980 * \c *policy and \c *constraint.
981 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100982 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100983 * \c *policy is unchanged.
984 */
985static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100986 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100987 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100989{
990 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 psa_key_policy_algorithm_intersection(key_type, policy->alg,
992 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200993 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
995 constraint->alg2);
996 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
997 return PSA_ERROR_INVALID_ARGUMENT;
998 }
999 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1000 return PSA_ERROR_INVALID_ARGUMENT;
1001 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001002 policy->usage &= constraint->usage;
1003 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001004 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001006}
1007
Przemek Stekiel061f6942022-11-30 10:51:35 +01001008/** Get the description of a key given its identifier and policy constraints
1009 * and lock it.
1010 *
1011 * The key must have allow all the usage flags set in \p usage. If \p alg is
1012 * nonzero, the key must allow operations with this algorithm. If \p alg is
1013 * zero, the algorithm is not checked.
1014 *
1015 * In case of a persistent key, the function loads the description of the key
1016 * into a key slot if not already done.
1017 *
Ryan Everett098c6652024-01-03 13:03:36 +00001018 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001019 * It is the responsibility of the caller to then unregister
1020 * once they have finished reading the contents of the slot.
1021 * The caller unregisters by calling psa_unregister_read() or
1022 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1023 * if and only if the caller already holds the global key slot mutex
1024 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1025 * the unregister with mutex lock and unlock operations.
Przemek Stekiel061f6942022-11-30 10:51:35 +01001026 */
1027static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001028 mbedtls_svc_key_id_t key,
1029 psa_key_slot_t **p_slot,
1030 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001032{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001034 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 status = psa_get_and_lock_key_slot(key, p_slot);
1037 if (status != PSA_SUCCESS) {
1038 return status;
1039 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001040 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001041
1042 /* Enforce that usage policy for the key slot contains all the flags
1043 * required by the usage parameter. There is one exception: public
1044 * keys can always be exported, so we treat public key objects as
1045 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001047 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001051 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001052 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001053 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001054
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001055 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (alg != 0) {
1057 status = psa_key_policy_permits(&slot->attr.policy,
1058 slot->attr.type,
1059 alg);
1060 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001061 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001063 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001066
1067error:
1068 *p_slot = NULL;
Ryan Everettfb792ca2024-01-31 13:40:05 +00001069 psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001072}
Darryl Green940d72c2018-07-13 13:18:51 +01001073
Ronald Cron5c522922020-11-14 16:35:34 +01001074/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075 *
1076 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001077 * available, as opposed to a key in a secure element and/or to be used
1078 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001079 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001080 * This is a temporary function that may be used instead of
1081 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1082 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001083 *
Ryan Everett098c6652024-01-03 13:03:36 +00001084 * On success, the returned key slot has been registered for reading.
Ryan Everett93cea572024-02-20 18:01:29 +00001085 * It is the responsibility of the caller to then unregister
1086 * once they have finished reading the contents of the slot.
1087 * The caller unregisters by calling psa_unregister_read() or
1088 * psa_unregister_read_under_mutex(). psa_unregister_read() must be called
1089 * if and only if the caller already holds the global key slot mutex
1090 * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates
1091 * psa_unregister_read() with mutex lock and unlock operations.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001092 */
Ronald Cron5c522922020-11-14 16:35:34 +01001093static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1094 mbedtls_svc_key_id_t key,
1095 psa_key_slot_t **p_slot,
1096 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001098{
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1100 usage, alg);
1101 if (status != PSA_SUCCESS) {
1102 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001103 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
Ryan Everettfb792ca2024-01-31 13:40:05 +00001106 psa_unregister_read_under_mutex(*p_slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 *p_slot = NULL;
1108 return PSA_ERROR_NOT_SUPPORTED;
1109 }
1110
1111 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001112}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001115{
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001117 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001119
Ronald Cronea0f8a62020-11-25 17:52:23 +01001120 slot->key.data = NULL;
1121 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001124}
1125
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001126/** Completely wipe a slot in memory, including its policy.
1127 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001129{
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 /*
1133 * As the return error code may not be handled in case of multiple errors,
Ryan Everett7ed542e2024-01-17 11:39:09 +00001134 * do our best to report an unexpected amount of registered readers or
1135 * an unexpected state.
1136 * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for
1137 * wiping.
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1139 * function is called as part of the execution of a test suite, the
1140 * execution of the test suite is stopped in error if the assertion fails.
1141 */
Ryan Everett7ed542e2024-01-17 11:39:09 +00001142 switch (slot->state) {
1143 case PSA_SLOT_FULL:
1144 /* In this state psa_wipe_key_slot() must only be called if the
1145 * caller is the last reader. */
1146 case PSA_SLOT_PENDING_DELETION:
1147 /* In this state psa_wipe_key_slot() must only be called if the
1148 * caller is the last reader. */
1149 if (slot->registered_readers != 1) {
1150 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 1);
1151 status = PSA_ERROR_CORRUPTION_DETECTED;
1152 }
1153 break;
1154 case PSA_SLOT_FILLING:
1155 /* In this state registered_readers must be 0. */
1156 if (slot->registered_readers != 0) {
1157 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->registered_readers == 0);
1158 status = PSA_ERROR_CORRUPTION_DETECTED;
1159 }
1160 break;
1161 case PSA_SLOT_EMPTY:
1162 /* The slot is already empty, it cannot be wiped. */
1163 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY);
1164 status = PSA_ERROR_CORRUPTION_DETECTED;
1165 break;
1166 default:
1167 /* The slot's state is invalid. */
1168 status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronddd3d052020-10-30 14:07:07 +01001169 }
1170
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001171 /* Multipart operations may still be using the key. This is safe
1172 * because all multipart operation objects are independent from
1173 * the key slot: if they need to access the key after the setup
1174 * phase, they have a copy of the key. Note that this means that
1175 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001176 /* At this point, key material and other type-specific content has
1177 * been wiped. Clear remaining metadata. We can call memset and not
Ryan Everettaa33c512023-12-21 17:32:07 +00001178 * zeroize because the metadata is not particularly sensitive.
1179 * This memset also sets the slot's state to PSA_SLOT_EMPTY. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 memset(slot, 0, sizeof(*slot));
1181 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001182}
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001186 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001187 psa_status_t status; /* status of the last operation */
1188 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001189#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1190 psa_se_drv_table_entry_t *driver;
1191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_svc_key_id_is_null(key)) {
1194 return PSA_SUCCESS;
1195 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001196
Ronald Cronf2911112020-10-29 17:51:10 +01001197 /*
Ryan Everett7ed542e2024-01-17 11:39:09 +00001198 * Get the description of the key in a key slot, and register to read it.
1199 * In the case of a persistent key, this will load the key description
1200 * from persistent memory if not done yet.
1201 * We cannot avoid this loading as without it we don't know if
Ronald Cronf2911112020-10-29 17:51:10 +01001202 * the key is operated by an SE or not and this information is needed by
Ryan Everett7ed542e2024-01-17 11:39:09 +00001203 * the current implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 status = psa_get_and_lock_key_slot(key, &slot);
1205 if (status != PSA_SUCCESS) {
1206 return status;
1207 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001208
Ryan Everettc053d962024-01-25 17:56:32 +00001209#if defined(MBEDTLS_THREADING_C)
Ryan Everett763971f2024-01-29 17:13:36 +00001210 /* We cannot unlock between setting the state to PENDING_DELETION
1211 * and destroying the key in storage, as otherwise another thread
1212 * could load the key into a new slot and the key will not be
1213 * fully destroyed. */
Ryan Everettc053d962024-01-25 17:56:32 +00001214 PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock(
1215 &mbedtls_threading_key_slot_mutex));
Ryan Everettd868b742024-03-08 18:35:09 +00001216
1217 if (slot->state == PSA_SLOT_PENDING_DELETION) {
1218 /* Another thread has destroyed the key between us locking the slot
1219 * and us gaining the mutex. Unregister from the slot,
1220 * and report that the key does not exist. */
1221 status = psa_unregister_read(slot);
1222
1223 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1224 &mbedtls_threading_key_slot_mutex));
1225 return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status;
1226 }
Ryan Everettc053d962024-01-25 17:56:32 +00001227#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001228 /* Set the key slot containing the key description's state to
1229 * PENDING_DELETION. This stops new operations from registering
1230 * to read the slot. Current readers can safely continue to access
1231 * the key within the slot; the last registered reader will
1232 * automatically wipe the slot when they call psa_unregister_read().
1233 * If the key is persistent, we can now delete the copy of the key
1234 * from memory. If the key is opaque, we require the driver to
1235 * deal with the deletion. */
Ryan Everettd868b742024-03-08 18:35:09 +00001236 overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1237 PSA_SLOT_PENDING_DELETION);
Ryan Everettc053d962024-01-25 17:56:32 +00001238
Ryan Everettd868b742024-03-08 18:35:09 +00001239 if (overall_status != PSA_SUCCESS) {
Ryan Everettc053d962024-01-25 17:56:32 +00001240 goto exit;
1241 }
Ronald Cronf2911112020-10-29 17:51:10 +01001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001244 /* Refuse the destruction of a read-only key (which may or may not work
1245 * if we attempt it, depending on whether the key is merely read-only
1246 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001247 * Just do the best we can, which is to wipe the copy in memory
1248 * (done in this function's cleanup code). */
1249 overall_status = PSA_ERROR_NOT_PERMITTED;
1250 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001251 }
1252
Gilles Peskine354f7672019-07-12 23:46:38 +02001253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1255 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001256 /* For a key in a secure element, we need to do three things:
1257 * remove the key file in internal storage, destroy the
1258 * key inside the secure element, and update the driver's
1259 * persistent data. Start a transaction that will encompass these
1260 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 status = psa_crypto_save_transaction();
1266 if (status != PSA_SUCCESS) {
1267 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001268 /* We should still try to destroy the key in the secure
1269 * element and the key metadata in storage. This is especially
1270 * important if the error is that the storage is full.
1271 * But how to do it exactly without risking an inconsistent
1272 * state after a reset?
1273 * https://github.com/ARMmbed/mbed-crypto/issues/215
1274 */
1275 overall_status = status;
1276 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 status = psa_destroy_se_key(driver,
1280 psa_key_slot_get_slot_number(slot));
1281 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001284 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1286
Darryl Greend49a4992018-06-18 17:27:26 +01001287#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001289 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001290 * The slot will still hold a copy of the key until the last reader
1291 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_destroy_persistent_key(slot->attr.id);
1293 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 }
Darryl Greend49a4992018-06-18 17:27:26 +01001296 }
1297#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001298
Gilles Peskinefc762652019-07-22 19:30:34 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (driver != NULL) {
1301 status = psa_save_se_persistent_data(driver);
1302 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 }
1305 status = psa_crypto_stop_transaction();
1306 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001309 }
1310#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1311
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001313 /* Unregister from reading the slot. If we are the last active reader
1314 * then this will wipe the slot. */
1315 status = psa_unregister_read(slot);
1316 /* Prioritize CORRUPTION_DETECTED from unregistering over
1317 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001319 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 }
Ryan Everettc053d962024-01-25 17:56:32 +00001321
1322#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001323 /* Don't overwrite existing errors if the unlock fails. */
1324 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001325 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1326 &mbedtls_threading_key_slot_mutex));
1327#endif
1328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
1331
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001332/** Retrieve all the publicly-accessible attributes of a key.
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1335 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001337 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001338 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1343 if (status != PSA_SUCCESS) {
1344 return status;
1345 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001346
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001347 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001348
1349#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1351 psa_set_key_slot_number(attributes,
1352 psa_key_slot_get_slot_number(slot));
1353 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001354#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001355
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001356 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357}
1358
Gilles Peskinec8000c02019-08-02 20:15:51 +02001359#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1360psa_status_t psa_get_key_slot_number(
1361 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001363{
Gilles Peskine972539c2024-02-28 01:49:45 +01001364 if (attributes->has_slot_number) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001365 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return PSA_SUCCESS;
1367 } else {
1368 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001369 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001370}
1371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1374 size_t key_buffer_size,
1375 uint8_t *data,
1376 size_t data_size,
1377 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001378{
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (key_buffer_size > data_size) {
1380 return PSA_ERROR_BUFFER_TOO_SMALL;
1381 }
1382 memcpy(data, key_buffer, key_buffer_size);
1383 memset(data + key_buffer_size, 0,
1384 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001385 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001387}
1388
Ronald Cron7285cda2020-11-26 14:46:37 +01001389psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 const psa_key_attributes_t *attributes,
1391 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001393{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001394 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (key_type_is_raw_bytes(type) ||
1397 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001398 PSA_KEY_TYPE_IS_ECC(type) ||
1399 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return psa_export_key_buffer_internal(
1401 key_buffer, key_buffer_size,
1402 data, data_size, data_length);
1403 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001404 /* This shouldn't happen in the reference implementation, but
1405 it is valid for a special-purpose implementation to omit
1406 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408 }
1409}
1410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everett45ac5262024-01-08 17:15:19 +00001412 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 size_t data_size,
1414 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415{
1416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1417 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1418 psa_key_slot_t *slot;
Ryan Everett45ac5262024-01-08 17:15:19 +00001419 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420
Ronald Crone907e552021-01-18 13:22:38 +01001421 /* Reject a zero-length output buffer now, since this can never be a
1422 * valid key representation. This way we know that data must be a valid
1423 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (data_size == 0) {
1425 return PSA_ERROR_BUFFER_TOO_SMALL;
1426 }
Ronald Crone907e552021-01-18 13:22:38 +01001427
Ronald Cron9486f9d2020-11-26 13:36:23 +01001428 /* Set the key to empty now, so that even when there are errors, we always
1429 * set data_length to a value between 0 and data_size. On error, setting
1430 * the key to empty is a good choice because an empty key representation is
1431 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001432 *data_length = 0;
1433
Ronald Cron9486f9d2020-11-26 13:36:23 +01001434 /* Export requires the EXPORT flag. There is an exception for public keys,
1435 * which don't require any flag, but
1436 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1439 PSA_KEY_USAGE_EXPORT, 0);
1440 if (status != PSA_SUCCESS) {
1441 return status;
1442 }
mohammad160306e79202018-03-28 13:17:44 +03001443
Ryan Everett45ac5262024-01-08 17:15:19 +00001444 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1445
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001446 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 slot->key.data, slot->key.bytes,
1448 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001449
Ryan Everett35f68532024-01-25 12:02:03 +00001450#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everett45ac5262024-01-08 17:15:19 +00001451exit:
Ryan Everett35f68532024-01-25 12:02:03 +00001452#endif
Ryan Everetta103ec92024-01-31 13:59:57 +00001453 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001454
Ryan Everett45ac5262024-01-08 17:15:19 +00001455 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001457}
1458
Ronald Cron7285cda2020-11-26 14:46:37 +01001459psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 const psa_key_attributes_t *attributes,
1461 const uint8_t *key_buffer,
1462 size_t key_buffer_size,
1463 uint8_t *data,
1464 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001466{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001467 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001468
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001469 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001470 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1471 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001472 /* Exporting public -> public */
1473 return psa_export_key_buffer_internal(
1474 key_buffer, key_buffer_size,
1475 data, data_size, data_length);
1476 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001477#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001478 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1479 return mbedtls_psa_rsa_export_public_key(attributes,
1480 key_buffer,
1481 key_buffer_size,
1482 data,
1483 data_size,
1484 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001485#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001486 /* We don't know how to convert a private RSA key to public. */
1487 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001488#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001489 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001490 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001491#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001492 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1493 return mbedtls_psa_ecp_export_public_key(attributes,
1494 key_buffer,
1495 key_buffer_size,
1496 data,
1497 data_size,
1498 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001499#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001500 /* We don't know how to convert a private ECC key to public */
1501 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001502#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001503 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001504 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001505#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001506 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001507 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001508 key_buffer,
1509 key_buffer_size,
1510 data, data_size,
1511 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001512#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001513 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001514#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001515 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001517 (void) key_buffer;
1518 (void) key_buffer_size;
1519 (void) data;
1520 (void) data_size;
1521 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001523 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524}
Ronald Crond18b5f82020-11-25 16:46:05 +01001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everettb1d2c672024-01-08 17:19:30 +00001527 uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 size_t data_size,
1529 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001530{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001532 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001533 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001534
Ryan Everettb1d2c672024-01-08 17:19:30 +00001535 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001536
Ronald Crone907e552021-01-18 13:22:38 +01001537 /* Reject a zero-length output buffer now, since this can never be a
1538 * valid key representation. This way we know that data must be a valid
1539 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (data_size == 0) {
1541 return PSA_ERROR_BUFFER_TOO_SMALL;
1542 }
Ronald Crone907e552021-01-18 13:22:38 +01001543
Darryl Greendd8fb772018-11-07 16:00:44 +00001544 /* Set the key to empty now, so that even when there are errors, we always
1545 * set data_length to a value between 0 and data_size. On error, setting
1546 * the key to empty is a good choice because an empty key representation is
1547 * unlikely to be accepted anywhere. */
1548 *data_length = 0;
1549
1550 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1552 if (status != PSA_SUCCESS) {
1553 return status;
1554 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001555
Ryan Everettb1d2c672024-01-08 17:19:30 +00001556 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1559 status = PSA_ERROR_INVALID_ARGUMENT;
1560 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001561 }
1562
Ronald Cron67227982020-11-26 15:16:05 +01001563 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001564 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001566
1567exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001568 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001569
Ryan Everettb1d2c672024-01-08 17:19:30 +00001570 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001572}
1573
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001574/** Validate that a key policy is internally well-formed.
1575 *
1576 * This function only rejects invalid policies. It does not validate the
1577 * consistency of the policy with respect to other attributes of the key
1578 * such as the key type.
1579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001581{
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1583 PSA_KEY_USAGE_COPY |
1584 PSA_KEY_USAGE_ENCRYPT |
1585 PSA_KEY_USAGE_DECRYPT |
1586 PSA_KEY_USAGE_SIGN_MESSAGE |
1587 PSA_KEY_USAGE_VERIFY_MESSAGE |
1588 PSA_KEY_USAGE_SIGN_HASH |
1589 PSA_KEY_USAGE_VERIFY_HASH |
1590 PSA_KEY_USAGE_VERIFY_DERIVATION |
1591 PSA_KEY_USAGE_DERIVE)) != 0) {
1592 return PSA_ERROR_INVALID_ARGUMENT;
1593 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001596}
1597
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598/** Validate the internal consistency of key attributes.
1599 *
1600 * This function only rejects invalid attribute values. If does not
1601 * validate the consistency of the attributes with any key data that may
1602 * be involved in the creation of the key.
1603 *
1604 * Call this function early in the key creation process.
1605 *
1606 * \param[in] attributes Key attributes for the new key.
1607 * \param[out] p_drv On any return, the driver for the key, if any.
1608 * NULL for a transparent key.
1609 *
1610 */
1611static psa_status_t psa_validate_key_attributes(
1612 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001614{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001615 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1617 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 status = psa_validate_key_location(lifetime, p_drv);
1620 if (status != PSA_SUCCESS) {
1621 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 status = psa_validate_key_persistence(lifetime);
1625 if (status != PSA_SUCCESS) {
1626 return status;
1627 }
1628
1629 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1630 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1631 return PSA_ERROR_INVALID_ARGUMENT;
1632 }
1633 } else {
1634 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1635 return PSA_ERROR_INVALID_ARGUMENT;
1636 }
1637 }
1638
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001639 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (status != PSA_SUCCESS) {
1641 return status;
1642 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001643
1644 /* Refuse to create overly large keys.
1645 * Note that this doesn't trigger on import if the attributes don't
1646 * explicitly specify a size (so psa_get_key_bits returns 0), so
1647 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1649 return PSA_ERROR_NOT_SUPPORTED;
1650 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001653}
1654
Gilles Peskine4747d192019-04-17 15:05:45 +02001655/** Prepare a key slot to receive key material.
1656 *
1657 * This function allocates a key slot and sets its metadata.
1658 *
1659 * If this function fails, call psa_fail_key_creation().
1660 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001661 * This function is intended to be used as follows:
1662 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001663 * it with the specified attributes, and in case of a volatile key assign it
1664 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001665 * -# Populate the slot with the key material.
1666 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1667 * In case of failure at any step, stop the sequence and call
1668 * psa_fail_key_creation().
1669 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001670 * On success, the key slot's state is PSA_SLOT_FILLING.
1671 * It is the responsibility of the caller to change the slot's state to
1672 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001673 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001674 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001675 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001676 * \param[out] p_slot On success, a pointer to the prepared slot.
1677 * \param[out] p_drv On any return, the driver for the key, if any.
1678 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001679 *
1680 * \retval #PSA_SUCCESS
1681 * The key slot is ready to receive key material.
1682 * \return If this function fails, the key slot is an invalid state.
1683 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 */
1685static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001686 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001688 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001690{
1691 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001692 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_key_slot_t *slot;
1694
Gilles Peskinedf179142019-07-15 22:02:14 +02001695 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001696 *p_drv = NULL;
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 status = psa_validate_key_attributes(attributes, p_drv);
1699 if (status != PSA_SUCCESS) {
1700 return status;
1701 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001702
Ryan Everett3d8118d2024-01-30 16:58:47 +00001703#if defined(MBEDTLS_THREADING_C)
1704 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1705 &mbedtls_threading_key_slot_mutex));
1706#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001707 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001708#if defined(MBEDTLS_THREADING_C)
1709 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1710 &mbedtls_threading_key_slot_mutex));
1711#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (status != PSA_SUCCESS) {
1713 return status;
1714 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 slot = *p_slot;
1716
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001717 /* We're storing the declared bit-size of the key. It's up to each
1718 * creation mechanism to verify that this information is correct.
1719 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001720 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001721 * is optional (import, copy). In case of a volatile key, assign it the
1722 * volatile key identifier associated to the slot returned to contain its
1723 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001724
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001725 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001727#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1728 slot->attr.id = volatile_key_id;
1729#else
1730 slot->attr.id.key_id = volatile_key_id;
1731#endif
1732 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001733
Gilles Peskinecbaff462019-07-12 23:46:04 +02001734#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001735 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001736 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001737 * create the key file in internal storage, create the
1738 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001739 * persistent data. This is done by starting a transaction that will
1740 * encompass these three actions.
1741 * For registering a volatile key, we just need to find an appropriate
1742 * slot number inside the SE. Since the key is designated volatile, creating
1743 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001744 /* The first thing to do is to find a slot number for the new key.
1745 * We save the slot number in persistent storage as part of the
1746 * transaction data. It will be needed to recover if the power
1747 * fails during the key creation process, to clean up on the secure
1748 * element side after restarting. Obtaining a slot number from the
1749 * secure element driver updates its persistent state, but we do not yet
1750 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001751 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001753 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1755 &slot_number);
1756 if (status != PSA_SUCCESS) {
1757 return status;
1758 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001759
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001760 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001762 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001763 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001764 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 status = psa_crypto_save_transaction();
1766 if (status != PSA_SUCCESS) {
1767 (void) psa_crypto_stop_transaction();
1768 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001769 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001770 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001771
1772 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001774 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001777 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001779 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001783}
Gilles Peskine4747d192019-04-17 15:05:45 +02001784
1785/** Finalize the creation of a key once its key material has been set.
1786 *
1787 * This entails writing the key to persistent storage.
1788 *
1789 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001790 * See the documentation of psa_start_key_creation() for the intended use
1791 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001793 * If the finalization succeeds, the function sets the key slot's state to
1794 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1795 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001796 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001797 * \param[in,out] slot Pointer to the slot with key material.
1798 * \param[in] driver The secure element driver for the key,
1799 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001800 * \param[out] key On success, identifier of the key. Note that the
1801 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001802 *
1803 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001804 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001805 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1806 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1807 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1808 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1809 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1810 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001811 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001812 * \return If this function fails, the key slot is an invalid state.
1813 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001814 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001815static psa_status_t psa_finish_key_creation(
1816 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001817 psa_se_drv_table_entry_t *driver,
1818 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001819{
1820 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001821 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001822 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001823
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001824#if defined(MBEDTLS_THREADING_C)
1825 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1826 &mbedtls_threading_key_slot_mutex));
1827#endif
1828
Gilles Peskine4747d192019-04-17 15:05:45 +02001829#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001831#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001833 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001834 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001836
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001837 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1838 sizeof(data.slot_number),
1839 "Slot number size does not match psa_se_key_data_storage_t");
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1842 status = psa_save_persistent_key(&slot->attr,
1843 (uint8_t *) &data,
1844 sizeof(data));
1845 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001846#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1847 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001848 /* Key material is saved in export representation in the slot, so
1849 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 status = psa_save_persistent_key(&slot->attr,
1851 slot->key.data,
1852 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001853 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001855#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1856
Gilles Peskinecbaff462019-07-12 23:46:04 +02001857#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001858 /* Finish the transaction for a key creation. This does not
1859 * happen when registering an existing key. Detect this case
1860 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001861 * creation of a persistent key in a secure element requires a transaction,
1862 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (driver != NULL &&
1864 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1865 status = psa_save_se_persistent_data(driver);
1866 if (status != PSA_SUCCESS) {
1867 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001868
1869#if defined(MBEDTLS_THREADING_C)
1870 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1871 &mbedtls_threading_key_slot_mutex));
1872#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001874 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001876 }
1877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001881 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1882 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 }
Ronald Cron50972942020-11-14 11:28:25 +01001887
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001888#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 Peskine4747d192019-04-17 15:05:45 +02001893}
1894
1895/** Abort the creation of a key.
1896 *
1897 * You may call this function after calling psa_start_key_creation(),
1898 * or after psa_finish_key_creation() fails. In other circumstances, this
1899 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001900 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001901 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001903 * \param[in,out] slot Pointer to the slot with key material.
1904 * \param[in] driver The secure element driver for the key,
1905 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001907static void psa_fail_key_creation(psa_key_slot_t *slot,
1908 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001909{
Gilles Peskine011e4282019-06-26 18:34:38 +02001910 (void) driver;
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001915
Ryan Everettb7101442024-01-23 20:09:49 +00001916#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001917 /* If the lock operation fails we still wipe the slot.
1918 * Operations will no longer work after a failed lock,
1919 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001920 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1921#endif
1922
Gilles Peskine011e4282019-06-26 18:34:38 +02001923#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001924 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001925 * element, and the failure happened later (when saving metadata
1926 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001927 * element.
1928 * https://github.com/ARMmbed/mbed-crypto/issues/217
1929 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001930
Gilles Peskined7729582019-08-05 15:55:54 +02001931 /* Abort the ongoing transaction if any (there may not be one if
1932 * the creation process failed before starting one, or if the
1933 * key creation is a registration of a key in a secure element).
1934 * Earlier functions must already have done what it takes to undo any
1935 * partial creation. All that's left is to update the transaction data
1936 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001941
1942#if defined(MBEDTLS_THREADING_C)
1943 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1944#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001945}
1946
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001947/** Validate optional attributes during key creation.
1948 *
1949 * Some key attributes are optional during key creation. If they are
1950 * specified in the attributes structure, check that they are consistent
1951 * with the data in the slot.
1952 *
1953 * This function should be called near the end of key creation, after
1954 * the slot in memory is fully populated but before saving persistent data.
1955 */
1956static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001960 if (attributes->type != 0) {
1961 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 return PSA_ERROR_INVALID_ARGUMENT;
1963 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001964 }
1965
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001966 if (attributes->bits != 0) {
1967 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 return PSA_ERROR_INVALID_ARGUMENT;
1969 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 }
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973}
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everettf028fe12024-01-08 17:14:44 +00001976 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 size_t data_length,
1978 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001979{
1980 psa_status_t status;
Ryan Everettf028fe12024-01-08 17:14:44 +00001981 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001983 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301985 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 *key = MBEDTLS_SVC_KEY_ID_INIT;
1988
Gilles Peskine0f84d622019-09-12 19:03:13 +02001989 /* Reject zero-length symmetric keys (including raw data key objects).
1990 * This also rejects any key which might be encoded as an empty string,
1991 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (data_length == 0) {
1993 return PSA_ERROR_INVALID_ARGUMENT;
1994 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001995
Archana449608b2021-09-08 15:36:05 +05301996 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (data_length > SIZE_MAX / 8) {
1998 return PSA_ERROR_NOT_SUPPORTED;
1999 }
Archana6ed4bda2021-08-04 10:47:15 +05302000
Ryan Everettf028fe12024-01-08 17:14:44 +00002001 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2004 &slot, &driver);
2005 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002009 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302010 * storage ( thus not in the case of importing a key in a secure element
2011 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2012 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002014 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302015 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 attributes, data, data_length, &storage_size);
2017 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 }
Archanad8a83dc2021-06-14 10:04:16 +05302020 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 status = psa_allocate_buffer_to_slot(slot, storage_size);
2022 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002025 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002026
2027 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 status = psa_driver_wrapper_import_key(attributes,
2029 data, data_length,
2030 slot->key.data,
2031 slot->key.bytes,
2032 &slot->key.bytes, &bits);
2033 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002038 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002040 status = PSA_ERROR_INVALID_ARGUMENT;
2041 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002042 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002043
Archana6ed4bda2021-08-04 10:47:15 +05302044 /* Enforce a size limit, and in particular ensure that the bit
2045 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302047 status = PSA_ERROR_NOT_SUPPORTED;
2048 goto exit;
2049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 status = psa_validate_optional_attributes(slot, attributes);
2051 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002052 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056exit:
Ryan Everettf028fe12024-01-08 17:14:44 +00002057 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (status != PSA_SUCCESS) {
2059 psa_fail_key_creation(slot, driver);
2060 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002063}
2064
Gilles Peskined7729582019-08-05 15:55:54 +02002065#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2066psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002068{
2069 psa_status_t status;
2070 psa_key_slot_t *slot = NULL;
2071 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002073
2074 /* Leaving attributes unspecified is not currently supported.
2075 * It could make sense to query the key type and size from the
2076 * secure element, but not all secure elements support this
2077 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2079 return PSA_ERROR_NOT_SUPPORTED;
2080 }
2081 if (psa_get_key_bits(attributes) == 0) {
2082 return PSA_ERROR_NOT_SUPPORTED;
2083 }
Gilles Peskined7729582019-08-05 15:55:54 +02002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2086 &slot, &driver);
2087 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Gilles Peskined7729582019-08-05 15:55:54 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002092
2093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (status != PSA_SUCCESS) {
2095 psa_fail_key_creation(slot, driver);
2096 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002097
Gilles Peskined7729582019-08-05 15:55:54 +02002098 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 psa_close_key(key);
2100 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002101}
2102#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2105 const psa_key_attributes_t *specified_attributes,
2106 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002107{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002109 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 psa_key_slot_t *source_slot = NULL;
2111 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002113 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302114 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002115
Ronald Cron81709fc2020-11-14 12:10:32 +01002116 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2117
Archana8a180362021-07-05 02:18:48 +05302118 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2120 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 status = psa_validate_optional_attributes(source_slot,
2125 specified_attributes);
2126 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002129
Archana9d17bf42021-09-10 06:22:44 +05302130 /* The target key type and number of bits have been validated by
2131 * psa_validate_optional_attributes() to be either equal to zero or
2132 * equal to the ones of the source key. So it is safe to inherit
2133 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302134 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002135 actual_attributes.bits = source_slot->attr.bits;
2136 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302137
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002140 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 &source_slot->attr.policy);
2142 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2147 &target_slot, &driver);
2148 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 }
2151 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2152 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302153 /*
Archana9d17bf42021-09-10 06:22:44 +05302154 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302155 * the source key would need to be exported as plaintext and re-imported
2156 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302157 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302158 * appropriate API invocations from the application, if needed.
2159 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002160 status = PSA_ERROR_NOT_SUPPORTED;
2161 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002162 }
Archana8a180362021-07-05 02:18:48 +05302163 /*
2164 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302165 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302166 * - For opaque keys this translates to an invocation of the drivers'
2167 * copy_key entry point through the dispatch layer.
2168 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002169 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2171 &storage_size);
2172 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 }
Archana374fe5b2021-09-08 15:50:28 +05302175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2177 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 }
Archana374fe5b2021-09-08 15:50:28 +05302180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_driver_wrapper_copy_key(&actual_attributes,
2182 source_slot->key.data,
2183 source_slot->key.bytes,
2184 target_slot->key.data,
2185 target_slot->key.bytes,
2186 &target_slot->key.bytes);
2187 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 }
2190 } else {
2191 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302192 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 source_slot->key.bytes);
2194 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 }
Archana8a180362021-07-05 02:18:48 +05302197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (status != PSA_SUCCESS) {
2201 psa_fail_key_creation(target_slot, driver);
2202 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002203
Ryan Everetta103ec92024-01-31 13:59:57 +00002204 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002207}
2208
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002209
2210
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002211/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002212/* Message digests */
2213/****************************************************************/
2214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (operation->id == 0) {
2219 return PSA_SUCCESS;
2220 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223 operation->id = 0;
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226}
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2229 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002232
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002233 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002235 status = PSA_ERROR_BAD_STATE;
2236 goto exit;
2237 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002240 status = PSA_ERROR_INVALID_ARGUMENT;
2241 goto exit;
2242 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002243
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002244 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2245 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002249
2250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (status != PSA_SUCCESS) {
2252 psa_hash_abort(operation);
2253 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002254
2255 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256}
2257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002259 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002263 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002266 status = PSA_ERROR_BAD_STATE;
2267 goto exit;
2268 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002269
Steven Cooremanc8288352021-03-04 14:02:19 +01002270 /* Don't require hash implementations to behave correctly on a
2271 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (input_length == 0) {
2273 return PSA_SUCCESS;
2274 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002275
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002276 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002278
2279exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (status != PSA_SUCCESS) {
2281 psa_hash_abort(operation);
2282 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002284 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286}
2287
Thomas Daubney31d8c0b2024-01-18 15:26:59 +00002288static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002289 uint8_t *hash,
2290 size_t hash_size,
2291 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2294
Steven Cooremanfbe09282021-03-08 18:41:12 +01002295 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (operation->id == 0) {
2297 return PSA_ERROR_BAD_STATE;
2298 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002300 status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 operation, hash, hash_size, hash_length);
2302 psa_hash_abort(operation);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002303
2304 return status;
2305}
2306
2307psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2308 uint8_t *hash_external,
2309 size_t hash_size,
2310 size_t *hash_length)
2311{
2312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2313 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2314
2315 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2316 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2317
Thomas Daubneydedd1002024-01-25 16:52:50 +00002318#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002319exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002320#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002321 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002326 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328{
Ronald Cron69a63422021-10-18 09:47:58 +02002329 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 size_t actual_hash_length;
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2332 LOCAL_INPUT_DECLARE(hash_external, hash);
2333
2334 status = psa_hash_finish_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 operation,
2336 actual_hash, sizeof(actual_hash),
2337 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002344 status = PSA_ERROR_INVALID_SIGNATURE;
2345 goto exit;
2346 }
2347
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002348 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002349 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002350 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352
2353exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2355 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 }
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002358 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360}
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002363 const uint8_t *input_external, size_t input_length,
2364 uint8_t *hash_external, size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002366{
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2368 LOCAL_INPUT_DECLARE(input_external, input);
2369 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2370
Steven Cooremanfbe09282021-03-08 18:41:12 +01002371 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if (!PSA_ALG_IS_HASH(alg)) {
2373 return PSA_ERROR_INVALID_ARGUMENT;
2374 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002375
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002376 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2377 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2378 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneyd2411562024-01-25 17:25:07 +00002379 hash, hash_size, hash_length);
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002380
Thomas Daubneydedd1002024-01-25 16:52:50 +00002381#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002382exit:
Thomas Daubneydedd1002024-01-25 16:52:50 +00002383#endif
Thomas Daubney1c5118e2024-01-12 16:50:26 +00002384 LOCAL_INPUT_FREE(input_external, input);
2385 LOCAL_OUTPUT_FREE(hash_external, hash);
2386 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002387}
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney51ffac92024-01-18 15:46:26 +00002390 const uint8_t *input_external, size_t input_length,
2391 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002392{
Ronald Cron69a63422021-10-18 09:47:58 +02002393 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002394 size_t actual_hash_length;
Thomas Daubney51ffac92024-01-18 15:46:26 +00002395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2396
2397 LOCAL_INPUT_DECLARE(input_external, input);
2398 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney51ffac92024-01-18 15:46:26 +00002401 status = PSA_ERROR_INVALID_ARGUMENT;
2402 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002404
Thomas Daubney51ffac92024-01-18 15:46:26 +00002405 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2406 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 alg, input, input_length,
2408 actual_hash, sizeof(actual_hash),
2409 &actual_hash_length);
2410 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002411 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 }
2413 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002414 status = PSA_ERROR_INVALID_SIGNATURE;
2415 goto exit;
2416 }
Thomas Daubney51ffac92024-01-18 15:46:26 +00002417
2418 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Dave Rodgman78701152023-08-29 14:20:18 +01002419 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002420 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002422
2423exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney51ffac92024-01-18 15:46:26 +00002425
2426 LOCAL_INPUT_FREE(input_external, input);
2427 LOCAL_INPUT_FREE(hash_external, hash);
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002430}
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2433 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002434{
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (source_operation->id == 0 ||
2436 target_operation->id != 0) {
2437 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002438 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2441 target_operation);
2442 if (status != PSA_SUCCESS) {
2443 psa_hash_abort(target_operation);
2444 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002447}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448
2449
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451/* MAC */
2452/****************************************************************/
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455{
Steven Cooremane6804192021-03-19 18:28:56 +01002456 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (operation->id == 0) {
2458 return PSA_SUCCESS;
2459 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002462 operation->mac_size = 0;
2463 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002464 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467}
2468
Ronald Cron2dff3b22021-06-17 16:33:22 +02002469static psa_status_t psa_mac_finalize_alg_and_key_validation(
2470 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002471 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 psa_key_type_t key_type = psa_get_key_type(attributes);
2476 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (!PSA_ALG_IS_MAC(alg)) {
2479 return PSA_ERROR_INVALID_ARGUMENT;
2480 }
Steven Cooremane6804192021-03-19 18:28:56 +01002481
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002482 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 status = psa_mac_key_can_do(alg, key_type);
2484 if (status != PSA_SUCCESS) {
2485 return status;
2486 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002487
Steven Cooremand1a68f12021-05-10 11:13:41 +02002488 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002492 /* A very short MAC is too short for security since it can be
2493 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2494 * so we make this our minimum, even though 32 bits is still
2495 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002497 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2500 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002501 /* It's impossible to "truncate" to a larger length than the full length
2502 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002504 }
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002507 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2508 * that is disabled in the compile-time configuration. The result can
2509 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2510 * configuration into account. In this case, force a return of
2511 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2512 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2513 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2514 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2515 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002517 }
2518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002520}
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2523 mbedtls_svc_key_id_t key,
2524 psa_algorithm_t alg,
2525 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002526{
2527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2528 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002529 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530
2531 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002533 status = PSA_ERROR_BAD_STATE;
2534 goto exit;
2535 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536
2537 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 key,
2539 &slot,
2540 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2541 alg);
2542 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002543 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002546 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 &operation->mac_size);
2548 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551
2552 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002553 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (is_sign) {
2555 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002556 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 slot->key.data,
2558 slot->key.bytes,
2559 alg);
2560 } else {
2561 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002562 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 slot->key.data,
2564 slot->key.bytes,
2565 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002567
Gilles Peskinefbfac682018-07-08 20:51:54 +02002568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (status != PSA_SUCCESS) {
2570 psa_mac_abort(operation);
2571 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002572
Ryan Everettfb9857f2024-02-14 12:16:41 +00002573 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576}
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2579 mbedtls_svc_key_id_t key,
2580 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002581{
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002583}
2584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2586 mbedtls_svc_key_id_t key,
2587 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002588{
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002590}
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002593 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2597 LOCAL_INPUT_DECLARE(input_external, input);
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (operation->id == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002600 status = PSA_ERROR_BAD_STATE;
2601 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002604 /* Don't require hash implementations to behave correctly on a
2605 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (input_length == 0) {
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002607 status = PSA_SUCCESS;
2608 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002610
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002611 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2612 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (status != PSA_SUCCESS) {
2615 psa_mac_abort(operation);
2616 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002617
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002618#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002619exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002620#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002621 LOCAL_INPUT_FREE(input_external, input);
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624}
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002627 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 size_t mac_size,
2629 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002630{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002631 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2632 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002633 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002634 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002637 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002638 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002639 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002642 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002643 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002644 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002645
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002646 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2647 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002649 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002650 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002651 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002654 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002655 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002656 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002657
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 status = psa_driver_wrapper_mac_sign_finish(operation,
2660 mac, operation->mac_size,
2661 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002662
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002663exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002664 /* In case of success, set the potential excess room in the output buffer
2665 * to an invalid value, to avoid potentially leaking a longer MAC.
2666 * In case of error, set the output length and content to a safe default,
2667 * such that in case the caller misses an error check, the output would be
2668 * an unachievable MAC.
2669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002671 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002672 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002673 }
mohammad16036df908f2018-04-02 08:34:15 -07002674
Thomas Daubney03f1ea32024-02-01 16:16:27 +00002675 if (mac != NULL) {
Thomas Daubney1ffc5cb2024-01-31 18:09:36 +00002676 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
2677 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002680 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002683}
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002686 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2690 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002691 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002694 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002695 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002696 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002699 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002700 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002701 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002704 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002705 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002706 }
2707
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002708 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 status = psa_driver_wrapper_mac_verify_finish(operation,
2710 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002711
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002712exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 abort_status = psa_mac_abort(operation);
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002714 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717}
2718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2720 psa_algorithm_t alg,
2721 const uint8_t *input,
2722 size_t input_length,
2723 uint8_t *mac,
2724 size_t mac_size,
2725 size_t *mac_length,
2726 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002727{
2728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002729 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_key_slot_t *slot;
2731 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732
Ronald Cron51131b52021-06-17 17:17:20 +02002733 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 key,
2735 &slot,
2736 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2737 alg);
2738 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002739 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002741
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002742 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 &operation_mac_size);
2744 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002749 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002750 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002751 }
2752
2753 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002754 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 slot->key.data, slot->key.bytes,
2756 alg,
2757 input, input_length,
2758 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002759
2760exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002761 /* In case of success, set the potential excess room in the output buffer
2762 * to an invalid value, to avoid potentially leaking a longer MAC.
2763 * In case of error, set the output length and content to a safe default,
2764 * such that in case the caller misses an error check, the output would be
2765 * an unachievable MAC.
2766 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002768 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002769 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002770 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002771
2772 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002773
Ryan Everetta103ec92024-01-31 13:59:57 +00002774 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777}
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002781 const uint8_t *input_external,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002782 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002783 uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 size_t mac_size,
2785 size_t *mac_length)
2786{
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2788 LOCAL_INPUT_DECLARE(input_external, input);
2789 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2790
2791 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2792 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2793 status = psa_mac_compute_internal(key, alg,
Thomas Daubney7480a742024-01-30 11:29:47 +00002794 input, input_length,
2795 mac, mac_size, mac_length, 1);
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002796
2797#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002798exit:
Thomas Daubneyc6705c62024-01-30 11:21:47 +00002799#endif
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002800 LOCAL_INPUT_FREE(input_external, input);
2801 LOCAL_OUTPUT_FREE(mac_external, mac);
2802
2803 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002804}
2805
2806psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2807 psa_algorithm_t alg,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002808 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 size_t input_length,
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002810 const uint8_t *mac_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002812{
2813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002814 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2815 size_t actual_mac_length;
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002816 LOCAL_INPUT_DECLARE(input_external, input);
2817 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002818
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002819 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 status = psa_mac_compute_internal(key, alg,
2821 input, input_length,
2822 actual_mac, sizeof(actual_mac),
2823 &actual_mac_length, 0);
2824 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002825 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002829 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002830 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002831 }
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002832
2833 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Dave Rodgman78701152023-08-29 14:20:18 +01002834 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002835 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002836 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002837 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002838
2839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney8db8d1a2024-01-18 13:18:58 +00002841 LOCAL_INPUT_FREE(input_external, input);
2842 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002845}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002846
Gilles Peskinee59236f2018-01-27 23:32:46 +01002847/****************************************************************/
2848/* Asymmetric cryptography */
2849/****************************************************************/
2850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2852 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002853{
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 if (input_is_message) {
2855 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2856 return PSA_ERROR_INVALID_ARGUMENT;
2857 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2860 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2861 return PSA_ERROR_INVALID_ARGUMENT;
2862 }
2863 }
2864 } else {
2865 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2866 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002867 }
2868 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002871}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2874 int input_is_message,
2875 psa_algorithm_t alg,
2876 const uint8_t *input,
2877 size_t input_length,
2878 uint8_t *signature,
2879 size_t signature_size,
2880 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002881{
2882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2883 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2884 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885
2886 *signature_length = 0;
2887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 status = psa_sign_verify_check_alg(input_is_message, alg);
2889 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002892
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002893 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002894 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002895 * buffer can in principle be empty since it doesn't actually have
2896 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 if (signature_size == 0) {
2898 return PSA_ERROR_BUFFER_TOO_SMALL;
2899 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002900
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 key, &slot,
2903 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2904 PSA_KEY_USAGE_SIGN_HASH,
2905 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 status = PSA_ERROR_INVALID_ARGUMENT;
2913 goto exit;
2914 }
2915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002917 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002918 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002919 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 signature, signature_size, signature_length);
2921 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002922
2923 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002924 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 }
2928
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929
2930exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002931 psa_wipe_tag_output_buffer(signature, status, signature_size,
2932 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002933
Ryan Everetta103ec92024-01-31 13:59:57 +00002934 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002937}
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2940 int input_is_message,
2941 psa_algorithm_t alg,
2942 const uint8_t *input,
2943 size_t input_length,
2944 const uint8_t *signature,
2945 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946{
2947 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2948 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2949 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 status = psa_sign_verify_check_alg(input_is_message, alg);
2952 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002953 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002955
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002956 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 key, &slot,
2958 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2959 PSA_KEY_USAGE_VERIFY_HASH,
2960 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (status != PSA_SUCCESS) {
2963 return status;
2964 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002967 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002968 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002969 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 signature, signature_length);
2971 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002972 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002973 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002976 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002977
Ryan Everetta103ec92024-01-31 13:59:57 +00002978 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002981
2982}
2983
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002984psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002985 const psa_key_attributes_t *attributes,
2986 const uint8_t *key_buffer,
2987 size_t key_buffer_size,
2988 psa_algorithm_t alg,
2989 const uint8_t *input,
2990 size_t input_length,
2991 uint8_t *signature,
2992 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002994{
2995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002998 size_t hash_length;
2999 uint8_t hash[PSA_HASH_MAX_SIZE];
3000
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003001 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 PSA_ALG_SIGN_GET_HASH(alg),
3003 input, input_length,
3004 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003007 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003009
3010 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 attributes, key_buffer, key_buffer_size,
3012 alg, hash, hash_length,
3013 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003014 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003017}
3018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3020 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003021 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003023 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 size_t signature_size,
3025 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003026{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003027 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3028 LOCAL_INPUT_DECLARE(input_external, input);
3029 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3030
3031 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3032 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3033 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3034 signature_size, signature_length);
3035
Thomas Daubney3e65f522024-01-30 12:37:25 +00003036#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003037exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003038#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003039 LOCAL_INPUT_FREE(input_external, input);
3040 LOCAL_OUTPUT_FREE(signature_external, signature);
3041 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003042}
3043
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003044psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045 const psa_key_attributes_t *attributes,
3046 const uint8_t *key_buffer,
3047 size_t key_buffer_size,
3048 psa_algorithm_t alg,
3049 const uint8_t *input,
3050 size_t input_length,
3051 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003053{
3054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003057 size_t hash_length;
3058 uint8_t hash[PSA_HASH_MAX_SIZE];
3059
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003060 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 PSA_ALG_SIGN_GET_HASH(alg),
3062 input, input_length,
3063 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003066 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003068
3069 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 attributes, key_buffer, key_buffer_size,
3071 alg, hash, hash_length,
3072 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003073 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003076}
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3079 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003080 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 size_t input_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003082 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003084{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3086 LOCAL_INPUT_DECLARE(input_external, input);
3087 LOCAL_INPUT_DECLARE(signature_external, signature);
3088
3089 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3090 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3091 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3092 signature_length);
3093
Thomas Daubney3e65f522024-01-30 12:37:25 +00003094#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003095exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003096#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003097 LOCAL_INPUT_FREE(input_external, input);
3098 LOCAL_INPUT_FREE(signature_external, signature);
3099
3100 return status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003101}
3102
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003103psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003104 const psa_key_attributes_t *attributes,
3105 const uint8_t *key_buffer, size_t key_buffer_size,
3106 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003108{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003109 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3111 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003112#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3114 return mbedtls_psa_rsa_sign_hash(
3115 attributes,
3116 key_buffer, key_buffer_size,
3117 alg, hash, hash_length,
3118 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003119#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3120 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 } else {
3122 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003123 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003124 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003126#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3128 return mbedtls_psa_ecdsa_sign_hash(
3129 attributes,
3130 key_buffer, key_buffer_size,
3131 alg, hash, hash_length,
3132 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003133#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3134 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 } else {
3136 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003137 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003138 }
Ronald Cron4501c982021-03-19 20:09:32 +01003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 (void) key_buffer;
3141 (void) key_buffer_size;
3142 (void) hash;
3143 (void) hash_length;
3144 (void) signature;
3145 (void) signature_size;
3146 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003149}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3152 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003153 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003155 uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 size_t signature_size,
3157 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003158{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3160 LOCAL_INPUT_DECLARE(hash_external, hash);
3161 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3162
3163 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3164 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3165 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3166 signature_size, signature_length);
3167
Thomas Daubney3e65f522024-01-30 12:37:25 +00003168#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003169exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003170#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003171 LOCAL_INPUT_FREE(hash_external, hash);
3172 LOCAL_OUTPUT_FREE(signature_external, signature);
3173
3174 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003175}
3176
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003177psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003178 const psa_key_attributes_t *attributes,
3179 const uint8_t *key_buffer, size_t key_buffer_size,
3180 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003182{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003183 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3185 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003186#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3188 return mbedtls_psa_rsa_verify_hash(
3189 attributes,
3190 key_buffer, key_buffer_size,
3191 alg, hash, hash_length,
3192 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003193#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3194 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 } else {
3196 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003197 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003198 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003200#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3202 return mbedtls_psa_ecdsa_verify_hash(
3203 attributes,
3204 key_buffer, key_buffer_size,
3205 alg, hash, hash_length,
3206 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003207#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3208 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 } else {
3210 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003211 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003212 }
Ronald Cron4501c982021-03-19 20:09:32 +01003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 (void) key_buffer;
3215 (void) key_buffer_size;
3216 (void) hash;
3217 (void) hash_length;
3218 (void) signature;
3219 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003222}
3223
Gilles Peskine449bd832023-01-11 14:50:10 +01003224psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3225 psa_algorithm_t alg,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003226 const uint8_t *hash_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 size_t hash_length,
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003228 const uint8_t *signature_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003230{
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3232 LOCAL_INPUT_DECLARE(hash_external, hash);
3233 LOCAL_INPUT_DECLARE(signature_external, signature);
3234
3235 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3236 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3237 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3238 signature_length);
3239
Thomas Daubney3e65f522024-01-30 12:37:25 +00003240#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003241exit:
Thomas Daubney3e65f522024-01-30 12:37:25 +00003242#endif
Thomas Daubney4f8847b2024-01-18 17:23:02 +00003243 LOCAL_INPUT_FREE(hash_external, hash);
3244 LOCAL_INPUT_FREE(signature_external, signature);
3245
3246 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003247}
3248
Gilles Peskine449bd832023-01-11 14:50:10 +01003249psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3250 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003251 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003253 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003255 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 size_t output_size,
3257 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003261 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003263 LOCAL_INPUT_DECLARE(input_external, input);
3264 LOCAL_INPUT_DECLARE(salt_external, salt);
3265 LOCAL_OUTPUT_DECLARE(output_external, output);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266
Darryl Green5cc689a2018-07-24 15:34:10 +01003267 (void) input;
3268 (void) input_length;
3269 (void) salt;
3270 (void) output;
3271 (void) output_size;
3272
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003273 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3276 return PSA_ERROR_INVALID_ARGUMENT;
3277 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003278
Valerio Setti5bb454a2024-01-15 10:43:16 +01003279 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3281 if (status != PSA_SUCCESS) {
3282 return status;
3283 }
3284 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3285 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003286 status = PSA_ERROR_INVALID_ARGUMENT;
3287 goto exit;
3288 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003290 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3291 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3292 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003293
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003294 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003295 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003296 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003298exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003299 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003300
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003301 LOCAL_INPUT_FREE(input_external, input);
3302 LOCAL_INPUT_FREE(salt_external, salt);
3303 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003306}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3309 psa_algorithm_t alg,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003310 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 size_t input_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003312 const uint8_t *salt_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 size_t salt_length,
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003314 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 size_t output_size,
3316 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003318 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003319 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003320 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003321
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003322 LOCAL_INPUT_DECLARE(input_external, input);
3323 LOCAL_INPUT_DECLARE(salt_external, salt);
3324 LOCAL_OUTPUT_DECLARE(output_external, output);
3325
Darryl Green5cc689a2018-07-24 15:34:10 +01003326 (void) input;
3327 (void) input_length;
3328 (void) salt;
3329 (void) output;
3330 (void) output_size;
3331
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003332 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3335 return PSA_ERROR_INVALID_ARGUMENT;
3336 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003337
Valerio Setti5bb454a2024-01-15 10:43:16 +01003338 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3340 if (status != PSA_SUCCESS) {
3341 return status;
3342 }
3343 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003344 status = PSA_ERROR_INVALID_ARGUMENT;
3345 goto exit;
3346 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003348 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3349 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3350 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmann93fa4e12024-03-12 15:02:28 +00003351
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003352 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003353 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003354 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003356
3357exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003358 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003359
Thomas Daubney6adbb2a2024-01-18 18:10:32 +00003360 LOCAL_INPUT_FREE(input_external, input);
3361 LOCAL_INPUT_FREE(salt_external, salt);
3362 LOCAL_OUTPUT_FREE(output_external, output);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003365}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003366
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367/****************************************************************/
3368/* Asymmetric interruptible cryptography */
3369/****************************************************************/
3370
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003371static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3372
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373void psa_interruptible_set_max_ops(uint32_t max_ops)
3374{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003375 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003376}
3377
3378uint32_t psa_interruptible_get_max_ops(void)
3379{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003380 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003381}
3382
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383uint32_t psa_sign_hash_get_num_ops(
3384 const psa_sign_hash_interruptible_operation_t *operation)
3385{
Paul Elliott296ede92022-12-15 17:00:30 +00003386 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003387}
3388
3389uint32_t psa_verify_hash_get_num_ops(
3390 const psa_verify_hash_interruptible_operation_t *operation)
3391{
Paul Elliott296ede92022-12-15 17:00:30 +00003392 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393}
3394
Paul Elliott59ad9452022-12-18 15:09:02 +00003395static psa_status_t psa_sign_hash_abort_internal(
3396 psa_sign_hash_interruptible_operation_t *operation)
3397{
3398 if (operation->id == 0) {
3399 /* The object has (apparently) been initialized but it is not (yet)
3400 * in use. It's ok to call abort on such an object, and there's
3401 * nothing to do. */
3402 return PSA_SUCCESS;
3403 }
3404
3405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3406
3407 status = psa_driver_wrapper_sign_hash_abort(operation);
3408
3409 operation->id = 0;
3410
Paul Elliotte6145dc2023-02-07 12:51:21 +00003411 /* Do not clear either the error_occurred or num_ops elements here as they
3412 * only want to be cleared by the application calling abort, not by abort
3413 * being called at completion of an operation. */
3414
Paul Elliott59ad9452022-12-18 15:09:02 +00003415 return status;
3416}
3417
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418psa_status_t psa_sign_hash_start(
3419 psa_sign_hash_interruptible_operation_t *operation,
3420 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann4a523a62024-03-11 13:32:16 +00003421 const uint8_t *hash_external, size_t hash_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422{
3423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3424 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3425 psa_key_slot_t *slot;
3426
David Horstmann4a523a62024-03-11 13:32:16 +00003427 LOCAL_INPUT_DECLARE(hash_external, hash);
3428
Paul Elliottc9774412023-02-06 15:14:07 +00003429 /* Check that start has not been previously called, or operation has not
3430 * previously errored. */
3431 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432 return PSA_ERROR_BAD_STATE;
3433 }
3434
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435 status = psa_sign_verify_check_alg(0, alg);
3436 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003437 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438 return status;
3439 }
3440
3441 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3442 PSA_KEY_USAGE_SIGN_HASH,
3443 alg);
3444
3445 if (status != PSA_SUCCESS) {
3446 goto exit;
3447 }
3448
3449 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3450 status = PSA_ERROR_INVALID_ARGUMENT;
3451 goto exit;
3452 }
3453
David Horstmann4a523a62024-03-11 13:32:16 +00003454 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3455
Paul Elliott296ede92022-12-15 17:00:30 +00003456 /* Ensure ops count gets reset, in case of operation re-use. */
3457 operation->num_ops = 0;
3458
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003459 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460 slot->key.data,
3461 slot->key.bytes, alg,
3462 hash, hash_length);
3463exit:
3464
3465 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003466 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003467 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468 }
3469
Ryan Everettdcc03d52024-02-14 15:44:13 +00003470 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471
Paul Elliottc9774412023-02-06 15:14:07 +00003472 if (unlock_status != PSA_SUCCESS) {
3473 operation->error_occurred = 1;
3474 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475
David Horstmann4a523a62024-03-11 13:32:16 +00003476 LOCAL_INPUT_FREE(hash_external, hash);
3477
Paul Elliottc9774412023-02-06 15:14:07 +00003478 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003479}
3480
3481
3482psa_status_t psa_sign_hash_complete(
3483 psa_sign_hash_interruptible_operation_t *operation,
David Horstmann4a523a62024-03-11 13:32:16 +00003484 uint8_t *signature_external, size_t signature_size,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 size_t *signature_length)
3486{
3487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3488
David Horstmann4a523a62024-03-11 13:32:16 +00003489 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3490
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 *signature_length = 0;
3492
Paul Elliottc9774412023-02-06 15:14:07 +00003493 /* Check that start has been called first, and that operation has not
3494 * previously errored. */
3495 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496 status = PSA_ERROR_BAD_STATE;
3497 goto exit;
3498 }
3499
Paul Elliott096abc42023-02-03 18:33:23 +00003500 /* Immediately reject a zero-length signature buffer. This guarantees that
3501 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502 if (signature_size == 0) {
3503 status = PSA_ERROR_BUFFER_TOO_SMALL;
3504 goto exit;
3505 }
3506
David Horstmann4a523a62024-03-11 13:32:16 +00003507 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3508
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3510 signature_size,
3511 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512
Paul Elliott296ede92022-12-15 17:00:30 +00003513 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003514 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003515
Paul Elliottebe225c2023-02-10 14:32:53 +00003516exit:
3517
David Horstmannc5064c82024-03-11 17:02:03 +00003518 if (signature != NULL) {
3519 psa_wipe_tag_output_buffer(signature, status, signature_size,
3520 *signature_length);
3521 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522
Paul Elliott53bb3122023-02-10 14:22:22 +00003523 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003524 if (status != PSA_SUCCESS) {
3525 operation->error_occurred = 1;
3526 }
3527
Paul Elliott59ad9452022-12-18 15:09:02 +00003528 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003529 }
3530
David Horstmann4a523a62024-03-11 13:32:16 +00003531 LOCAL_OUTPUT_FREE(signature_external, signature);
3532
Paul Elliott9fe12f62022-11-30 19:16:02 +00003533 return status;
3534}
3535
3536psa_status_t psa_sign_hash_abort(
3537 psa_sign_hash_interruptible_operation_t *operation)
3538{
Paul Elliott59ad9452022-12-18 15:09:02 +00003539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3540
3541 status = psa_sign_hash_abort_internal(operation);
3542
3543 /* We clear the number of ops done here, so that it is not cleared when
3544 * the operation fails or succeeds, only on manual abort. */
3545 operation->num_ops = 0;
3546
Paul Elliottc9774412023-02-06 15:14:07 +00003547 /* Likewise, failure state. */
3548 operation->error_occurred = 0;
3549
Paul Elliott59ad9452022-12-18 15:09:02 +00003550 return status;
3551}
3552
3553static psa_status_t psa_verify_hash_abort_internal(
3554 psa_verify_hash_interruptible_operation_t *operation)
3555{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003556 if (operation->id == 0) {
3557 /* The object has (apparently) been initialized but it is not (yet)
3558 * in use. It's ok to call abort on such an object, and there's
3559 * nothing to do. */
3560 return PSA_SUCCESS;
3561 }
3562
Paul Elliott59ad9452022-12-18 15:09:02 +00003563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3564
3565 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003566
3567 operation->id = 0;
3568
Paul Elliotte6145dc2023-02-07 12:51:21 +00003569 /* Do not clear either the error_occurred or num_ops elements here as they
3570 * only want to be cleared by the application calling abort, not by abort
3571 * being called at completion of an operation. */
3572
Paul Elliott59ad9452022-12-18 15:09:02 +00003573 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003574}
3575
3576psa_status_t psa_verify_hash_start(
3577 psa_verify_hash_interruptible_operation_t *operation,
3578 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
David Horstmann0fea6a52024-03-11 13:41:05 +00003579 const uint8_t *hash_external, size_t hash_length,
3580 const uint8_t *signature_external, size_t signature_length)
Paul Elliott9fe12f62022-11-30 19:16:02 +00003581{
3582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3583 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3584 psa_key_slot_t *slot;
3585
David Horstmann0fea6a52024-03-11 13:41:05 +00003586 LOCAL_INPUT_DECLARE(hash_external, hash);
3587 LOCAL_INPUT_DECLARE(signature_external, signature);
3588
Paul Elliottc9774412023-02-06 15:14:07 +00003589 /* Check that start has not been previously called, or operation has not
3590 * previously errored. */
3591 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003592 return PSA_ERROR_BAD_STATE;
3593 }
3594
3595 status = psa_sign_verify_check_alg(0, alg);
3596 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003597 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598 return status;
3599 }
3600
3601 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3602 PSA_KEY_USAGE_VERIFY_HASH,
3603 alg);
3604
3605 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003606 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003607 return status;
3608 }
3609
David Horstmann0fea6a52024-03-11 13:41:05 +00003610 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3611 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3612
Paul Elliott296ede92022-12-15 17:00:30 +00003613 /* Ensure ops count gets reset, in case of operation re-use. */
3614 operation->num_ops = 0;
3615
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003616 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003617 slot->key.data,
3618 slot->key.bytes,
3619 alg, hash, hash_length,
3620 signature, signature_length);
David Horstmann0fea6a52024-03-11 13:41:05 +00003621#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
3622exit:
3623#endif
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624
3625 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003626 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003627 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003628 }
3629
Ryan Everett291267f2024-02-14 15:59:15 +00003630 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631
Paul Elliottc9774412023-02-06 15:14:07 +00003632 if (unlock_status != PSA_SUCCESS) {
3633 operation->error_occurred = 1;
3634 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003635
David Horstmann0fea6a52024-03-11 13:41:05 +00003636 LOCAL_INPUT_FREE(hash_external, hash);
3637 LOCAL_INPUT_FREE(signature_external, signature);
3638
Paul Elliottc9774412023-02-06 15:14:07 +00003639 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003640}
3641
3642psa_status_t psa_verify_hash_complete(
3643 psa_verify_hash_interruptible_operation_t *operation)
3644{
3645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3646
Paul Elliottc9774412023-02-06 15:14:07 +00003647 /* Check that start has been called first, and that operation has not
3648 * previously errored. */
3649 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003650 status = PSA_ERROR_BAD_STATE;
3651 goto exit;
3652 }
3653
3654 status = psa_driver_wrapper_verify_hash_complete(operation);
3655
Paul Elliott296ede92022-12-15 17:00:30 +00003656 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003657 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003658 operation);
3659
Paul Elliottebe225c2023-02-10 14:32:53 +00003660exit:
3661
Paul Elliott9fe12f62022-11-30 19:16:02 +00003662 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003663 if (status != PSA_SUCCESS) {
3664 operation->error_occurred = 1;
3665 }
3666
Paul Elliott59ad9452022-12-18 15:09:02 +00003667 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003668 }
3669
3670 return status;
3671}
3672
3673psa_status_t psa_verify_hash_abort(
3674 psa_verify_hash_interruptible_operation_t *operation)
3675{
Paul Elliott59ad9452022-12-18 15:09:02 +00003676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003677
Paul Elliott59ad9452022-12-18 15:09:02 +00003678 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003679
Paul Elliott59ad9452022-12-18 15:09:02 +00003680 /* We clear the number of ops done here, so that it is not cleared when
3681 * the operation fails or succeeds, only on manual abort. */
3682 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003683
Paul Elliottc9774412023-02-06 15:14:07 +00003684 /* Likewise, failure state. */
3685 operation->error_occurred = 0;
3686
Paul Elliott59ad9452022-12-18 15:09:02 +00003687 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003688}
3689
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690/****************************************************************/
3691/* Asymmetric interruptible cryptography internal */
3692/* implementations */
3693/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003694
Paul Elliott588f8ed2022-12-02 18:10:26 +00003695void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3696{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003697
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3699 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3700 defined(MBEDTLS_ECP_RESTARTABLE)
3701
3702 /* Internal implementation uses zero to indicate infinite number max ops,
3703 * therefore avoid this value, and set to minimum possible. */
3704 if (max_ops == 0) {
3705 max_ops = 1;
3706 }
3707
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003709#else
3710 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3712 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3713 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3714}
3715
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003717 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718{
3719#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3720 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3721 defined(MBEDTLS_ECP_RESTARTABLE)
3722
Paul Elliott93d9ca82023-02-15 18:14:21 +00003723 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724#else
3725 (void) operation;
3726 return 0;
3727#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3728 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3729 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3730}
3731
3732uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003733 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734{
3735 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3736 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3737 defined(MBEDTLS_ECP_RESTARTABLE)
3738
Paul Elliott93d9ca82023-02-15 18:14:21 +00003739 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740#else
3741 (void) operation;
3742 return 0;
3743#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3744 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3745 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3746}
3747
3748psa_status_t mbedtls_psa_sign_hash_start(
3749 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3750 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3751 size_t key_buffer_size, psa_algorithm_t alg,
3752 const uint8_t *hash, size_t hash_length)
3753{
3754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003755 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003757 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003758 return PSA_ERROR_NOT_SUPPORTED;
3759 }
3760
3761 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003762 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003763 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764
3765#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003766 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3767 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768
Paul Elliotta1c94092023-02-15 16:38:04 +00003769 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3770
Paul Elliott93d9ca82023-02-15 18:14:21 +00003771 /* Ensure num_ops is zero'ed in case of context re-use. */
3772 operation->num_ops = 0;
3773
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003774 status = mbedtls_psa_ecp_load_representation(attributes->type,
3775 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003776 key_buffer,
3777 key_buffer_size,
3778 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003779
Paul Elliott068fe072023-01-16 13:59:15 +00003780 if (status != PSA_SUCCESS) {
3781 return status;
3782 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003783
Paul Elliott1bc59df2023-02-05 13:41:57 +00003784 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003785 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786
Paul Elliott068fe072023-01-16 13:59:15 +00003787 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003788 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003789 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott0290a762023-02-09 14:30:24 +00003791 /* We only need to store the same length of hash as the private key size
3792 * here, it would be truncated by the internal implementation anyway. */
3793 required_hash_length = (hash_length < operation->coordinate_bytes ?
3794 hash_length : operation->coordinate_bytes);
3795
Paul Elliottba70ad42023-02-15 18:23:53 +00003796 if (required_hash_length > sizeof(operation->hash)) {
3797 /* Shouldn't happen, but better safe than sorry. */
3798 return PSA_ERROR_CORRUPTION_DETECTED;
3799 }
3800
Paul Elliott0290a762023-02-09 14:30:24 +00003801 memcpy(operation->hash, hash, required_hash_length);
3802 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003803
3804 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805
3806#else
Paul Elliott068fe072023-01-16 13:59:15 +00003807 (void) operation;
3808 (void) key_buffer;
3809 (void) key_buffer_size;
3810 (void) alg;
3811 (void) hash;
3812 (void) hash_length;
3813 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003814 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815
Paul Elliott068fe072023-01-16 13:59:15 +00003816 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3818 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3819 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003820}
3821
3822psa_status_t mbedtls_psa_sign_hash_complete(
3823 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3824 uint8_t *signature, size_t signature_size,
3825 size_t *signature_length)
3826{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3828 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3829 defined(MBEDTLS_ECP_RESTARTABLE)
3830
Paul Elliotta1c94092023-02-15 16:38:04 +00003831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003832 mbedtls_mpi r;
3833 mbedtls_mpi s;
3834
Paul Elliott46845252023-02-03 14:59:11 +00003835 mbedtls_mpi_init(&r);
3836 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003838 /* Ensure max_ops is set to the current value (or default). */
3839 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3840
Paul Elliotta1c94092023-02-15 16:38:04 +00003841 if (signature_size < 2 * operation->coordinate_bytes) {
3842 status = PSA_ERROR_BUFFER_TOO_SMALL;
3843 goto exit;
3844 }
3845
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003847
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3849 status = mbedtls_to_psa_error(
3850 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003851 &r,
3852 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 &operation->ctx->d,
3854 operation->hash,
3855 operation->hash_length,
3856 operation->md_alg,
3857 mbedtls_psa_get_random,
3858 MBEDTLS_PSA_RANDOM_STATE,
3859 &operation->restart_ctx));
3860#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003861 status = PSA_ERROR_NOT_SUPPORTED;
3862 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3864 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865 status = mbedtls_to_psa_error(
3866 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003867 &r,
3868 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869 &operation->ctx->d,
3870 operation->hash,
3871 operation->hash_length,
3872 mbedtls_psa_get_random,
3873 MBEDTLS_PSA_RANDOM_STATE,
3874 mbedtls_psa_get_random,
3875 MBEDTLS_PSA_RANDOM_STATE,
3876 &operation->restart_ctx));
3877 }
3878
Paul Elliottf8e5b562023-02-19 18:43:45 +00003879 /* Hide the fact that the restart context only holds a delta of number of
3880 * ops done during the last operation, not an absolute value. */
3881 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3882
Paul Elliott724bd252023-02-08 12:35:08 +00003883 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003885 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003887 operation->coordinate_bytes)
3888 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889
3890 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003891 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892 }
3893
3894 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003895 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003897 operation->coordinate_bytes,
3898 operation->coordinate_bytes)
3899 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900
3901 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003902 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903 }
3904
Paul Elliott1bc59df2023-02-05 13:41:57 +00003905 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906
Paul Elliott724bd252023-02-08 12:35:08 +00003907 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908 }
Paul Elliott724bd252023-02-08 12:35:08 +00003909
3910exit:
3911
3912 mbedtls_mpi_free(&r);
3913 mbedtls_mpi_free(&s);
3914 return status;
3915
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916 #else
3917
3918 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919 (void) signature;
3920 (void) signature_size;
3921 (void) signature_length;
3922
3923 return PSA_ERROR_NOT_SUPPORTED;
3924
3925#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3926 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3927 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3928}
3929
3930psa_status_t mbedtls_psa_sign_hash_abort(
3931 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3932{
3933
3934#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3935 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3936 defined(MBEDTLS_ECP_RESTARTABLE)
3937
3938 if (operation->ctx) {
3939 mbedtls_ecdsa_free(operation->ctx);
3940 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003941 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942 }
3943
3944 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3945
Paul Elliott93d9ca82023-02-15 18:14:21 +00003946 operation->num_ops = 0;
3947
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948 return PSA_SUCCESS;
3949
3950#else
3951
3952 (void) operation;
3953
3954 return PSA_ERROR_NOT_SUPPORTED;
3955
3956#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3957 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3958 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3959}
3960
3961psa_status_t mbedtls_psa_verify_hash_start(
3962 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3963 const psa_key_attributes_t *attributes,
3964 const uint8_t *key_buffer, size_t key_buffer_size,
3965 psa_algorithm_t alg,
3966 const uint8_t *hash, size_t hash_length,
3967 const uint8_t *signature, size_t signature_length)
3968{
3969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003970 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003971 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003973 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003974 return PSA_ERROR_NOT_SUPPORTED;
3975 }
3976
3977 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003978 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003979 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980
3981#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003982 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3983 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003984
Paul Elliotta1c94092023-02-15 16:38:04 +00003985 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3986 mbedtls_mpi_init(&operation->r);
3987 mbedtls_mpi_init(&operation->s);
3988
Paul Elliott93d9ca82023-02-15 18:14:21 +00003989 /* Ensure num_ops is zero'ed in case of context re-use. */
3990 operation->num_ops = 0;
3991
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003992 status = mbedtls_psa_ecp_load_representation(attributes->type,
3993 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003994 key_buffer,
3995 key_buffer_size,
3996 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997
Paul Elliott068fe072023-01-16 13:59:15 +00003998 if (status != PSA_SUCCESS) {
3999 return status;
4000 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001
Paul Elliottc569fc22023-02-10 13:02:54 +00004002 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004003
Paul Elliott1bc59df2023-02-05 13:41:57 +00004004 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004005 return PSA_ERROR_INVALID_SIGNATURE;
4006 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007
Paul Elliott068fe072023-01-16 13:59:15 +00004008 status = mbedtls_to_psa_error(
4009 mbedtls_mpi_read_binary(&operation->r,
4010 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004011 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004012
Paul Elliott068fe072023-01-16 13:59:15 +00004013 if (status != PSA_SUCCESS) {
4014 return status;
4015 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004016
Paul Elliott068fe072023-01-16 13:59:15 +00004017 status = mbedtls_to_psa_error(
4018 mbedtls_mpi_read_binary(&operation->s,
4019 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004020 coordinate_bytes,
4021 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022
Paul Elliott068fe072023-01-16 13:59:15 +00004023 if (status != PSA_SUCCESS) {
4024 return status;
4025 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004026
Paul Elliott2c9843f2023-02-15 17:32:42 +00004027 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028
Paul Elliott2c9843f2023-02-15 17:32:42 +00004029 if (status != PSA_SUCCESS) {
4030 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004031 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004032
Paul Elliott0290a762023-02-09 14:30:24 +00004033 /* We only need to store the same length of hash as the private key size
4034 * here, it would be truncated by the internal implementation anyway. */
4035 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4036 coordinate_bytes);
4037
Paul Elliottba70ad42023-02-15 18:23:53 +00004038 if (required_hash_length > sizeof(operation->hash)) {
4039 /* Shouldn't happen, but better safe than sorry. */
4040 return PSA_ERROR_CORRUPTION_DETECTED;
4041 }
4042
Paul Elliott0290a762023-02-09 14:30:24 +00004043 memcpy(operation->hash, hash, required_hash_length);
4044 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004045
4046 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004047#else
Paul Elliott068fe072023-01-16 13:59:15 +00004048 (void) operation;
4049 (void) key_buffer;
4050 (void) key_buffer_size;
4051 (void) alg;
4052 (void) hash;
4053 (void) hash_length;
4054 (void) signature;
4055 (void) signature_length;
4056 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004057 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004058 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004059
Paul Elliott068fe072023-01-16 13:59:15 +00004060 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004061#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4062 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4063 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004064}
4065
4066psa_status_t mbedtls_psa_verify_hash_complete(
4067 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4068{
4069
4070#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4071 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4072 defined(MBEDTLS_ECP_RESTARTABLE)
4073
Paul Elliottf8e5b562023-02-19 18:43:45 +00004074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4075
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004076 /* Ensure max_ops is set to the current value (or default). */
4077 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4078
Paul Elliottf8e5b562023-02-19 18:43:45 +00004079 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004080 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4081 operation->hash,
4082 operation->hash_length,
4083 &operation->ctx->Q,
4084 &operation->r,
4085 &operation->s,
4086 &operation->restart_ctx));
4087
Paul Elliottf8e5b562023-02-19 18:43:45 +00004088 /* Hide the fact that the restart context only holds a delta of number of
4089 * ops done during the last operation, not an absolute value. */
4090 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4091
4092 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004093#else
4094 (void) operation;
4095
4096 return PSA_ERROR_NOT_SUPPORTED;
4097
4098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4099 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4100 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4101}
4102
4103psa_status_t mbedtls_psa_verify_hash_abort(
4104 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4105{
4106
4107#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4108 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4109 defined(MBEDTLS_ECP_RESTARTABLE)
4110
4111 if (operation->ctx) {
4112 mbedtls_ecdsa_free(operation->ctx);
4113 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004114 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004115 }
4116
4117 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4118
Paul Elliott93d9ca82023-02-15 18:14:21 +00004119 operation->num_ops = 0;
4120
Paul Elliott588f8ed2022-12-02 18:10:26 +00004121 mbedtls_mpi_free(&operation->r);
4122 mbedtls_mpi_free(&operation->s);
4123
4124 return PSA_SUCCESS;
4125
4126#else
4127 (void) operation;
4128
4129 return PSA_ERROR_NOT_SUPPORTED;
4130
4131#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4132 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4133 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4134}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004135
David Horstmann6e99bb22024-02-06 15:27:49 +00004136static psa_status_t psa_generate_random_internal(uint8_t *output,
4137 size_t output_size)
4138{
4139 GUARD_MODULE_INITIALIZED;
4140
David Horstmann6e99bb22024-02-06 15:27:49 +00004141#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4142
David Horstmanndb909142024-03-12 16:07:08 +00004143 psa_status_t status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004144 size_t output_length = 0;
4145 status = mbedtls_psa_external_get_random(&global_data.rng,
4146 output, output_size,
4147 &output_length);
4148 if (status != PSA_SUCCESS) {
David Horstmanndb909142024-03-12 16:07:08 +00004149 return status;
David Horstmann6e99bb22024-02-06 15:27:49 +00004150 }
4151 /* Breaking up a request into smaller chunks is currently not supported
4152 * for the external RNG interface. */
4153 if (output_length != output_size) {
David Horstmanndb909142024-03-12 16:07:08 +00004154 return PSA_ERROR_INSUFFICIENT_ENTROPY;
David Horstmann6e99bb22024-02-06 15:27:49 +00004155 }
David Horstmanndb909142024-03-12 16:07:08 +00004156 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004157
4158#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4159
4160 while (output_size > 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004161 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
David Horstmann6e99bb22024-02-06 15:27:49 +00004162 size_t request_size =
4163 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4164 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4165 output_size);
David Horstmann93fa4e12024-03-12 15:02:28 +00004166#if defined(MBEDTLS_CTR_DRBG_C)
4167 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
4168#elif defined(MBEDTLS_HMAC_DRBG_C)
4169 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
4170#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
David Horstmann6e99bb22024-02-06 15:27:49 +00004171 if (ret != 0) {
David Horstmann93fa4e12024-03-12 15:02:28 +00004172 return mbedtls_to_psa_error(ret);
David Horstmann6e99bb22024-02-06 15:27:49 +00004173 }
4174 output_size -= request_size;
4175 output += request_size;
4176 }
David Horstmann93fa4e12024-03-12 15:02:28 +00004177 return PSA_SUCCESS;
David Horstmann6e99bb22024-02-06 15:27:49 +00004178#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
David Horstmann6e99bb22024-02-06 15:27:49 +00004179}
4180
4181
mohammad1603503973b2018-03-12 15:59:30 +02004182/****************************************************************/
4183/* Symmetric cryptography */
4184/****************************************************************/
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4187 mbedtls_svc_key_id_t key,
4188 psa_algorithm_t alg,
4189 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004190{
4191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004193 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4195 PSA_KEY_USAGE_ENCRYPT :
4196 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02004197
4198 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004200 status = PSA_ERROR_BAD_STATE;
4201 goto exit;
4202 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004205 status = PSA_ERROR_INVALID_ARGUMENT;
4206 goto exit;
4207 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4210 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004213
Ronald Cron49fafa92021-03-10 08:34:23 +01004214 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004215 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004216 * so we only set it (in the driver wrapper) after resources have been
4217 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004218 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004220 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004222 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 }
4224 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004225
4226 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (cipher_operation == MBEDTLS_ENCRYPT) {
4228 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004229 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 slot->key.data,
4231 slot->key.bytes,
4232 alg);
4233 } else {
4234 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004235 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 slot->key.data,
4237 slot->key.bytes,
4238 alg);
4239 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004240
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (status != PSA_SUCCESS) {
4243 psa_cipher_abort(operation);
4244 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004245
Ryan Everettc0053cc2024-02-14 16:27:13 +00004246 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004249}
4250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4252 mbedtls_svc_key_id_t key,
4253 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004254{
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004256}
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4259 mbedtls_svc_key_id_t key,
4260 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004261{
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004263}
4264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004266 uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 size_t iv_size,
4268 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004269{
Ronald Cron6d051732020-10-01 14:10:20 +02004270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergeybef1f632023-03-06 15:25:06 -07004271 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004272
Gabor Mezei358eb212024-02-07 18:10:13 +01004273 LOCAL_OUTPUT_DECLARE(iv_external, iv);
4274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004276 status = PSA_ERROR_BAD_STATE;
4277 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004278 }
4279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004281 status = PSA_ERROR_BAD_STATE;
4282 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004283 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004284
Ronald Cron2fb90522021-07-05 12:31:44 +02004285 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004287 status = PSA_ERROR_BUFFER_TOO_SMALL;
4288 goto exit;
4289 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004292 status = PSA_ERROR_GENERIC_ERROR;
4293 goto exit;
4294 }
4295
Gabor Mezei358eb212024-02-07 18:10:13 +01004296 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
4297
Gabor Mezei1b5b58d2024-03-04 17:15:08 +01004298 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004300 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 }
Ronald Cron5618a392021-03-26 09:52:26 +01004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei358eb212024-02-07 18:10:13 +01004304 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004305
4306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 if (status == PSA_SUCCESS) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004308 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004309 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004311 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 psa_cipher_abort(operation);
Gabor Mezei358eb212024-02-07 18:10:13 +01004313 if (iv != NULL) {
4314 mbedtls_platform_zeroize(iv, default_iv_length);
4315 }
Ronald Cron2fb90522021-07-05 12:31:44 +02004316 }
Ronald Cron6d051732020-10-01 14:10:20 +02004317
Gabor Mezei358eb212024-02-07 18:10:13 +01004318 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004320}
4321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004323 const uint8_t *iv_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004325{
Ronald Cron6d051732020-10-01 14:10:20 +02004326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004327
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004328 LOCAL_INPUT_DECLARE(iv_external, iv);
4329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004331 status = PSA_ERROR_BAD_STATE;
4332 goto exit;
4333 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004336 status = PSA_ERROR_BAD_STATE;
4337 goto exit;
4338 }
Ronald Crona0d68172021-03-26 10:15:08 +01004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004341 status = PSA_ERROR_INVALID_ARGUMENT;
4342 goto exit;
4343 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004344
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004345 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
4346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 status = psa_driver_wrapper_cipher_set_iv(operation,
4348 iv,
4349 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004350
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004353 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 } else {
4355 psa_cipher_abort(operation);
4356 }
Gabor Mezei7abf8ee2024-02-01 10:39:26 +01004357
4358 LOCAL_INPUT_FREE(iv_external, iv);
4359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004361}
4362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004364 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004366 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 size_t output_size,
4368 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004369{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004371
Gabor Mezei212eb082024-01-24 16:56:00 +01004372 LOCAL_INPUT_DECLARE(input_external, input);
4373 LOCAL_OUTPUT_DECLARE(output_external, output);
4374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004376 status = PSA_ERROR_BAD_STATE;
4377 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004378 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004381 status = PSA_ERROR_BAD_STATE;
4382 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004383 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004384
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004385 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezei0b041162024-02-29 10:08:16 +00004386 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 status = psa_driver_wrapper_cipher_update(operation,
4389 input,
4390 input_length,
4391 output,
4392 output_size,
4393 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004394
4395exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (status != PSA_SUCCESS) {
4397 psa_cipher_abort(operation);
4398 }
Ronald Cron6d051732020-10-01 14:10:20 +02004399
Gabor Mezei212eb082024-01-24 16:56:00 +01004400 LOCAL_INPUT_FREE(input_external, input);
4401 LOCAL_OUTPUT_FREE(output_external, output);
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004404}
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei212eb082024-01-24 16:56:00 +01004407 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 size_t output_size,
4409 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004410{
David Saadab4ecc272019-02-14 13:48:10 +02004411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004412
Gabor Mezei212eb082024-01-24 16:56:00 +01004413 LOCAL_OUTPUT_DECLARE(output_external, output);
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004416 status = PSA_ERROR_BAD_STATE;
4417 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004418 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004421 status = PSA_ERROR_BAD_STATE;
4422 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004423 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004424
Gabor Mezei0b041162024-02-29 10:08:16 +00004425 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 status = psa_driver_wrapper_cipher_finish(operation,
4428 output,
4429 output_size,
4430 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004431
4432exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (status == PSA_SUCCESS) {
Gabor Mezei212eb082024-01-24 16:56:00 +01004434 status = psa_cipher_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004436 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02004438 }
Gabor Mezei212eb082024-01-24 16:56:00 +01004439
4440 LOCAL_OUTPUT_FREE(output_external, output);
4441
4442 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004443}
4444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004446{
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004448 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004449 * in use. It's ok to call abort on such an object, and there's
4450 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004452 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004455
Ronald Cron49fafa92021-03-10 08:34:23 +01004456 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004457 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004458 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004461}
4462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4464 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004465 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004467 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 size_t output_size,
4469 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004470{
4471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004472 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004473 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004474 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4475 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004476
David Horstmann62a56d92023-12-14 18:12:47 +00004477 LOCAL_INPUT_DECLARE(input_external, input);
4478 LOCAL_OUTPUT_DECLARE(output_external, output);
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004481 status = PSA_ERROR_INVALID_ARGUMENT;
4482 goto exit;
4483 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4486 PSA_KEY_USAGE_ENCRYPT,
4487 alg);
4488 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4493 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004494 status = PSA_ERROR_GENERIC_ERROR;
4495 goto exit;
4496 }
4497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (default_iv_length > 0) {
4499 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004500 status = PSA_ERROR_BUFFER_TOO_SMALL;
4501 goto exit;
4502 }
4503
David Horstmann6e99bb22024-02-06 15:27:49 +00004504 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004506 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004508 }
4509
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004510 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4511 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4512
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004513 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004514 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004515 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004516 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004518
4519exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004520 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004522 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004523 }
Ronald Cron23919522021-07-08 19:00:07 +02004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (status == PSA_SUCCESS) {
4526 if (default_iv_length > 0) {
4527 memcpy(output, local_iv, default_iv_length);
4528 }
4529 *output_length += default_iv_length;
4530 } else {
4531 *output_length = 0;
4532 }
4533
David Horstmann62a56d92023-12-14 18:12:47 +00004534 LOCAL_INPUT_FREE(input_external, input);
4535 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004538}
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4541 psa_algorithm_t alg,
Gabor Mezei212eb082024-01-24 16:56:00 +01004542 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 size_t input_length,
Gabor Mezei212eb082024-01-24 16:56:00 +01004544 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 size_t output_size,
4546 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004547{
4548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004549 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004550 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004551
Gabor Mezei212eb082024-01-24 16:56:00 +01004552 LOCAL_INPUT_DECLARE(input_external, input);
4553 LOCAL_OUTPUT_DECLARE(output_external, output);
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004556 status = PSA_ERROR_INVALID_ARGUMENT;
4557 goto exit;
4558 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4561 PSA_KEY_USAGE_DECRYPT,
4562 alg);
4563 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004564 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4568 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004569 status = PSA_ERROR_INVALID_ARGUMENT;
4570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004572 status = PSA_ERROR_INVALID_ARGUMENT;
4573 goto exit;
4574 }
4575
Gabor Mezei8b8e4852024-01-31 17:45:29 +01004576 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4577 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4578
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004579 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004580 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004581 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004583
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004584exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004585 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004587 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004591 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 }
Ronald Cron23919522021-07-08 19:00:07 +02004593
Gabor Mezei212eb082024-01-24 16:56:00 +01004594 LOCAL_INPUT_FREE(input_external, input);
4595 LOCAL_OUTPUT_FREE(output_external, output);
4596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004598}
4599
4600
mohammad16035955c982018-04-26 00:53:03 +03004601/****************************************************************/
4602/* AEAD */
4603/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004604
Paul Elliottbaff51c2021-09-28 17:44:45 +01004605/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004606static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004607{
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004609}
4610
4611/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004612static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4613 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004614{
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004618#if defined(PSA_WANT_ALG_GCM)
4619 case PSA_ALG_GCM:
4620 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 * arbitrarily large nonces. Please note that we do not generally
4622 * recommend the usage of nonces of greater length than
4623 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4624 * size, which can then lead to collisions if you encrypt a very
4625 * large number of messages.*/
4626 if (nonce_length != 0) {
4627 return PSA_SUCCESS;
4628 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004629 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004630#endif /* PSA_WANT_ALG_GCM */
4631#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004632 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 if (nonce_length >= 7 && nonce_length <= 13) {
4634 return PSA_SUCCESS;
4635 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004636 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004637#endif /* PSA_WANT_ALG_CCM */
4638#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004639 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if (nonce_length == 12) {
4641 return PSA_SUCCESS;
4642 } else if (nonce_length == 8) {
4643 return PSA_ERROR_NOT_SUPPORTED;
4644 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004645 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004646#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004647 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004648 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004650 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004653}
4654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004656{
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4658 return PSA_ERROR_INVALID_ARGUMENT;
4659 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004662}
4663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4665 psa_algorithm_t alg,
David Horstmann9d09a022023-11-28 19:25:00 +00004666 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 size_t nonce_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004668 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 size_t additional_data_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004670 const uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 size_t plaintext_length,
David Horstmann9d09a022023-11-28 19:25:00 +00004672 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 size_t ciphertext_size,
4674 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004675{
Ronald Cron215633c2021-03-16 17:15:37 +01004676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4677 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004678
David Horstmann9d09a022023-11-28 19:25:00 +00004679 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4680 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4681 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4682 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4683
mohammad1603f08a5502018-06-03 15:05:47 +03004684 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 status = psa_aead_check_algorithm(alg);
4687 if (status != PSA_SUCCESS) {
4688 return status;
4689 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004690
Ronald Cron9a986162021-03-26 12:40:07 +01004691 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4693 if (status != PSA_SUCCESS) {
4694 return status;
4695 }
mohammad16035955c982018-04-26 00:53:03 +03004696
David Horstmann9d09a022023-11-28 19:25:00 +00004697 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4698 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4699 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4700 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 status = psa_aead_check_nonce_length(alg, nonce_length);
4703 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004706
Ronald Cronde822812021-03-17 16:08:20 +01004707 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004708 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004709 alg,
4710 nonce, nonce_length,
4711 additional_data, additional_data_length,
4712 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4716 memset(ciphertext, 0, ciphertext_size);
4717 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004718
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004719exit:
David Horstmann9d09a022023-11-28 19:25:00 +00004720 LOCAL_INPUT_FREE(nonce_external, nonce);
4721 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4722 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4723 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4724
Ryan Everetta103ec92024-01-31 13:59:57 +00004725 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004728}
4729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4731 psa_algorithm_t alg,
David Horstmann7f2e0402023-11-28 19:43:53 +00004732 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 size_t nonce_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004734 const uint8_t *additional_data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 size_t additional_data_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004736 const uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 size_t ciphertext_length,
David Horstmann7f2e0402023-11-28 19:43:53 +00004738 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 size_t plaintext_size,
4740 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004741{
Ronald Cron215633c2021-03-16 17:15:37 +01004742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4743 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004744
David Horstmann7f2e0402023-11-28 19:43:53 +00004745 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4746 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4747 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4748 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4749
Gilles Peskineee652a32018-06-01 19:23:52 +02004750 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 status = psa_aead_check_algorithm(alg);
4753 if (status != PSA_SUCCESS) {
4754 return status;
4755 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004756
Ronald Cron9a986162021-03-26 12:40:07 +01004757 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4759 if (status != PSA_SUCCESS) {
4760 return status;
4761 }
mohammad16035955c982018-04-26 00:53:03 +03004762
David Horstmann7f2e0402023-11-28 19:43:53 +00004763 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4764 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4765 additional_data);
4766 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4767 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 status = psa_aead_check_nonce_length(alg, nonce_length);
4770 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004771 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004773
Ronald Cronde822812021-03-17 16:08:20 +01004774 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004775 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004776 alg,
4777 nonce, nonce_length,
4778 additional_data, additional_data_length,
4779 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (status != PSA_SUCCESS && plaintext_size != 0) {
4783 memset(plaintext, 0, plaintext_size);
4784 }
mohammad160360a64d02018-06-03 17:20:42 +03004785
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004786exit:
David Horstmann7f2e0402023-11-28 19:43:53 +00004787 LOCAL_INPUT_FREE(nonce_external, nonce);
4788 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4789 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4790 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4791
Ryan Everetta103ec92024-01-31 13:59:57 +00004792 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 return status;
mohammad16035955c982018-04-26 00:53:03 +03004795}
4796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4798{
4799 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004802#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004804 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4806 return PSA_ERROR_INVALID_ARGUMENT;
4807 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004808 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004809#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004810
Przemek Stekiel86679c72022-10-06 17:06:56 +02004811#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004813 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4815 return PSA_ERROR_INVALID_ARGUMENT;
4816 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004817 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004818#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004819
Przemek Stekiel86679c72022-10-06 17:06:56 +02004820#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004822 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (tag_len != 16) {
4824 return PSA_ERROR_INVALID_ARGUMENT;
4825 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004826 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004827#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004828
4829 default:
4830 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004834}
4835
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004836/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004837static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4838 int is_encrypt,
4839 mbedtls_svc_key_id_t key,
4840 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004841{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4843 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4844 psa_key_slot_t *slot = NULL;
4845 psa_key_usage_t key_usage = 0;
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_aead_check_algorithm(alg);
4848 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004849 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004853 status = PSA_ERROR_BAD_STATE;
4854 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004855 }
4856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (operation->nonce_set || operation->lengths_set ||
4858 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004859 status = PSA_ERROR_BAD_STATE;
4860 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004861 }
4862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004864 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004866 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4870 alg);
4871 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004872 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004876 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (is_encrypt) {
4880 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004881 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 slot->key.data,
4883 slot->key.bytes,
4884 alg);
4885 } else {
4886 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004887 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 slot->key.data,
4889 slot->key.bytes,
4890 alg);
4891 }
4892 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004893 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004895
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004896 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004897
4898exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004899 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004902 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004904 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 } else {
4906 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004907 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004908
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004910}
4911
Paul Elliott302ff6b2021-04-20 18:10:30 +01004912/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004913psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4914 mbedtls_svc_key_id_t key,
4915 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004916{
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918}
4919
4920/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004921psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4922 mbedtls_svc_key_id_t key,
4923 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004924{
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004926}
4927
David Horstmannfed23772023-12-19 17:49:20 +00004928static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
4929 const uint8_t *nonce,
4930 size_t nonce_length)
4931{
4932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4933
4934 if (operation->id == 0) {
4935 status = PSA_ERROR_BAD_STATE;
4936 goto exit;
4937 }
4938
4939 if (operation->nonce_set) {
4940 status = PSA_ERROR_BAD_STATE;
4941 goto exit;
4942 }
4943
4944 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4945 if (status != PSA_SUCCESS) {
4946 status = PSA_ERROR_INVALID_ARGUMENT;
4947 goto exit;
4948 }
4949
4950 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4951 nonce_length);
4952
4953exit:
4954 if (status == PSA_SUCCESS) {
4955 operation->nonce_set = 1;
4956 } else {
4957 psa_aead_abort(operation);
4958 }
4959
4960 return status;
4961}
4962
Paul Elliott302ff6b2021-04-20 18:10:30 +01004963/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004964psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
David Horstmannd3cad8b2023-12-11 14:46:04 +00004965 uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 size_t nonce_size,
4967 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004968{
4969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004970 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004971 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004972
David Horstmannd3cad8b2023-12-11 14:46:04 +00004973 LOCAL_OUTPUT_DECLARE(nonce_external, nonce);
4974 LOCAL_OUTPUT_ALLOC(nonce_external, nonce_size, nonce);
4975
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004976 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004979 status = PSA_ERROR_BAD_STATE;
4980 goto exit;
4981 }
4982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004984 status = PSA_ERROR_BAD_STATE;
4985 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986 }
4987
Paul Elliotte193ea82021-10-01 13:00:16 +01004988 /* For CCM, this size may not be correct according to the PSA
4989 * specification. The PSA Crypto 1.0.1 specification states:
4990 *
4991 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4992 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4993 *
4994 * However this restriction that L has to be the smallest integer is not
4995 * applied in practice, and it is not implementable here since the
4996 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4998 operation->alg);
4999 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000 status = PSA_ERROR_BUFFER_TOO_SMALL;
5001 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005002 }
5003
David Horstmann6e99bb22024-02-06 15:27:49 +00005004 status = psa_generate_random_internal(local_nonce, required_nonce_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008
David Horstmannfed23772023-12-19 17:49:20 +00005009 status = psa_aead_set_nonce_internal(operation, local_nonce,
5010 required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011
Paul Elliott39dc6b82021-05-11 19:16:09 +01005012exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (status == PSA_SUCCESS) {
5014 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005015 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 } else {
5017 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01005018 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005019
David Horstmannd3cad8b2023-12-11 14:46:04 +00005020 LOCAL_OUTPUT_FREE(nonce_external, nonce);
5021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005023}
5024
5025/* Set the nonce for a multipart authenticated encryption or decryption
5026 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005027psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
David Horstmann8f0ef512023-12-11 15:17:11 +00005028 const uint8_t *nonce_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030{
David Horstmannfed23772023-12-19 17:49:20 +00005031 psa_status_t status;
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005032
David Horstmann8f0ef512023-12-11 15:17:11 +00005033 LOCAL_INPUT_DECLARE(nonce_external, nonce);
5034 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
Paul Elliottcee785c2021-05-20 14:29:20 +01005035
David Horstmannfed23772023-12-19 17:49:20 +00005036 status = psa_aead_set_nonce_internal(operation, nonce, nonce_length);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005037
David Horstmann18dc0322023-12-20 16:16:43 +00005038/* Exit label is only needed for buffer copying, prevent unused warnings. */
5039#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Paul Elliott39dc6b82021-05-11 19:16:09 +01005040exit:
David Horstmann18dc0322023-12-20 16:16:43 +00005041#endif
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005042
David Horstmann8f0ef512023-12-11 15:17:11 +00005043 LOCAL_INPUT_FREE(nonce_external, nonce);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005046}
5047
5048/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005049psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
5050 size_t ad_length,
5051 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005052{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005056 status = PSA_ERROR_BAD_STATE;
5057 goto exit;
5058 }
5059
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (operation->lengths_set || operation->ad_started ||
5061 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005062 status = PSA_ERROR_BAD_STATE;
5063 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005064 }
5065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005067#if defined(PSA_WANT_ALG_GCM)
5068 case PSA_ALG_GCM:
5069 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 * bits. Without the guard this code will generate warnings on 32bit
5071 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005072#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (((uint64_t) ad_length) >> 61 != 0 ||
5074 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005075 status = PSA_ERROR_INVALID_ARGUMENT;
5076 goto exit;
5077 }
Paul Elliott325d3742021-09-27 17:56:28 +01005078#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005079 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005080#endif /* PSA_WANT_ALG_GCM */
5081#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005082 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005084 status = PSA_ERROR_INVALID_ARGUMENT;
5085 goto exit;
5086 }
5087 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005088#endif /* PSA_WANT_ALG_CCM */
5089#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005090 case PSA_ALG_CHACHA20_POLY1305:
5091 /* No length restrictions for ChaChaPoly. */
5092 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005093#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005094 default:
5095 break;
5096 }
Paul Elliott325d3742021-09-27 17:56:28 +01005097
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5099 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005100
Paul Elliott39dc6b82021-05-11 19:16:09 +01005101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005103 operation->ad_remaining = ad_length;
5104 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005105 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 } else {
5107 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005108 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005111}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005112
5113/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
David Horstmann25dac6e2023-12-11 15:23:13 +00005115 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005117{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5119
David Horstmann25dac6e2023-12-11 15:23:13 +00005120 LOCAL_INPUT_DECLARE(input_external, input);
5121 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005124 status = PSA_ERROR_BAD_STATE;
5125 goto exit;
5126 }
5127
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005129 status = PSA_ERROR_BAD_STATE;
5130 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005131 }
5132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (operation->lengths_set) {
5134 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005135 status = PSA_ERROR_INVALID_ARGUMENT;
5136 goto exit;
5137 }
5138
5139 operation->ad_remaining -= input_length;
5140 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005141#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005143 status = PSA_ERROR_BAD_STATE;
5144 goto exit;
5145 }
5146#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 status = psa_driver_wrapper_aead_update_ad(operation, input,
5149 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005150
Paul Elliott39dc6b82021-05-11 19:16:09 +01005151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005153 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 } else {
5155 psa_aead_abort(operation);
5156 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005157
David Horstmann25dac6e2023-12-11 15:23:13 +00005158 LOCAL_INPUT_FREE(input_external, input);
5159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005161}
5162
5163/* Encrypt or decrypt a message fragment in an active multipart AEAD
5164 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005165psa_status_t psa_aead_update(psa_aead_operation_t *operation,
David Horstmann2914fac2023-12-11 15:28:37 +00005166 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 size_t input_length,
David Horstmann2914fac2023-12-11 15:28:37 +00005168 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 size_t output_size,
5170 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005171{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005173
David Horstmann2914fac2023-12-11 15:28:37 +00005174
5175 LOCAL_INPUT_DECLARE(input_external, input);
5176 LOCAL_OUTPUT_DECLARE(output_external, output);
5177
5178 LOCAL_INPUT_ALLOC(input_external, input_length, input);
5179 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
5180
Paul Elliott302ff6b2021-04-20 18:10:30 +01005181 *output_length = 0;
5182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005184 status = PSA_ERROR_BAD_STATE;
5185 goto exit;
5186 }
5187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005189 status = PSA_ERROR_BAD_STATE;
5190 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005191 }
5192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005194 /* Additional data length was supplied, but not all the additional
5195 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005197 status = PSA_ERROR_INVALID_ARGUMENT;
5198 goto exit;
5199 }
5200
5201 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005203 status = PSA_ERROR_INVALID_ARGUMENT;
5204 goto exit;
5205 }
5206
5207 operation->body_remaining -= input_length;
5208 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005209#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005211 status = PSA_ERROR_BAD_STATE;
5212 goto exit;
5213 }
5214#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5217 output, output_size,
5218 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005219
Paul Elliott39dc6b82021-05-11 19:16:09 +01005220exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005222 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 } else {
5224 psa_aead_abort(operation);
5225 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005226
David Horstmann2914fac2023-12-11 15:28:37 +00005227 LOCAL_INPUT_FREE(input_external, input);
5228 LOCAL_OUTPUT_FREE(output_external, output);
5229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005231}
5232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005234{
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 if (operation->id == 0 || !operation->nonce_set) {
5236 return PSA_ERROR_BAD_STATE;
5237 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5240 operation->body_remaining != 0)) {
5241 return PSA_ERROR_INVALID_ARGUMENT;
5242 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005245}
5246
Paul Elliott302ff6b2021-04-20 18:10:30 +01005247/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
David Horstmann6db0e732023-12-11 15:35:59 +00005249 uint8_t *ciphertext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 size_t ciphertext_size,
5251 size_t *ciphertext_length,
David Horstmann6db0e732023-12-11 15:35:59 +00005252 uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 size_t tag_size,
5254 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005255{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5257
David Horstmann6db0e732023-12-11 15:35:59 +00005258 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
5259 LOCAL_OUTPUT_DECLARE(tag_external, tag);
5260
5261 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
5262 LOCAL_OUTPUT_ALLOC(tag_external, tag_size, tag);
5263
Paul Elliott302ff6b2021-04-20 18:10:30 +01005264 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005265 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 status = psa_aead_final_checks(operation);
5268 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005273 status = PSA_ERROR_BAD_STATE;
5274 goto exit;
5275 }
5276
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5278 ciphertext_size,
5279 ciphertext_length,
5280 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005281
Paul Elliott39dc6b82021-05-11 19:16:09 +01005282exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005283
5284
Paul Elliottf47b0952021-05-21 18:02:33 +01005285 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005286 * the zero tag size, make sure the tag is set to something implausible.
5287 * Even if the operation succeeds, make sure we clear the rest of the
5288 * buffer to prevent potential leakage of anything previously placed in
5289 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005290 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005293
David Horstmann6db0e732023-12-11 15:35:59 +00005294 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
5295 LOCAL_OUTPUT_FREE(tag_external, tag);
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005298}
5299
5300/* Finish authenticating and decrypting a message in a multipart AEAD
5301 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005302psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
David Horstmanne000a0a2023-12-11 16:37:04 +00005303 uint8_t *plaintext_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 size_t plaintext_size,
5305 size_t *plaintext_length,
David Horstmanne000a0a2023-12-11 16:37:04 +00005306 const uint8_t *tag_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005308{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5310
David Horstmanne000a0a2023-12-11 16:37:04 +00005311 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
5312 LOCAL_INPUT_DECLARE(tag_external, tag);
5313
5314 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
5315 LOCAL_INPUT_ALLOC(tag_external, tag_length, tag);
5316
Paul Elliott302ff6b2021-04-20 18:10:30 +01005317 *plaintext_length = 0;
5318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 status = psa_aead_final_checks(operation);
5320 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005321 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005325 status = PSA_ERROR_BAD_STATE;
5326 goto exit;
5327 }
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5330 plaintext_size,
5331 plaintext_length,
5332 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005333
5334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005336
David Horstmanne000a0a2023-12-11 16:37:04 +00005337 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
5338 LOCAL_INPUT_FREE(tag_external, tag);
5339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005341}
5342
5343/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005345{
5346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005349 /* The object has (apparently) been initialized but it is not (yet)
5350 * in use. It's ok to call abort on such an object, and there's
5351 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005353 }
5354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005360}
5361
Gilles Peskinee59236f2018-01-27 23:32:46 +01005362/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005363/* Generators */
5364/****************************************************************/
5365
Przemek Stekiel69c46792022-06-10 12:59:51 +02005366#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005367 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005368 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305369 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305370 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005371#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005372#endif /* At least one builtin KDF */
5373
Przemek Stekiel69c46792022-06-10 12:59:51 +02005374#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005375 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5376 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5377static psa_status_t psa_key_derivation_start_hmac(
5378 psa_mac_operation_t *operation,
5379 psa_algorithm_t hash_alg,
5380 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005382{
5383 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5386 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005388
Steven Cooreman72f736a2021-05-07 14:14:37 +02005389 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 status = psa_driver_wrapper_mac_sign_setup(operation,
5393 &attributes,
5394 hmac_key, hmac_key_length,
5395 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 psa_reset_key_attributes(&attributes);
5398 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005399}
5400#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005401
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005402#define HKDF_STATE_INIT 0 /* no input yet */
5403#define HKDF_STATE_STARTED 1 /* got salt */
5404#define HKDF_STATE_KEYED 2 /* got key */
5405#define HKDF_STATE_OUTPUT 3 /* output started */
5406
Gilles Peskinecbe66502019-05-16 16:59:18 +02005407static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005409{
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5411 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5412 } else {
5413 return operation->alg;
5414 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005415}
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005418{
5419 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5421 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005422 /* The object has (apparently) been initialized but it is not
5423 * in use. It's ok to call abort on such an object, and there's
5424 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005426#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5428 mbedtls_free(operation->ctx.hkdf.info);
5429 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5430 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005431#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005432#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5433 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5435 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5436 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5437 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005438 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005440 }
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005443 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005445 }
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005448 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005450 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005451#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005453 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005455 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005456#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005457 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005458
5459 /* We leave the fields Ai and output_block to be erased safely by the
5460 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005462#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5463 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005464#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5466 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5467 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5468 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005469#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305470#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305471 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305472 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005473 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305474 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305475 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305476
5477 status = PSA_SUCCESS;
5478 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305479#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005480 {
5481 status = PSA_ERROR_BAD_STATE;
5482 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 mbedtls_platform_zeroize(operation, sizeof(*operation));
5484 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005485}
5486
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005489{
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005493 }
5494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005497}
5498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5500 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501{
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (operation->alg == 0) {
5503 return PSA_ERROR_BAD_STATE;
5504 }
5505 if (capacity > operation->capacity) {
5506 return PSA_ERROR_INVALID_ARGUMENT;
5507 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005510}
5511
Przemek Stekiel69c46792022-06-10 12:59:51 +02005512#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005513/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005514static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5515 psa_algorithm_t kdf_alg,
5516 uint8_t *output,
5517 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005518{
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5520 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005521 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005522 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005523#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005525#else
5526 const uint8_t last_block = 0xff;
5527#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (hkdf->state < HKDF_STATE_KEYED ||
5530 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005531#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005533#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 )) {
5535 return PSA_ERROR_BAD_STATE;
5536 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005537 hkdf->state = HKDF_STATE_OUTPUT;
5538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005540 /* Copy what remains of the current block */
5541 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005543 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 }
5545 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005546 output += n;
5547 output_length -= n;
5548 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005550 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005552 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005553 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005554 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005555 * object was corrupted or if this function is called directly
5556 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (hkdf->block_number == last_block) {
5558 return PSA_ERROR_BAD_STATE;
5559 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005560
5561 /* We need a new block */
5562 ++hkdf->block_number;
5563 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5566 hash_alg,
5567 hkdf->prk,
5568 hash_length);
5569 if (status != PSA_SUCCESS) {
5570 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005571 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005572
5573 if (hkdf->block_number != 1) {
5574 status = psa_mac_update(&hkdf->hmac,
5575 hkdf->output_block,
5576 hash_length);
5577 if (status != PSA_SUCCESS) {
5578 return status;
5579 }
5580 }
5581 status = psa_mac_update(&hkdf->hmac,
5582 hkdf->info,
5583 hkdf->info_length);
5584 if (status != PSA_SUCCESS) {
5585 return status;
5586 }
5587 status = psa_mac_update(&hkdf->hmac,
5588 &hkdf->block_number, 1);
5589 if (status != PSA_SUCCESS) {
5590 return status;
5591 }
5592 status = psa_mac_sign_finish(&hkdf->hmac,
5593 hkdf->output_block,
5594 sizeof(hkdf->output_block),
5595 &hmac_output_length);
5596 if (status != PSA_SUCCESS) {
5597 return status;
5598 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005599 }
5600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005602}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005603#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005604
John Durkop07cc04a2020-11-16 22:08:34 -08005605#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5606 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005607static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5608 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005610{
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5612 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005613 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5614 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005615 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005616
Janos Follath7742fee2019-06-17 12:58:10 +01005617 /* We can't be wanting more output after block 0xff, otherwise
5618 * the capacity check in psa_key_derivation_output_bytes() would have
5619 * prevented this call. It could happen only if the operation
5620 * object was corrupted or if this function is called directly
5621 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 if (tls12_prf->block_number == 0xff) {
5623 return PSA_ERROR_CORRUPTION_DETECTED;
5624 }
Janos Follath7742fee2019-06-17 12:58:10 +01005625
5626 /* We need a new block */
5627 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005628 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005629
5630 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5631 *
5632 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5633 *
5634 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5635 * HMAC_hash(secret, A(2) + seed) +
5636 * HMAC_hash(secret, A(3) + seed) + ...
5637 *
5638 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005639 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005640 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005641 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005642 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005643 * is currently extracted as `output_block` and where i is
5644 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005645 */
5646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 status = psa_key_derivation_start_hmac(&hmac,
5648 hash_alg,
5649 tls12_prf->secret,
5650 tls12_prf->secret_length);
5651 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005652 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005654
5655 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005657 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5658 * the variable seed and in this instance means it in the context of the
5659 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 status = psa_mac_update(&hmac,
5661 tls12_prf->label,
5662 tls12_prf->label_length);
5663 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005664 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 }
5666 status = psa_mac_update(&hmac,
5667 tls12_prf->seed,
5668 tls12_prf->seed_length);
5669 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005670 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 }
5672 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005673 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5675 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005676 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005678 }
5679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 status = psa_mac_sign_finish(&hmac,
5681 tls12_prf->Ai, hash_length,
5682 &hmac_output_length);
5683 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005684 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 }
5686 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005687 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005689
5690 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 status = psa_key_derivation_start_hmac(&hmac,
5692 hash_alg,
5693 tls12_prf->secret,
5694 tls12_prf->secret_length);
5695 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005696 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 }
5698 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5699 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005700 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 }
5702 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5703 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005704 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 }
5706 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5707 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005708 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 }
5710 status = psa_mac_sign_finish(&hmac,
5711 tls12_prf->output_block, hash_length,
5712 &hmac_output_length);
5713 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005714 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005716
Janos Follath7742fee2019-06-17 12:58:10 +01005717
5718cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 cleanup_status = psa_mac_abort(&hmac);
5720 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005721 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005725}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005726
Janos Follath844eb0e2019-06-19 12:10:49 +01005727static psa_status_t psa_key_derivation_tls12_prf_read(
5728 psa_tls12_prf_key_derivation_t *tls12_prf,
5729 psa_algorithm_t alg,
5730 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005732{
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5734 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005735 psa_status_t status;
5736 uint8_t offset, length;
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005739 case PSA_TLS12_PRF_STATE_LABEL_SET:
5740 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5741 break;
5742 case PSA_TLS12_PRF_STATE_OUTPUT:
5743 break;
5744 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005746 }
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005749 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 if (tls12_prf->left_in_block == 0) {
5751 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5752 alg);
5753 if (status != PSA_SUCCESS) {
5754 return status;
5755 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005756
5757 continue;
5758 }
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005761 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005763 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005765
5766 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005768 output += length;
5769 output_length -= length;
5770 tls12_prf->left_in_block -= length;
5771 }
5772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005774}
John Durkop07cc04a2020-11-16 22:08:34 -08005775#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5776 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005777
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005778#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5779static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5780 psa_tls12_ecjpake_to_pms_t *ecjpake,
5781 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005783{
5784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005785 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 if (output_length != 32) {
5788 return PSA_ERROR_INVALID_ARGUMENT;
5789 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5792 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5793 &output_size);
5794 if (status != PSA_SUCCESS) {
5795 return status;
5796 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 if (output_size != output_length) {
5799 return PSA_ERROR_GENERIC_ERROR;
5800 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005803}
5804#endif
5805
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305806#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305807static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5808 psa_pbkdf2_key_derivation_t *pbkdf2,
5809 psa_algorithm_t prf_alg,
5810 uint8_t prf_output_length,
5811 psa_key_attributes_t *attributes)
5812{
5813 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305814 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305815 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305816 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305817 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305818 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305819 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305820
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305821 mac_operation.is_sign = 1;
5822 mac_operation.mac_size = prf_output_length;
5823 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305824
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305825 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5826 attributes,
5827 pbkdf2->password,
5828 pbkdf2->password_length,
5829 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305830 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305831 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305832 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305833 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5834 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305835 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305836 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305837 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305838 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305839 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305840 }
5841 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5842 &mac_output_length);
5843 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305844 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305845 }
5846
5847 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305848 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305849 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305850 }
5851
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305852 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305853
5854 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305855 /* We are passing prf_output_length as mac_size because the driver
5856 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305857 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305858 status = psa_driver_wrapper_mac_compute(attributes,
5859 pbkdf2->password,
5860 pbkdf2->password_length,
5861 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305862 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305863 &mac_output_length);
5864 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305865 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305866 }
5867
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305868 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305869 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305870
5871cleanup:
5872 /* Zeroise buffers to clear sensitive data from memory. */
5873 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5874 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305875}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305876
5877static psa_status_t psa_key_derivation_pbkdf2_read(
5878 psa_pbkdf2_key_derivation_t *pbkdf2,
5879 psa_algorithm_t kdf_alg,
5880 uint8_t *output,
5881 size_t output_length)
5882{
5883 psa_status_t status;
5884 psa_algorithm_t prf_alg;
5885 uint8_t prf_output_length;
5886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5887 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5888 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5889
5890 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5891 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5892 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5893 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305894 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5895 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305896 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305897 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305898 } else {
5899 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305900 }
5901
5902 switch (pbkdf2->state) {
5903 case PSA_PBKDF2_STATE_PASSWORD_SET:
5904 /* Initially we need a new block so bytes_used is equal to block size*/
5905 pbkdf2->bytes_used = prf_output_length;
5906 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5907 break;
5908 case PSA_PBKDF2_STATE_OUTPUT:
5909 break;
5910 default:
5911 return PSA_ERROR_BAD_STATE;
5912 }
5913
5914 while (output_length != 0) {
5915 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5916 if (n > output_length) {
5917 n = (uint8_t) output_length;
5918 }
5919 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5920 output += n;
5921 output_length -= n;
5922 pbkdf2->bytes_used += n;
5923
5924 if (output_length == 0) {
5925 break;
5926 }
5927
5928 /* We need a new block */
5929 pbkdf2->bytes_used = 0;
5930 pbkdf2->block_number++;
5931
5932 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5933 prf_output_length,
5934 &attributes);
5935 if (status != PSA_SUCCESS) {
5936 return status;
5937 }
5938 }
5939
5940 return PSA_SUCCESS;
5941}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305942#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305943
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005944psa_status_t psa_key_derivation_output_bytes(
5945 psa_key_derivation_operation_t *operation,
Ryan Everettf943e222024-01-19 14:46:39 +00005946 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005948{
5949 psa_status_t status;
Ryan Everettf943e222024-01-19 14:46:39 +00005950 LOCAL_OUTPUT_DECLARE(output_external, output);
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005955 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005957 }
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005960 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005961 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005962 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5963 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005964 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005965 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005967 }
Ryan Everettf943e222024-01-19 14:46:39 +00005968
5969 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
Ryan Everettda9227d2024-01-25 11:37:22 +00005970 if (output_length > operation->capacity) {
5971 operation->capacity = 0;
5972 /* Go through the error path to wipe all confidential data now
5973 * that the operation object is useless. */
5974 status = PSA_ERROR_INSUFFICIENT_DATA;
5975 goto exit;
5976 }
5977
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005978 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005979
Przemek Stekiel69c46792022-06-10 12:59:51 +02005980#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5982 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5983 output, output_length);
5984 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005985#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005986#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5987 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5989 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5990 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5991 kdf_alg, output,
5992 output_length);
5993 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005994#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5995 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005996#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005998 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
6000 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006001#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306002#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306003 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05306004 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
6005 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05306006 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306007#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006008
Gilles Peskineeab56e42018-07-12 17:12:33 +02006009 {
Steven Cooreman74afe472021-02-10 17:19:22 +01006010 (void) kdf_alg;
Ryan Everettf943e222024-01-19 14:46:39 +00006011 status = PSA_ERROR_BAD_STATE;
6012 LOCAL_OUTPUT_FREE(output_external, output);
6013
6014 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006015 }
6016
6017exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006019 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006020 * This allows us to differentiate between exhausted operations and
6021 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
6022 * operations. */
6023 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006025 operation->alg = alg;
Ryan Everettee5920a2024-02-09 15:09:28 +00006026 if (output != NULL) {
6027 memset(output, '!', output_length);
6028 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006029 }
Ryan Everettda9227d2024-01-25 11:37:22 +00006030
6031 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006033}
6034
David Brown7807bf72021-02-09 16:28:23 -07006035#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006036static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02006037{
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 if (data_size >= 8) {
6039 mbedtls_des_key_set_parity(data);
6040 }
6041 if (data_size >= 16) {
6042 mbedtls_des_key_set_parity(data + 8);
6043 }
6044 if (data_size >= 24) {
6045 mbedtls_des_key_set_parity(data + 16);
6046 }
Gilles Peskine08542d82018-07-19 17:05:42 +02006047}
David Brown7807bf72021-02-09 16:28:23 -07006048#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02006049
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006050/*
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 * ECC keys on a Weierstrass elliptic curve require the generation
6052 * of a private key which is an integer
6053 * in the range [1, N - 1], where N is the boundary of the private key domain:
6054 * N is the prime p for Diffie-Hellman, or the order of the
6055 * curve’s base point for ECC.
6056 *
6057 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
6058 * This function generates the private key using the following process:
6059 *
6060 * 1. Draw a byte string of length ceiling(m/8) bytes.
6061 * 2. If m is not a multiple of 8, set the most significant
6062 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
6063 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
6064 * 4. If k > N - 2, discard the result and return to step 1.
6065 * 5. Output k + 1 as the private key.
6066 *
6067 * This method allows compliance to NIST standards, specifically the methods titled
6068 * Key-Pair Generation by Testing Candidates in the following publications:
6069 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
6070 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
6071 * Diffie-Hellman keys.
6072 *
6073 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
6074 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
6075 *
6076 * Note: Function allocates memory for *data buffer, so given *data should be
6077 * always NULL.
6078 */
Valerio Setti86587ab2023-06-27 13:09:30 +02006079#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
6080#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006081static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006082 psa_key_slot_t *slot,
6083 size_t bits,
6084 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006085 uint8_t **data
6086 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006087{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006088 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006089 mbedtls_mpi k;
6090 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006093 size_t m;
6094 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 mbedtls_mpi_init(&k);
6097 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006098
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006099 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006101 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01006102 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006105 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01006106 goto cleanup;
6107 }
6108
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006109 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006113
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006114 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006115 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006116 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006117
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006118 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006119
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006120 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006122
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006123 /* Note: This function is always called with *data == NULL and it
6124 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 *data = mbedtls_calloc(1, m_bytes);
6126 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006127 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006128 goto cleanup;
6129 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006132 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006134 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006136
6137 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006139 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006140 * (8 * ceiling(m/8) - m) bits of the first byte in
6141 * the string to zero.
6142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006144 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006145 }
6146
6147 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 * big-endian byte string.
6149 */
6150 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006151
6152 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 * Result of comparison is returned. When it indicates error
6154 * then this function is called again.
6155 */
6156 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006157 }
6158
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006159 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6161 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006162cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (ret != 0) {
6164 status = mbedtls_to_psa_error(ret);
6165 }
6166 if (status != PSA_SUCCESS) {
6167 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006168 *data = NULL;
6169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 mbedtls_mpi_free(&k);
6171 mbedtls_mpi_free(&diff_N_2);
6172 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006173}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006174
6175/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6176 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6177 *
6178 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6179 * draw a 32-byte string and process it as specified in
6180 * Elliptic Curves for Security [RFC7748] §5.
6181 *
6182 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6183 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6184 *
6185 * Note: Function allocates memory for *data buffer, so given *data should be
6186 * always NULL.
6187 */
6188
6189static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6190 size_t bits,
6191 psa_key_derivation_operation_t *operation,
6192 uint8_t **data
6193 )
6194{
6195 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006196 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006199 case 255:
6200 output_length = 32;
6201 break;
6202 case 448:
6203 output_length = 56;
6204 break;
6205 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006207 break;
6208 }
6209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 if (*data == NULL) {
6213 return PSA_ERROR_INSUFFICIENT_MEMORY;
6214 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006219 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006223 case 255:
6224 (*data)[0] &= 248;
6225 (*data)[31] &= 127;
6226 (*data)[31] |= 64;
6227 break;
6228 case 448:
6229 (*data)[0] &= 252;
6230 (*data)[55] |= 128;
6231 break;
6232 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006234 break;
6235 }
6236
6237 return status;
6238}
Valerio Setti86587ab2023-06-27 13:09:30 +02006239#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6240static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6241 psa_key_slot_t *slot, size_t bits,
6242 psa_key_derivation_operation_t *operation, uint8_t **data)
6243{
6244 (void) slot;
6245 (void) bits;
6246 (void) operation;
6247 (void) data;
6248 return PSA_ERROR_NOT_SUPPORTED;
6249}
6250
6251static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6252 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6253{
6254 (void) bits;
6255 (void) operation;
6256 (void) data;
6257 return PSA_ERROR_NOT_SUPPORTED;
6258}
6259#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6260#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006261
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006262static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006263 psa_key_slot_t *slot,
6264 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006266{
6267 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306269 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6273 return PSA_ERROR_INVALID_ARGUMENT;
6274 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006275
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006276#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006277 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6279 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6280 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006281 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6283 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006284 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 }
6286 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006287 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6289 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006292 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006293 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006294#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006295 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (key_type_is_raw_bytes(slot->attr.type)) {
6297 if (bits % 8 != 0) {
6298 return PSA_ERROR_INVALID_ARGUMENT;
6299 }
6300 data = mbedtls_calloc(1, bytes);
6301 if (data == NULL) {
6302 return PSA_ERROR_INSUFFICIENT_MEMORY;
6303 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 status = psa_key_derivation_output_bytes(operation, data, bytes);
6306 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006307 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 }
David Brown12ca5032021-02-11 11:02:00 -07006309#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6311 psa_des_set_key_parity(data, bytes);
6312 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006313#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 } else {
6315 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006316 }
Ronald Crondd04d422020-11-28 15:14:42 +01006317
Ronald Cronbf33c932020-11-28 18:06:53 +01006318 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006319
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006320 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006321 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 &storage_size);
6323 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 }
Archanad8a83dc2021-06-14 10:04:16 +05306326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 status = psa_allocate_buffer_to_slot(slot, storage_size);
6328 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306329 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 }
Archanad8a83dc2021-06-14 10:04:16 +05306331
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006332 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 data, bytes,
6334 slot->key.data,
6335 slot->key.bytes,
6336 &slot->key.bytes, &bits);
6337 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006338 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006340
6341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 mbedtls_free(data);
6343 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006344}
6345
Gilles Peskine092ce512024-02-20 12:31:24 +01006346static const psa_key_production_parameters_t default_production_parameters =
6347 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006348
Gilles Peskine092ce512024-02-20 12:31:24 +01006349int psa_key_production_parameters_are_default(
6350 const psa_key_production_parameters_t *params,
6351 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006352{
Gilles Peskine092ce512024-02-20 12:31:24 +01006353 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006354 return 0;
6355 }
Gilles Peskine092ce512024-02-20 12:31:24 +01006356 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006357 return 0;
6358 }
6359 return 1;
6360}
6361
6362psa_status_t psa_key_derivation_output_key_ext(
6363 const psa_key_attributes_t *attributes,
6364 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006365 const psa_key_production_parameters_t *params,
6366 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006367 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006368{
6369 psa_status_t status;
6370 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006371 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006372
Ronald Cron81709fc2020-11-14 12:10:32 +01006373 *key = MBEDTLS_SVC_KEY_ID_INIT;
6374
Gilles Peskine0f84d622019-09-12 19:03:13 +02006375 /* Reject any attempt to create a zero-length key so that we don't
6376 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (psa_get_key_bits(attributes) == 0) {
6378 return PSA_ERROR_INVALID_ARGUMENT;
6379 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006380
Gilles Peskine092ce512024-02-20 12:31:24 +01006381 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006382 return PSA_ERROR_INVALID_ARGUMENT;
6383 }
6384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (operation->alg == PSA_ALG_NONE) {
6386 return PSA_ERROR_BAD_STATE;
6387 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (!operation->can_output_key) {
6390 return PSA_ERROR_NOT_PERMITTED;
6391 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6394 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006395#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006397 /* Deriving a key in a secure element is not implemented yet. */
6398 status = PSA_ERROR_NOT_SUPPORTED;
6399 }
6400#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (status == PSA_SUCCESS) {
6402 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01006403 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (status == PSA_SUCCESS) {
6407 status = psa_finish_key_creation(slot, driver, key);
6408 }
6409 if (status != PSA_SUCCESS) {
6410 psa_fail_key_creation(slot, driver);
6411 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006414}
6415
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006416psa_status_t psa_key_derivation_output_key(
6417 const psa_key_attributes_t *attributes,
6418 psa_key_derivation_operation_t *operation,
6419 mbedtls_svc_key_id_t *key)
6420{
Gilles Peskinec81393b2024-02-14 20:51:28 +01006421 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01006422 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01006423 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01006424}
Gilles Peskineeab56e42018-07-12 17:12:33 +02006425
6426
6427/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006428/* Key derivation */
6429/****************************************************************/
6430
Steven Cooreman932ffb72021-02-15 12:14:32 +01006431#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006432static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006433{
6434#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6436 return 1;
6437 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006438#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006439#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6441 return 1;
6442 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006443#endif
6444#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6446 return 1;
6447 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006448#endif
6449#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6451 return 1;
6452 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006453#endif
6454#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6456 return 1;
6457 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006458#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006459#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6461 return 1;
6462 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006463#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306464#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6465 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6466 return 1;
6467 }
6468#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306469#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6470 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6471 return 1;
6472 }
6473#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006475}
6476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006478{
6479 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_status_t status = psa_hash_setup(&operation, alg);
6481 psa_hash_abort(&operation);
6482 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006483}
6484
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306485static psa_status_t psa_key_derivation_set_maximum_capacity(
6486 psa_key_derivation_operation_t *operation,
6487 psa_algorithm_t kdf_alg)
6488{
6489#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6490 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6491 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6492 return PSA_SUCCESS;
6493 }
6494#endif
6495#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6496 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6497#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306498 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306499 PSA_KEY_TYPE_AES,
6500 128U,
6501 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306502#else
6503 operation->capacity = SIZE_MAX;
6504#endif
6505 return PSA_SUCCESS;
6506 }
6507#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6508
6509 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6510 * invalid or meaningless but it does not affect this function */
6511 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6512 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306513 if (hash_size == 0) {
6514 return PSA_ERROR_NOT_SUPPORTED;
6515 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306516
6517 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6518 * we might fail later, which is somewhat unfriendly and potentially
6519 * risk-prone. */
6520 psa_status_t status = psa_hash_try_support(hash_alg);
6521 if (status != PSA_SUCCESS) {
6522 return status;
6523 }
6524
6525#if defined(PSA_WANT_ALG_HKDF)
6526 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6527 operation->capacity = 255 * hash_size;
6528 } else
6529#endif
6530#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6531 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6532 operation->capacity = hash_size;
6533 } else
6534#endif
6535#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6536 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6537 operation->capacity = 255 * hash_size;
6538 } else
6539#endif
6540#if defined(PSA_WANT_ALG_TLS12_PRF)
6541 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6542 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6543 operation->capacity = SIZE_MAX;
6544 } else
6545#endif
6546#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6547 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6548 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6549 /* Master Secret is always 48 bytes
6550 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6551 operation->capacity = 48U;
6552 } else
6553#endif
6554#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6555 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6556#if (SIZE_MAX > UINT32_MAX)
6557 operation->capacity = UINT32_MAX * hash_size;
6558#else
6559 operation->capacity = SIZE_MAX;
6560#endif
6561 } else
6562#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6563 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306564 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306565 status = PSA_ERROR_NOT_SUPPORTED;
6566 }
6567 return status;
6568}
6569
Gilles Peskine969c5d62019-01-16 15:53:06 +01006570static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006571 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006573{
Janos Follath5fe19732019-06-20 15:09:30 +01006574 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6575 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006577
Gilles Peskine969c5d62019-01-16 15:53:06 +01006578 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 if (!is_kdf_alg_supported(kdf_alg)) {
6580 return PSA_ERROR_NOT_SUPPORTED;
6581 }
John Durkop07cc04a2020-11-16 22:08:34 -08006582
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306583 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6584 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306585 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006586}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006589{
6590#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 if (alg == PSA_ALG_ECDH) {
6592 return PSA_SUCCESS;
6593 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006594#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006595#if defined(PSA_WANT_ALG_FFDH)
6596 if (alg == PSA_ALG_FFDH) {
6597 return PSA_SUCCESS;
6598 }
6599#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006600 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006602}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006603
6604static int psa_key_derivation_allows_free_form_secret_input(
6605 psa_algorithm_t kdf_alg)
6606{
6607#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6608 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6609 return 0;
6610 }
6611#endif
6612 (void) kdf_alg;
6613 return 1;
6614}
John Durkop07cc04a2020-11-16 22:08:34 -08006615#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6618 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006619{
6620 psa_status_t status;
6621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 if (operation->alg != 0) {
6623 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006624 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6627 return PSA_ERROR_INVALID_ARGUMENT;
6628 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6629#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6630 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6631 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6632 status = psa_key_agreement_try_support(ka_alg);
6633 if (status != PSA_SUCCESS) {
6634 return status;
6635 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006636 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6637 return PSA_ERROR_INVALID_ARGUMENT;
6638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6640#else
6641 return PSA_ERROR_NOT_SUPPORTED;
6642#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6643 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6644#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6645 status = psa_key_derivation_setup_kdf(operation, alg);
6646#else
6647 return PSA_ERROR_NOT_SUPPORTED;
6648#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6649 } else {
6650 return PSA_ERROR_INVALID_ARGUMENT;
6651 }
6652
6653 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006654 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 }
6656 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006657}
6658
Przemek Stekiel69c46792022-06-10 12:59:51 +02006659#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006660static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6661 psa_algorithm_t kdf_alg,
6662 psa_key_derivation_step_t step,
6663 const uint8_t *data,
6664 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006665{
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006667 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006669 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006670#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6672 return PSA_ERROR_INVALID_ARGUMENT;
6673 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006674#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 if (hkdf->state != HKDF_STATE_INIT) {
6676 return PSA_ERROR_BAD_STATE;
6677 } else {
6678 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6679 hash_alg,
6680 data, data_length);
6681 if (status != PSA_SUCCESS) {
6682 return status;
6683 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006684 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006686 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006687 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006688#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006690 /* We shouldn't be in different state as HKDF_EXPAND only allows
6691 * two inputs: SECRET (this case) and INFO which does not modify
6692 * the state. It could happen only if the hkdf
6693 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if (hkdf->state != HKDF_STATE_INIT) {
6695 return PSA_ERROR_BAD_STATE;
6696 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006697
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006698 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6700 return PSA_ERROR_INVALID_ARGUMENT;
6701 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 memcpy(hkdf->prk, data, data_length);
6704 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006705#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006706 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006707 /* HKDF: If no salt was provided, use an empty salt.
6708 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006710#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6712 return PSA_ERROR_BAD_STATE;
6713 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006714#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6716 hash_alg,
6717 NULL, 0);
6718 if (status != PSA_SUCCESS) {
6719 return status;
6720 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006721 hkdf->state = HKDF_STATE_STARTED;
6722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (hkdf->state != HKDF_STATE_STARTED) {
6724 return PSA_ERROR_BAD_STATE;
6725 }
6726 status = psa_mac_update(&hkdf->hmac,
6727 data, data_length);
6728 if (status != PSA_SUCCESS) {
6729 return status;
6730 }
6731 status = psa_mac_sign_finish(&hkdf->hmac,
6732 hkdf->prk,
6733 sizeof(hkdf->prk),
6734 &data_length);
6735 if (status != PSA_SUCCESS) {
6736 return status;
6737 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006738 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006739
Gilles Peskine2b522db2019-04-12 15:11:49 +02006740 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006741 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006742#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006744 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006746 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006748#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006749 {
6750 /* Block 0 is empty, and the next block will be
6751 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006753 }
6754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006756 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006757#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6759 return PSA_ERROR_INVALID_ARGUMENT;
6760 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006761#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006762#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6764 hkdf->state == HKDF_STATE_INIT) {
6765 return PSA_ERROR_BAD_STATE;
6766 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006767#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (hkdf->state == HKDF_STATE_OUTPUT) {
6769 return PSA_ERROR_BAD_STATE;
6770 }
6771 if (hkdf->info_set) {
6772 return PSA_ERROR_BAD_STATE;
6773 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006774 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (data_length != 0) {
6776 hkdf->info = mbedtls_calloc(1, data_length);
6777 if (hkdf->info == NULL) {
6778 return PSA_ERROR_INSUFFICIENT_MEMORY;
6779 }
6780 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006781 }
6782 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006784 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006786 }
6787}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006788#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006789
John Durkop07cc04a2020-11-16 22:08:34 -08006790#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6791 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006792static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6793 const uint8_t *data,
6794 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006795{
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6797 return PSA_ERROR_BAD_STATE;
6798 }
Janos Follathf08e2652019-06-13 09:05:41 +01006799
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 if (data_length != 0) {
6801 prf->seed = mbedtls_calloc(1, data_length);
6802 if (prf->seed == NULL) {
6803 return PSA_ERROR_INSUFFICIENT_MEMORY;
6804 }
Janos Follathf08e2652019-06-13 09:05:41 +01006805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006807 prf->seed_length = data_length;
6808 }
Janos Follathf08e2652019-06-13 09:05:41 +01006809
Gilles Peskine43f958b2020-12-13 14:55:14 +01006810 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006813}
6814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6816 const uint8_t *data,
6817 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006818{
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6820 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6821 return PSA_ERROR_BAD_STATE;
6822 }
Janos Follath81550542019-06-13 14:26:34 +01006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if (data_length != 0) {
6825 prf->secret = mbedtls_calloc(1, data_length);
6826 if (prf->secret == NULL) {
6827 return PSA_ERROR_INSUFFICIENT_MEMORY;
6828 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006829
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006831 prf->secret_length = data_length;
6832 }
Janos Follath81550542019-06-13 14:26:34 +01006833
Gilles Peskine43f958b2020-12-13 14:55:14 +01006834 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006835
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006837}
6838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6840 const uint8_t *data,
6841 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006842{
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6844 return PSA_ERROR_BAD_STATE;
6845 }
Janos Follath63028dd2019-06-13 09:15:47 +01006846
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 if (data_length != 0) {
6848 prf->label = mbedtls_calloc(1, data_length);
6849 if (prf->label == NULL) {
6850 return PSA_ERROR_INSUFFICIENT_MEMORY;
6851 }
Janos Follath63028dd2019-06-13 09:15:47 +01006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006854 prf->label_length = data_length;
6855 }
Janos Follath63028dd2019-06-13 09:15:47 +01006856
Gilles Peskine43f958b2020-12-13 14:55:14 +01006857 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006860}
6861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6863 psa_key_derivation_step_t step,
6864 const uint8_t *data,
6865 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006866{
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006868 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006870 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006872 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006874 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006876 }
6877}
John Durkop07cc04a2020-11-16 22:08:34 -08006878#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6879 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6880
6881#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6882static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6883 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006884 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006886{
6887 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6889 4 + data_length + prf->other_secret_length :
6890 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6893 return PSA_ERROR_INVALID_ARGUMENT;
6894 }
John Durkop07cc04a2020-11-16 22:08:34 -08006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 uint8_t *pms = mbedtls_calloc(1, pms_len);
6897 if (pms == NULL) {
6898 return PSA_ERROR_INSUFFICIENT_MEMORY;
6899 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006900 uint8_t *cur = pms;
6901
6902 /* pure-PSK:
6903 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006904 *
6905 * The premaster secret is formed as follows: if the PSK is N octets
6906 * long, concatenate a uint16 with the value N, N zero octets, a second
6907 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006908 *
6909 * mixed-PSK:
6910 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6911 * follows: concatenate a uint16 with the length of the other secret,
6912 * the other secret itself, uint16 with the length of PSK, and the
6913 * PSK itself.
6914 * For details please check:
6915 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6916 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6917 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006918 */
6919
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6921 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6922 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6923 if (prf->other_secret_length != 0) {
6924 memcpy(cur, prf->other_secret, prf->other_secret_length);
6925 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006926 cur += prf->other_secret_length;
6927 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 } else {
6929 *cur++ = MBEDTLS_BYTE_1(data_length);
6930 *cur++ = MBEDTLS_BYTE_0(data_length);
6931 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006932 cur += data_length;
6933 }
6934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 *cur++ = MBEDTLS_BYTE_1(data_length);
6936 *cur++ = MBEDTLS_BYTE_0(data_length);
6937 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006938 cur += data_length;
6939
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006940 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006941
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006942 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006944}
Janos Follath6660f0e2019-06-17 08:44:03 +01006945
Przemek Stekiele47201b2022-04-19 13:53:28 +02006946static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6947 psa_tls12_prf_key_derivation_t *prf,
6948 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006950{
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6952 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006954
6955 if (data_length != 0) {
6956 prf->other_secret = mbedtls_calloc(1, data_length);
6957 if (prf->other_secret == NULL) {
6958 return PSA_ERROR_INSUFFICIENT_MEMORY;
6959 }
6960
6961 memcpy(prf->other_secret, data, data_length);
6962 prf->other_secret_length = data_length;
6963 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006964 prf->other_secret_length = 0;
6965 }
6966
6967 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006970}
6971
Janos Follath6660f0e2019-06-17 08:44:03 +01006972static psa_status_t psa_tls12_prf_psk_to_ms_input(
6973 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006974 psa_key_derivation_step_t step,
6975 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006977{
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006979 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 return psa_tls12_prf_psk_to_ms_set_key(prf,
6981 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006982 break;
6983 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6985 data,
6986 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006987 break;
6988 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006990 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006991
Przemek Stekiele47201b2022-04-19 13:53:28 +02006992 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006993}
John Durkop07cc04a2020-11-16 22:08:34 -08006994#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006995
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006996#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6997static psa_status_t psa_tls12_ecjpake_to_pms_input(
6998 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006999 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007000 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007002{
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
7004 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
7005 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04007006 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007007
7008 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (data[0] != 0x04) {
7010 return PSA_ERROR_INVALID_ARGUMENT;
7011 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007012
7013 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007015
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007017}
7018#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307019
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307020#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307021static psa_status_t psa_pbkdf2_set_input_cost(
7022 psa_pbkdf2_key_derivation_t *pbkdf2,
7023 psa_key_derivation_step_t step,
7024 uint64_t data)
7025{
7026 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
7027 return PSA_ERROR_INVALID_ARGUMENT;
7028 }
7029
7030 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
7031 return PSA_ERROR_BAD_STATE;
7032 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307033
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307034 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307035 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307036 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05307037
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307038 if (data == 0) {
7039 return PSA_ERROR_INVALID_ARGUMENT;
7040 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05307041
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307042 pbkdf2->input_cost = data;
7043 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
7044
7045 return PSA_SUCCESS;
7046}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307047
7048static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
7049 const uint8_t *data,
7050 size_t data_length)
7051{
Gilles Peskineead17662023-08-20 22:02:51 +02007052 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
7053 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
7054 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
7055 /* Appending to existing salt. No state change. */
7056 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307057 return PSA_ERROR_BAD_STATE;
7058 }
7059
Gilles Peskineca57d782023-07-20 19:08:06 +02007060 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02007061 /* Appending an empty string, nothing to do. */
7062 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307063 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05307064
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307065 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
7066 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307067 return PSA_ERROR_INSUFFICIENT_MEMORY;
7068 }
7069
Gilles Peskineead17662023-08-20 22:02:51 +02007070 if (pbkdf2->salt_length != 0) {
7071 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
7072 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307073 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05307074 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05307075 mbedtls_free(pbkdf2->salt);
7076 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307077 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05307078 return PSA_SUCCESS;
7079}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307080
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307081#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307082static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307083 const uint8_t *input,
7084 size_t input_len,
7085 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05307086 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307087{
7088 psa_status_t status = PSA_SUCCESS;
7089 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007090 return psa_hash_compute(hash_alg, input, input_len, output,
7091 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
7092 } else if (input_len > 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307093 memcpy(output, input, input_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307094 }
Ryan Everett5d2e82f2024-02-07 17:24:59 +00007095 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307096 return status;
7097}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307098#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307099
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307100#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307101static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
7102 size_t input_len,
7103 uint8_t *output,
7104 size_t *output_len)
7105{
7106 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307107 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05307109 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307110 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
7111 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
7112 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307113 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05307114 * mac_size as the driver function sets mac_output_length = mac_size
7115 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307116 status = psa_driver_wrapper_mac_compute(&attributes,
7117 zeros, sizeof(zeros),
7118 PSA_ALG_CMAC, input, input_len,
7119 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307120 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
7121 128U,
7122 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307123 output_len);
7124 } else {
7125 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05307126 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307127 }
7128 return status;
7129}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05307130#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307131
7132static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307133 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307134 const uint8_t *data,
7135 size_t data_length)
7136{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05307137 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307138 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
7139 return PSA_ERROR_BAD_STATE;
7140 }
7141
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307142#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307143 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
7144 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
7145 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
7146 pbkdf2->password,
7147 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307148 } else
7149#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
7150#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
7151 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05307152 status = psa_pbkdf2_cmac_set_password(data, data_length,
7153 pbkdf2->password,
7154 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05307155 } else
7156#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
7157 {
7158 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307159 }
7160
7161 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
7162
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307163 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307164}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307165
7166static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307167 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307168 psa_key_derivation_step_t step,
7169 const uint8_t *data,
7170 size_t data_length)
7171{
7172 switch (step) {
7173 case PSA_KEY_DERIVATION_INPUT_SALT:
7174 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
7175 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307176 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307177 default:
7178 return PSA_ERROR_INVALID_ARGUMENT;
7179 }
7180}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307181#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307182
Gilles Peskineb8965192019-09-24 16:21:10 +02007183/** Check whether the given key type is acceptable for the given
7184 * input step of a key derivation.
7185 *
7186 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
7187 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
7188 * Both secret and non-secret inputs can alternatively have the type
7189 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7190 * that the input was passed as a buffer rather than via a key object.
7191 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007192static int psa_key_derivation_check_input_type(
7193 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007195{
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007197 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 if (key_type == PSA_KEY_TYPE_DERIVE) {
7199 return PSA_SUCCESS;
7200 }
7201 if (key_type == PSA_KEY_TYPE_NONE) {
7202 return PSA_SUCCESS;
7203 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007204 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007205 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 if (key_type == PSA_KEY_TYPE_DERIVE) {
7207 return PSA_SUCCESS;
7208 }
7209 if (key_type == PSA_KEY_TYPE_NONE) {
7210 return PSA_SUCCESS;
7211 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007212 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007213 case PSA_KEY_DERIVATION_INPUT_LABEL:
7214 case PSA_KEY_DERIVATION_INPUT_SALT:
7215 case PSA_KEY_DERIVATION_INPUT_INFO:
7216 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
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;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307224 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7225 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7226 return PSA_SUCCESS;
7227 }
7228 if (key_type == PSA_KEY_TYPE_DERIVE) {
7229 return PSA_SUCCESS;
7230 }
7231 if (key_type == PSA_KEY_TYPE_NONE) {
7232 return PSA_SUCCESS;
7233 }
7234 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007237}
7238
Janos Follathb80a94e2019-06-12 15:54:46 +01007239static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007240 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007241 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007242 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007243 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007245{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007246 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 status = psa_key_derivation_check_input_type(step, key_type);
7250 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007253
Przemek Stekiel69c46792022-06-10 12:59:51 +02007254#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7256 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7257 step, data, data_length);
7258 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007259#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007260#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7262 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7263 step, data, data_length);
7264 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007265#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7266#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7268 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7269 step, data, data_length);
7270 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007271#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007272#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007274 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7276 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007277#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307278#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307279 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307280 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7281 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307282 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307283#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007284 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007285 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007286 (void) data;
7287 (void) data_length;
7288 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007290 }
7291
Gilles Peskine224b0d62019-09-23 18:13:17 +02007292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 if (status != PSA_SUCCESS) {
7294 psa_key_derivation_abort(operation);
7295 }
7296 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007297}
7298
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307299static psa_status_t psa_key_derivation_input_integer_internal(
7300 psa_key_derivation_operation_t *operation,
7301 psa_key_derivation_step_t step,
7302 uint64_t value)
7303{
7304 psa_status_t status;
7305 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7306
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307307#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307308 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307309 status = psa_pbkdf2_set_input_cost(
7310 &operation->ctx.pbkdf2, step, value);
7311 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307312#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307313 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307314 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307315 (void) value;
7316 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307317 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307318 }
7319
7320 if (status != PSA_SUCCESS) {
7321 psa_key_derivation_abort(operation);
7322 }
7323 return status;
7324}
7325
Janos Follath51f4a0f2019-06-14 11:35:55 +01007326psa_status_t psa_key_derivation_input_bytes(
7327 psa_key_derivation_operation_t *operation,
7328 psa_key_derivation_step_t step,
Ryan Everettd1e398c2024-01-19 14:46:00 +00007329 const uint8_t *data_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007331{
Ryan Everettd1e398c2024-01-19 14:46:00 +00007332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7333 LOCAL_INPUT_DECLARE(data_external, data);
7334
7335 LOCAL_INPUT_ALLOC(data_external, data_length, data);
7336
7337 status = psa_key_derivation_input_internal(operation, step,
7338 PSA_KEY_TYPE_NONE,
7339 data, data_length);
Ryan Everettb41c3c92024-01-25 11:56:35 +00007340#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everettd1e398c2024-01-19 14:46:00 +00007341exit:
Ryan Everettb41c3c92024-01-25 11:56:35 +00007342#endif
Ryan Everettd1e398c2024-01-19 14:46:00 +00007343 LOCAL_INPUT_FREE(data_external, data);
7344 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007345}
7346
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307347psa_status_t psa_key_derivation_input_integer(
7348 psa_key_derivation_operation_t *operation,
7349 psa_key_derivation_step_t step,
7350 uint64_t value)
7351{
7352 return psa_key_derivation_input_integer_internal(operation, step, value);
7353}
7354
Janos Follath51f4a0f2019-06-14 11:35:55 +01007355psa_status_t psa_key_derivation_input_key(
7356 psa_key_derivation_operation_t *operation,
7357 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007359{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007361 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007362 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007363
Ronald Cron5c522922020-11-14 16:35:34 +01007364 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7366 if (status != PSA_SUCCESS) {
7367 psa_key_derivation_abort(operation);
7368 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007369 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007370
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307371 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7372 * permission to output to a key object. */
7373 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7374 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007375 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 status = psa_key_derivation_input_internal(operation,
7379 step, slot->attr.type,
7380 slot->key.data,
7381 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007382
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007383 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007384
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007386}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007387
7388
7389
7390/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007391/* Key agreement */
7392/****************************************************************/
7393
Gilles Peskine449bd832023-01-11 14:50:10 +01007394psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7395 const uint8_t *key_buffer,
7396 size_t key_buffer_size,
7397 psa_algorithm_t alg,
7398 const uint8_t *peer_key,
7399 size_t peer_key_length,
7400 uint8_t *shared_secret,
7401 size_t shared_secret_size,
7402 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007403{
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007405#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007406 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7408 key_buffer_size, alg,
7409 peer_key, peer_key_length,
7410 shared_secret,
7411 shared_secret_size,
7412 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007413#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007414
7415#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7416 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007417 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007418 peer_key,
7419 peer_key_length,
7420 key_buffer,
7421 key_buffer_size,
7422 shared_secret,
7423 shared_secret_size,
7424 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007425#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7426
Gilles Peskine01d718c2018-09-18 12:01:02 +02007427 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007428 (void) attributes;
7429 (void) key_buffer;
7430 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007431 (void) peer_key;
7432 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007433 (void) shared_secret;
7434 (void) shared_secret_size;
7435 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007437 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007438}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007439
Aditya Deshpande17845b82022-10-13 17:21:01 +01007440/** Internal function for raw key agreement
7441 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007442 * to the driver's implementation if a driver is present.
7443 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007444 * (psa_key_agreement_raw_builtin).
7445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007446static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7447 psa_key_slot_t *private_key,
7448 const uint8_t *peer_key,
7449 size_t peer_key_length,
7450 uint8_t *shared_secret,
7451 size_t shared_secret_size,
7452 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007453{
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7455 return PSA_ERROR_NOT_SUPPORTED;
7456 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007457
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007458 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 private_key->key.data,
7460 private_key->key.bytes, alg,
7461 peer_key, peer_key_length,
7462 shared_secret,
7463 shared_secret_size,
7464 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007465}
7466
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007467/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007468 * to potentially free embedded data structures and wipe confidential data.
7469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007470static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7471 psa_key_derivation_step_t step,
7472 psa_key_slot_t *private_key,
7473 const uint8_t *peer_key,
7474 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007475{
7476 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00007477 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02007478 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007480
7481 /* Step 1: run the secret agreement algorithm to generate the shared
7482 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 status = psa_key_agreement_raw_internal(ka_alg,
7484 private_key,
7485 peer_key, peer_key_length,
7486 shared_secret,
7487 sizeof(shared_secret),
7488 &shared_secret_length);
7489 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007490 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007492
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007493 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007494 * the shared secret. A shared secret is permitted wherever a key
7495 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 status = psa_key_derivation_input_internal(operation, step,
7497 PSA_KEY_TYPE_DERIVE,
7498 shared_secret,
7499 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7502 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007503}
7504
Gilles Peskine449bd832023-01-11 14:50:10 +01007505psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7506 psa_key_derivation_step_t step,
7507 mbedtls_svc_key_id_t private_key,
Thomas Daubney9739ac02024-02-15 13:15:47 +00007508 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007510{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007512 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007513 psa_key_slot_t *slot;
Thomas Daubney9739ac02024-02-15 13:15:47 +00007514 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7517 return PSA_ERROR_INVALID_ARGUMENT;
7518 }
Ronald Cron5c522922020-11-14 16:35:34 +01007519 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7521 if (status != PSA_SUCCESS) {
7522 return status;
7523 }
Thomas Daubney9739ac02024-02-15 13:15:47 +00007524
Thomas Daubney692fb3c2024-03-12 16:20:41 +00007525 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 status = psa_key_agreement_internal(operation, step,
7527 slot,
7528 peer_key, peer_key_length);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007529
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007530#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney9739ac02024-02-15 13:15:47 +00007531exit:
Thomas Daubney50f58fc2024-02-15 14:24:03 +00007532#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (status != PSA_SUCCESS) {
7534 psa_key_derivation_abort(operation);
7535 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007536 /* If a private key has been added as SECRET, we allow the derived
7537 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007539 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007541 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007542
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007543 unlock_status = psa_unregister_read_under_mutex(slot);
Thomas Daubney9739ac02024-02-15 13:15:47 +00007544 LOCAL_INPUT_FREE(peer_key_external, peer_key);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007547}
7548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7550 mbedtls_svc_key_id_t private_key,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007551 const uint8_t *peer_key_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 size_t peer_key_length,
Thomas Daubney81899ab2024-02-15 12:57:26 +00007553 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 size_t output_size,
7555 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007556{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007558 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007559 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007560 size_t expected_length;
Thomas Daubney81899ab2024-02-15 12:57:26 +00007561 LOCAL_INPUT_DECLARE(peer_key_external, peer_key);
7562 LOCAL_OUTPUT_DECLARE(output_external, output);
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007563 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007566 status = PSA_ERROR_INVALID_ARGUMENT;
7567 goto exit;
7568 }
Ronald Cron5c522922020-11-14 16:35:34 +01007569 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7571 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007574
Gilles Peskine3e561302022-04-14 00:17:15 +02007575 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7576 * for the output size. The PSA specification only guarantees that this
7577 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7578 * but it might be nice to allow smaller buffers if the output fits.
7579 * At the time of writing this comment, with only ECDH implemented,
7580 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7581 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7582 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007583 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7585 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007586 status = PSA_ERROR_BUFFER_TOO_SMALL;
7587 goto exit;
7588 }
7589
Thomas Daubney81899ab2024-02-15 12:57:26 +00007590 LOCAL_INPUT_ALLOC(peer_key_external, peer_key_length, peer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 status = psa_key_agreement_raw_internal(alg, slot,
7592 peer_key, peer_key_length,
7593 output, output_size,
7594 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007595
7596exit:
Thomas Daubney5390aca2024-02-22 11:06:04 +00007597 /* Check for successful allocation of output,
7598 * with an unsuccessful status. */
Thomas Daubney0576a6a2024-02-21 15:15:00 +00007599 if (output != NULL && status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007600 /* If an error happens and is not handled properly, the output
7601 * may be used as a key to protect sensitive data. Arrange for such
7602 * a key to be random, which is likely to result in decryption or
7603 * verification errors. This is better than filling the buffer with
7604 * some constant data such as zeros, which would result in the data
7605 * being protected with a reproducible, easily knowable key.
7606 */
David Horstmann6e99bb22024-02-06 15:27:49 +00007607 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007608 *output_length = output_size;
7609 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007610
Thomas Daubney5390aca2024-02-22 11:06:04 +00007611 if (output == NULL) {
Thomas Daubney89d8c2a2024-02-21 11:16:16 +00007612 /* output allocation failed. */
7613 *output_length = 0;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007614 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007615
Ryan Everetta103ec92024-01-31 13:59:57 +00007616 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007617
Thomas Daubney81899ab2024-02-15 12:57:26 +00007618 LOCAL_INPUT_FREE(peer_key_external, peer_key);
7619 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007621}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007622
7623
7624/****************************************************************/
7625/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007626/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007627
Gilles Peskine672a7712023-04-28 21:00:28 +02007628#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7629#include "entropy_poll.h"
7630#endif
7631
Gilles Peskine30524eb2020-11-13 17:02:26 +01007632/** Initialize the PSA random generator.
7633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007634static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007635{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007636#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007638#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7639
Gilles Peskine30524eb2020-11-13 17:02:26 +01007640 /* Set default configuration if
7641 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007643 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 }
7645 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007646 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007650#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7651 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7652 /* The PSA entropy injection feature depends on using NV seed as an entropy
7653 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 mbedtls_entropy_add_source(&rng->entropy,
7655 mbedtls_nv_seed_poll, NULL,
7656 MBEDTLS_ENTROPY_BLOCK_SIZE,
7657 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007658#endif
7659
Valerio Setti061d4e42024-02-26 12:52:44 +01007660 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007661#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007662}
7663
7664/** Deinitialize the PSA random generator.
7665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007666static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007667{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007668#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007670#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007671 mbedtls_psa_drbg_free(&rng->drbg);
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007673#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007674}
7675
7676/** Seed the PSA random generator.
7677 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007678static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007679{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007680#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7681 /* Do nothing: the external RNG seeds itself. */
7682 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007684#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007685 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007686 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 drbg_seed, sizeof(drbg_seed) - 1);
7688 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007689#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007690}
7691
David Horstmann6e99bb22024-02-06 15:27:49 +00007692psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007694{
David Horstmann6e99bb22024-02-06 15:27:49 +00007695 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007696
David Horstmann6e99bb22024-02-06 15:27:49 +00007697 LOCAL_OUTPUT_DECLARE(output_external, output);
7698 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007699
David Horstmann6e99bb22024-02-06 15:27:49 +00007700 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007701
David Horstmanne097bbd2024-02-28 14:17:10 +00007702#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann6e99bb22024-02-06 15:27:49 +00007703exit:
David Horstmanne097bbd2024-02-28 14:17:10 +00007704#endif
David Horstmann6e99bb22024-02-06 15:27:49 +00007705 LOCAL_OUTPUT_FREE(output_external, output);
7706 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007707}
7708
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007709#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007710psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7711 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007712{
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 if (global_data.initialized) {
7714 return PSA_ERROR_NOT_PERMITTED;
7715 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007716
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7718 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7719 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7720 return PSA_ERROR_INVALID_ARGUMENT;
7721 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007722
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007724}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007725#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007726
Ronald Cron3772afe2021-02-08 16:10:05 +01007727/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007728 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007729 * \param type The key type
7730 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007731 *
7732 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007733 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007734 * \retval #PSA_ERROR_INVALID_ARGUMENT
7735 * The size in bits of the key is not valid.
7736 * \retval #PSA_ERROR_NOT_SUPPORTED
7737 * The type and/or the size in bits of the key or the combination of
7738 * the two is not supported.
7739 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007740static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007742{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007744
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 if (key_type_is_raw_bytes(type)) {
7746 status = psa_validate_unstructured_key_bit_size(type, bits);
7747 if (status != PSA_SUCCESS) {
7748 return status;
7749 }
7750 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007751#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7753 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7754 return PSA_ERROR_NOT_SUPPORTED;
7755 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007756 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007757 return PSA_ERROR_NOT_SUPPORTED;
7758 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007759
Ronald Cron01b2aba2020-10-05 09:42:02 +02007760 /* Accept only byte-aligned keys, for the same reasons as
7761 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 if (bits % 8 != 0) {
7763 return PSA_ERROR_NOT_SUPPORTED;
7764 }
7765 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007766#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007767
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007768#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007769 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007770 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 return PSA_SUCCESS;
7772 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007773#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007774
Valerio Settia55f0422023-07-10 15:34:41 +02007775#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007776 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007777 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007778 return PSA_ERROR_NOT_SUPPORTED;
7779 }
7780 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007781#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007782 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007784 }
7785
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007787}
7788
Ronald Cron55ed0592020-10-05 10:30:40 +02007789psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007790 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007791 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007793{
7794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007795 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007796
Gilles Peskine7a18f962024-02-12 16:48:11 +01007797 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007798 (void) params;
7799 (void) params_data_length;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007800
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 if (key_type_is_raw_bytes(type)) {
David Horstmann93fa4e12024-03-12 15:02:28 +00007802 status = psa_generate_random_internal(key_buffer, key_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 if (status != PSA_SUCCESS) {
7804 return status;
7805 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007806
David Brown12ca5032021-02-11 11:02:00 -07007807#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 if (type == PSA_KEY_TYPE_DES) {
7809 psa_des_set_key_parity(key_buffer, key_buffer_size);
7810 }
David Brown8107e312021-02-12 08:08:46 -07007811#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007813
Valerio Setti76df8c12023-07-11 14:11:28 +02007814#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7816 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007817 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007818 key_buffer,
7819 key_buffer_size,
7820 key_buffer_length);
7821 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007822#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007823
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007824#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7826 return mbedtls_psa_ecp_generate_key(attributes,
7827 key_buffer,
7828 key_buffer_size,
7829 key_buffer_length);
7830 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007831#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007832
Valerio Settia55f0422023-07-10 15:34:41 +02007833#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007834 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7835 return mbedtls_psa_ffdh_generate_key(attributes,
7836 key_buffer,
7837 key_buffer_size,
7838 key_buffer_length);
7839 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007840#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007841 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 (void) key_buffer_length;
7843 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007844 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007845
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007847}
Darryl Green0c6575a2018-11-07 16:05:30 +00007848
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007849psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007850 const psa_key_production_parameters_t *params,
7851 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007852 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007853{
7854 psa_status_t status;
7855 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007856 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007857 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007858
Ronald Cron81709fc2020-11-14 12:10:32 +01007859 *key = MBEDTLS_SVC_KEY_ID_INIT;
7860
Gilles Peskine0f84d622019-09-12 19:03:13 +02007861 /* Reject any attempt to create a zero-length key so that we don't
7862 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 if (psa_get_key_bits(attributes) == 0) {
7864 return PSA_ERROR_INVALID_ARGUMENT;
7865 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007866
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007867 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007868 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007869 return PSA_ERROR_INVALID_ARGUMENT;
7870 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007871
Gilles Peskine7a18f962024-02-12 16:48:11 +01007872#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007873 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007874 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007875 return PSA_ERROR_INVALID_ARGUMENT;
7876 }
7877 } else
7878#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007879 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Ronald Cron81709fc2020-11-14 12:10:32 +01007880 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine11792082019-08-06 18:36:36 +02007881 }
7882
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7884 &slot, &driver);
7885 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007886 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 }
Gilles Peskine11792082019-08-06 18:36:36 +02007888
Ronald Cron977c2472020-10-13 08:32:21 +02007889 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307890 * storage ( thus not in the case of generating a key in a secure element
7891 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7892 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007894 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007896 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007897 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007899 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007901
7902 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007903 attributes->type,
7904 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007906 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 attributes, &key_buffer_size);
7908 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 }
Ronald Cron977c2472020-10-13 08:32:21 +02007911 }
Ronald Cron977c2472020-10-13 08:32:21 +02007912
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7914 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 }
Ronald Cron977c2472020-10-13 08:32:21 +02007917 }
7918
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007920 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007921 slot->key.data, slot->key.bytes,
7922 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 if (status != PSA_SUCCESS) {
7924 psa_remove_key_data_from_memory(slot);
7925 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007926
Gilles Peskine11792082019-08-06 18:36:36 +02007927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 if (status == PSA_SUCCESS) {
7929 status = psa_finish_key_creation(slot, driver, key);
7930 }
7931 if (status != PSA_SUCCESS) {
7932 psa_fail_key_creation(slot, driver);
7933 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007936}
7937
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007938psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7939 mbedtls_svc_key_id_t *key)
7940{
7941 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007942 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007943 key);
7944}
7945
Gilles Peskinee59236f2018-01-27 23:32:46 +01007946/****************************************************************/
7947/* Module setup */
7948/****************************************************************/
7949
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007950#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007951psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 void (* entropy_init)(mbedtls_entropy_context *ctx),
7953 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007954{
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7956 return PSA_ERROR_BAD_STATE;
7957 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007958 global_data.rng.entropy_init = entropy_init;
7959 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007961}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007962#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007963
Gilles Peskine449bd832023-01-11 14:50:10 +01007964void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007965{
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 psa_wipe_all_key_slots();
7967 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7968 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007969 }
7970 /* Wipe all remaining data, including configuration.
7971 * In particular, this sets all state indicator to the value
7972 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007974
7975 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007977}
7978
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007979#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7980/** Recover a transaction that was interrupted by a power failure.
7981 *
7982 * This function is called during initialization, before psa_crypto_init()
7983 * returns. If this function returns a failure status, the initialization
7984 * fails.
7985 */
7986static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007988{
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007990 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7991 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 /* TODO - fall through to the failure case until this
7993 * is implemented.
7994 * https://github.com/ARMmbed/mbed-crypto/issues/218
7995 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007996 default:
7997 /* We found an unsupported transaction in the storage.
7998 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008000 }
8001}
8002#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
8003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01008005{
Gilles Peskine66fb1262018-12-10 16:29:04 +01008006 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008007
Gilles Peskinec6b69072018-11-20 21:42:52 +01008008 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008009 if (global_data.initialized != 0) {
8010 return PSA_SUCCESS;
8011 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01008012
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01008013 /* Init drivers */
8014 status = psa_driver_wrapper_init();
8015 if (status != PSA_SUCCESS) {
8016 goto exit;
8017 }
8018 global_data.drivers_initialized = 1;
8019
Valerio Setti402cfba2023-11-13 10:24:32 +01008020 status = psa_initialize_key_slots();
8021 if (status != PSA_SUCCESS) {
8022 goto exit;
8023 }
8024
Gilles Peskine30524eb2020-11-13 17:02:26 +01008025 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01008027 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 status = mbedtls_psa_random_seed(&global_data.rng);
8029 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01008030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01008032 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008033
Gilles Peskine4b734222019-07-24 15:56:31 +02008034#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 status = psa_crypto_load_transaction();
8036 if (status == PSA_SUCCESS) {
8037 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
8038 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02008039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 }
8041 status = psa_crypto_stop_transaction();
8042 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02008043 /* There's no transaction to complete. It's all good. */
8044 status = PSA_SUCCESS;
8045 }
Gilles Peskine4b734222019-07-24 15:56:31 +02008046#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02008047
Gilles Peskinec6b69072018-11-20 21:42:52 +01008048 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01008049 global_data.initialized = 1;
8050
8051exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if (status != PSA_SUCCESS) {
8053 mbedtls_psa_crypto_free();
8054 }
8055 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01008056}
8057
Yanray Wangffc3c482023-07-11 12:01:04 +08008058#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008059psa_status_t psa_crypto_driver_pake_get_password_len(
8060 const psa_crypto_driver_pake_inputs_t *inputs,
8061 size_t *password_len)
8062{
8063 if (inputs->password_len == 0) {
8064 return PSA_ERROR_BAD_STATE;
8065 }
8066
8067 *password_len = inputs->password_len;
8068
8069 return PSA_SUCCESS;
8070}
8071
8072psa_status_t psa_crypto_driver_pake_get_password(
8073 const psa_crypto_driver_pake_inputs_t *inputs,
8074 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
8075{
8076 if (inputs->password_len == 0) {
8077 return PSA_ERROR_BAD_STATE;
8078 }
8079
8080 if (buffer_size < inputs->password_len) {
8081 return PSA_ERROR_BUFFER_TOO_SMALL;
8082 }
8083
8084 memcpy(buffer, inputs->password, inputs->password_len);
8085 *buffer_length = inputs->password_len;
8086
8087 return PSA_SUCCESS;
8088}
8089
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008090psa_status_t psa_crypto_driver_pake_get_user_len(
8091 const psa_crypto_driver_pake_inputs_t *inputs,
8092 size_t *user_len)
8093{
8094 if (inputs->user_len == 0) {
8095 return PSA_ERROR_BAD_STATE;
8096 }
8097
8098 *user_len = inputs->user_len;
8099
8100 return PSA_SUCCESS;
8101}
8102
8103psa_status_t psa_crypto_driver_pake_get_user(
8104 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008105 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008106{
8107 if (inputs->user_len == 0) {
8108 return PSA_ERROR_BAD_STATE;
8109 }
8110
Przemek Stekielc0e62502023-03-14 11:49:36 +01008111 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008112 return PSA_ERROR_BUFFER_TOO_SMALL;
8113 }
8114
Przemek Stekielc0e62502023-03-14 11:49:36 +01008115 memcpy(user_id, inputs->user, inputs->user_len);
8116 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008117
8118 return PSA_SUCCESS;
8119}
8120
8121psa_status_t psa_crypto_driver_pake_get_peer_len(
8122 const psa_crypto_driver_pake_inputs_t *inputs,
8123 size_t *peer_len)
8124{
8125 if (inputs->peer_len == 0) {
8126 return PSA_ERROR_BAD_STATE;
8127 }
8128
8129 *peer_len = inputs->peer_len;
8130
8131 return PSA_SUCCESS;
8132}
8133
8134psa_status_t psa_crypto_driver_pake_get_peer(
8135 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01008136 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008137{
8138 if (inputs->peer_len == 0) {
8139 return PSA_ERROR_BAD_STATE;
8140 }
8141
Przemek Stekielc0e62502023-03-14 11:49:36 +01008142 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008143 return PSA_ERROR_BUFFER_TOO_SMALL;
8144 }
8145
Przemek Stekielc0e62502023-03-14 11:49:36 +01008146 memcpy(peer_id, inputs->peer, inputs->peer_len);
8147 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01008148
8149 return PSA_SUCCESS;
8150}
8151
Przemek Stekielca8d2b22023-01-17 16:21:33 +01008152psa_status_t psa_crypto_driver_pake_get_cipher_suite(
8153 const psa_crypto_driver_pake_inputs_t *inputs,
8154 psa_pake_cipher_suite_t *cipher_suite)
8155{
8156 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
8157 return PSA_ERROR_BAD_STATE;
8158 }
8159
8160 *cipher_suite = inputs->cipher_suite;
8161
8162 return PSA_SUCCESS;
8163}
8164
Neil Armstronga7d08c32022-06-01 18:21:20 +02008165psa_status_t psa_pake_setup(
8166 psa_pake_operation_t *operation,
8167 const psa_pake_cipher_suite_t *cipher_suite)
8168{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008170
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008171 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 status = PSA_ERROR_BAD_STATE;
8173 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008174 }
8175
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008176 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01008177 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008178 status = PSA_ERROR_INVALID_ARGUMENT;
8179 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008180 }
8181
Przemek Stekiel51eac532022-12-07 11:04:51 +01008182 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8183
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008185 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8186 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008187 operation->data.inputs.cipher_suite = *cipher_suite;
8188
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008189#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008191 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008192 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008193
David Horstmann16f01512023-06-14 17:21:07 +01008194 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008195 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008196 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008197#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008198 {
8199 status = PSA_ERROR_NOT_SUPPORTED;
8200 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201 }
8202
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008203 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8204
Przemek Stekiel51eac532022-12-07 11:04:51 +01008205 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008206exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008207 psa_pake_abort(operation);
8208 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008209}
8210
8211psa_status_t psa_pake_set_password_key(
8212 psa_pake_operation_t *operation,
8213 mbedtls_svc_key_id_t password)
8214{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008216 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8217 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008218 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008219
Przemek Stekiel51eac532022-12-07 11:04:51 +01008220 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008221 status = PSA_ERROR_BAD_STATE;
8222 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008223 }
8224
Przemek Stekiel6c764412022-11-22 14:05:12 +01008225 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8226 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008227 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008228 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008230 }
8231
Gilles Peskine7a5d9202024-02-28 01:18:23 +01008232 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008233
Przemek Stekiel51eac532022-12-07 11:04:51 +01008234 if (type != PSA_KEY_TYPE_PASSWORD &&
8235 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8236 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008238 }
8239
Przemek Stekiel51eac532022-12-07 11:04:51 +01008240 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8241 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008242 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008243 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008244 }
8245
8246 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8247 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01008248 operation->data.inputs.attributes = slot->attr;
8249
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008250exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008251 if (status != PSA_SUCCESS) {
8252 psa_pake_abort(operation);
8253 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00008254 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008255 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008256}
8257
8258psa_status_t psa_pake_set_user(
8259 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008260 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008261 size_t user_id_len)
8262{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008264 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008265
8266 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_BAD_STATE;
8268 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008269 }
8270
Przemek Stekiel51eac532022-12-07 11:04:51 +01008271 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 status = PSA_ERROR_INVALID_ARGUMENT;
8273 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008274 }
8275
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008276 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008277 status = PSA_ERROR_BAD_STATE;
8278 goto exit;
8279 }
8280
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008281 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8282 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008283 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8284 goto exit;
8285 }
8286
David Horstmann4f534ae2024-01-22 17:35:59 +00008287 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8288
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008289 memcpy(operation->data.inputs.user, user_id, user_id_len);
8290 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008291
David Horstmann4f534ae2024-01-22 17:35:59 +00008292 status = PSA_SUCCESS;
8293
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008294exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008295 LOCAL_INPUT_FREE(user_id_external, user_id);
8296 if (status != PSA_SUCCESS) {
8297 psa_pake_abort(operation);
8298 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008299 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008300}
8301
8302psa_status_t psa_pake_set_peer(
8303 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008304 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008305 size_t peer_id_len)
8306{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008308 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008309
8310 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008311 status = PSA_ERROR_BAD_STATE;
8312 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008313 }
8314
Przemek Stekiel51eac532022-12-07 11:04:51 +01008315 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008316 status = PSA_ERROR_INVALID_ARGUMENT;
8317 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008318 }
8319
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008320 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008321 status = PSA_ERROR_BAD_STATE;
8322 goto exit;
8323 }
8324
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008325 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8326 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008327 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8328 goto exit;
8329 }
8330
David Horstmann4f534ae2024-01-22 17:35:59 +00008331 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8332
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008333 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8334 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008335
David Horstmann4f534ae2024-01-22 17:35:59 +00008336 status = PSA_SUCCESS;
8337
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008338exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008339 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8340 if (status != PSA_SUCCESS) {
8341 psa_pake_abort(operation);
8342 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008343 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008344}
8345
8346psa_status_t psa_pake_set_role(
8347 psa_pake_operation_t *operation,
8348 psa_pake_role_t role)
8349{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008351
Przemek Stekiel51eac532022-12-07 11:04:51 +01008352 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008353 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008354 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008355 }
8356
Przemek Stekiel09104b82023-03-06 14:11:51 +01008357 switch (operation->alg) {
8358#if defined(PSA_WANT_ALG_JPAKE)
8359 case PSA_ALG_JPAKE:
8360 if (role == PSA_PAKE_ROLE_NONE) {
8361 return PSA_SUCCESS;
8362 }
8363 status = PSA_ERROR_INVALID_ARGUMENT;
8364 break;
8365#endif
8366 default:
8367 (void) role;
8368 status = PSA_ERROR_NOT_SUPPORTED;
8369 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008370 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008371exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008372 psa_pake_abort(operation);
8373 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008374}
8375
David Horstmanne7f21e62023-05-12 18:17:21 +01008376/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008377#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008378static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008379 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008380{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008381 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008382 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008383 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008384
David Horstmann024e5c52023-06-14 15:48:21 +01008385 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008386 is_x1 = (stage->outputs < 1);
8387 } else {
8388 is_x1 = (stage->inputs < 1);
8389 }
8390
David Horstmann74a3d8c2023-06-14 18:28:19 +01008391 key_share_step = is_x1 ?
8392 PSA_JPAKE_X1_STEP_KEY_SHARE :
8393 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008394 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008395 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8396 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8397 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8398 } else {
8399 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008400 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008401 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008402}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008403#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008404
Przemek Stekiel2797d372022-12-22 11:19:22 +01008405static psa_status_t psa_pake_complete_inputs(
8406 psa_pake_operation_t *operation)
8407{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008409 /* Create copy of the inputs on stack as inputs share memory
8410 with the driver context which will be setup by the driver. */
8411 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008412
Przemek Stekielfde11282023-03-13 16:06:09 +01008413 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008414 return PSA_ERROR_BAD_STATE;
8415 }
8416
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008417 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008418 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8419 return PSA_ERROR_BAD_STATE;
8420 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008421 }
8422
Przemek Stekiel18620a32023-01-17 16:34:52 +01008423 /* Clear driver context */
8424 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8425
8426 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008427
8428 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008429 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008430
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008431 /* User and peer are translated to role. */
8432 mbedtls_free(inputs.user);
8433 mbedtls_free(inputs.peer);
8434
Przemek Stekiel2797d372022-12-22 11:19:22 +01008435 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008436#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008437 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008438 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008439 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008440#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008441 {
8442 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008443 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008444 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008445 return status;
8446}
8447
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008448#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008449static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008450 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008451 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008452 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008453{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008454 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8455 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8456 step != PSA_PAKE_STEP_ZK_PROOF) {
8457 return PSA_ERROR_INVALID_ARGUMENT;
8458 }
8459
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008460 psa_jpake_computation_stage_t *computation_stage =
8461 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008462
David Horstmann5da95602023-06-08 15:37:12 +01008463 if (computation_stage->round != PSA_JPAKE_FIRST &&
8464 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008465 return PSA_ERROR_BAD_STATE;
8466 }
8467
David Horstmanne7f21e62023-05-12 18:17:21 +01008468 /* Check that the step we are given is the one we were expecting */
8469 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008470 return PSA_ERROR_BAD_STATE;
8471 }
8472
David Horstmanne7f21e62023-05-12 18:17:21 +01008473 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8474 computation_stage->inputs == 0 &&
8475 computation_stage->outputs == 0) {
8476 /* Start of the round, so function decides whether we are inputting
8477 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008478 computation_stage->io_mode = io_mode;
8479 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008480 /* Middle of the round so the mode we are in must match the function
8481 * called by the user */
8482 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008483 }
8484
8485 return PSA_SUCCESS;
8486}
8487
David Horstmanne7f21e62023-05-12 18:17:21 +01008488static psa_status_t psa_jpake_epilogue(
8489 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008490 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008491{
David Horstmanne7f21e62023-05-12 18:17:21 +01008492 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008493 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008494
David Horstmanne7f21e62023-05-12 18:17:21 +01008495 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8496 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008497 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008498 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008499 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008500 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008501 }
8502 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008503 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008504 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008505 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008506 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008507 }
8508 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008509 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8510 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008511 /* End of a round, move to the next round */
8512 stage->inputs = 0;
8513 stage->outputs = 0;
8514 stage->round++;
8515 }
8516 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008517 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008518 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008519 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008520 return PSA_SUCCESS;
8521}
David Horstmanne7f21e62023-05-12 18:17:21 +01008522
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008523#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008524
Neil Armstronga7d08c32022-06-01 18:21:20 +02008525psa_status_t psa_pake_output(
8526 psa_pake_operation_t *operation,
8527 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008528 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008529 size_t output_size,
8530 size_t *output_length)
8531{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008533 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008534 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008535 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008536
8537 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008538 status = psa_pake_complete_inputs(operation);
8539 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008540 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008541 }
8542 }
8543
8544 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008545 status = PSA_ERROR_BAD_STATE;
8546 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008547 }
8548
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008549 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008550 status = PSA_ERROR_INVALID_ARGUMENT;
8551 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008552 }
8553
Przemek Stekiele12ed362022-12-21 12:54:46 +01008554 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008555#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008556 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008557 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008558 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008559 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008560 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008561 driver_step = convert_jpake_computation_stage_to_driver_step(
8562 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008563 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008564#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008565 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008566 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008567 status = PSA_ERROR_NOT_SUPPORTED;
8568 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008569 }
8570
David Horstmannc75639d2024-01-22 17:56:39 +00008571 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
8572
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008573 status = psa_driver_wrapper_pake_output(operation, driver_step,
8574 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008575
8576 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008577 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008578 }
8579
8580 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008581#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008582 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008583 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008584 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008585 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008586 }
8587 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008588#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008589 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008590 status = PSA_ERROR_NOT_SUPPORTED;
8591 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008592 }
8593
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008594exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008595 LOCAL_OUTPUT_FREE(output_external, output);
8596 if (status != PSA_SUCCESS) {
8597 psa_pake_abort(operation);
8598 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008599 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008600}
8601
8602psa_status_t psa_pake_input(
8603 psa_pake_operation_t *operation,
8604 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008605 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008606 size_t input_length)
8607{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008609 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008610 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8611 operation->primitive,
8612 step);
David Horstmannc75639d2024-01-22 17:56:39 +00008613 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008614
8615 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008616 status = psa_pake_complete_inputs(operation);
8617 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008618 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008619 }
8620 }
8621
8622 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008623 status = PSA_ERROR_BAD_STATE;
8624 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008625 }
8626
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008627 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008628 status = PSA_ERROR_INVALID_ARGUMENT;
8629 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008630 }
8631
Przemek Stekiele12ed362022-12-21 12:54:46 +01008632 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008633#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008634 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008635 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008636 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008637 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008638 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008639 driver_step = convert_jpake_computation_stage_to_driver_step(
8640 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008641 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008642#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008643 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008644 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008645 status = PSA_ERROR_NOT_SUPPORTED;
8646 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008647 }
8648
David Horstmannc75639d2024-01-22 17:56:39 +00008649 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008650 status = psa_driver_wrapper_pake_input(operation, driver_step,
8651 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008652
8653 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008654 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008655 }
8656
8657 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008658#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008659 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008660 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008661 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008662 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008663 }
8664 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008665#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008666 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008667 status = PSA_ERROR_NOT_SUPPORTED;
8668 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008669 }
8670
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008671exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008672 LOCAL_INPUT_FREE(input_external, input);
8673 if (status != PSA_SUCCESS) {
8674 psa_pake_abort(operation);
8675 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008676 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008677}
8678
8679psa_status_t psa_pake_get_implicit_key(
8680 psa_pake_operation_t *operation,
8681 psa_key_derivation_operation_t *output)
8682{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008684 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008685 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008686 size_t shared_key_len = 0;
8687
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008688 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008689 status = PSA_ERROR_BAD_STATE;
8690 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008691 }
8692
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008693#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008694 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008695 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008696 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008697 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008698 status = PSA_ERROR_BAD_STATE;
8699 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008700 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008701 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008702#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008703 {
8704 status = PSA_ERROR_NOT_SUPPORTED;
8705 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008706 }
8707
Przemek Stekiel0c781802022-11-29 14:53:13 +01008708 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8709 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008710 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008711 &shared_key_len);
8712
8713 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008714 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008715 }
8716
8717 status = psa_key_derivation_input_bytes(output,
8718 PSA_KEY_DERIVATION_INPUT_SECRET,
8719 shared_key,
8720 shared_key_len);
8721
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008722 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008723exit:
8724 abort_status = psa_pake_abort(operation);
8725 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008726}
8727
8728psa_status_t psa_pake_abort(
8729 psa_pake_operation_t *operation)
8730{
Przemek Stekield69dca92023-01-31 19:59:20 +01008731 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008732
Przemek Stekield69dca92023-01-31 19:59:20 +01008733 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008734 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008735 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008736
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008737 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8738 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008739 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008740 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008741 }
8742 if (operation->data.inputs.user != NULL) {
8743 mbedtls_free(operation->data.inputs.user);
8744 }
8745 if (operation->data.inputs.peer != NULL) {
8746 mbedtls_free(operation->data.inputs.peer);
8747 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008748 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008749 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008750
Przemek Stekield69dca92023-01-31 19:59:20 +01008751 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008752}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008753#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008754
David Horstmann00d7a042023-12-07 18:39:17 +00008755/* Memory copying test hooks. These are called before input copy, after input
8756 * copy, before output copy and after output copy, respectively.
8757 * They are used by memory-poisoning tests to temporarily unpoison buffers
8758 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00008759#if defined(MBEDTLS_TEST_HOOKS)
8760void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8761void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8762void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8763void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8764#endif
David Horstmannfde97392023-10-30 20:29:43 +00008765
David Horstmann1b7279a2023-11-15 15:18:30 +00008766/** Copy from an input buffer to a local copy.
8767 *
8768 * \param[in] input Pointer to input buffer.
8769 * \param[in] input_len Length of the input buffer.
8770 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8771 * \param[out] input_copy_len Length of the local copy buffer.
8772 * \return #PSA_SUCCESS, if the buffer was successfully
8773 * copied.
8774 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8775 * copy is too small to hold contents of the
8776 * input buffer.
8777 */
8778MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008779psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8780 uint8_t *input_copy, size_t input_copy_len)
8781{
8782 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008783 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008784 }
8785
David Horstmann372b8bb2023-11-24 16:21:04 +00008786#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008787 if (psa_input_pre_copy_hook != NULL) {
8788 psa_input_pre_copy_hook(input, input_len);
8789 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008790#endif
8791
David Horstmann777e7412023-11-15 17:33:47 +00008792 if (input_len > 0) {
8793 memcpy(input_copy, input, input_len);
8794 }
David Horstmannfde97392023-10-30 20:29:43 +00008795
David Horstmann372b8bb2023-11-24 16:21:04 +00008796#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008797 if (psa_input_post_copy_hook != NULL) {
8798 psa_input_post_copy_hook(input, input_len);
8799 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008800#endif
8801
David Horstmannfde97392023-10-30 20:29:43 +00008802 return PSA_SUCCESS;
8803}
8804
David Horstmann1b7279a2023-11-15 15:18:30 +00008805/** Copy from a local output buffer into a user-supplied one.
8806 *
8807 * \param[in] output_copy Pointer to a local buffer containing the output.
8808 * \param[in] output_copy_len Length of the local buffer.
8809 * \param[out] output Pointer to user-supplied output buffer.
8810 * \param[out] output_len Length of the user-supplied output buffer.
8811 * \return #PSA_SUCCESS, if the buffer was successfully
8812 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008813 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008814 * user-supplied output buffer is too small to
8815 * hold the contents of the local buffer.
8816 */
8817MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008818psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8819 uint8_t *output, size_t output_len)
8820{
8821 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008822 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008823 }
David Horstmann777e7412023-11-15 17:33:47 +00008824
David Horstmann372b8bb2023-11-24 16:21:04 +00008825#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008826 if (psa_output_pre_copy_hook != NULL) {
8827 psa_output_pre_copy_hook(output, output_len);
8828 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008829#endif
8830
David Horstmann777e7412023-11-15 17:33:47 +00008831 if (output_copy_len > 0) {
8832 memcpy(output, output_copy, output_copy_len);
8833 }
8834
David Horstmann372b8bb2023-11-24 16:21:04 +00008835#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008836 if (psa_output_post_copy_hook != NULL) {
8837 psa_output_post_copy_hook(output, output_len);
8838 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008839#endif
8840
David Horstmann8978f5c2023-10-30 20:37:12 +00008841 return PSA_SUCCESS;
8842}
8843
David Horstmannf1734052023-11-20 17:15:32 +00008844psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8845 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008846{
8847 psa_status_t status;
8848
David Horstmann9db14482023-11-23 15:50:37 +00008849 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008850
David Horstmannc5cc1c32023-11-15 18:11:26 +00008851 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008852 return PSA_SUCCESS;
8853 }
8854
David Horstmannf1734052023-11-20 17:15:32 +00008855 local_input->buffer = mbedtls_calloc(input_len, 1);
8856 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008857 /* Since we dealt with the zero-length case above, we know that
8858 * a NULL return value means a failure of allocation. */
8859 return PSA_ERROR_INSUFFICIENT_MEMORY;
8860 }
David Horstmannf1734052023-11-20 17:15:32 +00008861 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008862
David Horstmannf1734052023-11-20 17:15:32 +00008863 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008864
8865 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008866 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008867 if (status != PSA_SUCCESS) {
8868 goto error;
8869 }
8870
8871 return PSA_SUCCESS;
8872
8873error:
David Horstmannf1734052023-11-20 17:15:32 +00008874 mbedtls_free(local_input->buffer);
8875 local_input->buffer = NULL;
8876 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008877 return status;
8878}
8879
David Horstmannf1734052023-11-20 17:15:32 +00008880void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008881{
David Horstmannf1734052023-11-20 17:15:32 +00008882 mbedtls_free(local_input->buffer);
8883 local_input->buffer = NULL;
8884 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008885}
8886
David Horstmann89875a42023-11-20 12:54:09 +00008887psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8888 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008889{
David Horstmann9db14482023-11-23 15:50:37 +00008890 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008891
David Horstmannc5cc1c32023-11-15 18:11:26 +00008892 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008893 return PSA_SUCCESS;
8894 }
David Horstmann89875a42023-11-20 12:54:09 +00008895 local_output->buffer = mbedtls_calloc(output_len, 1);
8896 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008897 /* Since we dealt with the zero-length case above, we know that
8898 * a NULL return value means a failure of allocation. */
8899 return PSA_ERROR_INSUFFICIENT_MEMORY;
8900 }
David Horstmann89875a42023-11-20 12:54:09 +00008901 local_output->length = output_len;
8902 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008903
8904 return PSA_SUCCESS;
8905}
8906
David Horstmann89875a42023-11-20 12:54:09 +00008907psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008908{
David Horstmannc335a4e2023-11-15 15:57:32 +00008909 psa_status_t status;
8910
David Horstmann89875a42023-11-20 12:54:09 +00008911 if (local_output->buffer == NULL) {
8912 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008913 return PSA_SUCCESS;
8914 }
David Horstmann89875a42023-11-20 12:54:09 +00008915 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008916 /* We have an internal copy but nothing to copy back to. */
8917 return PSA_ERROR_CORRUPTION_DETECTED;
8918 }
8919
David Horstmann89875a42023-11-20 12:54:09 +00008920 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8921 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008922 if (status != PSA_SUCCESS) {
8923 return status;
8924 }
David Horstmann9467ea32023-11-08 17:44:18 +00008925
David Horstmann89875a42023-11-20 12:54:09 +00008926 mbedtls_free(local_output->buffer);
8927 local_output->buffer = NULL;
8928 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008929
8930 return PSA_SUCCESS;
8931}
8932
Gilles Peskinee59236f2018-01-27 23:32:46 +01008933#endif /* MBEDTLS_PSA_CRYPTO_C */