blob: dfb97ee09dc0f2fa68bceff3fb7915cf8ced42b6 [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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.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
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
David Horstmann513101b2023-11-29 17:24:08 +0000113#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann36df4b22023-12-13 15:55:25 +0000114
David Horstmann62a56d92023-12-14 18:12:47 +0000115/* Declare a local copy of an input buffer and a variable that will be used
116 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000117 *
118 * Note: This macro must be called before any operations which may jump to
119 * the exit label, so that the local input copy object is safe to be freed.
120 *
121 * Assumptions:
122 * - input is the name of a pointer to the buffer to be copied
123 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000124 * - input_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000125 */
David Horstmann62a56d92023-12-14 18:12:47 +0000126#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
127 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
128 const uint8_t *input_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000129
David Horstmann62a56d92023-12-14 18:12:47 +0000130/* Allocate a copy of the buffer input and set the pointer input_copy to
131 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000132 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000133 * Assumptions:
134 * - psa_status_t status exists
135 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000136 * - input is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000137 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000138 */
David Horstmann62a56d92023-12-14 18:12:47 +0000139#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000140 status = psa_crypto_local_input_alloc(input, length, \
141 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000142 if (status != PSA_SUCCESS) { \
143 goto exit; \
144 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000145 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000146
David Horstmann36df4b22023-12-13 15:55:25 +0000147/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
148 *
David Horstmann3e72db42023-12-08 14:08:18 +0000149 * Assumptions:
David Horstmann62a56d92023-12-14 18:12:47 +0000150 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann36df4b22023-12-13 15:55:25 +0000151 * - input is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000152 */
David Horstmann62a56d92023-12-14 18:12:47 +0000153#define LOCAL_INPUT_FREE(input, input_copy) \
154 input_copy = NULL; \
David Horstmann36df4b22023-12-13 15:55:25 +0000155 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmanne9a88ab2023-11-29 17:07:13 +0000156
David Horstmann62a56d92023-12-14 18:12:47 +0000157/* Declare a local copy of an output buffer and a variable that will be used
158 * to store a pointer to the start of the buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000159 *
160 * Note: This macro must be called before any operations which may jump to
161 * the exit label, so that the local output copy object is safe to be freed.
162 *
163 * Assumptions:
164 * - output is the name of a pointer to the buffer to be copied
165 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmann62a56d92023-12-14 18:12:47 +0000166 * - output_copy_name is a name that is unused in the current scope
David Horstmann36df4b22023-12-13 15:55:25 +0000167 */
David Horstmann62a56d92023-12-14 18:12:47 +0000168#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
169 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
170 uint8_t *output_copy_name = NULL;
David Horstmann36df4b22023-12-13 15:55:25 +0000171
David Horstmann62a56d92023-12-14 18:12:47 +0000172/* Allocate a copy of the buffer output and set the pointer output_copy to
173 * point to the start of the copy.
David Horstmann36df4b22023-12-13 15:55:25 +0000174 *
David Horstmanne9a88ab2023-11-29 17:07:13 +0000175 * Assumptions:
176 * - psa_status_t status exists
177 * - An exit label is declared
David Horstmann36df4b22023-12-13 15:55:25 +0000178 * - output is the name of a pointer to the buffer to be copied
David Horstmann62a56d92023-12-14 18:12:47 +0000179 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmanne9a88ab2023-11-29 17:07:13 +0000180 */
David Horstmann62a56d92023-12-14 18:12:47 +0000181#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann36df4b22023-12-13 15:55:25 +0000182 status = psa_crypto_local_output_alloc(output, length, \
183 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmanne9a88ab2023-11-29 17:07:13 +0000184 if (status != PSA_SUCCESS) { \
185 goto exit; \
186 } \
David Horstmann62a56d92023-12-14 18:12:47 +0000187 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmanne9a88ab2023-11-29 17:07:13 +0000188
David Horstmann62a56d92023-12-14 18:12:47 +0000189/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann3e72db42023-12-08 14:08:18 +0000190 * after first copying back its contents to the original buffer.
David Horstmann36df4b22023-12-13 15:55:25 +0000191 *
David Horstmann3e72db42023-12-08 14:08:18 +0000192 * Assumptions:
David Horstmann3e72db42023-12-08 14:08:18 +0000193 * - psa_status_t status exists
David Horstmann62a56d92023-12-14 18:12:47 +0000194 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
195 * - output is the name of the original buffer that was copied
David Horstmann3e72db42023-12-08 14:08:18 +0000196 */
David Horstmann62a56d92023-12-14 18:12:47 +0000197#define LOCAL_OUTPUT_FREE(output, output_copy) \
198 output_copy = NULL; \
David Horstmann5a945f52023-12-13 14:09:08 +0000199 do { \
200 psa_status_t local_output_status; \
David Horstmann36df4b22023-12-13 15:55:25 +0000201 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann5a945f52023-12-13 14:09:08 +0000202 if (local_output_status != PSA_SUCCESS) { \
203 /* Since this error case is an internal error, it's more serious than \
204 * any existing error code and so it's fine to overwrite the existing \
205 * status. */ \
206 status = local_output_status; \
207 } \
208 } while (0)
David Horstmann513101b2023-11-29 17:24:08 +0000209#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann62a56d92023-12-14 18:12:47 +0000210#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
211 const uint8_t *input_copy_name = NULL;
212#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
213 input_copy = input;
214#define LOCAL_INPUT_FREE(input, input_copy) \
215 input_copy = NULL;
216#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
217 uint8_t *output_copy_name = NULL;
218#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
219 output_copy = output;
220#define LOCAL_OUTPUT_FREE(output, output_copy) \
221 output_copy = NULL;
David Horstmann513101b2023-11-29 17:24:08 +0000222#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000223
224
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100225int psa_can_do_hash(psa_algorithm_t hash_alg)
226{
227 (void) hash_alg;
228 return global_data.drivers_initialized;
229}
Valerio Settia55f0422023-07-10 15:34:41 +0200230#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200231 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200232 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200233static int psa_is_dh_key_size_valid(size_t bits)
234{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200235 if (bits != 2048 && bits != 3072 && bits != 4096 &&
236 bits != 6144 && bits != 8192) {
237 return 0;
238 }
239
240 return 1;
241}
Valerio Settia55f0422023-07-10 15:34:41 +0200242#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200243 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200244 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100247{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100248 /* Mbed TLS error codes can combine a high-level error code and a
249 * low-level error code. The low-level error usually reflects the
250 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 int low_level_ret = -(-ret & 0x007f);
252 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100253 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100256#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
258 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100260 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
261 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
263
264#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200265 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
266 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
267 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
268 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
269 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200271 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200273 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100275#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200276
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100277#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000278 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100281#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100282
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100283#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100288#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100289
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100290#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200291 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100293#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200294
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100295#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200296 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200298 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100300#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200301
Dave Rodgman509b5672023-08-16 19:26:23 +0100302#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100317#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
320 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100321 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
322 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
326 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100330#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100331
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100332#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100335#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
338 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
339 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100342#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200345 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100349#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100350
Gilles Peskine82e57d12020-11-13 21:31:17 +0100351#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100353 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
354 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100355 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100357 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
358 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100360 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100362#endif
363
Dave Rodgmandea266f2023-08-31 11:52:43 +0100364#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100367 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100371#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100372 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100374#endif
375#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100376
Dave Rodgman509b5672023-08-16 19:26:23 +0100377#if defined(MBEDTLS_BIGNUM_C)
378#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100379 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100381#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100382 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100384 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100386 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100388 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100390 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100392 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100394 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100396#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100397
Dave Rodgman509b5672023-08-16 19:26:23 +0100398#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
402 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100404#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
405 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100406 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100408#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
410 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
415 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 case MBEDTLS_ERR_PK_INVALID_ALG:
420 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
421 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200425 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100427#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100428
Gilles Peskineff2d2002019-05-06 15:26:23 +0200429 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200431 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200433
Dave Rodgman509b5672023-08-16 19:26:23 +0100434#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100439 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100441 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100443 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
444 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100446 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100448 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100450 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100452#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200453
Dave Rodgman1dab4452023-09-01 09:59:51 +0100454#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300455 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300456 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300458 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300460 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300462 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
463 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300465 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100467 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100469
Dave Rodgman509b5672023-08-16 19:26:23 +0100470#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000471 case MBEDTLS_ERR_ECP_IN_PROGRESS:
472 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100473#endif
474#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000475
Janos Follatha13b9052019-11-22 12:48:59 +0000476 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300478
Gilles Peskinee59236f2018-01-27 23:32:46 +0100479 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100481 }
482}
483
Paul Elliottdc42ca82023-02-24 18:11:59 +0000484/**
485 * \brief For output buffers which contain "tags"
486 * (outputs that may be checked for validity like
487 * hashes, MACs and signatures), fill the unused
488 * part of the output buffer (the whole buffer on
489 * error, the trailing part on success) with
490 * something that isn't a valid tag (barring an
491 * attack on the tag and deliberately-crafted
492 * input), in case the caller doesn't check the
493 * return status properly.
494 *
495 * \param output_buffer Pointer to buffer to wipe. May not be NULL
496 * unless \p output_buffer_size is zero.
497 * \param status Status of function called to generate
498 * output_buffer originally
499 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
500 * could be NULL.
501 * \param output_buffer_length Length of data written to output_buffer, must be
502 * less than \p output_buffer_size
503 */
504static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000505 size_t output_buffer_size, size_t output_buffer_length)
506{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000507 size_t offset = 0;
508
509 if (output_buffer_size == 0) {
510 /* If output_buffer_size is 0 then we have nothing to do. We must not
511 call memset because output_buffer may be NULL in this case */
512 return;
513 }
514
515 if (status == PSA_SUCCESS) {
516 offset = output_buffer_length;
517 }
518
519 memset(output_buffer + offset, '!', output_buffer_size - offset);
520}
521
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200522
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200523
524
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100525/****************************************************************/
526/* Key management */
527/****************************************************************/
528
Valerio Settia9aab1a2023-06-19 13:39:54 +0200529#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200530psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
531 size_t *bits)
532{
533 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200534#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200535 case MBEDTLS_ECP_DP_SECP192R1:
536 *bits = 192;
537 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100538#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200539#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200540 case MBEDTLS_ECP_DP_SECP224R1:
541 *bits = 224;
542 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100543#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200544#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200545 case MBEDTLS_ECP_DP_SECP256R1:
546 *bits = 256;
547 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100548#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200549#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200550 case MBEDTLS_ECP_DP_SECP384R1:
551 *bits = 384;
552 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100553#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200554#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200555 case MBEDTLS_ECP_DP_SECP521R1:
556 *bits = 521;
557 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100558#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200559#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200560 case MBEDTLS_ECP_DP_BP256R1:
561 *bits = 256;
562 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100563#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200564#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200565 case MBEDTLS_ECP_DP_BP384R1:
566 *bits = 384;
567 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100568#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200569#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200570 case MBEDTLS_ECP_DP_BP512R1:
571 *bits = 512;
572 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100573#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200574#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200575 case MBEDTLS_ECP_DP_CURVE25519:
576 *bits = 255;
577 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100578#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200579#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200580 case MBEDTLS_ECP_DP_SECP192K1:
581 *bits = 192;
582 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100583#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200584#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200585 case MBEDTLS_ECP_DP_SECP224K1:
586 *bits = 224;
587 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100588#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200589#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200590 case MBEDTLS_ECP_DP_SECP256K1:
591 *bits = 256;
592 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100593#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200594#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200595 case MBEDTLS_ECP_DP_CURVE448:
596 *bits = 448;
597 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100598#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200599 default:
600 *bits = 0;
601 return 0;
602 }
603}
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
606 size_t bits,
607 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200608{
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100610 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100612#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100613 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100615#endif
616#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100617 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100619#endif
620#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100621 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100623#endif
624#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100625 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100627#endif
628#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100629 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100631 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (bits_is_sloppy) {
633 return MBEDTLS_ECP_DP_SECP521R1;
634 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100635 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100636#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100637 }
638 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100639
Paul Elliott8ff510a2020-06-02 17:19:28 +0100640 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100642#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100643 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100645#endif
646#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100647 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100649#endif
650#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100651 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100653#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100654 }
655 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100656
Paul Elliott8ff510a2020-06-02 17:19:28 +0100657 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100659#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100660 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100662 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (bits_is_sloppy) {
664 return MBEDTLS_ECP_DP_CURVE25519;
665 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100666 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100667#endif
668#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100669 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100671#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100672 }
673 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100674
Paul Elliott8ff510a2020-06-02 17:19:28 +0100675 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100677#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100678 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100680#endif
681#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100682 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100684#endif
685#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100686 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100688#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100689 }
690 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200691 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100692
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100693 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200695}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200696#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
699 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200700{
701 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200703 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200704 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200705 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200706 case PSA_KEY_TYPE_PASSWORD:
707 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200708 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100709#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200710 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (bits != 128 && bits != 192 && bits != 256) {
712 return PSA_ERROR_INVALID_ARGUMENT;
713 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200714 break;
715#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200716#if defined(PSA_WANT_KEY_TYPE_ARIA)
717 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (bits != 128 && bits != 192 && bits != 256) {
719 return PSA_ERROR_INVALID_ARGUMENT;
720 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200721 break;
722#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100723#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200724 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (bits != 128 && bits != 192 && bits != 256) {
726 return PSA_ERROR_INVALID_ARGUMENT;
727 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200728 break;
729#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100730#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200731 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (bits != 64 && bits != 128 && bits != 192) {
733 return PSA_ERROR_INVALID_ARGUMENT;
734 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200735 break;
736#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100737#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200738 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (bits != 256) {
740 return PSA_ERROR_INVALID_ARGUMENT;
741 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200742 break;
743#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200744 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200746 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (bits % 8 != 0) {
748 return PSA_ERROR_INVALID_ARGUMENT;
749 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200752}
753
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100755 *
Steven Cooremancd640932021-03-08 12:00:27 +0100756 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
757 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100758 *
759 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100760 * \param[in] key_type The key type of the key to be used with the
761 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100762 *
763 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100765 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100766 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100767 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100768MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100769 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100771{
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (PSA_ALG_IS_HMAC(algorithm)) {
773 if (key_type == PSA_KEY_TYPE_HMAC) {
774 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100775 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100776 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
779 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
780 * key. */
781 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
782 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
783 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
784 * the block length (larger than 1) for block ciphers. */
785 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
786 return PSA_SUCCESS;
787 }
788 }
789 }
790
791 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100792}
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
795 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200796{
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (slot->key.data != NULL) {
798 return PSA_ERROR_ALREADY_EXISTS;
799 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 slot->key.data = mbedtls_calloc(1, buffer_length);
802 if (slot->key.data == NULL) {
803 return PSA_ERROR_INSUFFICIENT_MEMORY;
804 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200805
Ronald Cronea0f8a62020-11-25 17:52:23 +0100806 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200808}
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
811 const uint8_t *data,
812 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200813{
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 psa_status_t status = psa_allocate_buffer_to_slot(slot,
815 data_length);
816 if (status != PSA_SUCCESS) {
817 return status;
818 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 memcpy(slot->key.data, data, data_length);
821 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200822}
823
Ronald Crona0fe59f2020-11-29 11:14:53 +0100824psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100825 const psa_key_attributes_t *attributes,
826 const uint8_t *data, size_t data_length,
827 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
831 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200833 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (data_length == 0) {
835 return PSA_ERROR_NOT_SUPPORTED;
836 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (key_type_is_raw_bytes(type)) {
839 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
842 *bits);
843 if (status != PSA_SUCCESS) {
844 return status;
845 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200846
Ronald Crondd04d422020-11-28 15:14:42 +0100847 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100849 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 return PSA_SUCCESS;
853 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200854#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200855 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100856 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200857 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100858 return PSA_ERROR_INVALID_ARGUMENT;
859 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200860 return mbedtls_psa_ffdh_import_key(attributes,
861 data, data_length,
862 key_buffer, key_buffer_size,
863 key_buffer_length,
864 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100865 }
Valerio Settia55f0422023-07-10 15:34:41 +0200866#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200867 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200868#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
870 if (PSA_KEY_TYPE_IS_ECC(type)) {
871 return mbedtls_psa_ecp_import_key(attributes,
872 data, data_length,
873 key_buffer, key_buffer_size,
874 key_buffer_length,
875 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100876 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200877#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800878 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200879#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
880 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
882 if (PSA_KEY_TYPE_IS_RSA(type)) {
883 return mbedtls_psa_rsa_import_key(attributes,
884 data, data_length,
885 key_buffer, key_buffer_size,
886 key_buffer_length,
887 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200888 }
Valerio Settife478902023-07-25 12:27:19 +0200889#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
890 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800891 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100892 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100895}
896
Gilles Peskinef603c712019-01-19 13:40:11 +0100897/** Calculate the intersection of two algorithm usage policies.
898 *
899 * Return 0 (which allows no operation) on incompatibility.
900 */
901static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100902 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100903 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100905{
Gilles Peskine549ea862019-05-22 11:45:59 +0200906 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (alg1 == alg2) {
908 return alg1;
909 }
Steven Cooreman03488022021-02-18 12:07:20 +0100910 /* If the policies are from the same hash-and-sign family, check
911 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
913 PSA_ALG_IS_SIGN_HASH(alg2) &&
914 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
915 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
916 return alg2;
917 }
918 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
919 return alg1;
920 }
Steven Cooreman03488022021-02-18 12:07:20 +0100921 }
922 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100923 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100924 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
926 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
927 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
928 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
929 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100930 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100931
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100932 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
934 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
935 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
936 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100937 }
938 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
940 (alg1_len <= alg2_len)) {
941 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100942 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
944 (alg2_len <= alg1_len)) {
945 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100946 }
947 }
948 /* If the policies are from the same MAC family, check whether one
949 * of them is a minimum-MAC-length policy. Calculate the most
950 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
952 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
953 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100954 /* Validate the combination of key type and algorithm. Since the base
955 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
957 return 0;
958 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100959
Steven Cooreman31a876d2021-03-03 20:47:40 +0100960 /* Get the (exact or at-least) output lengths for both sides of the
961 * requested intersection. None of the currently supported algorithms
962 * have an output length dependent on the actual key size, so setting it
963 * to a bogus value of 0 is currently OK.
964 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100965 * Note that for at-least-this-length wildcard algorithms, the output
966 * length is set to the shortest allowed length, which allows us to
967 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
969 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100970 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100971
Steven Cooremana1d83222021-02-25 10:20:29 +0100972 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
974 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
975 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100976 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100977
978 /* If only one is an at-least-this-length policy, the intersection would
979 * be the other (fixed-length) policy as long as said fixed length is
980 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
982 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100983 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
985 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100987
Steven Cooremancd640932021-03-08 12:00:27 +0100988 /* If none of them are wildcards, check whether they define the same tag
989 * length. This is still possible here when one is default-length and
990 * the other specific-length. Ensure to always return the
991 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (alg1_len == alg2_len) {
993 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
994 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100995 }
996 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100998}
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000static int psa_key_algorithm_permits(psa_key_type_t key_type,
1001 psa_algorithm_t policy_alg,
1002 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +02001003{
Gilles Peskine549ea862019-05-22 11:45:59 +02001004 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (requested_alg == policy_alg) {
1006 return 1;
1007 }
Steven Cooreman03488022021-02-18 12:07:20 +01001008 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
1009 * and requested_alg is the same hash-and-sign family with any hash,
1010 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
1012 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
1013 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
1014 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +01001015 }
1016 /* If policy_alg is a wildcard AEAD algorithm of the same base as
1017 * the requested algorithm, check the requested tag length to be
1018 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (PSA_ALG_IS_AEAD(policy_alg) &&
1020 PSA_ALG_IS_AEAD(requested_alg) &&
1021 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
1022 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
1023 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
1024 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
1025 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +01001026 }
Steven Cooremancd640932021-03-08 12:00:27 +01001027 /* If policy_alg is a MAC algorithm of the same base as the requested
1028 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (PSA_ALG_IS_MAC(policy_alg) &&
1030 PSA_ALG_IS_MAC(requested_alg) &&
1031 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
1032 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01001033 /* Validate the combination of key type and algorithm. Since the policy
1034 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
1036 return 0;
1037 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01001038
Steven Cooreman31a876d2021-03-03 20:47:40 +01001039 /* Get both the requested output length for the algorithm which is to be
1040 * verified, and the default output length for the base algorithm.
1041 * Note that none of the currently supported algorithms have an output
1042 * length dependent on actual key size, so setting it to a bogus value
1043 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01001044 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01001046 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 key_type, 0,
1048 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001049
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001050 /* If the policy is default-length, only allow an algorithm with
1051 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
1053 return requested_output_length == default_output_length;
1054 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001055
1056 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +01001057 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
1059 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
1060 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001061 }
1062
Steven Cooremancd640932021-03-08 12:00:27 +01001063 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
1064 * check for the requested MAC length to be equal to or longer than the
1065 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
1067 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
1068 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001069 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001070 }
Steven Cooremance48e852020-10-05 16:02:45 +02001071 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001072 * a key derivation with that key agreement should also be allowed. This
1073 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
1075 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
1076 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
1077 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +02001078 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001079 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001081}
1082
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001083/** Test whether a policy permits an algorithm.
1084 *
1085 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +01001086 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001087 * \note This function requires providing the key type for which the policy is
1088 * being validated, since some algorithm policy definitions (e.g. MAC)
1089 * have different properties depending on what kind of cipher it is
1090 * combined with.
1091 *
Steven Cooreman7e39f052021-02-23 14:18:32 +01001092 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1093 * allowed by the \p policy.
1094 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1095 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1096 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1099 psa_key_type_t key_type,
1100 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001101{
Steven Cooremand788fab2021-02-25 11:29:17 +01001102 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (alg == 0) {
1104 return PSA_ERROR_INVALID_ARGUMENT;
1105 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001106
Steven Cooreman7e39f052021-02-23 14:18:32 +01001107 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (PSA_ALG_IS_WILDCARD(alg)) {
1109 return PSA_ERROR_INVALID_ARGUMENT;
1110 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1113 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1114 return PSA_SUCCESS;
1115 } else {
1116 return PSA_ERROR_NOT_PERMITTED;
1117 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001118}
1119
Gilles Peskinef603c712019-01-19 13:40:11 +01001120/** Restrict a key policy based on a constraint.
1121 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001122 * \note This function requires providing the key type for which the policy is
1123 * being restricted, since some algorithm policy definitions (e.g. MAC)
1124 * have different properties depending on what kind of cipher it is
1125 * combined with.
1126 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001127 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001128 * \param[in,out] policy The policy to restrict.
1129 * \param[in] constraint The policy constraint to apply.
1130 *
1131 * \retval #PSA_SUCCESS
1132 * \c *policy contains the intersection of the original value of
1133 * \c *policy and \c *constraint.
1134 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001135 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001136 * \c *policy is unchanged.
1137 */
1138static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001139 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001140 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001142{
1143 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1145 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001146 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1148 constraint->alg2);
1149 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1150 return PSA_ERROR_INVALID_ARGUMENT;
1151 }
1152 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1153 return PSA_ERROR_INVALID_ARGUMENT;
1154 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001155 policy->usage &= constraint->usage;
1156 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001157 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001159}
1160
Przemek Stekiel061f6942022-11-30 10:51:35 +01001161/** Get the description of a key given its identifier and policy constraints
1162 * and lock it.
1163 *
1164 * The key must have allow all the usage flags set in \p usage. If \p alg is
1165 * nonzero, the key must allow operations with this algorithm. If \p alg is
1166 * zero, the algorithm is not checked.
1167 *
1168 * In case of a persistent key, the function loads the description of the key
1169 * into a key slot if not already done.
1170 *
1171 * On success, the returned key slot is locked. It is the responsibility of
1172 * the caller to unlock the key slot when it does not access it anymore.
1173 */
1174static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001175 mbedtls_svc_key_id_t key,
1176 psa_key_slot_t **p_slot,
1177 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001179{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001181 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 status = psa_get_and_lock_key_slot(key, p_slot);
1184 if (status != PSA_SUCCESS) {
1185 return status;
1186 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001187 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001188
1189 /* Enforce that usage policy for the key slot contains all the flags
1190 * required by the usage parameter. There is one exception: public
1191 * keys can always be exported, so we treat public key objects as
1192 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001194 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001198 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001199 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001200 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001201
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001202 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (alg != 0) {
1204 status = psa_key_policy_permits(&slot->attr.policy,
1205 slot->attr.type,
1206 alg);
1207 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001208 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001210 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001213
1214error:
1215 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001219}
Darryl Green940d72c2018-07-13 13:18:51 +01001220
Ronald Cron5c522922020-11-14 16:35:34 +01001221/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001222 *
1223 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001224 * available, as opposed to a key in a secure element and/or to be used
1225 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001226 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001227 * This is a temporary function that may be used instead of
1228 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1229 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230 *
Ronald Cron5c522922020-11-14 16:35:34 +01001231 * On success, the returned key slot is locked. It is the responsibility of the
1232 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001233 */
Ronald Cron5c522922020-11-14 16:35:34 +01001234static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1235 mbedtls_svc_key_id_t key,
1236 psa_key_slot_t **p_slot,
1237 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001239{
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1241 usage, alg);
1242 if (status != PSA_SUCCESS) {
1243 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001244 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1247 psa_unlock_key_slot(*p_slot);
1248 *p_slot = NULL;
1249 return PSA_ERROR_NOT_SUPPORTED;
1250 }
1251
1252 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001253}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001256{
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001258 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001260
Ronald Cronea0f8a62020-11-25 17:52:23 +01001261 slot->key.data = NULL;
1262 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001265}
1266
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001267/** Completely wipe a slot in memory, including its policy.
1268 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001270{
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 /*
1274 * As the return error code may not be handled in case of multiple errors,
1275 * do our best to report an unexpected lock counter. Assert with
1276 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1277 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1278 * function is called as part of the execution of a test suite, the
1279 * execution of the test suite is stopped in error if the assertion fails.
1280 */
1281 if (slot->lock_count != 1) {
1282 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001283 status = PSA_ERROR_CORRUPTION_DETECTED;
1284 }
1285
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001286 /* Multipart operations may still be using the key. This is safe
1287 * because all multipart operation objects are independent from
1288 * the key slot: if they need to access the key after the setup
1289 * phase, they have a copy of the key. Note that this means that
1290 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001291 /* At this point, key material and other type-specific content has
1292 * been wiped. Clear remaining metadata. We can call memset and not
1293 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memset(slot, 0, sizeof(*slot));
1295 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001296}
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001300 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 psa_status_t status; /* status of the last operation */
1302 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1304 psa_se_drv_table_entry_t *driver;
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (mbedtls_svc_key_id_is_null(key)) {
1308 return PSA_SUCCESS;
1309 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001310
Ronald Cronf2911112020-10-29 17:51:10 +01001311 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001312 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001313 * key, this will load the key description from persistent memory if not
1314 * done yet. We cannot avoid this loading as without it we don't know if
1315 * the key is operated by an SE or not and this information is needed by
1316 * the current implementation.
1317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 status = psa_get_and_lock_key_slot(key, &slot);
1319 if (status != PSA_SUCCESS) {
1320 return status;
1321 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001322
Ronald Cronf2911112020-10-29 17:51:10 +01001323 /*
1324 * If the key slot containing the key description is under access by the
1325 * library (apart from the present access), the key cannot be destroyed
1326 * yet. For the time being, just return in error. Eventually (to be
1327 * implemented), the key should be destroyed when all accesses have
1328 * stopped.
1329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (slot->lock_count > 1) {
1331 psa_unlock_key_slot(slot);
1332 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001333 }
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001336 /* Refuse the destruction of a read-only key (which may or may not work
1337 * if we attempt it, depending on whether the key is merely read-only
1338 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001339 * Just do the best we can, which is to wipe the copy in memory
1340 * (done in this function's cleanup code). */
1341 overall_status = PSA_ERROR_NOT_PERMITTED;
1342 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001343 }
1344
Gilles Peskine354f7672019-07-12 23:46:38 +02001345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1347 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001348 /* For a key in a secure element, we need to do three things:
1349 * remove the key file in internal storage, destroy the
1350 * key inside the secure element, and update the driver's
1351 * persistent data. Start a transaction that will encompass these
1352 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001354 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001356 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 status = psa_crypto_save_transaction();
1358 if (status != PSA_SUCCESS) {
1359 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001360 /* We should still try to destroy the key in the secure
1361 * element and the key metadata in storage. This is especially
1362 * important if the error is that the storage is full.
1363 * But how to do it exactly without risking an inconsistent
1364 * state after a reset?
1365 * https://github.com/ARMmbed/mbed-crypto/issues/215
1366 */
1367 overall_status = status;
1368 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001369 }
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 status = psa_destroy_se_key(driver,
1372 psa_key_slot_get_slot_number(slot));
1373 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001376 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1378
Darryl Greend49a4992018-06-18 17:27:26 +01001379#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1381 status = psa_destroy_persistent_key(slot->attr.id);
1382 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001383 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001385
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001386 /* TODO: other slots may have a copy of the same key. We should
1387 * invalidate them.
1388 * https://github.com/ARMmbed/mbed-crypto/issues/214
1389 */
Darryl Greend49a4992018-06-18 17:27:26 +01001390 }
1391#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001392
Gilles Peskinefc762652019-07-22 19:30:34 +02001393#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (driver != NULL) {
1395 status = psa_save_se_persistent_data(driver);
1396 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001397 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 }
1399 status = psa_crypto_stop_transaction();
1400 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001401 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001403 }
1404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1405
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001408 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001410 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 }
1412 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001413}
1414
Valerio Settib2bcedb2023-07-10 11:24:00 +02001415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001416 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001417static psa_status_t psa_get_rsa_public_exponent(
1418 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001420{
1421 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001423 uint8_t *buffer = NULL;
1424 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1428 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 }
1431 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001432 /* It's the default value, which is reported as an empty string,
1433 * so there's nothing to do. */
1434 goto exit;
1435 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 buflen = mbedtls_mpi_size(&mpi);
1438 buffer = mbedtls_calloc(1, buflen);
1439 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001440 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1441 goto exit;
1442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1444 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001447 attributes->domain_parameters = buffer;
1448 attributes->domain_parameters_size = buflen;
1449
1450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 mbedtls_mpi_free(&mpi);
1452 if (ret != 0) {
1453 mbedtls_free(buffer);
1454 }
1455 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001456}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001459
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001460/** Retrieve all the publicly-accessible attributes of a key.
1461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1463 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001466 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001467 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1472 if (status != PSA_SUCCESS) {
1473 return status;
1474 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001475
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001476 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1478 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001479
1480#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1482 psa_set_key_slot_number(attributes,
1483 psa_key_slot_get_slot_number(slot));
1484 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001485#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001488#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1489 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001490 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001491 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001492 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001493 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001494 * is not yet implemented.
1495 * https://github.com/ARMmbed/mbed-crypto/issues/216
1496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001498 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001499
Ronald Cron90857082020-11-25 14:58:33 +01001500 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 slot->attr.type,
1502 slot->key.data,
1503 slot->key.bytes,
1504 &rsa);
1505 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001506 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 status = psa_get_rsa_public_exponent(rsa,
1510 attributes);
1511 mbedtls_rsa_free(rsa);
1512 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001513 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001514 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001515#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1516 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001517 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001518 default:
1519 /* Nothing else to do. */
1520 break;
1521 }
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (status != PSA_SUCCESS) {
1524 psa_reset_key_attributes(attributes);
1525 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530}
1531
Gilles Peskinec8000c02019-08-02 20:15:51 +02001532#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1533psa_status_t psa_get_key_slot_number(
1534 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001536{
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001538 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return PSA_SUCCESS;
1540 } else {
1541 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001542 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001543}
1544#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1547 size_t key_buffer_size,
1548 uint8_t *data,
1549 size_t data_size,
1550 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001551{
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (key_buffer_size > data_size) {
1553 return PSA_ERROR_BUFFER_TOO_SMALL;
1554 }
1555 memcpy(data, key_buffer, key_buffer_size);
1556 memset(data + key_buffer_size, 0,
1557 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001558 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001560}
1561
Ronald Cron7285cda2020-11-26 14:46:37 +01001562psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001563 const psa_key_attributes_t *attributes,
1564 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566{
Ronald Crond18b5f82020-11-25 16:46:05 +01001567 psa_key_type_t type = attributes->core.type;
1568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (key_type_is_raw_bytes(type) ||
1570 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001571 PSA_KEY_TYPE_IS_ECC(type) ||
1572 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 return psa_export_key_buffer_internal(
1574 key_buffer, key_buffer_size,
1575 data, data_size, data_length);
1576 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001577 /* This shouldn't happen in the reference implementation, but
1578 it is valid for a special-purpose implementation to omit
1579 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001581 }
1582}
1583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1585 uint8_t *data,
1586 size_t data_size,
1587 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001588{
1589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1590 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1591 psa_key_slot_t *slot;
1592
Ronald Crone907e552021-01-18 13:22:38 +01001593 /* Reject a zero-length output buffer now, since this can never be a
1594 * valid key representation. This way we know that data must be a valid
1595 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (data_size == 0) {
1597 return PSA_ERROR_BUFFER_TOO_SMALL;
1598 }
Ronald Crone907e552021-01-18 13:22:38 +01001599
Ronald Cron9486f9d2020-11-26 13:36:23 +01001600 /* Set the key to empty now, so that even when there are errors, we always
1601 * set data_length to a value between 0 and data_size. On error, setting
1602 * the key to empty is a good choice because an empty key representation is
1603 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001604 *data_length = 0;
1605
Ronald Cron9486f9d2020-11-26 13:36:23 +01001606 /* Export requires the EXPORT flag. There is an exception for public keys,
1607 * which don't require any flag, but
1608 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1611 PSA_KEY_USAGE_EXPORT, 0);
1612 if (status != PSA_SUCCESS) {
1613 return status;
1614 }
mohammad160306e79202018-03-28 13:17:44 +03001615
Ronald Crond18b5f82020-11-25 16:46:05 +01001616 psa_key_attributes_t attributes = {
1617 .core = slot->attr
1618 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 status = psa_driver_wrapper_export_key(&attributes,
1620 slot->key.data, slot->key.bytes,
1621 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001626}
1627
Ronald Cron7285cda2020-11-26 14:46:37 +01001628psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001629 const psa_key_attributes_t *attributes,
1630 const uint8_t *key_buffer,
1631 size_t key_buffer_size,
1632 uint8_t *data,
1633 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001635{
Ronald Crond18b5f82020-11-25 16:46:05 +01001636 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001637
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001638 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001639 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1640 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001641 /* Exporting public -> public */
1642 return psa_export_key_buffer_internal(
1643 key_buffer, key_buffer_size,
1644 data, data_size, data_length);
1645 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001646#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001647 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1648 return mbedtls_psa_rsa_export_public_key(attributes,
1649 key_buffer,
1650 key_buffer_size,
1651 data,
1652 data_size,
1653 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001654#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001655 /* We don't know how to convert a private RSA key to public. */
1656 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001657#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001658 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001659 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001660#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001661 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1662 return mbedtls_psa_ecp_export_public_key(attributes,
1663 key_buffer,
1664 key_buffer_size,
1665 data,
1666 data_size,
1667 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001668#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001669 /* We don't know how to convert a private ECC key to public */
1670 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001673 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001674#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001675 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001676 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001677 key_buffer,
1678 key_buffer_size,
1679 data, data_size,
1680 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001681#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001682 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001683#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001684 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001686 (void) key_buffer;
1687 (void) key_buffer_size;
1688 (void) data;
1689 (void) data_size;
1690 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001692 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693}
Ronald Crond18b5f82020-11-25 16:46:05 +01001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1696 uint8_t *data,
1697 size_t data_size,
1698 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001699{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001701 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001702 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001703 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001704
Ronald Crone907e552021-01-18 13:22:38 +01001705 /* Reject a zero-length output buffer now, since this can never be a
1706 * valid key representation. This way we know that data must be a valid
1707 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (data_size == 0) {
1709 return PSA_ERROR_BUFFER_TOO_SMALL;
1710 }
Ronald Crone907e552021-01-18 13:22:38 +01001711
Darryl Greendd8fb772018-11-07 16:00:44 +00001712 /* Set the key to empty now, so that even when there are errors, we always
1713 * set data_length to a value between 0 and data_size. On error, setting
1714 * the key to empty is a good choice because an empty key representation is
1715 * unlikely to be accepted anywhere. */
1716 *data_length = 0;
1717
1718 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1725 status = PSA_ERROR_INVALID_ARGUMENT;
1726 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001727 }
1728
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001729 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001730 .core = slot->attr
1731 };
Ronald Cron67227982020-11-26 15:16:05 +01001732 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001733 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001735
1736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001740}
1741
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001742MBEDTLS_STATIC_ASSERT(
1743 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1744 "One or more key attribute flag is listed as both external-only and dual-use")
1745MBEDTLS_STATIC_ASSERT(
1746 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1747 "One or more key attribute flag is listed as both internal-only and dual-use")
1748MBEDTLS_STATIC_ASSERT(
1749 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1750 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001751
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001752/** Validate that a key policy is internally well-formed.
1753 *
1754 * This function only rejects invalid policies. It does not validate the
1755 * consistency of the policy with respect to other attributes of the key
1756 * such as the key type.
1757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001759{
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1761 PSA_KEY_USAGE_COPY |
1762 PSA_KEY_USAGE_ENCRYPT |
1763 PSA_KEY_USAGE_DECRYPT |
1764 PSA_KEY_USAGE_SIGN_MESSAGE |
1765 PSA_KEY_USAGE_VERIFY_MESSAGE |
1766 PSA_KEY_USAGE_SIGN_HASH |
1767 PSA_KEY_USAGE_VERIFY_HASH |
1768 PSA_KEY_USAGE_VERIFY_DERIVATION |
1769 PSA_KEY_USAGE_DERIVE)) != 0) {
1770 return PSA_ERROR_INVALID_ARGUMENT;
1771 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001774}
1775
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001776/** Validate the internal consistency of key attributes.
1777 *
1778 * This function only rejects invalid attribute values. If does not
1779 * validate the consistency of the attributes with any key data that may
1780 * be involved in the creation of the key.
1781 *
1782 * Call this function early in the key creation process.
1783 *
1784 * \param[in] attributes Key attributes for the new key.
1785 * \param[out] p_drv On any return, the driver for the key, if any.
1786 * NULL for a transparent key.
1787 *
1788 */
1789static psa_status_t psa_validate_key_attributes(
1790 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001792{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001793 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1795 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 status = psa_validate_key_location(lifetime, p_drv);
1798 if (status != PSA_SUCCESS) {
1799 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001800 }
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 status = psa_validate_key_persistence(lifetime);
1803 if (status != PSA_SUCCESS) {
1804 return status;
1805 }
1806
1807 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1808 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1809 return PSA_ERROR_INVALID_ARGUMENT;
1810 }
1811 } else {
1812 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1813 return PSA_ERROR_INVALID_ARGUMENT;
1814 }
1815 }
1816
1817 status = psa_validate_key_policy(&attributes->core.policy);
1818 if (status != PSA_SUCCESS) {
1819 return status;
1820 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001821
1822 /* Refuse to create overly large keys.
1823 * Note that this doesn't trigger on import if the attributes don't
1824 * explicitly specify a size (so psa_get_key_bits returns 0), so
1825 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1827 return PSA_ERROR_NOT_SUPPORTED;
1828 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001829
Gilles Peskine91e8c332019-08-02 19:19:39 +02001830 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1832 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1833 return PSA_ERROR_INVALID_ARGUMENT;
1834 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001837}
1838
Gilles Peskine4747d192019-04-17 15:05:45 +02001839/** Prepare a key slot to receive key material.
1840 *
1841 * This function allocates a key slot and sets its metadata.
1842 *
1843 * If this function fails, call psa_fail_key_creation().
1844 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001845 * This function is intended to be used as follows:
1846 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001847 * it with the specified attributes, and in case of a volatile key assign it
1848 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001849 * -# Populate the slot with the key material.
1850 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1851 * In case of failure at any step, stop the sequence and call
1852 * psa_fail_key_creation().
1853 *
Ronald Cron5c522922020-11-14 16:35:34 +01001854 * On success, the key slot is locked. It is the responsibility of the caller
1855 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001856 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001857 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001858 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001859 * \param[out] p_slot On success, a pointer to the prepared slot.
1860 * \param[out] p_drv On any return, the driver for the key, if any.
1861 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 *
1863 * \retval #PSA_SUCCESS
1864 * The key slot is ready to receive key material.
1865 * \return If this function fails, the key slot is an invalid state.
1866 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 */
1868static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001869 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001871 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001873{
1874 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001875 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001876 psa_key_slot_t *slot;
1877
Gilles Peskinedf179142019-07-15 22:02:14 +02001878 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 *p_drv = NULL;
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 status = psa_validate_key_attributes(attributes, p_drv);
1882 if (status != PSA_SUCCESS) {
1883 return status;
1884 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1887 if (status != PSA_SUCCESS) {
1888 return status;
1889 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 slot = *p_slot;
1891
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001892 /* We're storing the declared bit-size of the key. It's up to each
1893 * creation mechanism to verify that this information is correct.
1894 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001895 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001896 * is optional (import, copy). In case of a volatile key, assign it the
1897 * volatile key identifier associated to the slot returned to contain its
1898 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001899
1900 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001902#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1903 slot->attr.id = volatile_key_id;
1904#else
1905 slot->attr.id.key_id = volatile_key_id;
1906#endif
1907 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001908
Gilles Peskine91e8c332019-08-02 19:19:39 +02001909 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001910 * external-only flags, query `attributes`. Thanks to the check
1911 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001912 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001913 * may have set. */
1914 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001915
Gilles Peskinecbaff462019-07-12 23:46:04 +02001916#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001917 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001918 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001919 * create the key file in internal storage, create the
1920 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001921 * persistent data. This is done by starting a transaction that will
1922 * encompass these three actions.
1923 * For registering a volatile key, we just need to find an appropriate
1924 * slot number inside the SE. Since the key is designated volatile, creating
1925 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001926 /* The first thing to do is to find a slot number for the new key.
1927 * We save the slot number in persistent storage as part of the
1928 * transaction data. It will be needed to recover if the power
1929 * fails during the key creation process, to clean up on the secure
1930 * element side after restarting. Obtaining a slot number from the
1931 * secure element driver updates its persistent state, but we do not yet
1932 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001933 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001935 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1937 &slot_number);
1938 if (status != PSA_SUCCESS) {
1939 return status;
1940 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1943 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001944 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001945 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001946 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_crypto_save_transaction();
1948 if (status != PSA_SUCCESS) {
1949 (void) psa_crypto_stop_transaction();
1950 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001951 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001952 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001953
1954 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001956 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001959 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001961 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1963
Ryan Everett975d4112023-11-16 13:37:51 +00001964 slot->status = PSA_SLOT_OCCUPIED;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001967}
Gilles Peskine4747d192019-04-17 15:05:45 +02001968
1969/** Finalize the creation of a key once its key material has been set.
1970 *
1971 * This entails writing the key to persistent storage.
1972 *
1973 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001974 * See the documentation of psa_start_key_creation() for the intended use
1975 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001976 *
Ronald Cron5c522922020-11-14 16:35:34 +01001977 * If the finalization succeeds, the function unlocks the key slot (it was
1978 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1979 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001980 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 * \param[in,out] slot Pointer to the slot with key material.
1982 * \param[in] driver The secure element driver for the key,
1983 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001984 * \param[out] key On success, identifier of the key. Note that the
1985 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001986 *
1987 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001988 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001989 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1990 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1991 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1992 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1993 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1994 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001995 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001996 * \return If this function fails, the key slot is an invalid state.
1997 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001998 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001999static psa_status_t psa_finish_key_creation(
2000 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002001 psa_se_drv_table_entry_t *driver,
2002 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002003{
2004 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002005 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002007
2008#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002012 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002013 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01002015
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00002016 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
2017 sizeof(data.slot_number),
2018 "Slot number size does not match psa_se_key_data_storage_t");
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
2021 status = psa_save_persistent_key(&slot->attr,
2022 (uint8_t *) &data,
2023 sizeof(data));
2024 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02002025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2026 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002027 /* Key material is saved in export representation in the slot, so
2028 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 status = psa_save_persistent_key(&slot->attr,
2030 slot->key.data,
2031 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02002032 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002033 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002034#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2035
Gilles Peskinecbaff462019-07-12 23:46:04 +02002036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002037 /* Finish the transaction for a key creation. This does not
2038 * happen when registering an existing key. Detect this case
2039 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002040 * creation of a persistent key in a secure element requires a transaction,
2041 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (driver != NULL &&
2043 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
2044 status = psa_save_se_persistent_data(driver);
2045 if (status != PSA_SUCCESS) {
2046 psa_destroy_persistent_key(slot->attr.id);
2047 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02002048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02002050 }
2051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002054 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 status = psa_unlock_key_slot(slot);
2056 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01002057 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002059 }
Ronald Cron50972942020-11-14 11:28:25 +01002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002062}
2063
2064/** Abort the creation of a key.
2065 *
2066 * You may call this function after calling psa_start_key_creation(),
2067 * or after psa_finish_key_creation() fails. In other circumstances, this
2068 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002069 * See the documentation of psa_start_key_creation() for the intended use
2070 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002071 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002072 * \param[in,out] slot Pointer to the slot with key material.
2073 * \param[in] driver The secure element driver for the key,
2074 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076static void psa_fail_key_creation(psa_key_slot_t *slot,
2077 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002078{
Gilles Peskine011e4282019-06-26 18:34:38 +02002079 (void) driver;
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002084
2085#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002086 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002087 * element, and the failure happened later (when saving metadata
2088 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002089 * element.
2090 * https://github.com/ARMmbed/mbed-crypto/issues/217
2091 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002092
Gilles Peskined7729582019-08-05 15:55:54 +02002093 /* Abort the ongoing transaction if any (there may not be one if
2094 * the creation process failed before starting one, or if the
2095 * key creation is a registration of a key in a secure element).
2096 * Earlier functions must already have done what it takes to undo any
2097 * partial creation. All that's left is to update the transaction data
2098 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002103}
2104
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002105/** Validate optional attributes during key creation.
2106 *
2107 * Some key attributes are optional during key creation. If they are
2108 * specified in the attributes structure, check that they are consistent
2109 * with the data in the slot.
2110 *
2111 * This function should be called near the end of key creation, after
2112 * the slot in memory is fully populated but before saving persistent data.
2113 */
2114static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002117{
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (attributes->core.type != 0) {
2119 if (attributes->core.type != slot->attr.type) {
2120 return PSA_ERROR_INVALID_ARGUMENT;
2121 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002122 }
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002125#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2126 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2128 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002129 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002130 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002132
Ronald Cron90857082020-11-25 14:58:33 +01002133 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 slot->attr.type,
2135 slot->key.data,
2136 slot->key.bytes,
2137 &rsa);
2138 if (status != PSA_SUCCESS) {
2139 return status;
2140 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 mbedtls_mpi_init(&actual);
2143 mbedtls_mpi_init(&required);
2144 ret = mbedtls_rsa_export(rsa,
2145 NULL, NULL, NULL, NULL, &actual);
2146 mbedtls_rsa_free(rsa);
2147 mbedtls_free(rsa);
2148 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 }
2151 ret = mbedtls_mpi_read_binary(&required,
2152 attributes->domain_parameters,
2153 attributes->domain_parameters_size);
2154 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002155 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
2157 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002158 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
2160rsa_exit:
2161 mbedtls_mpi_free(&actual);
2162 mbedtls_mpi_free(&required);
2163 if (ret != 0) {
2164 return mbedtls_to_psa_error(ret);
2165 }
2166 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002167#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2168 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002169 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002170 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002172 }
2173 }
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (attributes->core.bits != 0) {
2176 if (attributes->core.bits != slot->attr.bits) {
2177 return PSA_ERROR_INVALID_ARGUMENT;
2178 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002179 }
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002182}
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2185 const uint8_t *data,
2186 size_t data_length,
2187 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002188{
2189 psa_status_t status;
2190 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002191 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002192 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302193 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002194
Ronald Cron81709fc2020-11-14 12:10:32 +01002195 *key = MBEDTLS_SVC_KEY_ID_INIT;
2196
Gilles Peskine0f84d622019-09-12 19:03:13 +02002197 /* Reject zero-length symmetric keys (including raw data key objects).
2198 * This also rejects any key which might be encoded as an empty string,
2199 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (data_length == 0) {
2201 return PSA_ERROR_INVALID_ARGUMENT;
2202 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002203
Archana449608b2021-09-08 15:36:05 +05302204 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (data_length > SIZE_MAX / 8) {
2206 return PSA_ERROR_NOT_SUPPORTED;
2207 }
Archana6ed4bda2021-08-04 10:47:15 +05302208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2210 &slot, &driver);
2211 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002214
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002215 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302216 * storage ( thus not in the case of importing a key in a secure element
2217 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2218 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (slot->key.data == NULL) {
2220 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302221 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 attributes, data, data_length, &storage_size);
2223 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
Archanad8a83dc2021-06-14 10:04:16 +05302226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 status = psa_allocate_buffer_to_slot(slot, storage_size);
2228 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002231 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002232
2233 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 status = psa_driver_wrapper_import_key(attributes,
2235 data, data_length,
2236 slot->key.data,
2237 slot->key.bytes,
2238 &slot->key.bytes, &bits);
2239 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002244 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002246 status = PSA_ERROR_INVALID_ARGUMENT;
2247 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002248 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002249
Archana6ed4bda2021-08-04 10:47:15 +05302250 /* Enforce a size limit, and in particular ensure that the bit
2251 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302253 status = PSA_ERROR_NOT_SUPPORTED;
2254 goto exit;
2255 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 status = psa_validate_optional_attributes(slot, attributes);
2257 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (status != PSA_SUCCESS) {
2264 psa_fail_key_creation(slot, driver);
2265 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002268}
2269
Gilles Peskined7729582019-08-05 15:55:54 +02002270#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2271psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002273{
2274 psa_status_t status;
2275 psa_key_slot_t *slot = NULL;
2276 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002278
2279 /* Leaving attributes unspecified is not currently supported.
2280 * It could make sense to query the key type and size from the
2281 * secure element, but not all secure elements support this
2282 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2284 return PSA_ERROR_NOT_SUPPORTED;
2285 }
2286 if (psa_get_key_bits(attributes) == 0) {
2287 return PSA_ERROR_NOT_SUPPORTED;
2288 }
Gilles Peskined7729582019-08-05 15:55:54 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2291 &slot, &driver);
2292 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
Gilles Peskined7729582019-08-05 15:55:54 +02002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002297
2298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (status != PSA_SUCCESS) {
2300 psa_fail_key_creation(slot, driver);
2301 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002302
Gilles Peskined7729582019-08-05 15:55:54 +02002303 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 psa_close_key(key);
2305 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002306}
2307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2310 const psa_key_attributes_t *specified_attributes,
2311 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002312{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002314 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002315 psa_key_slot_t *source_slot = NULL;
2316 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002317 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002318 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302319 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002320
Ronald Cron81709fc2020-11-14 12:10:32 +01002321 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2322
Archana8a180362021-07-05 02:18:48 +05302323 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2325 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002326 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 status = psa_validate_optional_attributes(source_slot,
2330 specified_attributes);
2331 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002332 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002334
Archana9d17bf42021-09-10 06:22:44 +05302335 /* The target key type and number of bits have been validated by
2336 * psa_validate_optional_attributes() to be either equal to zero or
2337 * equal to the ones of the source key. So it is safe to inherit
2338 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302339 * */
Archana9d17bf42021-09-10 06:22:44 +05302340 actual_attributes.core.bits = source_slot->attr.bits;
2341 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302342
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 status = psa_restrict_key_policy(source_slot->attr.type,
2345 &actual_attributes.core.policy,
2346 &source_slot->attr.policy);
2347 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002348 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2352 &target_slot, &driver);
2353 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
2356 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2357 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302358 /*
Archana9d17bf42021-09-10 06:22:44 +05302359 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302360 * the source key would need to be exported as plaintext and re-imported
2361 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302362 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302363 * appropriate API invocations from the application, if needed.
2364 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002365 status = PSA_ERROR_NOT_SUPPORTED;
2366 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002367 }
Archana8a180362021-07-05 02:18:48 +05302368 /*
2369 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302370 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302371 * - For opaque keys this translates to an invocation of the drivers'
2372 * copy_key entry point through the dispatch layer.
2373 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2375 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2376 &storage_size);
2377 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302378 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 }
Archana374fe5b2021-09-08 15:50:28 +05302380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2382 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 }
Archana374fe5b2021-09-08 15:50:28 +05302385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 status = psa_driver_wrapper_copy_key(&actual_attributes,
2387 source_slot->key.data,
2388 source_slot->key.bytes,
2389 target_slot->key.data,
2390 target_slot->key.bytes,
2391 &target_slot->key.bytes);
2392 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 }
2395 } else {
2396 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302397 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 source_slot->key.bytes);
2399 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302400 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 }
Archana8a180362021-07-05 02:18:48 +05302402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002404exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (status != PSA_SUCCESS) {
2406 psa_fail_key_creation(target_slot, driver);
2407 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002412}
2413
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002414
2415
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002416/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002417/* Message digests */
2418/****************************************************************/
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002421{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002422 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (operation->id == 0) {
2424 return PSA_SUCCESS;
2425 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002428 operation->id = 0;
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431}
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2434 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002437
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002438 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002440 status = PSA_ERROR_BAD_STATE;
2441 goto exit;
2442 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002445 status = PSA_ERROR_INVALID_ARGUMENT;
2446 goto exit;
2447 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002448
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002449 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2450 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002454
2455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (status != PSA_SUCCESS) {
2457 psa_hash_abort(operation);
2458 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002459
2460 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461}
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2464 const uint8_t *input,
2465 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002466{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002470 status = PSA_ERROR_BAD_STATE;
2471 goto exit;
2472 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002473
Steven Cooremanc8288352021-03-04 14:02:19 +01002474 /* Don't require hash implementations to behave correctly on a
2475 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (input_length == 0) {
2477 return PSA_SUCCESS;
2478 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002481
2482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (status != PSA_SUCCESS) {
2484 psa_hash_abort(operation);
2485 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488}
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2491 uint8_t *hash,
2492 size_t hash_size,
2493 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002495 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (operation->id == 0) {
2497 return PSA_ERROR_BAD_STATE;
2498 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499
Steven Cooreman1e582352021-02-18 17:24:37 +01002500 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 operation, hash, hash_size, hash_length);
2502 psa_hash_abort(operation);
2503 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504}
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2507 const uint8_t *hash,
2508 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509{
Ronald Cron69a63422021-10-18 09:47:58 +02002510 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002512 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 operation,
2514 actual_hash, sizeof(actual_hash),
2515 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002522 status = PSA_ERROR_INVALID_SIGNATURE;
2523 goto exit;
2524 }
2525
Dave Rodgman78701152023-08-29 14:20:18 +01002526 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002527 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002529
2530exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2532 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002533 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002537}
2538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539psa_status_t psa_hash_compute(psa_algorithm_t alg,
2540 const uint8_t *input, size_t input_length,
2541 uint8_t *hash, size_t hash_size,
2542 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002543{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002544 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (!PSA_ALG_IS_HASH(alg)) {
2546 return PSA_ERROR_INVALID_ARGUMENT;
2547 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2550 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551}
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553psa_status_t psa_hash_compare(psa_algorithm_t alg,
2554 const uint8_t *input, size_t input_length,
2555 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002556{
Ronald Cron69a63422021-10-18 09:47:58 +02002557 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002558 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (!PSA_ALG_IS_HASH(alg)) {
2561 return PSA_ERROR_INVALID_ARGUMENT;
2562 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002563
Steven Cooreman1e582352021-02-18 17:24:37 +01002564 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 alg, input, input_length,
2566 actual_hash, sizeof(actual_hash),
2567 &actual_hash_length);
2568 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 }
2571 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002572 status = PSA_ERROR_INVALID_SIGNATURE;
2573 goto exit;
2574 }
Dave Rodgman78701152023-08-29 14:20:18 +01002575 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002576 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002578
2579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2581 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002582}
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2585 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002586{
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (source_operation->id == 0 ||
2588 target_operation->id != 0) {
2589 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002590 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2593 target_operation);
2594 if (status != PSA_SUCCESS) {
2595 psa_hash_abort(target_operation);
2596 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002599}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600
2601
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002602/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603/* MAC */
2604/****************************************************************/
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607{
Steven Cooremane6804192021-03-19 18:28:56 +01002608 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (operation->id == 0) {
2610 return PSA_SUCCESS;
2611 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002614 operation->mac_size = 0;
2615 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002616 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619}
2620
Ronald Cron2dff3b22021-06-17 16:33:22 +02002621static psa_status_t psa_mac_finalize_alg_and_key_validation(
2622 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002623 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 psa_key_type_t key_type = psa_get_key_type(attributes);
2628 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (!PSA_ALG_IS_MAC(alg)) {
2631 return PSA_ERROR_INVALID_ARGUMENT;
2632 }
Steven Cooremane6804192021-03-19 18:28:56 +01002633
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002634 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 status = psa_mac_key_can_do(alg, key_type);
2636 if (status != PSA_SUCCESS) {
2637 return status;
2638 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002639
Steven Cooremand1a68f12021-05-10 11:13:41 +02002640 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002644 /* A very short MAC is too short for security since it can be
2645 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2646 * so we make this our minimum, even though 32 bits is still
2647 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002649 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2652 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002653 /* It's impossible to "truncate" to a larger length than the full length
2654 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002656 }
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002659 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2660 * that is disabled in the compile-time configuration. The result can
2661 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2662 * configuration into account. In this case, force a return of
2663 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2664 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2665 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2666 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2667 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002669 }
2670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002672}
2673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2675 mbedtls_svc_key_id_t key,
2676 psa_algorithm_t alg,
2677 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002678{
2679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002681 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002682 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683
2684 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002686 status = PSA_ERROR_BAD_STATE;
2687 goto exit;
2688 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689
2690 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 key,
2692 &slot,
2693 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2694 alg);
2695 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002699 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700 .core = slot->attr
2701 };
2702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2704 &operation->mac_size);
2705 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708
2709 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002710 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (is_sign) {
2712 status = psa_driver_wrapper_mac_sign_setup(operation,
2713 &attributes,
2714 slot->key.data,
2715 slot->key.bytes,
2716 alg);
2717 } else {
2718 status = psa_driver_wrapper_mac_verify_setup(operation,
2719 &attributes,
2720 slot->key.data,
2721 slot->key.bytes,
2722 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724
Gilles Peskinefbfac682018-07-08 20:51:54 +02002725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (status != PSA_SUCCESS) {
2727 psa_mac_abort(operation);
2728 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733}
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2736 mbedtls_svc_key_id_t key,
2737 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002738{
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002740}
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2743 mbedtls_svc_key_id_t key,
2744 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745{
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002747}
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2750 const uint8_t *input,
2751 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752{
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (operation->id == 0) {
2754 return PSA_ERROR_BAD_STATE;
2755 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002757 /* Don't require hash implementations to behave correctly on a
2758 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (input_length == 0) {
2760 return PSA_SUCCESS;
2761 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2764 input, input_length);
2765 if (status != PSA_SUCCESS) {
2766 psa_mac_abort(operation);
2767 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770}
2771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2773 uint8_t *mac,
2774 size_t mac_size,
2775 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002776{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2778 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002781 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002782 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002783 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002786 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002787 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002788 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002789
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002790 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2791 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002793 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002794 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002795 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002798 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002799 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002800 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 status = psa_driver_wrapper_mac_sign_finish(operation,
2803 mac, operation->mac_size,
2804 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002805
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002806exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002807 /* In case of success, set the potential excess room in the output buffer
2808 * to an invalid value, to avoid potentially leaking a longer MAC.
2809 * In case of error, set the output length and content to a safe default,
2810 * such that in case the caller misses an error check, the output would be
2811 * an unachievable MAC.
2812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002814 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002815 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002816 }
mohammad16036df908f2018-04-02 08:34:15 -07002817
Paul Elliottdc42ca82023-02-24 18:11:59 +00002818 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002823}
2824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2826 const uint8_t *mac,
2827 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2830 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002833 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002834 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002835 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002838 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002839 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002840 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002843 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002844 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002845 }
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 status = psa_driver_wrapper_mac_verify_finish(operation,
2848 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002849
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854}
2855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2857 psa_algorithm_t alg,
2858 const uint8_t *input,
2859 size_t input_length,
2860 uint8_t *mac,
2861 size_t mac_size,
2862 size_t *mac_length,
2863 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864{
2865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002866 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2867 psa_key_slot_t *slot;
2868 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002869 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870
Ronald Cron51131b52021-06-17 17:17:20 +02002871 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 key,
2873 &slot,
2874 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2875 alg);
2876 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002877 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002879
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002880 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002881 .core = slot->attr
2882 };
2883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2885 &operation_mac_size);
2886 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002887 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002891 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002892 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002893 }
2894
2895 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 &attributes,
2897 slot->key.data, slot->key.bytes,
2898 alg,
2899 input, input_length,
2900 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002901
2902exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002903 /* In case of success, set the potential excess room in the output buffer
2904 * to an invalid value, to avoid potentially leaking a longer MAC.
2905 * In case of error, set the output length and content to a safe default,
2906 * such that in case the caller misses an error check, the output would be
2907 * an unachievable MAC.
2908 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002910 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002911 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002912 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002913
2914 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002919}
2920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002922 psa_algorithm_t alg,
2923 const uint8_t *input,
2924 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 uint8_t *mac,
2926 size_t mac_size,
2927 size_t *mac_length)
2928{
2929 return psa_mac_compute_internal(key, alg,
2930 input, input_length,
2931 mac, mac_size, mac_length, 1);
2932}
2933
2934psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2935 psa_algorithm_t alg,
2936 const uint8_t *input,
2937 size_t input_length,
2938 const uint8_t *mac,
2939 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002940{
2941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002942 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2943 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 status = psa_mac_compute_internal(key, alg,
2946 input, input_length,
2947 actual_mac, sizeof(actual_mac),
2948 &actual_mac_length, 0);
2949 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002954 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002955 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002956 }
Dave Rodgman78701152023-08-29 14:20:18 +01002957 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002958 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002959 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002960 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002961
2962exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002966}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002967
Gilles Peskinee59236f2018-01-27 23:32:46 +01002968/****************************************************************/
2969/* Asymmetric cryptography */
2970/****************************************************************/
2971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2973 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974{
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (input_is_message) {
2976 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2977 return PSA_ERROR_INVALID_ARGUMENT;
2978 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2981 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2982 return PSA_ERROR_INVALID_ARGUMENT;
2983 }
2984 }
2985 } else {
2986 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2987 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002988 }
2989 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002992}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2995 int input_is_message,
2996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 uint8_t *signature,
3000 size_t signature_size,
3001 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003002{
3003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3004 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3005 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003006 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003007
3008 *signature_length = 0;
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 status = psa_sign_verify_check_alg(input_is_message, alg);
3011 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003012 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003014
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003015 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02003016 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003017 * buffer can in principle be empty since it doesn't actually have
3018 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (signature_size == 0) {
3020 return PSA_ERROR_BUFFER_TOO_SMALL;
3021 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003022
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003023 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 key, &slot,
3025 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
3026 PSA_KEY_USAGE_SIGN_HASH,
3027 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003034 status = PSA_ERROR_INVALID_ARGUMENT;
3035 goto exit;
3036 }
3037
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003038 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003040 };
3041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003043 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003044 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 signature, signature_size, signature_length);
3047 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003048
3049 status = psa_driver_wrapper_sign_hash(
3050 &attributes, slot->key.data, slot->key.bytes,
3051 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003053 }
3054
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003055
3056exit:
Paul Elliott59200a22023-02-21 15:07:40 +00003057 psa_wipe_tag_output_buffer(signature, status, signature_size,
3058 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003063}
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3066 int input_is_message,
3067 psa_algorithm_t alg,
3068 const uint8_t *input,
3069 size_t input_length,
3070 const uint8_t *signature,
3071 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003072{
3073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3074 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3075 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 status = psa_sign_verify_check_alg(input_is_message, alg);
3078 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003079 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003081
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003082 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 key, &slot,
3084 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3085 PSA_KEY_USAGE_VERIFY_HASH,
3086 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 if (status != PSA_SUCCESS) {
3089 return status;
3090 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003091
3092 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003094 };
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003097 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003098 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003099 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 signature, signature_length);
3101 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003102 status = psa_driver_wrapper_verify_hash(
3103 &attributes, slot->key.data, slot->key.bytes,
3104 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003106 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003111
3112}
3113
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003114psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003115 const psa_key_attributes_t *attributes,
3116 const uint8_t *key_buffer,
3117 size_t key_buffer_size,
3118 psa_algorithm_t alg,
3119 const uint8_t *input,
3120 size_t input_length,
3121 uint8_t *signature,
3122 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003124{
3125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003128 size_t hash_length;
3129 uint8_t hash[PSA_HASH_MAX_SIZE];
3130
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003131 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 PSA_ALG_SIGN_GET_HASH(alg),
3133 input, input_length,
3134 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003137 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003139
3140 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 attributes, key_buffer, key_buffer_size,
3142 alg, hash, hash_length,
3143 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003144 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003147}
3148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3150 psa_algorithm_t alg,
3151 const uint8_t *input,
3152 size_t input_length,
3153 uint8_t *signature,
3154 size_t signature_size,
3155 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003156{
3157 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003158 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003160}
3161
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003162psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003163 const psa_key_attributes_t *attributes,
3164 const uint8_t *key_buffer,
3165 size_t key_buffer_size,
3166 psa_algorithm_t alg,
3167 const uint8_t *input,
3168 size_t input_length,
3169 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003171{
3172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003175 size_t hash_length;
3176 uint8_t hash[PSA_HASH_MAX_SIZE];
3177
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003178 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 PSA_ALG_SIGN_GET_HASH(alg),
3180 input, input_length,
3181 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003184 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003186
3187 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 attributes, key_buffer, key_buffer_size,
3189 alg, hash, hash_length,
3190 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003191 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003194}
3195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3197 psa_algorithm_t alg,
3198 const uint8_t *input,
3199 size_t input_length,
3200 const uint8_t *signature,
3201 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003202{
3203 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003204 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003206}
3207
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003208psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003209 const psa_key_attributes_t *attributes,
3210 const uint8_t *key_buffer, size_t key_buffer_size,
3211 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003213{
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3215 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3216 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003217#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3219 return mbedtls_psa_rsa_sign_hash(
3220 attributes,
3221 key_buffer, key_buffer_size,
3222 alg, hash, hash_length,
3223 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003224#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3225 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 } else {
3227 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3230 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003231#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3233 return mbedtls_psa_ecdsa_sign_hash(
3234 attributes,
3235 key_buffer, key_buffer_size,
3236 alg, hash, hash_length,
3237 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003238#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3239 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 } else {
3241 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003242 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003243 }
Ronald Cron4501c982021-03-19 20:09:32 +01003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 (void) key_buffer;
3246 (void) key_buffer_size;
3247 (void) hash;
3248 (void) hash_length;
3249 (void) signature;
3250 (void) signature_size;
3251 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003254}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3257 psa_algorithm_t alg,
3258 const uint8_t *hash,
3259 size_t hash_length,
3260 uint8_t *signature,
3261 size_t signature_size,
3262 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003263{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003264 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003265 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003267}
3268
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003269psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003270 const psa_key_attributes_t *attributes,
3271 const uint8_t *key_buffer, size_t key_buffer_size,
3272 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003274{
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3276 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3277 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003278#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3280 return mbedtls_psa_rsa_verify_hash(
3281 attributes,
3282 key_buffer, key_buffer_size,
3283 alg, hash, hash_length,
3284 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003285#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3286 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 } else {
3288 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3291 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003292#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3294 return mbedtls_psa_ecdsa_verify_hash(
3295 attributes,
3296 key_buffer, key_buffer_size,
3297 alg, hash, hash_length,
3298 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003299#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3300 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 } else {
3302 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003303 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003304 }
Ronald Cron4501c982021-03-19 20:09:32 +01003305
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 (void) key_buffer;
3307 (void) key_buffer_size;
3308 (void) hash;
3309 (void) hash_length;
3310 (void) signature;
3311 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003314}
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3317 psa_algorithm_t alg,
3318 const uint8_t *hash,
3319 size_t hash_length,
3320 const uint8_t *signature,
3321 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003322{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003323 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003324 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003326}
3327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3329 psa_algorithm_t alg,
3330 const uint8_t *input,
3331 size_t input_length,
3332 const uint8_t *salt,
3333 size_t salt_length,
3334 uint8_t *output,
3335 size_t output_size,
3336 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003339 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003340 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003341 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003342
Darryl Green5cc689a2018-07-24 15:34:10 +01003343 (void) input;
3344 (void) input_length;
3345 (void) salt;
3346 (void) output;
3347 (void) output_size;
3348
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003349 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3352 return PSA_ERROR_INVALID_ARGUMENT;
3353 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003354
Ronald Cron5c522922020-11-14 16:35:34 +01003355 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3357 if (status != PSA_SUCCESS) {
3358 return status;
3359 }
3360 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3361 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362 status = PSA_ERROR_INVALID_ARGUMENT;
3363 goto exit;
3364 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003366 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003368 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003369
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003370 status = psa_driver_wrapper_asymmetric_encrypt(
3371 &attributes, slot->key.data, slot->key.bytes,
3372 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003374exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003378}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3381 psa_algorithm_t alg,
3382 const uint8_t *input,
3383 size_t input_length,
3384 const uint8_t *salt,
3385 size_t salt_length,
3386 uint8_t *output,
3387 size_t output_size,
3388 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003389{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003391 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003392 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003393 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003394
Darryl Green5cc689a2018-07-24 15:34:10 +01003395 (void) input;
3396 (void) input_length;
3397 (void) salt;
3398 (void) output;
3399 (void) output_size;
3400
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003401 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3404 return PSA_ERROR_INVALID_ARGUMENT;
3405 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003406
Ronald Cron5c522922020-11-14 16:35:34 +01003407 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3409 if (status != PSA_SUCCESS) {
3410 return status;
3411 }
3412 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003413 status = PSA_ERROR_INVALID_ARGUMENT;
3414 goto exit;
3415 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003416
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003417 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003419 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003420
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003421 status = psa_driver_wrapper_asymmetric_decrypt(
3422 &attributes, slot->key.data, slot->key.bytes,
3423 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003425
3426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003430}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003431
Paul Elliott9fe12f62022-11-30 19:16:02 +00003432/****************************************************************/
3433/* Asymmetric interruptible cryptography */
3434/****************************************************************/
3435
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003436static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3437
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438void psa_interruptible_set_max_ops(uint32_t max_ops)
3439{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003440 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441}
3442
3443uint32_t psa_interruptible_get_max_ops(void)
3444{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003445 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446}
3447
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448uint32_t psa_sign_hash_get_num_ops(
3449 const psa_sign_hash_interruptible_operation_t *operation)
3450{
Paul Elliott296ede92022-12-15 17:00:30 +00003451 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452}
3453
3454uint32_t psa_verify_hash_get_num_ops(
3455 const psa_verify_hash_interruptible_operation_t *operation)
3456{
Paul Elliott296ede92022-12-15 17:00:30 +00003457 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458}
3459
Paul Elliott59ad9452022-12-18 15:09:02 +00003460static psa_status_t psa_sign_hash_abort_internal(
3461 psa_sign_hash_interruptible_operation_t *operation)
3462{
3463 if (operation->id == 0) {
3464 /* The object has (apparently) been initialized but it is not (yet)
3465 * in use. It's ok to call abort on such an object, and there's
3466 * nothing to do. */
3467 return PSA_SUCCESS;
3468 }
3469
3470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3471
3472 status = psa_driver_wrapper_sign_hash_abort(operation);
3473
3474 operation->id = 0;
3475
Paul Elliotte6145dc2023-02-07 12:51:21 +00003476 /* Do not clear either the error_occurred or num_ops elements here as they
3477 * only want to be cleared by the application calling abort, not by abort
3478 * being called at completion of an operation. */
3479
Paul Elliott59ad9452022-12-18 15:09:02 +00003480 return status;
3481}
3482
Paul Elliott9fe12f62022-11-30 19:16:02 +00003483psa_status_t psa_sign_hash_start(
3484 psa_sign_hash_interruptible_operation_t *operation,
3485 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3486 const uint8_t *hash, size_t hash_length)
3487{
3488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3489 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3490 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003491 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003492
Paul Elliottc9774412023-02-06 15:14:07 +00003493 /* Check that start has not been previously called, or operation has not
3494 * previously errored. */
3495 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496 return PSA_ERROR_BAD_STATE;
3497 }
3498
Paul Elliott9fe12f62022-11-30 19:16:02 +00003499 status = psa_sign_verify_check_alg(0, alg);
3500 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003501 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003502 return status;
3503 }
3504
3505 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3506 PSA_KEY_USAGE_SIGN_HASH,
3507 alg);
3508
3509 if (status != PSA_SUCCESS) {
3510 goto exit;
3511 }
3512
3513 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3514 status = PSA_ERROR_INVALID_ARGUMENT;
3515 goto exit;
3516 }
3517
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003518 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519 .core = slot->attr
3520 };
3521
Paul Elliott296ede92022-12-15 17:00:30 +00003522 /* Ensure ops count gets reset, in case of operation re-use. */
3523 operation->num_ops = 0;
3524
Paul Elliott9fe12f62022-11-30 19:16:02 +00003525 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3526 slot->key.data,
3527 slot->key.bytes, alg,
3528 hash, hash_length);
3529exit:
3530
3531 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003532 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003533 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003534 }
3535
3536 unlock_status = psa_unlock_key_slot(slot);
3537
Paul Elliottc9774412023-02-06 15:14:07 +00003538 if (unlock_status != PSA_SUCCESS) {
3539 operation->error_occurred = 1;
3540 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003541
Paul Elliottc9774412023-02-06 15:14:07 +00003542 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003543}
3544
3545
3546psa_status_t psa_sign_hash_complete(
3547 psa_sign_hash_interruptible_operation_t *operation,
3548 uint8_t *signature, size_t signature_size,
3549 size_t *signature_length)
3550{
3551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3552
3553 *signature_length = 0;
3554
Paul Elliottc9774412023-02-06 15:14:07 +00003555 /* Check that start has been called first, and that operation has not
3556 * previously errored. */
3557 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558 status = PSA_ERROR_BAD_STATE;
3559 goto exit;
3560 }
3561
Paul Elliott096abc42023-02-03 18:33:23 +00003562 /* Immediately reject a zero-length signature buffer. This guarantees that
3563 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003564 if (signature_size == 0) {
3565 status = PSA_ERROR_BUFFER_TOO_SMALL;
3566 goto exit;
3567 }
3568
3569 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3570 signature_size,
3571 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003572
Paul Elliott296ede92022-12-15 17:00:30 +00003573 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003574 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003575
Paul Elliottebe225c2023-02-10 14:32:53 +00003576exit:
3577
Paul Elliott59200a22023-02-21 15:07:40 +00003578 psa_wipe_tag_output_buffer(signature, status, signature_size,
3579 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003580
Paul Elliott53bb3122023-02-10 14:22:22 +00003581 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003582 if (status != PSA_SUCCESS) {
3583 operation->error_occurred = 1;
3584 }
3585
Paul Elliott59ad9452022-12-18 15:09:02 +00003586 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587 }
3588
3589 return status;
3590}
3591
3592psa_status_t psa_sign_hash_abort(
3593 psa_sign_hash_interruptible_operation_t *operation)
3594{
Paul Elliott59ad9452022-12-18 15:09:02 +00003595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3596
3597 status = psa_sign_hash_abort_internal(operation);
3598
3599 /* We clear the number of ops done here, so that it is not cleared when
3600 * the operation fails or succeeds, only on manual abort. */
3601 operation->num_ops = 0;
3602
Paul Elliottc9774412023-02-06 15:14:07 +00003603 /* Likewise, failure state. */
3604 operation->error_occurred = 0;
3605
Paul Elliott59ad9452022-12-18 15:09:02 +00003606 return status;
3607}
3608
3609static psa_status_t psa_verify_hash_abort_internal(
3610 psa_verify_hash_interruptible_operation_t *operation)
3611{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003612 if (operation->id == 0) {
3613 /* The object has (apparently) been initialized but it is not (yet)
3614 * in use. It's ok to call abort on such an object, and there's
3615 * nothing to do. */
3616 return PSA_SUCCESS;
3617 }
3618
Paul Elliott59ad9452022-12-18 15:09:02 +00003619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3620
3621 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622
3623 operation->id = 0;
3624
Paul Elliotte6145dc2023-02-07 12:51:21 +00003625 /* Do not clear either the error_occurred or num_ops elements here as they
3626 * only want to be cleared by the application calling abort, not by abort
3627 * being called at completion of an operation. */
3628
Paul Elliott59ad9452022-12-18 15:09:02 +00003629 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003630}
3631
3632psa_status_t psa_verify_hash_start(
3633 psa_verify_hash_interruptible_operation_t *operation,
3634 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3635 const uint8_t *hash, size_t hash_length,
3636 const uint8_t *signature, size_t signature_length)
3637{
3638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3639 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3640 psa_key_slot_t *slot;
3641
Paul Elliottc9774412023-02-06 15:14:07 +00003642 /* Check that start has not been previously called, or operation has not
3643 * previously errored. */
3644 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003645 return PSA_ERROR_BAD_STATE;
3646 }
3647
3648 status = psa_sign_verify_check_alg(0, alg);
3649 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003650 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003651 return status;
3652 }
3653
3654 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3655 PSA_KEY_USAGE_VERIFY_HASH,
3656 alg);
3657
3658 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003659 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003660 return status;
3661 }
3662
3663 psa_key_attributes_t attributes = {
3664 .core = slot->attr
3665 };
3666
Paul Elliott296ede92022-12-15 17:00:30 +00003667 /* Ensure ops count gets reset, in case of operation re-use. */
3668 operation->num_ops = 0;
3669
Paul Elliott9fe12f62022-11-30 19:16:02 +00003670 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3671 slot->key.data,
3672 slot->key.bytes,
3673 alg, hash, hash_length,
3674 signature, signature_length);
3675
3676 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003677 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003678 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003679 }
3680
3681 unlock_status = psa_unlock_key_slot(slot);
3682
Paul Elliottc9774412023-02-06 15:14:07 +00003683 if (unlock_status != PSA_SUCCESS) {
3684 operation->error_occurred = 1;
3685 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003686
Paul Elliottc9774412023-02-06 15:14:07 +00003687 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003688}
3689
3690psa_status_t psa_verify_hash_complete(
3691 psa_verify_hash_interruptible_operation_t *operation)
3692{
3693 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3694
Paul Elliottc9774412023-02-06 15:14:07 +00003695 /* Check that start has been called first, and that operation has not
3696 * previously errored. */
3697 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003698 status = PSA_ERROR_BAD_STATE;
3699 goto exit;
3700 }
3701
3702 status = psa_driver_wrapper_verify_hash_complete(operation);
3703
Paul Elliott296ede92022-12-15 17:00:30 +00003704 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003705 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003706 operation);
3707
Paul Elliottebe225c2023-02-10 14:32:53 +00003708exit:
3709
Paul Elliott9fe12f62022-11-30 19:16:02 +00003710 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003711 if (status != PSA_SUCCESS) {
3712 operation->error_occurred = 1;
3713 }
3714
Paul Elliott59ad9452022-12-18 15:09:02 +00003715 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003716 }
3717
3718 return status;
3719}
3720
3721psa_status_t psa_verify_hash_abort(
3722 psa_verify_hash_interruptible_operation_t *operation)
3723{
Paul Elliott59ad9452022-12-18 15:09:02 +00003724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003725
Paul Elliott59ad9452022-12-18 15:09:02 +00003726 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003727
Paul Elliott59ad9452022-12-18 15:09:02 +00003728 /* We clear the number of ops done here, so that it is not cleared when
3729 * the operation fails or succeeds, only on manual abort. */
3730 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003731
Paul Elliottc9774412023-02-06 15:14:07 +00003732 /* Likewise, failure state. */
3733 operation->error_occurred = 0;
3734
Paul Elliott59ad9452022-12-18 15:09:02 +00003735 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003736}
3737
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738/****************************************************************/
3739/* Asymmetric interruptible cryptography internal */
3740/* implementations */
3741/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003742
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3744{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003745
Paul Elliott588f8ed2022-12-02 18:10:26 +00003746#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3747 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3748 defined(MBEDTLS_ECP_RESTARTABLE)
3749
3750 /* Internal implementation uses zero to indicate infinite number max ops,
3751 * therefore avoid this value, and set to minimum possible. */
3752 if (max_ops == 0) {
3753 max_ops = 1;
3754 }
3755
Paul Elliott588f8ed2022-12-02 18:10:26 +00003756 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003757#else
3758 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3760 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3761 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3762}
3763
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003765 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003766{
3767#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3768 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3769 defined(MBEDTLS_ECP_RESTARTABLE)
3770
Paul Elliott93d9ca82023-02-15 18:14:21 +00003771 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772#else
3773 (void) operation;
3774 return 0;
3775#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3776 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3777 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3778}
3779
3780uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003781 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782{
3783 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3784 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3785 defined(MBEDTLS_ECP_RESTARTABLE)
3786
Paul Elliott93d9ca82023-02-15 18:14:21 +00003787 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788#else
3789 (void) operation;
3790 return 0;
3791#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3792 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3793 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3794}
3795
3796psa_status_t mbedtls_psa_sign_hash_start(
3797 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3798 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3799 size_t key_buffer_size, psa_algorithm_t alg,
3800 const uint8_t *hash, size_t hash_length)
3801{
3802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003803 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804
Paul Elliott068fe072023-01-16 13:59:15 +00003805 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3806 return PSA_ERROR_NOT_SUPPORTED;
3807 }
3808
3809 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003810 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003811 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
3813#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003814 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3815 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816
Paul Elliotta1c94092023-02-15 16:38:04 +00003817 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3818
Paul Elliott93d9ca82023-02-15 18:14:21 +00003819 /* Ensure num_ops is zero'ed in case of context re-use. */
3820 operation->num_ops = 0;
3821
Paul Elliott068fe072023-01-16 13:59:15 +00003822 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3823 attributes->core.bits,
3824 key_buffer,
3825 key_buffer_size,
3826 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827
Paul Elliott068fe072023-01-16 13:59:15 +00003828 if (status != PSA_SUCCESS) {
3829 return status;
3830 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831
Paul Elliott1bc59df2023-02-05 13:41:57 +00003832 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003833 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834
Paul Elliott068fe072023-01-16 13:59:15 +00003835 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003836 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003837 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838
Paul Elliott0290a762023-02-09 14:30:24 +00003839 /* We only need to store the same length of hash as the private key size
3840 * here, it would be truncated by the internal implementation anyway. */
3841 required_hash_length = (hash_length < operation->coordinate_bytes ?
3842 hash_length : operation->coordinate_bytes);
3843
Paul Elliottba70ad42023-02-15 18:23:53 +00003844 if (required_hash_length > sizeof(operation->hash)) {
3845 /* Shouldn't happen, but better safe than sorry. */
3846 return PSA_ERROR_CORRUPTION_DETECTED;
3847 }
3848
Paul Elliott0290a762023-02-09 14:30:24 +00003849 memcpy(operation->hash, hash, required_hash_length);
3850 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003851
3852 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
3854#else
Paul Elliott068fe072023-01-16 13:59:15 +00003855 (void) operation;
3856 (void) key_buffer;
3857 (void) key_buffer_size;
3858 (void) alg;
3859 (void) hash;
3860 (void) hash_length;
3861 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003862 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863
Paul Elliott068fe072023-01-16 13:59:15 +00003864 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3866 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3867 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003868}
3869
3870psa_status_t mbedtls_psa_sign_hash_complete(
3871 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3872 uint8_t *signature, size_t signature_size,
3873 size_t *signature_length)
3874{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3876 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3877 defined(MBEDTLS_ECP_RESTARTABLE)
3878
Paul Elliotta1c94092023-02-15 16:38:04 +00003879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003880 mbedtls_mpi r;
3881 mbedtls_mpi s;
3882
Paul Elliott46845252023-02-03 14:59:11 +00003883 mbedtls_mpi_init(&r);
3884 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003886 /* Ensure max_ops is set to the current value (or default). */
3887 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3888
Paul Elliotta1c94092023-02-15 16:38:04 +00003889 if (signature_size < 2 * operation->coordinate_bytes) {
3890 status = PSA_ERROR_BUFFER_TOO_SMALL;
3891 goto exit;
3892 }
3893
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003895
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3897 status = mbedtls_to_psa_error(
3898 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003899 &r,
3900 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901 &operation->ctx->d,
3902 operation->hash,
3903 operation->hash_length,
3904 operation->md_alg,
3905 mbedtls_psa_get_random,
3906 MBEDTLS_PSA_RANDOM_STATE,
3907 &operation->restart_ctx));
3908#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003909 status = PSA_ERROR_NOT_SUPPORTED;
3910 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003911#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3912 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003913 status = mbedtls_to_psa_error(
3914 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003915 &r,
3916 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003917 &operation->ctx->d,
3918 operation->hash,
3919 operation->hash_length,
3920 mbedtls_psa_get_random,
3921 MBEDTLS_PSA_RANDOM_STATE,
3922 mbedtls_psa_get_random,
3923 MBEDTLS_PSA_RANDOM_STATE,
3924 &operation->restart_ctx));
3925 }
3926
Paul Elliottf8e5b562023-02-19 18:43:45 +00003927 /* Hide the fact that the restart context only holds a delta of number of
3928 * ops done during the last operation, not an absolute value. */
3929 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3930
Paul Elliott724bd252023-02-08 12:35:08 +00003931 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003933 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003935 operation->coordinate_bytes)
3936 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
3938 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003939 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940 }
3941
3942 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003943 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003945 operation->coordinate_bytes,
3946 operation->coordinate_bytes)
3947 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
3949 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003950 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951 }
3952
Paul Elliott1bc59df2023-02-05 13:41:57 +00003953 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954
Paul Elliott724bd252023-02-08 12:35:08 +00003955 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003956 }
Paul Elliott724bd252023-02-08 12:35:08 +00003957
3958exit:
3959
3960 mbedtls_mpi_free(&r);
3961 mbedtls_mpi_free(&s);
3962 return status;
3963
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964 #else
3965
3966 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967 (void) signature;
3968 (void) signature_size;
3969 (void) signature_length;
3970
3971 return PSA_ERROR_NOT_SUPPORTED;
3972
3973#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3974 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3975 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3976}
3977
3978psa_status_t mbedtls_psa_sign_hash_abort(
3979 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3980{
3981
3982#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3983 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3984 defined(MBEDTLS_ECP_RESTARTABLE)
3985
3986 if (operation->ctx) {
3987 mbedtls_ecdsa_free(operation->ctx);
3988 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003989 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003990 }
3991
3992 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3993
Paul Elliott93d9ca82023-02-15 18:14:21 +00003994 operation->num_ops = 0;
3995
Paul Elliott588f8ed2022-12-02 18:10:26 +00003996 return PSA_SUCCESS;
3997
3998#else
3999
4000 (void) operation;
4001
4002 return PSA_ERROR_NOT_SUPPORTED;
4003
4004#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4005 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4006 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4007}
4008
4009psa_status_t mbedtls_psa_verify_hash_start(
4010 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
4011 const psa_key_attributes_t *attributes,
4012 const uint8_t *key_buffer, size_t key_buffer_size,
4013 psa_algorithm_t alg,
4014 const uint8_t *hash, size_t hash_length,
4015 const uint8_t *signature, size_t signature_length)
4016{
4017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00004018 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00004019 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004020
Paul Elliott068fe072023-01-16 13:59:15 +00004021 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
4022 return PSA_ERROR_NOT_SUPPORTED;
4023 }
4024
4025 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00004026 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00004027 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028
4029#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00004030 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4031 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00004032
Paul Elliotta1c94092023-02-15 16:38:04 +00004033 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
4034 mbedtls_mpi_init(&operation->r);
4035 mbedtls_mpi_init(&operation->s);
4036
Paul Elliott93d9ca82023-02-15 18:14:21 +00004037 /* Ensure num_ops is zero'ed in case of context re-use. */
4038 operation->num_ops = 0;
4039
Paul Elliott068fe072023-01-16 13:59:15 +00004040 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
4041 attributes->core.bits,
4042 key_buffer,
4043 key_buffer_size,
4044 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004045
Paul Elliott068fe072023-01-16 13:59:15 +00004046 if (status != PSA_SUCCESS) {
4047 return status;
4048 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004049
Paul Elliottc569fc22023-02-10 13:02:54 +00004050 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004051
Paul Elliott1bc59df2023-02-05 13:41:57 +00004052 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00004053 return PSA_ERROR_INVALID_SIGNATURE;
4054 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055
Paul Elliott068fe072023-01-16 13:59:15 +00004056 status = mbedtls_to_psa_error(
4057 mbedtls_mpi_read_binary(&operation->r,
4058 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00004059 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004060
Paul Elliott068fe072023-01-16 13:59:15 +00004061 if (status != PSA_SUCCESS) {
4062 return status;
4063 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004064
Paul Elliott068fe072023-01-16 13:59:15 +00004065 status = mbedtls_to_psa_error(
4066 mbedtls_mpi_read_binary(&operation->s,
4067 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004068 coordinate_bytes,
4069 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004070
Paul Elliott068fe072023-01-16 13:59:15 +00004071 if (status != PSA_SUCCESS) {
4072 return status;
4073 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004074
Paul Elliott2c9843f2023-02-15 17:32:42 +00004075 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004076
Paul Elliott2c9843f2023-02-15 17:32:42 +00004077 if (status != PSA_SUCCESS) {
4078 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004079 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004080
Paul Elliott0290a762023-02-09 14:30:24 +00004081 /* We only need to store the same length of hash as the private key size
4082 * here, it would be truncated by the internal implementation anyway. */
4083 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4084 coordinate_bytes);
4085
Paul Elliottba70ad42023-02-15 18:23:53 +00004086 if (required_hash_length > sizeof(operation->hash)) {
4087 /* Shouldn't happen, but better safe than sorry. */
4088 return PSA_ERROR_CORRUPTION_DETECTED;
4089 }
4090
Paul Elliott0290a762023-02-09 14:30:24 +00004091 memcpy(operation->hash, hash, required_hash_length);
4092 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004093
4094 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004095#else
Paul Elliott068fe072023-01-16 13:59:15 +00004096 (void) operation;
4097 (void) key_buffer;
4098 (void) key_buffer_size;
4099 (void) alg;
4100 (void) hash;
4101 (void) hash_length;
4102 (void) signature;
4103 (void) signature_length;
4104 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004105 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004106 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004107
Paul Elliott068fe072023-01-16 13:59:15 +00004108 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004109#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4110 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4111 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004112}
4113
4114psa_status_t mbedtls_psa_verify_hash_complete(
4115 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4116{
4117
4118#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4119 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4120 defined(MBEDTLS_ECP_RESTARTABLE)
4121
Paul Elliottf8e5b562023-02-19 18:43:45 +00004122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4123
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004124 /* Ensure max_ops is set to the current value (or default). */
4125 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4126
Paul Elliottf8e5b562023-02-19 18:43:45 +00004127 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004128 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4129 operation->hash,
4130 operation->hash_length,
4131 &operation->ctx->Q,
4132 &operation->r,
4133 &operation->s,
4134 &operation->restart_ctx));
4135
Paul Elliottf8e5b562023-02-19 18:43:45 +00004136 /* Hide the fact that the restart context only holds a delta of number of
4137 * ops done during the last operation, not an absolute value. */
4138 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4139
4140 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004141#else
4142 (void) operation;
4143
4144 return PSA_ERROR_NOT_SUPPORTED;
4145
4146#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4147 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4148 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4149}
4150
4151psa_status_t mbedtls_psa_verify_hash_abort(
4152 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4153{
4154
4155#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4156 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4157 defined(MBEDTLS_ECP_RESTARTABLE)
4158
4159 if (operation->ctx) {
4160 mbedtls_ecdsa_free(operation->ctx);
4161 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004162 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004163 }
4164
4165 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4166
Paul Elliott93d9ca82023-02-15 18:14:21 +00004167 operation->num_ops = 0;
4168
Paul Elliott588f8ed2022-12-02 18:10:26 +00004169 mbedtls_mpi_free(&operation->r);
4170 mbedtls_mpi_free(&operation->s);
4171
4172 return PSA_SUCCESS;
4173
4174#else
4175 (void) operation;
4176
4177 return PSA_ERROR_NOT_SUPPORTED;
4178
4179#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4180 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4181 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4182}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004183
mohammad1603503973b2018-03-12 15:59:30 +02004184/****************************************************************/
4185/* Symmetric cryptography */
4186/****************************************************************/
4187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4189 mbedtls_svc_key_id_t key,
4190 psa_algorithm_t alg,
4191 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004192{
4193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4194 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004195 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4197 PSA_KEY_USAGE_ENCRYPT :
4198 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004199 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004200
4201 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004203 status = PSA_ERROR_BAD_STATE;
4204 goto exit;
4205 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004208 status = PSA_ERROR_INVALID_ARGUMENT;
4209 goto exit;
4210 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4213 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004216
Ronald Cron49fafa92021-03-10 08:34:23 +01004217 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004218 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004219 * so we only set it (in the driver wrapper) after resources have been
4220 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004221 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004223 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004225 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 }
4227 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004228
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004229 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004231 };
4232
Ronald Cronab99ac22020-10-01 16:38:28 +02004233 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (cipher_operation == MBEDTLS_ENCRYPT) {
4235 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4236 &attributes,
4237 slot->key.data,
4238 slot->key.bytes,
4239 alg);
4240 } else {
4241 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4242 &attributes,
4243 slot->key.data,
4244 slot->key.bytes,
4245 alg);
4246 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004247
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 if (status != PSA_SUCCESS) {
4250 psa_cipher_abort(operation);
4251 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004256}
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258psa_status_t psa_cipher_encrypt_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_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004263}
4264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4266 mbedtls_svc_key_id_t key,
4267 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004268{
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004270}
4271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4273 uint8_t *iv,
4274 size_t iv_size,
4275 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004276{
Ronald Cron6d051732020-10-01 14:10:20 +02004277 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004278 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004279 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004282 status = PSA_ERROR_BAD_STATE;
4283 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004284 }
4285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004287 status = PSA_ERROR_BAD_STATE;
4288 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004289 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004290
Ronald Cron2fb90522021-07-05 12:31:44 +02004291 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004293 status = PSA_ERROR_BUFFER_TOO_SMALL;
4294 goto exit;
4295 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004298 status = PSA_ERROR_GENERIC_ERROR;
4299 goto exit;
4300 }
4301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 status = psa_generate_random(local_iv, default_iv_length);
4303 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 }
Ronald Cron5618a392021-03-26 09:52:26 +01004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_driver_wrapper_cipher_set_iv(operation,
4308 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004309
4310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 if (status == PSA_SUCCESS) {
4312 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004313 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004314 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004316 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004318 }
Ronald Cron6d051732020-10-01 14:10:20 +02004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004321}
4322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4324 const uint8_t *iv,
4325 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004326{
Ronald Cron6d051732020-10-01 14:10:20 +02004327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004328
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004330 status = PSA_ERROR_BAD_STATE;
4331 goto exit;
4332 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004335 status = PSA_ERROR_BAD_STATE;
4336 goto exit;
4337 }
Ronald Crona0d68172021-03-26 10:15:08 +01004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004340 status = PSA_ERROR_INVALID_ARGUMENT;
4341 goto exit;
4342 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 status = psa_driver_wrapper_cipher_set_iv(operation,
4345 iv,
4346 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004347
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004348exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004350 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 } else {
4352 psa_cipher_abort(operation);
4353 }
4354 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004355}
4356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4358 const uint8_t *input,
4359 size_t input_length,
4360 uint8_t *output,
4361 size_t output_size,
4362 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004363{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004367 status = PSA_ERROR_BAD_STATE;
4368 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004369 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004372 status = PSA_ERROR_BAD_STATE;
4373 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004374 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 status = psa_driver_wrapper_cipher_update(operation,
4377 input,
4378 input_length,
4379 output,
4380 output_size,
4381 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004382
4383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (status != PSA_SUCCESS) {
4385 psa_cipher_abort(operation);
4386 }
Ronald Cron6d051732020-10-01 14:10:20 +02004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004389}
4390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4392 uint8_t *output,
4393 size_t output_size,
4394 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004395{
David Saadab4ecc272019-02-14 13:48:10 +02004396 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004399 status = PSA_ERROR_BAD_STATE;
4400 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004401 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004404 status = PSA_ERROR_BAD_STATE;
4405 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004406 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 status = psa_driver_wrapper_cipher_finish(operation,
4409 output,
4410 output_size,
4411 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004412
4413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (status == PSA_SUCCESS) {
4415 return psa_cipher_abort(operation);
4416 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004417 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004421 }
mohammad1603503973b2018-03-12 15:59:30 +02004422}
4423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004425{
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004427 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004428 * in use. It's ok to call abort on such an object, and there's
4429 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004431 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004434
Ronald Cron49fafa92021-03-10 08:34:23 +01004435 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004436 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004437 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004440}
4441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4443 psa_algorithm_t alg,
David Horstmann36df4b22023-12-13 15:55:25 +00004444 const uint8_t *input_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 size_t input_length,
David Horstmann36df4b22023-12-13 15:55:25 +00004446 uint8_t *output_external,
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 size_t output_size,
4448 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004449{
4450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004451 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004452 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004453 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4454 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004455 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004456
David Horstmann62a56d92023-12-14 18:12:47 +00004457 LOCAL_INPUT_DECLARE(input_external, input);
4458 LOCAL_OUTPUT_DECLARE(output_external, output);
4459
David Horstmann36df4b22023-12-13 15:55:25 +00004460 LOCAL_INPUT_ALLOC(input_external, input_length, input);
David Horstmann36df4b22023-12-13 15:55:25 +00004461 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004464 status = PSA_ERROR_INVALID_ARGUMENT;
4465 goto exit;
4466 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4469 PSA_KEY_USAGE_ENCRYPT,
4470 alg);
4471 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004474
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004475 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004477 };
4478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4480 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004481 status = PSA_ERROR_GENERIC_ERROR;
4482 goto exit;
4483 }
4484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 if (default_iv_length > 0) {
4486 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004487 status = PSA_ERROR_BUFFER_TOO_SMALL;
4488 goto exit;
4489 }
4490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 status = psa_generate_random(local_iv, default_iv_length);
4492 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004493 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004495 }
4496
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004497 status = psa_driver_wrapper_cipher_encrypt(
4498 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004499 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004500 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004502
4503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 unlock_status = psa_unlock_key_slot(slot);
4505 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004506 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004507 }
Ronald Cron23919522021-07-08 19:00:07 +02004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (status == PSA_SUCCESS) {
4510 if (default_iv_length > 0) {
4511 memcpy(output, local_iv, default_iv_length);
4512 }
4513 *output_length += default_iv_length;
4514 } else {
4515 *output_length = 0;
4516 }
4517
David Horstmann62a56d92023-12-14 18:12:47 +00004518 LOCAL_INPUT_FREE(input_external, input);
4519 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannc6977b42023-11-27 17:43:05 +00004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004522}
4523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4525 psa_algorithm_t alg,
4526 const uint8_t *input,
4527 size_t input_length,
4528 uint8_t *output,
4529 size_t output_size,
4530 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004531{
4532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004533 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004534 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004535 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004538 status = PSA_ERROR_INVALID_ARGUMENT;
4539 goto exit;
4540 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4543 PSA_KEY_USAGE_DECRYPT,
4544 alg);
4545 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004546 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004548
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004549 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004551 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4554 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004555 status = PSA_ERROR_INVALID_ARGUMENT;
4556 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004558 status = PSA_ERROR_INVALID_ARGUMENT;
4559 goto exit;
4560 }
4561
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004562 status = psa_driver_wrapper_cipher_decrypt(
4563 &attributes, slot->key.data, slot->key.bytes,
4564 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004566
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004567exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 unlock_status = psa_unlock_key_slot(slot);
4569 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004570 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004574 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 }
Ronald Cron23919522021-07-08 19:00:07 +02004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004578}
4579
4580
mohammad16035955c982018-04-26 00:53:03 +03004581/****************************************************************/
4582/* AEAD */
4583/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004584
Paul Elliottbaff51c2021-09-28 17:44:45 +01004585/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004587{
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004589}
4590
4591/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004592static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4593 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004594{
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004598#if defined(PSA_WANT_ALG_GCM)
4599 case PSA_ALG_GCM:
4600 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 * arbitrarily large nonces. Please note that we do not generally
4602 * recommend the usage of nonces of greater length than
4603 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4604 * size, which can then lead to collisions if you encrypt a very
4605 * large number of messages.*/
4606 if (nonce_length != 0) {
4607 return PSA_SUCCESS;
4608 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004609 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004610#endif /* PSA_WANT_ALG_GCM */
4611#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004612 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 if (nonce_length >= 7 && nonce_length <= 13) {
4614 return PSA_SUCCESS;
4615 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004616 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004617#endif /* PSA_WANT_ALG_CCM */
4618#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004619 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if (nonce_length == 12) {
4621 return PSA_SUCCESS;
4622 } else if (nonce_length == 8) {
4623 return PSA_ERROR_NOT_SUPPORTED;
4624 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004625 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004626#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004627 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004628 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004630 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004633}
4634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004636{
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4638 return PSA_ERROR_INVALID_ARGUMENT;
4639 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004642}
4643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4645 psa_algorithm_t alg,
4646 const uint8_t *nonce,
4647 size_t nonce_length,
4648 const uint8_t *additional_data,
4649 size_t additional_data_length,
4650 const uint8_t *plaintext,
4651 size_t plaintext_length,
4652 uint8_t *ciphertext,
4653 size_t ciphertext_size,
4654 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004655{
Ronald Cron215633c2021-03-16 17:15:37 +01004656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4657 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004658
mohammad1603f08a5502018-06-03 15:05:47 +03004659 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 status = psa_aead_check_algorithm(alg);
4662 if (status != PSA_SUCCESS) {
4663 return status;
4664 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004665
Ronald Cron9a986162021-03-26 12:40:07 +01004666 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4668 if (status != PSA_SUCCESS) {
4669 return status;
4670 }
mohammad16035955c982018-04-26 00:53:03 +03004671
Ronald Cron215633c2021-03-16 17:15:37 +01004672 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004674 };
mohammad16035955c982018-04-26 00:53:03 +03004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 status = psa_aead_check_nonce_length(alg, nonce_length);
4677 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004680
Ronald Cronde822812021-03-17 16:08:20 +01004681 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004682 &attributes, slot->key.data, slot->key.bytes,
4683 alg,
4684 nonce, nonce_length,
4685 additional_data, additional_data_length,
4686 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4690 memset(ciphertext, 0, ciphertext_size);
4691 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004692
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004697}
4698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4700 psa_algorithm_t alg,
4701 const uint8_t *nonce,
4702 size_t nonce_length,
4703 const uint8_t *additional_data,
4704 size_t additional_data_length,
4705 const uint8_t *ciphertext,
4706 size_t ciphertext_length,
4707 uint8_t *plaintext,
4708 size_t plaintext_size,
4709 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004710{
Ronald Cron215633c2021-03-16 17:15:37 +01004711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4712 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004713
Gilles Peskineee652a32018-06-01 19:23:52 +02004714 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 status = psa_aead_check_algorithm(alg);
4717 if (status != PSA_SUCCESS) {
4718 return status;
4719 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004720
Ronald Cron9a986162021-03-26 12:40:07 +01004721 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4723 if (status != PSA_SUCCESS) {
4724 return status;
4725 }
mohammad16035955c982018-04-26 00:53:03 +03004726
Ronald Cron215633c2021-03-16 17:15:37 +01004727 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004729 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 status = psa_aead_check_nonce_length(alg, nonce_length);
4732 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004735
Ronald Cronde822812021-03-17 16:08:20 +01004736 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004737 &attributes, slot->key.data, slot->key.bytes,
4738 alg,
4739 nonce, nonce_length,
4740 additional_data, additional_data_length,
4741 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (status != PSA_SUCCESS && plaintext_size != 0) {
4745 memset(plaintext, 0, plaintext_size);
4746 }
mohammad160360a64d02018-06-03 17:20:42 +03004747
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 return status;
mohammad16035955c982018-04-26 00:53:03 +03004752}
4753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4755{
4756 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004759#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004761 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4763 return PSA_ERROR_INVALID_ARGUMENT;
4764 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004765 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004766#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004767
Przemek Stekiel86679c72022-10-06 17:06:56 +02004768#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004770 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4772 return PSA_ERROR_INVALID_ARGUMENT;
4773 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004774 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004775#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004776
Przemek Stekiel86679c72022-10-06 17:06:56 +02004777#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004779 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (tag_len != 16) {
4781 return PSA_ERROR_INVALID_ARGUMENT;
4782 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004783 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004784#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004785
4786 default:
4787 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004789 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004791}
4792
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004793/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004794static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4795 int is_encrypt,
4796 mbedtls_svc_key_id_t key,
4797 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004798{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4801 psa_key_slot_t *slot = NULL;
4802 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004803 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 status = psa_aead_check_algorithm(alg);
4806 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004811 status = PSA_ERROR_BAD_STATE;
4812 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004813 }
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (operation->nonce_set || operation->lengths_set ||
4816 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004817 status = PSA_ERROR_BAD_STATE;
4818 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004819 }
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004822 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004824 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4828 alg);
4829 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004832
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004833 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004834 .core = slot->attr
4835 };
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004838 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (is_encrypt) {
4842 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4843 &attributes,
4844 slot->key.data,
4845 slot->key.bytes,
4846 alg);
4847 } else {
4848 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4849 &attributes,
4850 slot->key.data,
4851 slot->key.bytes,
4852 alg);
4853 }
4854 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004855 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004859
4860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004864 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004866 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 } else {
4868 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004869 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004872}
4873
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004875psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4876 mbedtls_svc_key_id_t key,
4877 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878{
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880}
4881
4882/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004883psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4884 mbedtls_svc_key_id_t key,
4885 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004886{
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004888}
4889
4890/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004891psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4892 uint8_t *nonce,
4893 size_t nonce_size,
4894 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004895{
4896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004897 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004898 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004900 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004903 status = PSA_ERROR_BAD_STATE;
4904 goto exit;
4905 }
4906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004908 status = PSA_ERROR_BAD_STATE;
4909 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004910 }
4911
Paul Elliotte193ea82021-10-01 13:00:16 +01004912 /* For CCM, this size may not be correct according to the PSA
4913 * specification. The PSA Crypto 1.0.1 specification states:
4914 *
4915 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4916 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4917 *
4918 * However this restriction that L has to be the smallest integer is not
4919 * applied in practice, and it is not implementable here since the
4920 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4922 operation->alg);
4923 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004924 status = PSA_ERROR_BUFFER_TOO_SMALL;
4925 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004926 }
4927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 status = psa_generate_random(local_nonce, required_nonce_size);
4929 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934
Paul Elliott39dc6b82021-05-11 19:16:09 +01004935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (status == PSA_SUCCESS) {
4937 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004938 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 } else {
4940 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004941 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004944}
4945
4946/* Set the nonce for a multipart authenticated encryption or decryption
4947 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004948psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4949 const uint8_t *nonce,
4950 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004951{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004955 status = PSA_ERROR_BAD_STATE;
4956 goto exit;
4957 }
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960 status = PSA_ERROR_BAD_STATE;
4961 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004962 }
4963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4965 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004966 status = PSA_ERROR_INVALID_ARGUMENT;
4967 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004968 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4971 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004972
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004975 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 } else {
4977 psa_aead_abort(operation);
4978 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981}
4982
4983/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004984psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4985 size_t ad_length,
4986 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004988 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004991 status = PSA_ERROR_BAD_STATE;
4992 goto exit;
4993 }
4994
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (operation->lengths_set || operation->ad_started ||
4996 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004997 status = PSA_ERROR_BAD_STATE;
4998 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004999 }
5000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005002#if defined(PSA_WANT_ALG_GCM)
5003 case PSA_ALG_GCM:
5004 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 * bits. Without the guard this code will generate warnings on 32bit
5006 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01005007#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (((uint64_t) ad_length) >> 61 != 0 ||
5009 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005010 status = PSA_ERROR_INVALID_ARGUMENT;
5011 goto exit;
5012 }
Paul Elliott325d3742021-09-27 17:56:28 +01005013#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005014 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005015#endif /* PSA_WANT_ALG_GCM */
5016#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005017 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005019 status = PSA_ERROR_INVALID_ARGUMENT;
5020 goto exit;
5021 }
5022 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005023#endif /* PSA_WANT_ALG_CCM */
5024#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005025 case PSA_ALG_CHACHA20_POLY1305:
5026 /* No length restrictions for ChaChaPoly. */
5027 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01005028#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02005029 default:
5030 break;
5031 }
Paul Elliott325d3742021-09-27 17:56:28 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
5034 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005035
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005038 operation->ad_remaining = ad_length;
5039 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01005040 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 } else {
5042 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01005043 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005044
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005046}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01005047
5048/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005049psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
5050 const uint8_t *input,
5051 size_t input_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->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005061 status = PSA_ERROR_BAD_STATE;
5062 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063 }
5064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (operation->lengths_set) {
5066 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005067 status = PSA_ERROR_INVALID_ARGUMENT;
5068 goto exit;
5069 }
5070
5071 operation->ad_remaining -= input_length;
5072 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005073#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005075 status = PSA_ERROR_BAD_STATE;
5076 goto exit;
5077 }
5078#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 status = psa_driver_wrapper_aead_update_ad(operation, input,
5081 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005082
Paul Elliott39dc6b82021-05-11 19:16:09 +01005083exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005085 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 } else {
5087 psa_aead_abort(operation);
5088 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005091}
5092
5093/* Encrypt or decrypt a message fragment in an active multipart AEAD
5094 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005095psa_status_t psa_aead_update(psa_aead_operation_t *operation,
5096 const uint8_t *input,
5097 size_t input_length,
5098 uint8_t *output,
5099 size_t output_size,
5100 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005103
5104 *output_length = 0;
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005107 status = PSA_ERROR_BAD_STATE;
5108 goto exit;
5109 }
5110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005112 status = PSA_ERROR_BAD_STATE;
5113 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005114 }
5115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005117 /* Additional data length was supplied, but not all the additional
5118 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005120 status = PSA_ERROR_INVALID_ARGUMENT;
5121 goto exit;
5122 }
5123
5124 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005126 status = PSA_ERROR_INVALID_ARGUMENT;
5127 goto exit;
5128 }
5129
5130 operation->body_remaining -= input_length;
5131 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005132#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005134 status = PSA_ERROR_BAD_STATE;
5135 goto exit;
5136 }
5137#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5140 output, output_size,
5141 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005142
Paul Elliott39dc6b82021-05-11 19:16:09 +01005143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005145 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 } else {
5147 psa_aead_abort(operation);
5148 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151}
5152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005154{
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (operation->id == 0 || !operation->nonce_set) {
5156 return PSA_ERROR_BAD_STATE;
5157 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5160 operation->body_remaining != 0)) {
5161 return PSA_ERROR_INVALID_ARGUMENT;
5162 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005165}
5166
Paul Elliott302ff6b2021-04-20 18:10:30 +01005167/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5169 uint8_t *ciphertext,
5170 size_t ciphertext_size,
5171 size_t *ciphertext_length,
5172 uint8_t *tag,
5173 size_t tag_size,
5174 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005175{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5177
Paul Elliott302ff6b2021-04-20 18:10:30 +01005178 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005179 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 status = psa_aead_final_checks(operation);
5182 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005187 status = PSA_ERROR_BAD_STATE;
5188 goto exit;
5189 }
5190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5192 ciphertext_size,
5193 ciphertext_length,
5194 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005195
Paul Elliott39dc6b82021-05-11 19:16:09 +01005196exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005197
5198
Paul Elliottf47b0952021-05-21 18:02:33 +01005199 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005200 * the zero tag size, make sure the tag is set to something implausible.
5201 * Even if the operation succeeds, make sure we clear the rest of the
5202 * buffer to prevent potential leakage of anything previously placed in
5203 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005204 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005209}
5210
5211/* Finish authenticating and decrypting a message in a multipart AEAD
5212 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005213psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5214 uint8_t *plaintext,
5215 size_t plaintext_size,
5216 size_t *plaintext_length,
5217 const uint8_t *tag,
5218 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005219{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5221
Paul Elliott302ff6b2021-04-20 18:10:30 +01005222 *plaintext_length = 0;
5223
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 status = psa_aead_final_checks(operation);
5225 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005230 status = PSA_ERROR_BAD_STATE;
5231 goto exit;
5232 }
5233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5235 plaintext_size,
5236 plaintext_length,
5237 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005238
5239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005243}
5244
5245/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005247{
5248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005251 /* The object has (apparently) been initialized but it is not (yet)
5252 * in use. It's ok to call abort on such an object, and there's
5253 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005255 }
5256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005262}
5263
Gilles Peskinee59236f2018-01-27 23:32:46 +01005264/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265/* Generators */
5266/****************************************************************/
5267
Przemek Stekiel69c46792022-06-10 12:59:51 +02005268#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005269 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305271 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305272 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005273#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005274#endif /* At least one builtin KDF */
5275
Przemek Stekiel69c46792022-06-10 12:59:51 +02005276#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005277 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5278 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5279static psa_status_t psa_key_derivation_start_hmac(
5280 psa_mac_operation_t *operation,
5281 psa_algorithm_t hash_alg,
5282 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005284{
5285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5288 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5289 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005290
Steven Cooreman72f736a2021-05-07 14:14:37 +02005291 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 status = psa_driver_wrapper_mac_sign_setup(operation,
5295 &attributes,
5296 hmac_key, hmac_key_length,
5297 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 psa_reset_key_attributes(&attributes);
5300 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005301}
5302#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005303
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304#define HKDF_STATE_INIT 0 /* no input yet */
5305#define HKDF_STATE_STARTED 1 /* got salt */
5306#define HKDF_STATE_KEYED 2 /* got key */
5307#define HKDF_STATE_OUTPUT 3 /* output started */
5308
Gilles Peskinecbe66502019-05-16 16:59:18 +02005309static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005311{
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5313 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5314 } else {
5315 return operation->alg;
5316 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005317}
5318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005320{
5321 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5323 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005324 /* The object has (apparently) been initialized but it is not
5325 * in use. It's ok to call abort on such an object, and there's
5326 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005328#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5330 mbedtls_free(operation->ctx.hkdf.info);
5331 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5332 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005333#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005334#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5335 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5337 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5338 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5339 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005340 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005342 }
5343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005345 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005347 }
5348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005350 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005352 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005353#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005355 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005357 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005358#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005359 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005360
5361 /* We leave the fields Ai and output_block to be erased safely by the
5362 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005364#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5365 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005366#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5368 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5369 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5370 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005371#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305372#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305373 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305374 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005375 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305376 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305377 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305378
5379 status = PSA_SUCCESS;
5380 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305381#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005382 {
5383 status = PSA_ERROR_BAD_STATE;
5384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 mbedtls_platform_zeroize(operation, sizeof(*operation));
5386 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005387}
5388
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005391{
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005395 }
5396
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005399}
5400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5402 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005403{
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if (operation->alg == 0) {
5405 return PSA_ERROR_BAD_STATE;
5406 }
5407 if (capacity > operation->capacity) {
5408 return PSA_ERROR_INVALID_ARGUMENT;
5409 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005412}
5413
Przemek Stekiel69c46792022-06-10 12:59:51 +02005414#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005415/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005416static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5417 psa_algorithm_t kdf_alg,
5418 uint8_t *output,
5419 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005420{
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5422 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005423 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005424 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005425#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005427#else
5428 const uint8_t last_block = 0xff;
5429#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if (hkdf->state < HKDF_STATE_KEYED ||
5432 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005433#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005435#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 )) {
5437 return PSA_ERROR_BAD_STATE;
5438 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005439 hkdf->state = HKDF_STATE_OUTPUT;
5440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005442 /* Copy what remains of the current block */
5443 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005445 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 }
5447 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005448 output += n;
5449 output_length -= n;
5450 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005452 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005454 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005455 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005457 * object was corrupted or if this function is called directly
5458 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 if (hkdf->block_number == last_block) {
5460 return PSA_ERROR_BAD_STATE;
5461 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005462
5463 /* We need a new block */
5464 ++hkdf->block_number;
5465 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5468 hash_alg,
5469 hkdf->prk,
5470 hash_length);
5471 if (status != PSA_SUCCESS) {
5472 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005473 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005474
5475 if (hkdf->block_number != 1) {
5476 status = psa_mac_update(&hkdf->hmac,
5477 hkdf->output_block,
5478 hash_length);
5479 if (status != PSA_SUCCESS) {
5480 return status;
5481 }
5482 }
5483 status = psa_mac_update(&hkdf->hmac,
5484 hkdf->info,
5485 hkdf->info_length);
5486 if (status != PSA_SUCCESS) {
5487 return status;
5488 }
5489 status = psa_mac_update(&hkdf->hmac,
5490 &hkdf->block_number, 1);
5491 if (status != PSA_SUCCESS) {
5492 return status;
5493 }
5494 status = psa_mac_sign_finish(&hkdf->hmac,
5495 hkdf->output_block,
5496 sizeof(hkdf->output_block),
5497 &hmac_output_length);
5498 if (status != PSA_SUCCESS) {
5499 return status;
5500 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005501 }
5502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005504}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005505#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005506
John Durkop07cc04a2020-11-16 22:08:34 -08005507#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5508 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005509static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5510 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005512{
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5514 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005515 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5516 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005517 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005518
Janos Follath7742fee2019-06-17 12:58:10 +01005519 /* We can't be wanting more output after block 0xff, otherwise
5520 * the capacity check in psa_key_derivation_output_bytes() would have
5521 * prevented this call. It could happen only if the operation
5522 * object was corrupted or if this function is called directly
5523 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 if (tls12_prf->block_number == 0xff) {
5525 return PSA_ERROR_CORRUPTION_DETECTED;
5526 }
Janos Follath7742fee2019-06-17 12:58:10 +01005527
5528 /* We need a new block */
5529 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005530 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005531
5532 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5533 *
5534 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5535 *
5536 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5537 * HMAC_hash(secret, A(2) + seed) +
5538 * HMAC_hash(secret, A(3) + seed) + ...
5539 *
5540 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005541 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005542 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005543 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005544 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005545 * is currently extracted as `output_block` and where i is
5546 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005547 */
5548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 status = psa_key_derivation_start_hmac(&hmac,
5550 hash_alg,
5551 tls12_prf->secret,
5552 tls12_prf->secret_length);
5553 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005554 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005556
5557 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005559 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5560 * the variable seed and in this instance means it in the context of the
5561 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 status = psa_mac_update(&hmac,
5563 tls12_prf->label,
5564 tls12_prf->label_length);
5565 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005566 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 }
5568 status = psa_mac_update(&hmac,
5569 tls12_prf->seed,
5570 tls12_prf->seed_length);
5571 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005572 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 }
5574 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005575 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5577 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005578 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005580 }
5581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 status = psa_mac_sign_finish(&hmac,
5583 tls12_prf->Ai, hash_length,
5584 &hmac_output_length);
5585 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005586 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 }
5588 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005589 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005591
5592 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 status = psa_key_derivation_start_hmac(&hmac,
5594 hash_alg,
5595 tls12_prf->secret,
5596 tls12_prf->secret_length);
5597 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005598 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 }
5600 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5601 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005602 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 }
5604 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5605 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005606 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 }
5608 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5609 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005610 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 }
5612 status = psa_mac_sign_finish(&hmac,
5613 tls12_prf->output_block, hash_length,
5614 &hmac_output_length);
5615 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005616 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005618
Janos Follath7742fee2019-06-17 12:58:10 +01005619
5620cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 cleanup_status = psa_mac_abort(&hmac);
5622 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005623 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005627}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005628
Janos Follath844eb0e2019-06-19 12:10:49 +01005629static psa_status_t psa_key_derivation_tls12_prf_read(
5630 psa_tls12_prf_key_derivation_t *tls12_prf,
5631 psa_algorithm_t alg,
5632 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005634{
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5636 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005637 psa_status_t status;
5638 uint8_t offset, length;
5639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005641 case PSA_TLS12_PRF_STATE_LABEL_SET:
5642 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5643 break;
5644 case PSA_TLS12_PRF_STATE_OUTPUT:
5645 break;
5646 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005648 }
5649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005651 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 if (tls12_prf->left_in_block == 0) {
5653 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5654 alg);
5655 if (status != PSA_SUCCESS) {
5656 return status;
5657 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005658
5659 continue;
5660 }
5661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005663 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005665 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005667
5668 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005670 output += length;
5671 output_length -= length;
5672 tls12_prf->left_in_block -= length;
5673 }
5674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005676}
John Durkop07cc04a2020-11-16 22:08:34 -08005677#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5678 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005679
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005680#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5681static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5682 psa_tls12_ecjpake_to_pms_t *ecjpake,
5683 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005685{
5686 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005687 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 if (output_length != 32) {
5690 return PSA_ERROR_INVALID_ARGUMENT;
5691 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5694 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5695 &output_size);
5696 if (status != PSA_SUCCESS) {
5697 return status;
5698 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 if (output_size != output_length) {
5701 return PSA_ERROR_GENERIC_ERROR;
5702 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005705}
5706#endif
5707
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305708#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305709static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5710 psa_pbkdf2_key_derivation_t *pbkdf2,
5711 psa_algorithm_t prf_alg,
5712 uint8_t prf_output_length,
5713 psa_key_attributes_t *attributes)
5714{
5715 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305716 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305717 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305718 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305719 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305720 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305721 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305722
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305723 mac_operation.is_sign = 1;
5724 mac_operation.mac_size = prf_output_length;
5725 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305726
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305727 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5728 attributes,
5729 pbkdf2->password,
5730 pbkdf2->password_length,
5731 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305732 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305733 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305734 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305735 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5736 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305737 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305738 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305739 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305740 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305741 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305742 }
5743 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5744 &mac_output_length);
5745 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305746 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305747 }
5748
5749 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305750 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305751 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305752 }
5753
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305754 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305755
5756 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305757 /* We are passing prf_output_length as mac_size because the driver
5758 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305759 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305760 status = psa_driver_wrapper_mac_compute(attributes,
5761 pbkdf2->password,
5762 pbkdf2->password_length,
5763 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305764 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305765 &mac_output_length);
5766 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305767 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305768 }
5769
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305770 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305771 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305772
5773cleanup:
5774 /* Zeroise buffers to clear sensitive data from memory. */
5775 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5776 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305777}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305778
5779static psa_status_t psa_key_derivation_pbkdf2_read(
5780 psa_pbkdf2_key_derivation_t *pbkdf2,
5781 psa_algorithm_t kdf_alg,
5782 uint8_t *output,
5783 size_t output_length)
5784{
5785 psa_status_t status;
5786 psa_algorithm_t prf_alg;
5787 uint8_t prf_output_length;
5788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5789 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5790 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5791
5792 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5793 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5794 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5795 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305796 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5797 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305798 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305799 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305800 } else {
5801 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305802 }
5803
5804 switch (pbkdf2->state) {
5805 case PSA_PBKDF2_STATE_PASSWORD_SET:
5806 /* Initially we need a new block so bytes_used is equal to block size*/
5807 pbkdf2->bytes_used = prf_output_length;
5808 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5809 break;
5810 case PSA_PBKDF2_STATE_OUTPUT:
5811 break;
5812 default:
5813 return PSA_ERROR_BAD_STATE;
5814 }
5815
5816 while (output_length != 0) {
5817 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5818 if (n > output_length) {
5819 n = (uint8_t) output_length;
5820 }
5821 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5822 output += n;
5823 output_length -= n;
5824 pbkdf2->bytes_used += n;
5825
5826 if (output_length == 0) {
5827 break;
5828 }
5829
5830 /* We need a new block */
5831 pbkdf2->bytes_used = 0;
5832 pbkdf2->block_number++;
5833
5834 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5835 prf_output_length,
5836 &attributes);
5837 if (status != PSA_SUCCESS) {
5838 return status;
5839 }
5840 }
5841
5842 return PSA_SUCCESS;
5843}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305844#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305845
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005846psa_status_t psa_key_derivation_output_bytes(
5847 psa_key_derivation_operation_t *operation,
5848 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005850{
5851 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005855 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005857 }
5858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005860 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005861 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005862 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005863 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005864 goto exit;
5865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005867 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005868 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005869 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5870 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005872 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005874 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005876
Przemek Stekiel69c46792022-06-10 12:59:51 +02005877#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5879 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5880 output, output_length);
5881 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005882#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005883#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5884 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5886 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5887 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5888 kdf_alg, output,
5889 output_length);
5890 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005891#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5892 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005893#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005895 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5897 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005898#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305899#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305900 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305901 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5902 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305903 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305904#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005905
Gilles Peskineeab56e42018-07-12 17:12:33 +02005906 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005907 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005909 }
5910
5911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005913 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005914 * This allows us to differentiate between exhausted operations and
5915 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5916 * operations. */
5917 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005921 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005923}
5924
David Brown7807bf72021-02-09 16:28:23 -07005925#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005926static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005927{
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (data_size >= 8) {
5929 mbedtls_des_key_set_parity(data);
5930 }
5931 if (data_size >= 16) {
5932 mbedtls_des_key_set_parity(data + 8);
5933 }
5934 if (data_size >= 24) {
5935 mbedtls_des_key_set_parity(data + 16);
5936 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005937}
David Brown7807bf72021-02-09 16:28:23 -07005938#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005939
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005940/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 * ECC keys on a Weierstrass elliptic curve require the generation
5942 * of a private key which is an integer
5943 * in the range [1, N - 1], where N is the boundary of the private key domain:
5944 * N is the prime p for Diffie-Hellman, or the order of the
5945 * curve’s base point for ECC.
5946 *
5947 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5948 * This function generates the private key using the following process:
5949 *
5950 * 1. Draw a byte string of length ceiling(m/8) bytes.
5951 * 2. If m is not a multiple of 8, set the most significant
5952 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5953 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5954 * 4. If k > N - 2, discard the result and return to step 1.
5955 * 5. Output k + 1 as the private key.
5956 *
5957 * This method allows compliance to NIST standards, specifically the methods titled
5958 * Key-Pair Generation by Testing Candidates in the following publications:
5959 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5960 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5961 * Diffie-Hellman keys.
5962 *
5963 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5964 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5965 *
5966 * Note: Function allocates memory for *data buffer, so given *data should be
5967 * always NULL.
5968 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005969#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5970#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005971static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005972 psa_key_slot_t *slot,
5973 size_t bits,
5974 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005975 uint8_t **data
5976 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005977{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005978 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005979 mbedtls_mpi k;
5980 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005983 size_t m;
5984 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 mbedtls_mpi_init(&k);
5987 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005988
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005989 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005991 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005995 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005996 goto cleanup;
5997 }
5998
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005999 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006003
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006004 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006005 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006006 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006007
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006008 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006009
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006010 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006012
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006013 /* Note: This function is always called with *data == NULL and it
6014 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 *data = mbedtls_calloc(1, m_bytes);
6016 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006017 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006018 goto cleanup;
6019 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006022 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006024 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006026
6027 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006029 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006030 * (8 * ceiling(m/8) - m) bits of the first byte in
6031 * the string to zero.
6032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006034 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006035 }
6036
6037 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 * big-endian byte string.
6039 */
6040 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006041
6042 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 * Result of comparison is returned. When it indicates error
6044 * then this function is called again.
6045 */
6046 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006047 }
6048
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006049 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
6051 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006052cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (ret != 0) {
6054 status = mbedtls_to_psa_error(ret);
6055 }
6056 if (status != PSA_SUCCESS) {
6057 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006058 *data = NULL;
6059 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 mbedtls_mpi_free(&k);
6061 mbedtls_mpi_free(&diff_N_2);
6062 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006063}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006064
6065/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
6066 * is determined by the curve, and sets the mandatory bits accordingly. That is:
6067 *
6068 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6069 * draw a 32-byte string and process it as specified in
6070 * Elliptic Curves for Security [RFC7748] §5.
6071 *
6072 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6073 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6074 *
6075 * Note: Function allocates memory for *data buffer, so given *data should be
6076 * always NULL.
6077 */
6078
6079static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6080 size_t bits,
6081 psa_key_derivation_operation_t *operation,
6082 uint8_t **data
6083 )
6084{
6085 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006089 case 255:
6090 output_length = 32;
6091 break;
6092 case 448:
6093 output_length = 56;
6094 break;
6095 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006097 break;
6098 }
6099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 if (*data == NULL) {
6103 return PSA_ERROR_INSUFFICIENT_MEMORY;
6104 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006109 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006113 case 255:
6114 (*data)[0] &= 248;
6115 (*data)[31] &= 127;
6116 (*data)[31] |= 64;
6117 break;
6118 case 448:
6119 (*data)[0] &= 252;
6120 (*data)[55] |= 128;
6121 break;
6122 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006124 break;
6125 }
6126
6127 return status;
6128}
Valerio Setti86587ab2023-06-27 13:09:30 +02006129#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6130static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6131 psa_key_slot_t *slot, size_t bits,
6132 psa_key_derivation_operation_t *operation, uint8_t **data)
6133{
6134 (void) slot;
6135 (void) bits;
6136 (void) operation;
6137 (void) data;
6138 return PSA_ERROR_NOT_SUPPORTED;
6139}
6140
6141static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6142 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6143{
6144 (void) bits;
6145 (void) operation;
6146 (void) data;
6147 return PSA_ERROR_NOT_SUPPORTED;
6148}
6149#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6150#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006151
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006152static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006153 psa_key_slot_t *slot,
6154 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006156{
6157 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306159 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006161 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6164 return PSA_ERROR_INVALID_ARGUMENT;
6165 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006166
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006167#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006168 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6170 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6171 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006172 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6174 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 }
6177 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006178 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6180 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006183 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006184 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006185#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006186 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (key_type_is_raw_bytes(slot->attr.type)) {
6188 if (bits % 8 != 0) {
6189 return PSA_ERROR_INVALID_ARGUMENT;
6190 }
6191 data = mbedtls_calloc(1, bytes);
6192 if (data == NULL) {
6193 return PSA_ERROR_INSUFFICIENT_MEMORY;
6194 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 status = psa_key_derivation_output_bytes(operation, data, bytes);
6197 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 }
David Brown12ca5032021-02-11 11:02:00 -07006200#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6202 psa_des_set_key_parity(data, bytes);
6203 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006204#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 } else {
6206 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006207 }
Ronald Crondd04d422020-11-28 15:14:42 +01006208
Ronald Cronbf33c932020-11-28 18:06:53 +01006209 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006210 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006212 };
6213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6215 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6216 &storage_size);
6217 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 }
Archanad8a83dc2021-06-14 10:04:16 +05306220 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 status = psa_allocate_buffer_to_slot(slot, storage_size);
6222 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 }
Archanad8a83dc2021-06-14 10:04:16 +05306225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 status = psa_driver_wrapper_import_key(&attributes,
6227 data, bytes,
6228 slot->key.data,
6229 slot->key.bytes,
6230 &slot->key.bytes, &bits);
6231 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006232 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006234
6235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 mbedtls_free(data);
6237 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006238}
6239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6241 psa_key_derivation_operation_t *operation,
6242 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006243{
6244 psa_status_t status;
6245 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006246 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006247
Ronald Cron81709fc2020-11-14 12:10:32 +01006248 *key = MBEDTLS_SVC_KEY_ID_INIT;
6249
Gilles Peskine0f84d622019-09-12 19:03:13 +02006250 /* Reject any attempt to create a zero-length key so that we don't
6251 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (psa_get_key_bits(attributes) == 0) {
6253 return PSA_ERROR_INVALID_ARGUMENT;
6254 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (operation->alg == PSA_ALG_NONE) {
6257 return PSA_ERROR_BAD_STATE;
6258 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (!operation->can_output_key) {
6261 return PSA_ERROR_NOT_PERMITTED;
6262 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6265 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006266#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006268 /* Deriving a key in a secure element is not implemented yet. */
6269 status = PSA_ERROR_NOT_SUPPORTED;
6270 }
6271#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (status == PSA_SUCCESS) {
6273 status = psa_generate_derived_key_internal(slot,
6274 attributes->core.bits,
6275 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006276 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (status == PSA_SUCCESS) {
6278 status = psa_finish_key_creation(slot, driver, key);
6279 }
6280 if (status != PSA_SUCCESS) {
6281 psa_fail_key_creation(slot, driver);
6282 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006285}
6286
Gilles Peskineeab56e42018-07-12 17:12:33 +02006287
6288
6289/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006290/* Key derivation */
6291/****************************************************************/
6292
Steven Cooreman932ffb72021-02-15 12:14:32 +01006293#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006294static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006295{
6296#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6298 return 1;
6299 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006300#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006301#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6303 return 1;
6304 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006305#endif
6306#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6308 return 1;
6309 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006310#endif
6311#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6313 return 1;
6314 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006315#endif
6316#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6318 return 1;
6319 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006320#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006321#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6323 return 1;
6324 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006325#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306326#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6327 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6328 return 1;
6329 }
6330#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306331#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6332 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6333 return 1;
6334 }
6335#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006337}
6338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006340{
6341 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 psa_status_t status = psa_hash_setup(&operation, alg);
6343 psa_hash_abort(&operation);
6344 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006345}
6346
Gilles Peskine969c5d62019-01-16 15:53:06 +01006347static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006348 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006350{
Janos Follath5fe19732019-06-20 15:09:30 +01006351 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6352 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006354
Gilles Peskine969c5d62019-01-16 15:53:06 +01006355 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (!is_kdf_alg_supported(kdf_alg)) {
6357 return PSA_ERROR_NOT_SUPPORTED;
6358 }
John Durkop07cc04a2020-11-16 22:08:34 -08006359
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006360 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306361 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6363 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306364 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6365 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6366 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306367 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306368 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (hash_size == 0) {
6370 return PSA_ERROR_NOT_SUPPORTED;
6371 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006372
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006373 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6374 * we might fail later, which is somewhat unfriendly and potentially
6375 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 psa_status_t status = psa_hash_try_support(hash_alg);
6377 if (status != PSA_SUCCESS) {
6378 return status;
6379 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006380 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6383 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6384 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6385 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006386 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006387#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006388 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6390 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006391 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006393#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6394 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 operation->capacity = 255 * hash_size;
6396 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006397}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006400{
6401#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (alg == PSA_ALG_ECDH) {
6403 return PSA_SUCCESS;
6404 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006405#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006406#if defined(PSA_WANT_ALG_FFDH)
6407 if (alg == PSA_ALG_FFDH) {
6408 return PSA_SUCCESS;
6409 }
6410#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006411 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006413}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006414
6415static int psa_key_derivation_allows_free_form_secret_input(
6416 psa_algorithm_t kdf_alg)
6417{
6418#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6419 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6420 return 0;
6421 }
6422#endif
6423 (void) kdf_alg;
6424 return 1;
6425}
John Durkop07cc04a2020-11-16 22:08:34 -08006426#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6429 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006430{
6431 psa_status_t status;
6432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 if (operation->alg != 0) {
6434 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006435 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6438 return PSA_ERROR_INVALID_ARGUMENT;
6439 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6440#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6441 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6442 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6443 status = psa_key_agreement_try_support(ka_alg);
6444 if (status != PSA_SUCCESS) {
6445 return status;
6446 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006447 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6448 return PSA_ERROR_INVALID_ARGUMENT;
6449 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6451#else
6452 return PSA_ERROR_NOT_SUPPORTED;
6453#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6454 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6455#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6456 status = psa_key_derivation_setup_kdf(operation, alg);
6457#else
6458 return PSA_ERROR_NOT_SUPPORTED;
6459#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6460 } else {
6461 return PSA_ERROR_INVALID_ARGUMENT;
6462 }
6463
6464 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006465 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 }
6467 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006468}
6469
Przemek Stekiel69c46792022-06-10 12:59:51 +02006470#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006471static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6472 psa_algorithm_t kdf_alg,
6473 psa_key_derivation_step_t step,
6474 const uint8_t *data,
6475 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006476{
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006480 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006481#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6483 return PSA_ERROR_INVALID_ARGUMENT;
6484 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006485#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (hkdf->state != HKDF_STATE_INIT) {
6487 return PSA_ERROR_BAD_STATE;
6488 } else {
6489 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6490 hash_alg,
6491 data, data_length);
6492 if (status != PSA_SUCCESS) {
6493 return status;
6494 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006495 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006497 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006498 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006499#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006501 /* We shouldn't be in different state as HKDF_EXPAND only allows
6502 * two inputs: SECRET (this case) and INFO which does not modify
6503 * the state. It could happen only if the hkdf
6504 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (hkdf->state != HKDF_STATE_INIT) {
6506 return PSA_ERROR_BAD_STATE;
6507 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006508
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006509 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6511 return PSA_ERROR_INVALID_ARGUMENT;
6512 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 memcpy(hkdf->prk, data, data_length);
6515 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006516#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006517 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006518 /* HKDF: If no salt was provided, use an empty salt.
6519 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006521#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6523 return PSA_ERROR_BAD_STATE;
6524 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006525#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6527 hash_alg,
6528 NULL, 0);
6529 if (status != PSA_SUCCESS) {
6530 return status;
6531 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006532 hkdf->state = HKDF_STATE_STARTED;
6533 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 if (hkdf->state != HKDF_STATE_STARTED) {
6535 return PSA_ERROR_BAD_STATE;
6536 }
6537 status = psa_mac_update(&hkdf->hmac,
6538 data, data_length);
6539 if (status != PSA_SUCCESS) {
6540 return status;
6541 }
6542 status = psa_mac_sign_finish(&hkdf->hmac,
6543 hkdf->prk,
6544 sizeof(hkdf->prk),
6545 &data_length);
6546 if (status != PSA_SUCCESS) {
6547 return status;
6548 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006549 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006550
Gilles Peskine2b522db2019-04-12 15:11:49 +02006551 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006552 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006553#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006555 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006557 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006559#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006560 {
6561 /* Block 0 is empty, and the next block will be
6562 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006564 }
6565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006567 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006568#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6570 return PSA_ERROR_INVALID_ARGUMENT;
6571 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006572#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006573#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6575 hkdf->state == HKDF_STATE_INIT) {
6576 return PSA_ERROR_BAD_STATE;
6577 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006578#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 if (hkdf->state == HKDF_STATE_OUTPUT) {
6580 return PSA_ERROR_BAD_STATE;
6581 }
6582 if (hkdf->info_set) {
6583 return PSA_ERROR_BAD_STATE;
6584 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006585 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 if (data_length != 0) {
6587 hkdf->info = mbedtls_calloc(1, data_length);
6588 if (hkdf->info == NULL) {
6589 return PSA_ERROR_INSUFFICIENT_MEMORY;
6590 }
6591 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006592 }
6593 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006595 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006597 }
6598}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006599#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006600
John Durkop07cc04a2020-11-16 22:08:34 -08006601#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6602 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006603static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6604 const uint8_t *data,
6605 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006606{
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6608 return PSA_ERROR_BAD_STATE;
6609 }
Janos Follathf08e2652019-06-13 09:05:41 +01006610
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 if (data_length != 0) {
6612 prf->seed = mbedtls_calloc(1, data_length);
6613 if (prf->seed == NULL) {
6614 return PSA_ERROR_INSUFFICIENT_MEMORY;
6615 }
Janos Follathf08e2652019-06-13 09:05:41 +01006616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006618 prf->seed_length = data_length;
6619 }
Janos Follathf08e2652019-06-13 09:05:41 +01006620
Gilles Peskine43f958b2020-12-13 14:55:14 +01006621 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006624}
6625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6627 const uint8_t *data,
6628 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006629{
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6631 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6632 return PSA_ERROR_BAD_STATE;
6633 }
Janos Follath81550542019-06-13 14:26:34 +01006634
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 if (data_length != 0) {
6636 prf->secret = mbedtls_calloc(1, data_length);
6637 if (prf->secret == NULL) {
6638 return PSA_ERROR_INSUFFICIENT_MEMORY;
6639 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006642 prf->secret_length = data_length;
6643 }
Janos Follath81550542019-06-13 14:26:34 +01006644
Gilles Peskine43f958b2020-12-13 14:55:14 +01006645 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006648}
6649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6651 const uint8_t *data,
6652 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006653{
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6655 return PSA_ERROR_BAD_STATE;
6656 }
Janos Follath63028dd2019-06-13 09:15:47 +01006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 if (data_length != 0) {
6659 prf->label = mbedtls_calloc(1, data_length);
6660 if (prf->label == NULL) {
6661 return PSA_ERROR_INSUFFICIENT_MEMORY;
6662 }
Janos Follath63028dd2019-06-13 09:15:47 +01006663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006665 prf->label_length = data_length;
6666 }
Janos Follath63028dd2019-06-13 09:15:47 +01006667
Gilles Peskine43f958b2020-12-13 14:55:14 +01006668 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006671}
6672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6674 psa_key_derivation_step_t step,
6675 const uint8_t *data,
6676 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006677{
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006679 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006681 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006683 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006685 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006687 }
6688}
John Durkop07cc04a2020-11-16 22:08:34 -08006689#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6690 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6691
6692#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6693static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6694 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006695 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006697{
6698 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6700 4 + data_length + prf->other_secret_length :
6701 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6704 return PSA_ERROR_INVALID_ARGUMENT;
6705 }
John Durkop07cc04a2020-11-16 22:08:34 -08006706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 uint8_t *pms = mbedtls_calloc(1, pms_len);
6708 if (pms == NULL) {
6709 return PSA_ERROR_INSUFFICIENT_MEMORY;
6710 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006711 uint8_t *cur = pms;
6712
6713 /* pure-PSK:
6714 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006715 *
6716 * The premaster secret is formed as follows: if the PSK is N octets
6717 * long, concatenate a uint16 with the value N, N zero octets, a second
6718 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006719 *
6720 * mixed-PSK:
6721 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6722 * follows: concatenate a uint16 with the length of the other secret,
6723 * the other secret itself, uint16 with the length of PSK, and the
6724 * PSK itself.
6725 * For details please check:
6726 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6727 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6728 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006729 */
6730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6732 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6733 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6734 if (prf->other_secret_length != 0) {
6735 memcpy(cur, prf->other_secret, prf->other_secret_length);
6736 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006737 cur += prf->other_secret_length;
6738 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 } else {
6740 *cur++ = MBEDTLS_BYTE_1(data_length);
6741 *cur++ = MBEDTLS_BYTE_0(data_length);
6742 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006743 cur += data_length;
6744 }
6745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 *cur++ = MBEDTLS_BYTE_1(data_length);
6747 *cur++ = MBEDTLS_BYTE_0(data_length);
6748 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006749 cur += data_length;
6750
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006751 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006752
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006753 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006755}
Janos Follath6660f0e2019-06-17 08:44:03 +01006756
Przemek Stekiele47201b2022-04-19 13:53:28 +02006757static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6758 psa_tls12_prf_key_derivation_t *prf,
6759 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006761{
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6763 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006765
6766 if (data_length != 0) {
6767 prf->other_secret = mbedtls_calloc(1, data_length);
6768 if (prf->other_secret == NULL) {
6769 return PSA_ERROR_INSUFFICIENT_MEMORY;
6770 }
6771
6772 memcpy(prf->other_secret, data, data_length);
6773 prf->other_secret_length = data_length;
6774 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006775 prf->other_secret_length = 0;
6776 }
6777
6778 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6779
Gilles Peskine449bd832023-01-11 14:50:10 +01006780 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006781}
6782
Janos Follath6660f0e2019-06-17 08:44:03 +01006783static psa_status_t psa_tls12_prf_psk_to_ms_input(
6784 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006785 psa_key_derivation_step_t step,
6786 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006788{
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006790 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 return psa_tls12_prf_psk_to_ms_set_key(prf,
6792 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006793 break;
6794 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6796 data,
6797 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006798 break;
6799 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006801 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006802
Przemek Stekiele47201b2022-04-19 13:53:28 +02006803 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006804}
John Durkop07cc04a2020-11-16 22:08:34 -08006805#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006806
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006807#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6808static psa_status_t psa_tls12_ecjpake_to_pms_input(
6809 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006810 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006811 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006813{
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6815 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6816 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006817 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006818
6819 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (data[0] != 0x04) {
6821 return PSA_ERROR_INVALID_ARGUMENT;
6822 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006823
6824 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006828}
6829#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306830
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306831#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306832static psa_status_t psa_pbkdf2_set_input_cost(
6833 psa_pbkdf2_key_derivation_t *pbkdf2,
6834 psa_key_derivation_step_t step,
6835 uint64_t data)
6836{
6837 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6838 return PSA_ERROR_INVALID_ARGUMENT;
6839 }
6840
6841 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6842 return PSA_ERROR_BAD_STATE;
6843 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306844
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306845 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306846 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306847 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306848
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306849 if (data == 0) {
6850 return PSA_ERROR_INVALID_ARGUMENT;
6851 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306852
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306853 pbkdf2->input_cost = data;
6854 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6855
6856 return PSA_SUCCESS;
6857}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306858
6859static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6860 const uint8_t *data,
6861 size_t data_length)
6862{
Gilles Peskineead17662023-08-20 22:02:51 +02006863 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6864 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6865 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6866 /* Appending to existing salt. No state change. */
6867 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306868 return PSA_ERROR_BAD_STATE;
6869 }
6870
Gilles Peskineca57d782023-07-20 19:08:06 +02006871 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006872 /* Appending an empty string, nothing to do. */
6873 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306874 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306875
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306876 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6877 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306878 return PSA_ERROR_INSUFFICIENT_MEMORY;
6879 }
6880
Gilles Peskineead17662023-08-20 22:02:51 +02006881 if (pbkdf2->salt_length != 0) {
6882 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6883 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306884 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306885 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306886 mbedtls_free(pbkdf2->salt);
6887 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306888 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306889 return PSA_SUCCESS;
6890}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306891
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306892#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306893static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306894 const uint8_t *input,
6895 size_t input_len,
6896 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306897 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306898{
6899 psa_status_t status = PSA_SUCCESS;
6900 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6901 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306902 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306903 } else {
6904 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306905 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306906 }
6907 return status;
6908}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306909#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306910
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306911#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306912static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6913 size_t input_len,
6914 uint8_t *output,
6915 size_t *output_len)
6916{
6917 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306918 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306920 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306921 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6922 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6923 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306924 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306925 * mac_size as the driver function sets mac_output_length = mac_size
6926 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306927 status = psa_driver_wrapper_mac_compute(&attributes,
6928 zeros, sizeof(zeros),
6929 PSA_ALG_CMAC, input, input_len,
6930 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306931 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6932 128U,
6933 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306934 output_len);
6935 } else {
6936 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306937 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306938 }
6939 return status;
6940}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306941#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306942
6943static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306944 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306945 const uint8_t *data,
6946 size_t data_length)
6947{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306948 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306949 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6950 return PSA_ERROR_BAD_STATE;
6951 }
6952
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306953#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306954 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6955 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6956 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6957 pbkdf2->password,
6958 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306959 } else
6960#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6961#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6962 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306963 status = psa_pbkdf2_cmac_set_password(data, data_length,
6964 pbkdf2->password,
6965 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306966 } else
6967#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6968 {
6969 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306970 }
6971
6972 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6973
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306974 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306975}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306976
6977static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306978 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306979 psa_key_derivation_step_t step,
6980 const uint8_t *data,
6981 size_t data_length)
6982{
6983 switch (step) {
6984 case PSA_KEY_DERIVATION_INPUT_SALT:
6985 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6986 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306987 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306988 default:
6989 return PSA_ERROR_INVALID_ARGUMENT;
6990 }
6991}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306992#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306993
Gilles Peskineb8965192019-09-24 16:21:10 +02006994/** Check whether the given key type is acceptable for the given
6995 * input step of a key derivation.
6996 *
6997 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6998 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6999 * Both secret and non-secret inputs can alternatively have the type
7000 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
7001 * that the input was passed as a buffer rather than via a key object.
7002 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02007003static int psa_key_derivation_check_input_type(
7004 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02007006{
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007008 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (key_type == PSA_KEY_TYPE_DERIVE) {
7010 return PSA_SUCCESS;
7011 }
7012 if (key_type == PSA_KEY_TYPE_NONE) {
7013 return PSA_SUCCESS;
7014 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007015 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02007016 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 if (key_type == PSA_KEY_TYPE_DERIVE) {
7018 return PSA_SUCCESS;
7019 }
7020 if (key_type == PSA_KEY_TYPE_NONE) {
7021 return PSA_SUCCESS;
7022 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02007023 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007024 case PSA_KEY_DERIVATION_INPUT_LABEL:
7025 case PSA_KEY_DERIVATION_INPUT_SALT:
7026 case PSA_KEY_DERIVATION_INPUT_INFO:
7027 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
7029 return PSA_SUCCESS;
7030 }
7031 if (key_type == PSA_KEY_TYPE_NONE) {
7032 return PSA_SUCCESS;
7033 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007034 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05307035 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
7036 if (key_type == PSA_KEY_TYPE_PASSWORD) {
7037 return PSA_SUCCESS;
7038 }
7039 if (key_type == PSA_KEY_TYPE_DERIVE) {
7040 return PSA_SUCCESS;
7041 }
7042 if (key_type == PSA_KEY_TYPE_NONE) {
7043 return PSA_SUCCESS;
7044 }
7045 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007046 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02007048}
7049
Janos Follathb80a94e2019-06-12 15:54:46 +01007050static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007051 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007052 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02007053 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007054 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007056{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007057 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 status = psa_key_derivation_check_input_type(step, key_type);
7061 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02007062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02007064
Przemek Stekiel69c46792022-06-10 12:59:51 +02007065#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
7067 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
7068 step, data, data_length);
7069 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007070#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007071#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7073 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7074 step, data, data_length);
7075 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007076#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7079 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7080 step, data, data_length);
7081 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007082#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007083#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007085 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7087 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007088#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307089#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307090 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307091 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7092 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307093 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307094#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007095 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007096 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007097 (void) data;
7098 (void) data_length;
7099 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007101 }
7102
Gilles Peskine224b0d62019-09-23 18:13:17 +02007103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (status != PSA_SUCCESS) {
7105 psa_key_derivation_abort(operation);
7106 }
7107 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007108}
7109
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307110static psa_status_t psa_key_derivation_input_integer_internal(
7111 psa_key_derivation_operation_t *operation,
7112 psa_key_derivation_step_t step,
7113 uint64_t value)
7114{
7115 psa_status_t status;
7116 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7117
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307118#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307119 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307120 status = psa_pbkdf2_set_input_cost(
7121 &operation->ctx.pbkdf2, step, value);
7122 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307123#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307124 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307125 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307126 (void) value;
7127 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307128 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307129 }
7130
7131 if (status != PSA_SUCCESS) {
7132 psa_key_derivation_abort(operation);
7133 }
7134 return status;
7135}
7136
Janos Follath51f4a0f2019-06-14 11:35:55 +01007137psa_status_t psa_key_derivation_input_bytes(
7138 psa_key_derivation_operation_t *operation,
7139 psa_key_derivation_step_t step,
7140 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007142{
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 return psa_key_derivation_input_internal(operation, step,
7144 PSA_KEY_TYPE_NONE,
7145 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007146}
7147
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307148psa_status_t psa_key_derivation_input_integer(
7149 psa_key_derivation_operation_t *operation,
7150 psa_key_derivation_step_t step,
7151 uint64_t value)
7152{
7153 return psa_key_derivation_input_integer_internal(operation, step, value);
7154}
7155
Janos Follath51f4a0f2019-06-14 11:35:55 +01007156psa_status_t psa_key_derivation_input_key(
7157 psa_key_derivation_operation_t *operation,
7158 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007160{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007162 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007163 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007164
Ronald Cron5c522922020-11-14 16:35:34 +01007165 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7167 if (status != PSA_SUCCESS) {
7168 psa_key_derivation_abort(operation);
7169 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007170 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007171
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307172 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7173 * permission to output to a key object. */
7174 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7175 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007176 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007178
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 status = psa_key_derivation_input_internal(operation,
7180 step, slot->attr.type,
7181 slot->key.data,
7182 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007183
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007187}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007188
7189
7190
7191/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007192/* Key agreement */
7193/****************************************************************/
7194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7196 const uint8_t *key_buffer,
7197 size_t key_buffer_size,
7198 psa_algorithm_t alg,
7199 const uint8_t *peer_key,
7200 size_t peer_key_length,
7201 uint8_t *shared_secret,
7202 size_t shared_secret_size,
7203 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007204{
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007206#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007207 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7209 key_buffer_size, alg,
7210 peer_key, peer_key_length,
7211 shared_secret,
7212 shared_secret_size,
7213 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007214#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007215
7216#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7217 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007218 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007219 peer_key,
7220 peer_key_length,
7221 key_buffer,
7222 key_buffer_size,
7223 shared_secret,
7224 shared_secret_size,
7225 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007226#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7227
Gilles Peskine01d718c2018-09-18 12:01:02 +02007228 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007229 (void) attributes;
7230 (void) key_buffer;
7231 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007232 (void) peer_key;
7233 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007234 (void) shared_secret;
7235 (void) shared_secret_size;
7236 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007238 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007239}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007240
Aditya Deshpande17845b82022-10-13 17:21:01 +01007241/** Internal function for raw key agreement
7242 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007243 * to the driver's implementation if a driver is present.
7244 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007245 * (psa_key_agreement_raw_builtin).
7246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007247static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7248 psa_key_slot_t *private_key,
7249 const uint8_t *peer_key,
7250 size_t peer_key_length,
7251 uint8_t *shared_secret,
7252 size_t shared_secret_size,
7253 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007254{
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7256 return PSA_ERROR_NOT_SUPPORTED;
7257 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007258
7259 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007261 };
7262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 return psa_driver_wrapper_key_agreement(&attributes,
7264 private_key->key.data,
7265 private_key->key.bytes, alg,
7266 peer_key, peer_key_length,
7267 shared_secret,
7268 shared_secret_size,
7269 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007270}
7271
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007272/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007273 * to potentially free embedded data structures and wipe confidential data.
7274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007275static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7276 psa_key_derivation_step_t step,
7277 psa_key_slot_t *private_key,
7278 const uint8_t *peer_key,
7279 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007280{
7281 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007282 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007283 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007285
7286 /* Step 1: run the secret agreement algorithm to generate the shared
7287 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 status = psa_key_agreement_raw_internal(ka_alg,
7289 private_key,
7290 peer_key, peer_key_length,
7291 shared_secret,
7292 sizeof(shared_secret),
7293 &shared_secret_length);
7294 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007297
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007298 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007299 * the shared secret. A shared secret is permitted wherever a key
7300 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 status = psa_key_derivation_input_internal(operation, step,
7302 PSA_KEY_TYPE_DERIVE,
7303 shared_secret,
7304 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7307 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007308}
7309
Gilles Peskine449bd832023-01-11 14:50:10 +01007310psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7311 psa_key_derivation_step_t step,
7312 mbedtls_svc_key_id_t private_key,
7313 const uint8_t *peer_key,
7314 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007315{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007317 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007318 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7321 return PSA_ERROR_INVALID_ARGUMENT;
7322 }
Ronald Cron5c522922020-11-14 16:35:34 +01007323 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7325 if (status != PSA_SUCCESS) {
7326 return status;
7327 }
7328 status = psa_key_agreement_internal(operation, step,
7329 slot,
7330 peer_key, peer_key_length);
7331 if (status != PSA_SUCCESS) {
7332 psa_key_derivation_abort(operation);
7333 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007334 /* If a private key has been added as SECRET, we allow the derived
7335 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007337 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007339 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007344}
7345
Gilles Peskine449bd832023-01-11 14:50:10 +01007346psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7347 mbedtls_svc_key_id_t private_key,
7348 const uint8_t *peer_key,
7349 size_t peer_key_length,
7350 uint8_t *output,
7351 size_t output_size,
7352 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007353{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007355 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007356 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007357 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007358
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007360 status = PSA_ERROR_INVALID_ARGUMENT;
7361 goto exit;
7362 }
Ronald Cron5c522922020-11-14 16:35:34 +01007363 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7365 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007366 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007368
Gilles Peskine3e561302022-04-14 00:17:15 +02007369 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7370 * for the output size. The PSA specification only guarantees that this
7371 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7372 * but it might be nice to allow smaller buffers if the output fits.
7373 * At the time of writing this comment, with only ECDH implemented,
7374 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7375 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7376 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007377 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7379 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007380 status = PSA_ERROR_BUFFER_TOO_SMALL;
7381 goto exit;
7382 }
7383
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 status = psa_key_agreement_raw_internal(alg, slot,
7385 peer_key, peer_key_length,
7386 output, output_size,
7387 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007388
7389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007391 /* If an error happens and is not handled properly, the output
7392 * may be used as a key to protect sensitive data. Arrange for such
7393 * a key to be random, which is likely to result in decryption or
7394 * verification errors. This is better than filling the buffer with
7395 * some constant data such as zeros, which would result in the data
7396 * being protected with a reproducible, easily knowable key.
7397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007399 *output_length = output_size;
7400 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007401
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007405}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007406
7407
Gilles Peskine30524eb2020-11-13 17:02:26 +01007408
Gilles Peskine86a440b2018-11-12 18:39:40 +01007409/****************************************************************/
7410/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007411/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007412
Gilles Peskine672a7712023-04-28 21:00:28 +02007413#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7414#include "entropy_poll.h"
7415#endif
7416
Gilles Peskine30524eb2020-11-13 17:02:26 +01007417/** Initialize the PSA random generator.
7418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007419static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007420{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007421#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007423#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7424
Gilles Peskine30524eb2020-11-13 17:02:26 +01007425 /* Set default configuration if
7426 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007428 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 }
7430 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007431 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007433
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007435#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7436 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7437 /* The PSA entropy injection feature depends on using NV seed as an entropy
7438 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 mbedtls_entropy_add_source(&rng->entropy,
7440 mbedtls_nv_seed_poll, NULL,
7441 MBEDTLS_ENTROPY_BLOCK_SIZE,
7442 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007443#endif
7444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007446#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007447}
7448
7449/** Deinitialize the PSA random generator.
7450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007451static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007452{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007453#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007455#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7457 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007458#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007459}
7460
7461/** Seed the PSA random generator.
7462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007463static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007464{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007465#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7466 /* Do nothing: the external RNG seeds itself. */
7467 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007469#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007470 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7472 drbg_seed, sizeof(drbg_seed) - 1);
7473 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007474#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007475}
7476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477psa_status_t psa_generate_random(uint8_t *output,
7478 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007479{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007480 GUARD_MODULE_INITIALIZED;
7481
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007482#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7483
7484 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7486 output, output_size,
7487 &output_length);
7488 if (status != PSA_SUCCESS) {
7489 return status;
7490 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007491 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007492 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (output_length != output_size) {
7494 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7495 }
7496 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007497
7498#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7499
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007501 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7503 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7504 output_size);
7505 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7506 output, request_size);
7507 if (ret != 0) {
7508 return mbedtls_to_psa_error(ret);
7509 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007510 output_size -= request_size;
7511 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007512 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007514#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007515}
7516
Gilles Peskine8814fc42020-12-14 15:33:44 +01007517/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007518 *
7519 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7520 * `psa_generate_random(...)`. The state parameter is ignored since the
7521 * PSA API doesn't support passing an explicit state.
7522 *
7523 * In the non-external case, psa_generate_random() calls an
7524 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7525 * and semantics as mbedtls_psa_get_random(). As an optimization,
7526 * instead of doing this back-and-forth between the PSA API and the
7527 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7528 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007530#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7531int mbedtls_psa_get_random(void *p_rng,
7532 unsigned char *output,
7533 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007534{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007535 /* This function takes a pointer to the RNG state because that's what
7536 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7537 * its own state internally and doesn't let the caller access that state.
7538 * So we just ignore the state parameter, and in practice we'll pass
7539 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007540 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 psa_status_t status = psa_generate_random(output, output_size);
7542 if (status == PSA_SUCCESS) {
7543 return 0;
7544 } else {
7545 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7546 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007547}
7548#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7549
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007550#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007551psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7552 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007553{
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 if (global_data.initialized) {
7555 return PSA_ERROR_NOT_PERMITTED;
7556 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7559 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7560 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7561 return PSA_ERROR_INVALID_ARGUMENT;
7562 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007565}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007566#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007567
Ronald Cron3772afe2021-02-08 16:10:05 +01007568/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007569 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007570 * \param type The key type
7571 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007572 *
7573 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007574 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007575 * \retval #PSA_ERROR_INVALID_ARGUMENT
7576 * The size in bits of the key is not valid.
7577 * \retval #PSA_ERROR_NOT_SUPPORTED
7578 * The type and/or the size in bits of the key or the combination of
7579 * the two is not supported.
7580 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007581static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007583{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007584 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 if (key_type_is_raw_bytes(type)) {
7587 status = psa_validate_unstructured_key_bit_size(type, bits);
7588 if (status != PSA_SUCCESS) {
7589 return status;
7590 }
7591 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007592#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7594 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7595 return PSA_ERROR_NOT_SUPPORTED;
7596 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007597 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007598 return PSA_ERROR_NOT_SUPPORTED;
7599 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007600
Ronald Cron01b2aba2020-10-05 09:42:02 +02007601 /* Accept only byte-aligned keys, for the same reasons as
7602 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (bits % 8 != 0) {
7604 return PSA_ERROR_NOT_SUPPORTED;
7605 }
7606 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007607#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007608
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007609#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007611 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 return PSA_SUCCESS;
7613 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007614#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007615
Valerio Settia55f0422023-07-10 15:34:41 +02007616#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007617 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007618 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007619 return PSA_ERROR_NOT_SUPPORTED;
7620 }
7621 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007622#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007623 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007625 }
7626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007628}
7629
Ronald Cron55ed0592020-10-05 10:30:40 +02007630psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007631 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007633{
7634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007635 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 if ((attributes->domain_parameters == NULL) &&
7638 (attributes->domain_parameters_size != 0)) {
7639 return PSA_ERROR_INVALID_ARGUMENT;
7640 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (key_type_is_raw_bytes(type)) {
7643 status = psa_generate_random(key_buffer, key_buffer_size);
7644 if (status != PSA_SUCCESS) {
7645 return status;
7646 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007647
David Brown12ca5032021-02-11 11:02:00 -07007648#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 if (type == PSA_KEY_TYPE_DES) {
7650 psa_des_set_key_parity(key_buffer, key_buffer_size);
7651 }
David Brown8107e312021-02-12 08:08:46 -07007652#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007654
Valerio Setti76df8c12023-07-11 14:11:28 +02007655#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7657 return mbedtls_psa_rsa_generate_key(attributes,
7658 key_buffer,
7659 key_buffer_size,
7660 key_buffer_length);
7661 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007662#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007663
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007664#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7666 return mbedtls_psa_ecp_generate_key(attributes,
7667 key_buffer,
7668 key_buffer_size,
7669 key_buffer_length);
7670 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007672
Valerio Settia55f0422023-07-10 15:34:41 +02007673#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007674 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7675 return mbedtls_psa_ffdh_generate_key(attributes,
7676 key_buffer,
7677 key_buffer_size,
7678 key_buffer_length);
7679 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007680#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007681 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 (void) key_buffer_length;
7683 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007684 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007685
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007687}
Darryl Green0c6575a2018-11-07 16:05:30 +00007688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7690 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007691{
7692 psa_status_t status;
7693 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007694 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007695 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007696
Ronald Cron81709fc2020-11-14 12:10:32 +01007697 *key = MBEDTLS_SVC_KEY_ID_INIT;
7698
Gilles Peskine0f84d622019-09-12 19:03:13 +02007699 /* Reject any attempt to create a zero-length key so that we don't
7700 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 if (psa_get_key_bits(attributes) == 0) {
7702 return PSA_ERROR_INVALID_ARGUMENT;
7703 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007704
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007705 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7707 return PSA_ERROR_INVALID_ARGUMENT;
7708 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007709
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7711 &slot, &driver);
7712 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 }
Gilles Peskine11792082019-08-06 18:36:36 +02007715
Ronald Cron977c2472020-10-13 08:32:21 +02007716 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307717 * storage ( thus not in the case of generating a key in a secure element
7718 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7719 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (slot->key.data == NULL) {
7721 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7722 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007723 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 attributes->core.type, attributes->core.bits);
7725 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007728
7729 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 attributes->core.type,
7731 attributes->core.bits);
7732 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007733 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 attributes, &key_buffer_size);
7735 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 }
Ronald Cron977c2472020-10-13 08:32:21 +02007738 }
Ronald Cron977c2472020-10-13 08:32:21 +02007739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7741 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 }
Ronald Cron977c2472020-10-13 08:32:21 +02007744 }
7745
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 status = psa_driver_wrapper_generate_key(attributes,
7747 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007748
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 if (status != PSA_SUCCESS) {
7750 psa_remove_key_data_from_memory(slot);
7751 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007752
Gilles Peskine11792082019-08-06 18:36:36 +02007753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 if (status == PSA_SUCCESS) {
7755 status = psa_finish_key_creation(slot, driver, key);
7756 }
7757 if (status != PSA_SUCCESS) {
7758 psa_fail_key_creation(slot, driver);
7759 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007760
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007762}
7763
Gilles Peskinee59236f2018-01-27 23:32:46 +01007764/****************************************************************/
7765/* Module setup */
7766/****************************************************************/
7767
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007768#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007769psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 void (* entropy_init)(mbedtls_entropy_context *ctx),
7771 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007772{
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7774 return PSA_ERROR_BAD_STATE;
7775 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007776 global_data.rng.entropy_init = entropy_init;
7777 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007779}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007780#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007781
Gilles Peskine449bd832023-01-11 14:50:10 +01007782void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007783{
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 psa_wipe_all_key_slots();
7785 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7786 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007787 }
7788 /* Wipe all remaining data, including configuration.
7789 * In particular, this sets all state indicator to the value
7790 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007792
7793 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007795}
7796
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007797#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7798/** Recover a transaction that was interrupted by a power failure.
7799 *
7800 * This function is called during initialization, before psa_crypto_init()
7801 * returns. If this function returns a failure status, the initialization
7802 * fails.
7803 */
7804static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007806{
Gilles Peskine449bd832023-01-11 14:50:10 +01007807 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007808 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7809 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007810 /* TODO - fall through to the failure case until this
7811 * is implemented.
7812 * https://github.com/ARMmbed/mbed-crypto/issues/218
7813 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007814 default:
7815 /* We found an unsupported transaction in the storage.
7816 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007818 }
7819}
7820#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7821
Gilles Peskine449bd832023-01-11 14:50:10 +01007822psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007823{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007824 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007825
Gilles Peskinec6b69072018-11-20 21:42:52 +01007826 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007827 if (global_data.initialized != 0) {
7828 return PSA_SUCCESS;
7829 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007830
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007831 /* Init drivers */
7832 status = psa_driver_wrapper_init();
7833 if (status != PSA_SUCCESS) {
7834 goto exit;
7835 }
7836 global_data.drivers_initialized = 1;
7837
Gilles Peskine30524eb2020-11-13 17:02:26 +01007838 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007840 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007841 status = mbedtls_psa_random_seed(&global_data.rng);
7842 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007843 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007845 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007846
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 status = psa_initialize_key_slots();
7848 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007849 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007851
Gilles Peskine4b734222019-07-24 15:56:31 +02007852#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007853 status = psa_crypto_load_transaction();
7854 if (status == PSA_SUCCESS) {
7855 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7856 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007857 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 }
7859 status = psa_crypto_stop_transaction();
7860 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007861 /* There's no transaction to complete. It's all good. */
7862 status = PSA_SUCCESS;
7863 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007864#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007865
Gilles Peskinec6b69072018-11-20 21:42:52 +01007866 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007867 global_data.initialized = 1;
7868
7869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 if (status != PSA_SUCCESS) {
7871 mbedtls_psa_crypto_free();
7872 }
7873 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007874}
7875
Yanray Wangffc3c482023-07-11 12:01:04 +08007876#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007877psa_status_t psa_crypto_driver_pake_get_password_len(
7878 const psa_crypto_driver_pake_inputs_t *inputs,
7879 size_t *password_len)
7880{
7881 if (inputs->password_len == 0) {
7882 return PSA_ERROR_BAD_STATE;
7883 }
7884
7885 *password_len = inputs->password_len;
7886
7887 return PSA_SUCCESS;
7888}
7889
7890psa_status_t psa_crypto_driver_pake_get_password(
7891 const psa_crypto_driver_pake_inputs_t *inputs,
7892 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7893{
7894 if (inputs->password_len == 0) {
7895 return PSA_ERROR_BAD_STATE;
7896 }
7897
7898 if (buffer_size < inputs->password_len) {
7899 return PSA_ERROR_BUFFER_TOO_SMALL;
7900 }
7901
7902 memcpy(buffer, inputs->password, inputs->password_len);
7903 *buffer_length = inputs->password_len;
7904
7905 return PSA_SUCCESS;
7906}
7907
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007908psa_status_t psa_crypto_driver_pake_get_user_len(
7909 const psa_crypto_driver_pake_inputs_t *inputs,
7910 size_t *user_len)
7911{
7912 if (inputs->user_len == 0) {
7913 return PSA_ERROR_BAD_STATE;
7914 }
7915
7916 *user_len = inputs->user_len;
7917
7918 return PSA_SUCCESS;
7919}
7920
7921psa_status_t psa_crypto_driver_pake_get_user(
7922 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007923 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007924{
7925 if (inputs->user_len == 0) {
7926 return PSA_ERROR_BAD_STATE;
7927 }
7928
Przemek Stekielc0e62502023-03-14 11:49:36 +01007929 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007930 return PSA_ERROR_BUFFER_TOO_SMALL;
7931 }
7932
Przemek Stekielc0e62502023-03-14 11:49:36 +01007933 memcpy(user_id, inputs->user, inputs->user_len);
7934 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007935
7936 return PSA_SUCCESS;
7937}
7938
7939psa_status_t psa_crypto_driver_pake_get_peer_len(
7940 const psa_crypto_driver_pake_inputs_t *inputs,
7941 size_t *peer_len)
7942{
7943 if (inputs->peer_len == 0) {
7944 return PSA_ERROR_BAD_STATE;
7945 }
7946
7947 *peer_len = inputs->peer_len;
7948
7949 return PSA_SUCCESS;
7950}
7951
7952psa_status_t psa_crypto_driver_pake_get_peer(
7953 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007954 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007955{
7956 if (inputs->peer_len == 0) {
7957 return PSA_ERROR_BAD_STATE;
7958 }
7959
Przemek Stekielc0e62502023-03-14 11:49:36 +01007960 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007961 return PSA_ERROR_BUFFER_TOO_SMALL;
7962 }
7963
Przemek Stekielc0e62502023-03-14 11:49:36 +01007964 memcpy(peer_id, inputs->peer, inputs->peer_len);
7965 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007966
7967 return PSA_SUCCESS;
7968}
7969
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007970psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7971 const psa_crypto_driver_pake_inputs_t *inputs,
7972 psa_pake_cipher_suite_t *cipher_suite)
7973{
7974 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7975 return PSA_ERROR_BAD_STATE;
7976 }
7977
7978 *cipher_suite = inputs->cipher_suite;
7979
7980 return PSA_SUCCESS;
7981}
7982
Neil Armstronga7d08c32022-06-01 18:21:20 +02007983psa_status_t psa_pake_setup(
7984 psa_pake_operation_t *operation,
7985 const psa_pake_cipher_suite_t *cipher_suite)
7986{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007989 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007990 status = PSA_ERROR_BAD_STATE;
7991 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007992 }
7993
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007994 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007995 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007996 status = PSA_ERROR_INVALID_ARGUMENT;
7997 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007998 }
7999
Przemek Stekiel51eac532022-12-07 11:04:51 +01008000 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
8001
Przemek Stekiele12ed362022-12-21 12:54:46 +01008002 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01008003 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
8004 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008005 operation->data.inputs.cipher_suite = *cipher_suite;
8006
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008007#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008008 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008009 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01008010 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008011
David Horstmann16f01512023-06-14 17:21:07 +01008012 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01008013 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008014 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008015#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008016 {
8017 status = PSA_ERROR_NOT_SUPPORTED;
8018 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008019 }
8020
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01008021 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
8022
Przemek Stekiel51eac532022-12-07 11:04:51 +01008023 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008025 psa_pake_abort(operation);
8026 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008027}
8028
8029psa_status_t psa_pake_set_password_key(
8030 psa_pake_operation_t *operation,
8031 mbedtls_svc_key_id_t password)
8032{
Neil Armstrong5ae60962022-09-15 11:29:46 +02008033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01008034 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
8035 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008036 psa_key_attributes_t attributes;
8037 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008038
Przemek Stekiel51eac532022-12-07 11:04:51 +01008039 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008040 status = PSA_ERROR_BAD_STATE;
8041 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008042 }
8043
Przemek Stekiel6c764412022-11-22 14:05:12 +01008044 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
8045 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01008046 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008047 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008048 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008049 }
8050
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008051 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01008052 .core = slot->attr
8053 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02008054
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01008055 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008056
Przemek Stekiel51eac532022-12-07 11:04:51 +01008057 if (type != PSA_KEY_TYPE_PASSWORD &&
8058 type != PSA_KEY_TYPE_PASSWORD_HASH) {
8059 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008060 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008061 }
8062
Przemek Stekiel51eac532022-12-07 11:04:51 +01008063 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
8064 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01008065 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008066 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008067 }
8068
8069 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8070 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01008071 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008073 if (status != PSA_SUCCESS) {
8074 psa_pake_abort(operation);
8075 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008077 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008078}
8079
8080psa_status_t psa_pake_set_user(
8081 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008082 const uint8_t *user_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008083 size_t user_id_len)
8084{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008086 LOCAL_INPUT_DECLARE(user_id_external, user_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008087
8088 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008089 status = PSA_ERROR_BAD_STATE;
8090 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008091 }
8092
Przemek Stekiel51eac532022-12-07 11:04:51 +01008093 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008094 status = PSA_ERROR_INVALID_ARGUMENT;
8095 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008096 }
8097
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008098 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008099 status = PSA_ERROR_BAD_STATE;
8100 goto exit;
8101 }
8102
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008103 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8104 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008105 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8106 goto exit;
8107 }
8108
David Horstmann4f534ae2024-01-22 17:35:59 +00008109 LOCAL_INPUT_ALLOC(user_id_external, user_id_len, user_id);
8110
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008111 memcpy(operation->data.inputs.user, user_id, user_id_len);
8112 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008113
David Horstmann4f534ae2024-01-22 17:35:59 +00008114 status = PSA_SUCCESS;
8115
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008116exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008117 LOCAL_INPUT_FREE(user_id_external, user_id);
8118 if (status != PSA_SUCCESS) {
8119 psa_pake_abort(operation);
8120 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008121 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008122}
8123
8124psa_status_t psa_pake_set_peer(
8125 psa_pake_operation_t *operation,
David Horstmann4f534ae2024-01-22 17:35:59 +00008126 const uint8_t *peer_id_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008127 size_t peer_id_len)
8128{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann4f534ae2024-01-22 17:35:59 +00008130 LOCAL_INPUT_DECLARE(peer_id_external, peer_id);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008131
8132 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008133 status = PSA_ERROR_BAD_STATE;
8134 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008135 }
8136
Przemek Stekiel51eac532022-12-07 11:04:51 +01008137 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008138 status = PSA_ERROR_INVALID_ARGUMENT;
8139 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008140 }
8141
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008142 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008143 status = PSA_ERROR_BAD_STATE;
8144 goto exit;
8145 }
8146
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008147 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8148 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008149 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8150 goto exit;
8151 }
8152
David Horstmann4f534ae2024-01-22 17:35:59 +00008153 LOCAL_INPUT_ALLOC(peer_id_external, peer_id_len, peer_id);
8154
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008155 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8156 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008157
David Horstmann4f534ae2024-01-22 17:35:59 +00008158 status = PSA_SUCCESS;
8159
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008160exit:
David Horstmann4f534ae2024-01-22 17:35:59 +00008161 LOCAL_INPUT_FREE(peer_id_external, peer_id);
8162 if (status != PSA_SUCCESS) {
8163 psa_pake_abort(operation);
8164 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008165 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008166}
8167
8168psa_status_t psa_pake_set_role(
8169 psa_pake_operation_t *operation,
8170 psa_pake_role_t role)
8171{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008173
Przemek Stekiel51eac532022-12-07 11:04:51 +01008174 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008175 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008176 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008177 }
8178
Przemek Stekiel09104b82023-03-06 14:11:51 +01008179 switch (operation->alg) {
8180#if defined(PSA_WANT_ALG_JPAKE)
8181 case PSA_ALG_JPAKE:
8182 if (role == PSA_PAKE_ROLE_NONE) {
8183 return PSA_SUCCESS;
8184 }
8185 status = PSA_ERROR_INVALID_ARGUMENT;
8186 break;
8187#endif
8188 default:
8189 (void) role;
8190 status = PSA_ERROR_NOT_SUPPORTED;
8191 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008192 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008193exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008194 psa_pake_abort(operation);
8195 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008196}
8197
David Horstmanne7f21e62023-05-12 18:17:21 +01008198/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008199#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008200static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008201 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008202{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008203 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008204 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008206
David Horstmann024e5c52023-06-14 15:48:21 +01008207 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 is_x1 = (stage->outputs < 1);
8209 } else {
8210 is_x1 = (stage->inputs < 1);
8211 }
8212
David Horstmann74a3d8c2023-06-14 18:28:19 +01008213 key_share_step = is_x1 ?
8214 PSA_JPAKE_X1_STEP_KEY_SHARE :
8215 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008216 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008217 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8218 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8219 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8220 } else {
8221 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008222 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008223 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008224}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008226
Przemek Stekiel2797d372022-12-22 11:19:22 +01008227static psa_status_t psa_pake_complete_inputs(
8228 psa_pake_operation_t *operation)
8229{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008231 /* Create copy of the inputs on stack as inputs share memory
8232 with the driver context which will be setup by the driver. */
8233 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008234
Przemek Stekielfde11282023-03-13 16:06:09 +01008235 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008236 return PSA_ERROR_BAD_STATE;
8237 }
8238
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008239 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008240 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8241 return PSA_ERROR_BAD_STATE;
8242 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008243 }
8244
Przemek Stekiel18620a32023-01-17 16:34:52 +01008245 /* Clear driver context */
8246 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8247
8248 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008249
8250 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008251 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008252
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008253 /* User and peer are translated to role. */
8254 mbedtls_free(inputs.user);
8255 mbedtls_free(inputs.peer);
8256
Przemek Stekiel2797d372022-12-22 11:19:22 +01008257 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008258#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008259 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008260 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008261 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008262#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008263 {
8264 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008265 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008266 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008267 return status;
8268}
8269
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008270#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008271static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008273 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008274 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008275{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8277 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8278 step != PSA_PAKE_STEP_ZK_PROOF) {
8279 return PSA_ERROR_INVALID_ARGUMENT;
8280 }
8281
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008282 psa_jpake_computation_stage_t *computation_stage =
8283 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284
David Horstmann5da95602023-06-08 15:37:12 +01008285 if (computation_stage->round != PSA_JPAKE_FIRST &&
8286 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008287 return PSA_ERROR_BAD_STATE;
8288 }
8289
David Horstmanne7f21e62023-05-12 18:17:21 +01008290 /* Check that the step we are given is the one we were expecting */
8291 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008292 return PSA_ERROR_BAD_STATE;
8293 }
8294
David Horstmanne7f21e62023-05-12 18:17:21 +01008295 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8296 computation_stage->inputs == 0 &&
8297 computation_stage->outputs == 0) {
8298 /* Start of the round, so function decides whether we are inputting
8299 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008300 computation_stage->io_mode = io_mode;
8301 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008302 /* Middle of the round so the mode we are in must match the function
8303 * called by the user */
8304 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008305 }
8306
8307 return PSA_SUCCESS;
8308}
8309
David Horstmanne7f21e62023-05-12 18:17:21 +01008310static psa_status_t psa_jpake_epilogue(
8311 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008312 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008313{
David Horstmanne7f21e62023-05-12 18:17:21 +01008314 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008315 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008316
David Horstmanne7f21e62023-05-12 18:17:21 +01008317 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8318 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008319 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008320 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008321 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008322 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008323 }
8324 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008325 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008326 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008327 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008328 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008329 }
8330 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008331 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8332 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008333 /* End of a round, move to the next round */
8334 stage->inputs = 0;
8335 stage->outputs = 0;
8336 stage->round++;
8337 }
8338 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008339 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008340 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 return PSA_SUCCESS;
8343}
David Horstmanne7f21e62023-05-12 18:17:21 +01008344
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008345#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346
Neil Armstronga7d08c32022-06-01 18:21:20 +02008347psa_status_t psa_pake_output(
8348 psa_pake_operation_t *operation,
8349 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008350 uint8_t *output_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008351 size_t output_size,
8352 size_t *output_length)
8353{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008355 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
David Horstmannc75639d2024-01-22 17:56:39 +00008356 LOCAL_OUTPUT_DECLARE(output_external, output);
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008357 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008358
8359 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008360 status = psa_pake_complete_inputs(operation);
8361 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008362 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008363 }
8364 }
8365
8366 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008367 status = PSA_ERROR_BAD_STATE;
8368 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008369 }
8370
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008371 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008372 status = PSA_ERROR_INVALID_ARGUMENT;
8373 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008374 }
8375
Przemek Stekiele12ed362022-12-21 12:54:46 +01008376 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008377#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008378 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008379 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008380 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008381 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008382 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008383 driver_step = convert_jpake_computation_stage_to_driver_step(
8384 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008385 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008386#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008387 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008388 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008389 status = PSA_ERROR_NOT_SUPPORTED;
8390 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008391 }
8392
David Horstmannc75639d2024-01-22 17:56:39 +00008393 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
8394
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008395 status = psa_driver_wrapper_pake_output(operation, driver_step,
8396 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008397
8398 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008399 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008400 }
8401
8402 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008403#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008404 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008405 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008406 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008407 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008408 }
8409 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008410#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008411 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008412 status = PSA_ERROR_NOT_SUPPORTED;
8413 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008414 }
8415
David Horstmannc75639d2024-01-22 17:56:39 +00008416 status = PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008417exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008418 LOCAL_OUTPUT_FREE(output_external, output);
8419 if (status != PSA_SUCCESS) {
8420 psa_pake_abort(operation);
8421 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008422 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008423}
8424
8425psa_status_t psa_pake_input(
8426 psa_pake_operation_t *operation,
8427 psa_pake_step_t step,
David Horstmannc75639d2024-01-22 17:56:39 +00008428 const uint8_t *input_external,
Neil Armstronga7d08c32022-06-01 18:21:20 +02008429 size_t input_length)
8430{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008432 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008433 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8434 operation->primitive,
8435 step);
David Horstmannc75639d2024-01-22 17:56:39 +00008436 LOCAL_INPUT_DECLARE(input_external, input);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008437
8438 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008439 status = psa_pake_complete_inputs(operation);
8440 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008441 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008442 }
8443 }
8444
8445 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008446 status = PSA_ERROR_BAD_STATE;
8447 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008448 }
8449
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008450 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008451 status = PSA_ERROR_INVALID_ARGUMENT;
8452 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008453 }
8454
Przemek Stekiele12ed362022-12-21 12:54:46 +01008455 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008456#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008457 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008458 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008459 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008460 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008461 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008462 driver_step = convert_jpake_computation_stage_to_driver_step(
8463 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008464 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008465#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008466 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008467 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008468 status = PSA_ERROR_NOT_SUPPORTED;
8469 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008470 }
8471
David Horstmannc75639d2024-01-22 17:56:39 +00008472 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008473 status = psa_driver_wrapper_pake_input(operation, driver_step,
8474 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008475
8476 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008477 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008478 }
8479
8480 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008481#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008482 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008483 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008484 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008485 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008486 }
8487 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008488#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008489 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008490 status = PSA_ERROR_NOT_SUPPORTED;
8491 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008492 }
8493
David Horstmannc75639d2024-01-22 17:56:39 +00008494 status = PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008495exit:
David Horstmannc75639d2024-01-22 17:56:39 +00008496 LOCAL_INPUT_FREE(input_external, input);
8497 if (status != PSA_SUCCESS) {
8498 psa_pake_abort(operation);
8499 }
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008500 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008501}
8502
8503psa_status_t psa_pake_get_implicit_key(
8504 psa_pake_operation_t *operation,
8505 psa_key_derivation_operation_t *output)
8506{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008508 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008509 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008510 size_t shared_key_len = 0;
8511
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008512 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008513 status = PSA_ERROR_BAD_STATE;
8514 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008515 }
8516
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008517#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008518 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008519 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008520 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008521 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008522 status = PSA_ERROR_BAD_STATE;
8523 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008524 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008525 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008526#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008527 {
8528 status = PSA_ERROR_NOT_SUPPORTED;
8529 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008530 }
8531
Przemek Stekiel0c781802022-11-29 14:53:13 +01008532 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8533 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008534 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008535 &shared_key_len);
8536
8537 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008538 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008539 }
8540
8541 status = psa_key_derivation_input_bytes(output,
8542 PSA_KEY_DERIVATION_INPUT_SECRET,
8543 shared_key,
8544 shared_key_len);
8545
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008546 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008547exit:
8548 abort_status = psa_pake_abort(operation);
8549 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008550}
8551
8552psa_status_t psa_pake_abort(
8553 psa_pake_operation_t *operation)
8554{
Przemek Stekield69dca92023-01-31 19:59:20 +01008555 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008556
Przemek Stekield69dca92023-01-31 19:59:20 +01008557 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008558 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008559 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008560
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008561 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8562 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008563 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008564 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008565 }
8566 if (operation->data.inputs.user != NULL) {
8567 mbedtls_free(operation->data.inputs.user);
8568 }
8569 if (operation->data.inputs.peer != NULL) {
8570 mbedtls_free(operation->data.inputs.peer);
8571 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008572 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008573 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008574
Przemek Stekield69dca92023-01-31 19:59:20 +01008575 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008576}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008577#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008578
David Horstmann00d7a042023-12-07 18:39:17 +00008579/* Memory copying test hooks. These are called before input copy, after input
8580 * copy, before output copy and after output copy, respectively.
8581 * They are used by memory-poisoning tests to temporarily unpoison buffers
8582 * while they are copied. */
David Horstmanne138dce2023-11-28 15:21:56 +00008583#if defined(MBEDTLS_TEST_HOOKS)
8584void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8585void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8586void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8587void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8588#endif
David Horstmannfde97392023-10-30 20:29:43 +00008589
David Horstmann1b7279a2023-11-15 15:18:30 +00008590/** Copy from an input buffer to a local copy.
8591 *
8592 * \param[in] input Pointer to input buffer.
8593 * \param[in] input_len Length of the input buffer.
8594 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8595 * \param[out] input_copy_len Length of the local copy buffer.
8596 * \return #PSA_SUCCESS, if the buffer was successfully
8597 * copied.
8598 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8599 * copy is too small to hold contents of the
8600 * input buffer.
8601 */
8602MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008603psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8604 uint8_t *input_copy, size_t input_copy_len)
8605{
8606 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008607 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008608 }
8609
David Horstmann372b8bb2023-11-24 16:21:04 +00008610#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008611 if (psa_input_pre_copy_hook != NULL) {
8612 psa_input_pre_copy_hook(input, input_len);
8613 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008614#endif
8615
David Horstmann777e7412023-11-15 17:33:47 +00008616 if (input_len > 0) {
8617 memcpy(input_copy, input, input_len);
8618 }
David Horstmannfde97392023-10-30 20:29:43 +00008619
David Horstmann372b8bb2023-11-24 16:21:04 +00008620#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008621 if (psa_input_post_copy_hook != NULL) {
8622 psa_input_post_copy_hook(input, input_len);
8623 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008624#endif
8625
David Horstmannfde97392023-10-30 20:29:43 +00008626 return PSA_SUCCESS;
8627}
8628
David Horstmann1b7279a2023-11-15 15:18:30 +00008629/** Copy from a local output buffer into a user-supplied one.
8630 *
8631 * \param[in] output_copy Pointer to a local buffer containing the output.
8632 * \param[in] output_copy_len Length of the local buffer.
8633 * \param[out] output Pointer to user-supplied output buffer.
8634 * \param[out] output_len Length of the user-supplied output buffer.
8635 * \return #PSA_SUCCESS, if the buffer was successfully
8636 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008637 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008638 * user-supplied output buffer is too small to
8639 * hold the contents of the local buffer.
8640 */
8641MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008642psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8643 uint8_t *output, size_t output_len)
8644{
8645 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008646 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008647 }
David Horstmann777e7412023-11-15 17:33:47 +00008648
David Horstmann372b8bb2023-11-24 16:21:04 +00008649#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008650 if (psa_output_pre_copy_hook != NULL) {
8651 psa_output_pre_copy_hook(output, output_len);
8652 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008653#endif
8654
David Horstmann777e7412023-11-15 17:33:47 +00008655 if (output_copy_len > 0) {
8656 memcpy(output, output_copy, output_copy_len);
8657 }
8658
David Horstmann372b8bb2023-11-24 16:21:04 +00008659#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008660 if (psa_output_post_copy_hook != NULL) {
8661 psa_output_post_copy_hook(output, output_len);
8662 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008663#endif
8664
David Horstmann8978f5c2023-10-30 20:37:12 +00008665 return PSA_SUCCESS;
8666}
8667
David Horstmannf1734052023-11-20 17:15:32 +00008668psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8669 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008670{
8671 psa_status_t status;
8672
David Horstmann9db14482023-11-23 15:50:37 +00008673 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008674
David Horstmannc5cc1c32023-11-15 18:11:26 +00008675 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008676 return PSA_SUCCESS;
8677 }
8678
David Horstmannf1734052023-11-20 17:15:32 +00008679 local_input->buffer = mbedtls_calloc(input_len, 1);
8680 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008681 /* Since we dealt with the zero-length case above, we know that
8682 * a NULL return value means a failure of allocation. */
8683 return PSA_ERROR_INSUFFICIENT_MEMORY;
8684 }
David Horstmannf1734052023-11-20 17:15:32 +00008685 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008686
David Horstmannf1734052023-11-20 17:15:32 +00008687 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008688
8689 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008690 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008691 if (status != PSA_SUCCESS) {
8692 goto error;
8693 }
8694
8695 return PSA_SUCCESS;
8696
8697error:
David Horstmannf1734052023-11-20 17:15:32 +00008698 mbedtls_free(local_input->buffer);
8699 local_input->buffer = NULL;
8700 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008701 return status;
8702}
8703
David Horstmannf1734052023-11-20 17:15:32 +00008704void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008705{
David Horstmannf1734052023-11-20 17:15:32 +00008706 mbedtls_free(local_input->buffer);
8707 local_input->buffer = NULL;
8708 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008709}
8710
David Horstmann89875a42023-11-20 12:54:09 +00008711psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8712 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008713{
David Horstmann9db14482023-11-23 15:50:37 +00008714 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008715
David Horstmannc5cc1c32023-11-15 18:11:26 +00008716 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008717 return PSA_SUCCESS;
8718 }
David Horstmann89875a42023-11-20 12:54:09 +00008719 local_output->buffer = mbedtls_calloc(output_len, 1);
8720 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008721 /* Since we dealt with the zero-length case above, we know that
8722 * a NULL return value means a failure of allocation. */
8723 return PSA_ERROR_INSUFFICIENT_MEMORY;
8724 }
David Horstmann89875a42023-11-20 12:54:09 +00008725 local_output->length = output_len;
8726 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008727
8728 return PSA_SUCCESS;
8729}
8730
David Horstmann89875a42023-11-20 12:54:09 +00008731psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008732{
David Horstmannc335a4e2023-11-15 15:57:32 +00008733 psa_status_t status;
8734
David Horstmann89875a42023-11-20 12:54:09 +00008735 if (local_output->buffer == NULL) {
8736 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008737 return PSA_SUCCESS;
8738 }
David Horstmann89875a42023-11-20 12:54:09 +00008739 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008740 /* We have an internal copy but nothing to copy back to. */
8741 return PSA_ERROR_CORRUPTION_DETECTED;
8742 }
8743
David Horstmann89875a42023-11-20 12:54:09 +00008744 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8745 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008746 if (status != PSA_SUCCESS) {
8747 return status;
8748 }
David Horstmann9467ea32023-11-08 17:44:18 +00008749
David Horstmann89875a42023-11-20 12:54:09 +00008750 mbedtls_free(local_output->buffer);
8751 local_output->buffer = NULL;
8752 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008753
8754 return PSA_SUCCESS;
8755}
8756
Gilles Peskinee59236f2018-01-27 23:32:46 +01008757#endif /* MBEDTLS_PSA_CRYPTO_C */