blob: d745c46f7a180ac380322db732c07b3fd7f34a1f [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 Rodgman7ff79652023-11-03 12:04:52 +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"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
David Horstmannc0a2c302023-11-29 17:24:08 +0000109#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann926193a2023-12-13 15:55:25 +0000110
David Horstmannb80e35a2023-12-14 18:12:47 +0000111/* Declare a local copy of an input buffer and a variable that will be used
112 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000113 *
114 * Note: This macro must be called before any operations which may jump to
115 * the exit label, so that the local input copy object is safe to be freed.
116 *
117 * Assumptions:
118 * - input is the name of a pointer to the buffer to be copied
119 * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000120 * - input_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000121 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000122#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
123 psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
124 const uint8_t *input_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000125
David Horstmannb80e35a2023-12-14 18:12:47 +0000126/* Allocate a copy of the buffer input and set the pointer input_copy to
127 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000128 *
David Horstmann34980bd2023-11-29 17:07:13 +0000129 * Assumptions:
130 * - psa_status_t status exists
131 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000132 * - input is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000133 * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000134 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000135#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000136 status = psa_crypto_local_input_alloc(input, length, \
137 &LOCAL_INPUT_COPY_OF_##input); \
David Horstmann34980bd2023-11-29 17:07:13 +0000138 if (status != PSA_SUCCESS) { \
139 goto exit; \
140 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000141 input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000142
David Horstmann926193a2023-12-13 15:55:25 +0000143/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
144 *
David Horstmann1f532132023-12-08 14:08:18 +0000145 * Assumptions:
David Horstmannb80e35a2023-12-14 18:12:47 +0000146 * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
David Horstmann926193a2023-12-13 15:55:25 +0000147 * - input is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000148 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000149#define LOCAL_INPUT_FREE(input, input_copy) \
150 input_copy = NULL; \
David Horstmann926193a2023-12-13 15:55:25 +0000151 psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
David Horstmann34980bd2023-11-29 17:07:13 +0000152
David Horstmannb80e35a2023-12-14 18:12:47 +0000153/* Declare a local copy of an output buffer and a variable that will be used
154 * to store a pointer to the start of the buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000155 *
156 * Note: This macro must be called before any operations which may jump to
157 * the exit label, so that the local output copy object is safe to be freed.
158 *
159 * Assumptions:
160 * - output is the name of a pointer to the buffer to be copied
161 * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
David Horstmannb80e35a2023-12-14 18:12:47 +0000162 * - output_copy_name is a name that is unused in the current scope
David Horstmann926193a2023-12-13 15:55:25 +0000163 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000164#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
165 psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
166 uint8_t *output_copy_name = NULL;
David Horstmann926193a2023-12-13 15:55:25 +0000167
David Horstmannb80e35a2023-12-14 18:12:47 +0000168/* Allocate a copy of the buffer output and set the pointer output_copy to
169 * point to the start of the copy.
David Horstmann926193a2023-12-13 15:55:25 +0000170 *
David Horstmann34980bd2023-11-29 17:07:13 +0000171 * Assumptions:
172 * - psa_status_t status exists
173 * - An exit label is declared
David Horstmann926193a2023-12-13 15:55:25 +0000174 * - output is the name of a pointer to the buffer to be copied
David Horstmannb80e35a2023-12-14 18:12:47 +0000175 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
David Horstmann34980bd2023-11-29 17:07:13 +0000176 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000177#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
David Horstmann926193a2023-12-13 15:55:25 +0000178 status = psa_crypto_local_output_alloc(output, length, \
179 &LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann34980bd2023-11-29 17:07:13 +0000180 if (status != PSA_SUCCESS) { \
181 goto exit; \
182 } \
David Horstmannb80e35a2023-12-14 18:12:47 +0000183 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
David Horstmann34980bd2023-11-29 17:07:13 +0000184
Gabor Mezei3b0c3712024-01-24 13:07:17 +0100185/* Allocate a copy of the buffer output and set the pointer output_copy to
186 * point to the start of the copy.
187 *
188 * Assumptions:
189 * - psa_status_t status exists
190 * - An exit label is declared
191 * - output is the name of a pointer to the buffer to be copied
192 * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
193 */
194#define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \
195 status = psa_crypto_local_output_alloc_with_copy(output, length, \
196 &LOCAL_OUTPUT_COPY_OF_##output); \
197 if (status != PSA_SUCCESS) { \
198 goto exit; \
199 } \
200 output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
201
David Horstmannb80e35a2023-12-14 18:12:47 +0000202/* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
David Horstmann1f532132023-12-08 14:08:18 +0000203 * after first copying back its contents to the original buffer.
David Horstmann926193a2023-12-13 15:55:25 +0000204 *
David Horstmann1f532132023-12-08 14:08:18 +0000205 * Assumptions:
David Horstmann1f532132023-12-08 14:08:18 +0000206 * - psa_status_t status exists
David Horstmannb80e35a2023-12-14 18:12:47 +0000207 * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
208 * - output is the name of the original buffer that was copied
David Horstmann1f532132023-12-08 14:08:18 +0000209 */
David Horstmannb80e35a2023-12-14 18:12:47 +0000210#define LOCAL_OUTPUT_FREE(output, output_copy) \
211 output_copy = NULL; \
David Horstmann2b70a662023-12-13 14:09:08 +0000212 do { \
213 psa_status_t local_output_status; \
David Horstmann926193a2023-12-13 15:55:25 +0000214 local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
David Horstmann2b70a662023-12-13 14:09:08 +0000215 if (local_output_status != PSA_SUCCESS) { \
216 /* Since this error case is an internal error, it's more serious than \
217 * any existing error code and so it's fine to overwrite the existing \
218 * status. */ \
219 status = local_output_status; \
220 } \
221 } while (0)
David Horstmannc0a2c302023-11-29 17:24:08 +0000222#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmannb80e35a2023-12-14 18:12:47 +0000223#define LOCAL_INPUT_DECLARE(input, input_copy_name) \
224 const uint8_t *input_copy_name = NULL;
225#define LOCAL_INPUT_ALLOC(input, length, input_copy) \
226 input_copy = input;
227#define LOCAL_INPUT_FREE(input, input_copy) \
228 input_copy = NULL;
229#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
230 uint8_t *output_copy_name = NULL;
231#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
232 output_copy = output;
Gabor Mezei69f680a2024-01-29 17:27:44 +0100233#define LOCAL_OUTPUT_ALLOC_WITH_COPY(output, length, output_copy) \
234 output_copy = output;
David Horstmannb80e35a2023-12-14 18:12:47 +0000235#define LOCAL_OUTPUT_FREE(output, output_copy) \
236 output_copy = NULL;
David Horstmannc0a2c302023-11-29 17:24:08 +0000237#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100240{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100241 /* Mbed TLS error codes can combine a high-level error code and a
242 * low-level error code. The low-level error usually reflects the
243 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 int low_level_ret = -(-ret & 0x007f);
245 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100246 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100248
249 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
250 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
251 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200256 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200258
Gilles Peskine9a944802018-06-21 09:35:35 +0200259 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
260 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
261 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
262 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
263 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200265 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200267 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200269
Jaeden Amero93e21112019-02-20 13:57:28 +0000270#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000271 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000272#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
273 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
274#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100279
Jaeden Amero93e21112019-02-20 13:57:28 +0000280#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000281 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000282#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
283 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
284#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100289
290 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskine26869f22019-05-06 15:25:00 +0200297 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200299
300 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200302 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200304
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321
322 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
326 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100327 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
328 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
332 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100336#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100337
338 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
344 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
345 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347
348 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100354
Gilles Peskine82e57d12020-11-13 21:31:17 +0100355#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100357 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
358 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100359 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100361 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
362 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100364 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100366#endif
367
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200368 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
369 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
370 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200372
Gilles Peskinea5905292018-02-07 20:59:33 +0100373 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100379 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200381 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100383
Gilles Peskinef76aa772018-10-29 19:24:33 +0100384 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100386 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100388 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100390 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100392 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100394 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100396 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100398 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100400
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
404 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100406 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
409 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
414 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 case MBEDTLS_ERR_PK_INVALID_ALG:
419 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
420 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100422 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100426
Gilles Peskineff2d2002019-05-06 15:26:23 +0200427 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200429 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200431
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200432 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200434
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100439 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100441 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100445 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100446 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100448 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100450 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100452 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100454 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100456
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200457 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
458 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
459 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200461
Gilles Peskinea5905292018-02-07 20:59:33 +0100462 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100464 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100466
itayzafrir5c753392018-05-08 11:18:38 +0300467 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300468 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300470 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300472 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300474 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
475 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300477 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100479 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300481 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100483
Janos Follatha13b9052019-11-22 12:48:59 +0000484 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300486
Gilles Peskinee59236f2018-01-27 23:32:46 +0100487 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100489 }
490}
491
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200492
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200493
494
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495/****************************************************************/
496/* Key management */
497/****************************************************************/
498
John Durkop9814fa22020-11-04 12:28:15 -0800499#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800500 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100501 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
502 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
503 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
505 size_t bits,
506 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200507{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100509 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100512 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#endif
515#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100516 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100518#endif
519#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100520 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#endif
523#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100524 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100526#endif
527#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100530 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 if (bits_is_sloppy) {
532 return MBEDTLS_ECP_DP_SECP521R1;
533 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100534 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100535#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100536 }
537 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100538
Paul Elliott8ff510a2020-06-02 17:19:28 +0100539 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100542 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100544#endif
545#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100546 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100548#endif
549#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100550 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100552#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100553 }
554 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100555
Paul Elliott8ff510a2020-06-02 17:19:28 +0100556 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100558#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100559 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100561 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if (bits_is_sloppy) {
563 return MBEDTLS_ECP_DP_CURVE25519;
564 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100565 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100566#endif
567#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100568 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100570#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100571 }
572 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100573
Paul Elliott8ff510a2020-06-02 17:19:28 +0100574 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100577 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100579#endif
580#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100581 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100583#endif
584#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100585 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100587#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100588 }
589 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200590 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100591
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100592 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200594}
John Durkop9814fa22020-11-04 12:28:15 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100596 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
597 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
598 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
599 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
602 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200603{
604 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200607 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200608 case PSA_KEY_TYPE_DERIVE:
609 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100610#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200611 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 if (bits != 128 && bits != 192 && bits != 256) {
613 return PSA_ERROR_INVALID_ARGUMENT;
614 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200615 break;
616#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200617#if defined(PSA_WANT_KEY_TYPE_ARIA)
618 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 if (bits != 128 && bits != 192 && bits != 256) {
620 return PSA_ERROR_INVALID_ARGUMENT;
621 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200622 break;
623#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100624#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200625 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 if (bits != 128 && bits != 192 && bits != 256) {
627 return PSA_ERROR_INVALID_ARGUMENT;
628 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200629 break;
630#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100631#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200632 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (bits != 64 && bits != 128 && bits != 192) {
634 return PSA_ERROR_INVALID_ARGUMENT;
635 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200636 break;
637#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100638#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200639 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 if (bits < 8 || bits > 2048) {
641 return PSA_ERROR_INVALID_ARGUMENT;
642 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 break;
644#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100645#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200646 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 if (bits != 256) {
648 return PSA_ERROR_INVALID_ARGUMENT;
649 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200650 break;
651#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200654 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 if (bits % 8 != 0) {
656 return PSA_ERROR_INVALID_ARGUMENT;
657 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200660}
661
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100662/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100663 *
Steven Cooremancd640932021-03-08 12:00:27 +0100664 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
665 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666 *
667 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668 * \param[in] key_type The key type of the key to be used with the
669 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100670 *
671 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100672 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100675 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100677 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100679{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 if (PSA_ALG_IS_HMAC(algorithm)) {
681 if (key_type == PSA_KEY_TYPE_HMAC) {
682 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100683 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100684 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
687 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
688 * key. */
689 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
690 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
691 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
692 * the block length (larger than 1) for block ciphers. */
693 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
694 return PSA_SUCCESS;
695 }
696 }
697 }
698
699 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100700}
701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
703 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200704{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 if (slot->key.data != NULL) {
706 return PSA_ERROR_ALREADY_EXISTS;
707 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 slot->key.data = mbedtls_calloc(1, buffer_length);
710 if (slot->key.data == NULL) {
711 return PSA_ERROR_INSUFFICIENT_MEMORY;
712 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200713
Ronald Cronea0f8a62020-11-25 17:52:23 +0100714 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200716}
717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
719 const uint8_t *data,
720 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200721{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 psa_status_t status = psa_allocate_buffer_to_slot(slot,
723 data_length);
724 if (status != PSA_SUCCESS) {
725 return status;
726 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 memcpy(slot->key.data, data, data_length);
729 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200730}
731
Ronald Crona0fe59f2020-11-29 11:14:53 +0100732psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100733 const psa_key_attributes_t *attributes,
734 const uint8_t *data, size_t data_length,
735 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100738 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
739 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200741 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742 if (data_length == 0) {
743 return PSA_ERROR_NOT_SUPPORTED;
744 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 if (key_type_is_raw_bytes(type)) {
747 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200748
Steven Cooreman75b74362020-07-28 14:30:13 +0200749 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 if (data_length > SIZE_MAX / 8) {
751 return PSA_ERROR_NOT_SUPPORTED;
752 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200753
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200754 /* Enforce a size limit, and in particular ensure that the bit
755 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 if ((*bits) > PSA_MAX_KEY_BITS) {
757 return PSA_ERROR_NOT_SUPPORTED;
758 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 status = validate_unstructured_key_bit_size(type, *bits);
761 if (status != PSA_SUCCESS) {
762 return status;
763 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200764
Ronald Crondd04d422020-11-28 15:14:42 +0100765 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100767 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 return PSA_SUCCESS;
771 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800772#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
774 if (PSA_KEY_TYPE_IS_ECC(type)) {
775 return mbedtls_psa_ecp_import_key(attributes,
776 data, data_length,
777 key_buffer, key_buffer_size,
778 key_buffer_length,
779 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100780 }
John Durkop9814fa22020-11-04 12:28:15 -0800781#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
782 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700783#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
785 if (PSA_KEY_TYPE_IS_RSA(type)) {
786 return mbedtls_psa_rsa_import_key(attributes,
787 data, data_length,
788 key_buffer, key_buffer_size,
789 key_buffer_length,
790 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200791 }
John Durkop9814fa22020-11-04 12:28:15 -0800792#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
793 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100794 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Calculate the intersection of two algorithm usage policies.
800 *
801 * Return 0 (which allows no operation) on incompatibility.
802 */
803static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100804 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100806 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100807{
Gilles Peskine549ea862019-05-22 11:45:59 +0200808 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809 if (alg1 == alg2) {
810 return alg1;
811 }
Steven Cooreman03488022021-02-18 12:07:20 +0100812 /* If the policies are from the same hash-and-sign family, check
813 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
815 PSA_ALG_IS_SIGN_HASH(alg2) &&
816 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
817 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
818 return alg2;
819 }
820 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
821 return alg1;
822 }
Steven Cooreman03488022021-02-18 12:07:20 +0100823 }
824 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100825 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100826 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
828 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
829 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
830 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
831 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100832 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100834 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100835 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
836 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
837 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
838 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100839 }
840 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
842 (alg1_len <= alg2_len)) {
843 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100844 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
846 (alg2_len <= alg1_len)) {
847 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100848 }
849 }
850 /* If the policies are from the same MAC family, check whether one
851 * of them is a minimum-MAC-length policy. Calculate the most
852 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
854 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
855 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100856 /* Validate the combination of key type and algorithm. Since the base
857 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
859 return 0;
860 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861
Steven Cooreman31a876d2021-03-03 20:47:40 +0100862 /* Get the (exact or at-least) output lengths for both sides of the
863 * requested intersection. None of the currently supported algorithms
864 * have an output length dependent on the actual key size, so setting it
865 * to a bogus value of 0 is currently OK.
866 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100867 * Note that for at-least-this-length wildcard algorithms, the output
868 * length is set to the shortest allowed length, which allows us to
869 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100870 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
871 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100872 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873
Steven Cooremana1d83222021-02-25 10:20:29 +0100874 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
876 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
877 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100878 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879
880 /* If only one is an at-least-this-length policy, the intersection would
881 * be the other (fixed-length) policy as long as said fixed length is
882 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
884 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100885 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100886 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
887 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100889
Steven Cooremancd640932021-03-08 12:00:27 +0100890 /* If none of them are wildcards, check whether they define the same tag
891 * length. This is still possible here when one is default-length and
892 * the other specific-length. Ensure to always return the
893 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 if (alg1_len == alg2_len) {
895 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
896 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 }
898 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100900}
901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902static int psa_key_algorithm_permits(psa_key_type_t key_type,
903 psa_algorithm_t policy_alg,
904 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200905{
Gilles Peskine549ea862019-05-22 11:45:59 +0200906 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907 if (requested_alg == policy_alg) {
908 return 1;
909 }
Steven Cooreman03488022021-02-18 12:07:20 +0100910 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
911 * and requested_alg is the same hash-and-sign family with any hash,
912 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
914 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
915 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
916 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100917 }
918 /* If policy_alg is a wildcard AEAD algorithm of the same base as
919 * the requested algorithm, check the requested tag length to be
920 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 if (PSA_ALG_IS_AEAD(policy_alg) &&
922 PSA_ALG_IS_AEAD(requested_alg) &&
923 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
924 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
925 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
926 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
927 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100928 }
Steven Cooremancd640932021-03-08 12:00:27 +0100929 /* If policy_alg is a MAC algorithm of the same base as the requested
930 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100931 if (PSA_ALG_IS_MAC(policy_alg) &&
932 PSA_ALG_IS_MAC(requested_alg) &&
933 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
934 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100935 /* Validate the combination of key type and algorithm. Since the policy
936 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
938 return 0;
939 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100940
Steven Cooreman31a876d2021-03-03 20:47:40 +0100941 /* Get both the requested output length for the algorithm which is to be
942 * verified, and the default output length for the base algorithm.
943 * Note that none of the currently supported algorithms have an output
944 * length dependent on actual key size, so setting it to a bogus value
945 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100946 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100948 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 key_type, 0,
950 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100951
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 /* If the policy is default-length, only allow an algorithm with
953 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
955 return requested_output_length == default_output_length;
956 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957
958 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100959 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
961 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
962 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100963 }
964
Steven Cooremancd640932021-03-08 12:00:27 +0100965 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
966 * check for the requested MAC length to be equal to or longer than the
967 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
969 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
970 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100971 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200972 }
Steven Cooremance48e852020-10-05 16:02:45 +0200973 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200974 * a key derivation with that key agreement should also be allowed. This
975 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
977 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
978 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
979 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200980 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200983}
984
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985/** Test whether a policy permits an algorithm.
986 *
987 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100988 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100989 * \note This function requires providing the key type for which the policy is
990 * being validated, since some algorithm policy definitions (e.g. MAC)
991 * have different properties depending on what kind of cipher it is
992 * combined with.
993 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994 * \retval PSA_SUCCESS When \p alg is a specific algorithm
995 * allowed by the \p policy.
996 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
997 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
998 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100999 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1001 psa_key_type_t key_type,
1002 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001003{
Steven Cooremand788fab2021-02-25 11:29:17 +01001004 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 if (alg == 0) {
1006 return PSA_ERROR_INVALID_ARGUMENT;
1007 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001008
Steven Cooreman7e39f052021-02-23 14:18:32 +01001009 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (PSA_ALG_IS_WILDCARD(alg)) {
1011 return PSA_ERROR_INVALID_ARGUMENT;
1012 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1015 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1016 return PSA_SUCCESS;
1017 } else {
1018 return PSA_ERROR_NOT_PERMITTED;
1019 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020}
1021
Gilles Peskinef603c712019-01-19 13:40:11 +01001022/** Restrict a key policy based on a constraint.
1023 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001024 * \note This function requires providing the key type for which the policy is
1025 * being restricted, since some algorithm policy definitions (e.g. MAC)
1026 * have different properties depending on what kind of cipher it is
1027 * combined with.
1028 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001029 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001030 * \param[in,out] policy The policy to restrict.
1031 * \param[in] constraint The policy constraint to apply.
1032 *
1033 * \retval #PSA_SUCCESS
1034 * \c *policy contains the intersection of the original value of
1035 * \c *policy and \c *constraint.
1036 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001037 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 * \c *policy is unchanged.
1039 */
1040static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001041 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001042 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001044{
1045 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1047 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001048 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1050 constraint->alg2);
1051 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1052 return PSA_ERROR_INVALID_ARGUMENT;
1053 }
1054 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1055 return PSA_ERROR_INVALID_ARGUMENT;
1056 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001057 policy->usage &= constraint->usage;
1058 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001059 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001061}
1062
Ronald Cron5c522922020-11-14 16:35:34 +01001063/** Get the description of a key given its identifier and policy constraints
1064 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001065 *
Ronald Cron5c522922020-11-14 16:35:34 +01001066 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001067 * nonzero, the key must allow operations with this algorithm. If \p alg is
1068 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001069 *
Ronald Cron5c522922020-11-14 16:35:34 +01001070 * In case of a persistent key, the function loads the description of the key
1071 * into a key slot if not already done.
1072 *
1073 * On success, the returned key slot is locked. It is the responsibility of
1074 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001075 */
Ronald Cron5c522922020-11-14 16:35:34 +01001076static psa_status_t psa_get_and_lock_key_slot_with_policy(
1077 mbedtls_svc_key_id_t key,
1078 psa_key_slot_t **p_slot,
1079 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1083 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 status = psa_get_and_lock_key_slot(key, p_slot);
1086 if (status != PSA_SUCCESS) {
1087 return status;
1088 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001089 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001090
1091 /* Enforce that usage policy for the key slot contains all the flags
1092 * required by the usage parameter. There is one exception: public
1093 * keys can always be exported, so we treat public key objects as
1094 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001096 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001100 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001101 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001102 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001103
Shaun Case0e7791f2021-12-20 21:14:10 -08001104 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105 if (alg != 0) {
1106 status = psa_key_policy_permits(&slot->attr.policy,
1107 slot->attr.type,
1108 alg);
1109 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001110 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001112 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001115
1116error:
1117 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001121}
Darryl Green940d72c2018-07-13 13:18:51 +01001122
Ronald Cron5c522922020-11-14 16:35:34 +01001123/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001124 *
1125 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001126 * available, as opposed to a key in a secure element and/or to be used
1127 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001128 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001129 * This is a temporary function that may be used instead of
1130 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1131 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001132 *
Ronald Cron5c522922020-11-14 16:35:34 +01001133 * On success, the returned key slot is locked. It is the responsibility of the
1134 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001135 */
Ronald Cron5c522922020-11-14 16:35:34 +01001136static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1137 mbedtls_svc_key_id_t key,
1138 psa_key_slot_t **p_slot,
1139 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1143 usage, alg);
1144 if (status != PSA_SUCCESS) {
1145 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1149 psa_unlock_key_slot(*p_slot);
1150 *p_slot = NULL;
1151 return PSA_ERROR_NOT_SUPPORTED;
1152 }
1153
1154 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001155}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001158{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001159 /* Data pointer will always be either a valid pointer or NULL in an
1160 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 if (slot->key.data != NULL) {
1162 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1163 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001166 slot->key.data = NULL;
1167 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001170}
1171
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001172/** Completely wipe a slot in memory, including its policy.
1173 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001175{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001177
1178 /*
1179 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001180 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001181 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1182 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001183 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001184 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001186#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001188#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001189 status = PSA_ERROR_CORRUPTION_DETECTED;
1190 }
1191
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001192 /* Multipart operations may still be using the key. This is safe
1193 * because all multipart operation objects are independent from
1194 * the key slot: if they need to access the key after the setup
1195 * phase, they have a copy of the key. Note that this means that
1196 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001197 /* At this point, key material and other type-specific content has
1198 * been wiped. Clear remaining metadata. We can call memset and not
1199 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 memset(slot, 0, sizeof(*slot));
1201 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001202}
1203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001205{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001206 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001207 psa_status_t status; /* status of the last operation */
1208 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1210 psa_se_drv_table_entry_t *driver;
1211#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 if (mbedtls_svc_key_id_is_null(key)) {
1214 return PSA_SUCCESS;
1215 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001216
Ronald Cronf2911112020-10-29 17:51:10 +01001217 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001218 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001219 * key, this will load the key description from persistent memory if not
1220 * done yet. We cannot avoid this loading as without it we don't know if
1221 * the key is operated by an SE or not and this information is needed by
1222 * the current implementation.
1223 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001224 status = psa_get_and_lock_key_slot(key, &slot);
1225 if (status != PSA_SUCCESS) {
1226 return status;
1227 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001228
Ronald Cronf2911112020-10-29 17:51:10 +01001229 /*
1230 * If the key slot containing the key description is under access by the
1231 * library (apart from the present access), the key cannot be destroyed
1232 * yet. For the time being, just return in error. Eventually (to be
1233 * implemented), the key should be destroyed when all accesses have
1234 * stopped.
1235 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 if (slot->lock_count > 1) {
1237 psa_unlock_key_slot(slot);
1238 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001239 }
1240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001242 /* Refuse the destruction of a read-only key (which may or may not work
1243 * if we attempt it, depending on whether the key is merely read-only
1244 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001245 * Just do the best we can, which is to wipe the copy in memory
1246 * (done in this function's cleanup code). */
1247 overall_status = PSA_ERROR_NOT_PERMITTED;
1248 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001249 }
1250
Gilles Peskine354f7672019-07-12 23:46:38 +02001251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1253 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001254 /* For a key in a secure element, we need to do three things:
1255 * remove the key file in internal storage, destroy the
1256 * key inside the secure element, and update the driver's
1257 * persistent data. Start a transaction that will encompass these
1258 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001260 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 status = psa_crypto_save_transaction();
1264 if (status != PSA_SUCCESS) {
1265 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001266 /* We should still try to destroy the key in the secure
1267 * element and the key metadata in storage. This is especially
1268 * important if the error is that the storage is full.
1269 * But how to do it exactly without risking an inconsistent
1270 * state after a reset?
1271 * https://github.com/ARMmbed/mbed-crypto/issues/215
1272 */
1273 overall_status = status;
1274 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001275 }
1276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 status = psa_destroy_se_key(driver,
1278 psa_key_slot_get_slot_number(slot));
1279 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001280 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001282 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1284
Darryl Greend49a4992018-06-18 17:27:26 +01001285#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1287 status = psa_destroy_persistent_key(slot->attr.id);
1288 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001291
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001292 /* TODO: other slots may have a copy of the same key. We should
1293 * invalidate them.
1294 * https://github.com/ARMmbed/mbed-crypto/issues/214
1295 */
Darryl Greend49a4992018-06-18 17:27:26 +01001296 }
1297#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001298
Gilles Peskinefc762652019-07-22 19:30:34 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 if (driver != NULL) {
1301 status = psa_save_se_persistent_data(driver);
1302 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304 }
1305 status = psa_crypto_stop_transaction();
1306 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001309 }
1310#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1311
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001314 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001316 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 }
1318 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319}
1320
John Durkop07cc04a2020-11-16 22:08:34 -08001321#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001322 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001323static psa_status_t psa_get_rsa_public_exponent(
1324 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001326{
1327 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001329 uint8_t *buffer = NULL;
1330 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1334 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001335 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 }
1337 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001338 /* It's the default value, which is reported as an empty string,
1339 * so there's nothing to do. */
1340 goto exit;
1341 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 buflen = mbedtls_mpi_size(&mpi);
1344 buffer = mbedtls_calloc(1, buflen);
1345 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001346 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1347 goto exit;
1348 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1350 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001351 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001353 attributes->domain_parameters = buffer;
1354 attributes->domain_parameters_size = buflen;
1355
1356exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 mbedtls_mpi_free(&mpi);
1358 if (ret != 0) {
1359 mbedtls_free(buffer);
1360 }
1361 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001362}
John Durkop07cc04a2020-11-16 22:08:34 -08001363#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001364 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001365
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001366/** Retrieve all the publicly-accessible attributes of a key.
1367 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1369 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001373 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1378 if (status != PSA_SUCCESS) {
1379 return status;
1380 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001381
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001382 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1384 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001385
1386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1388 psa_set_key_slot_number(attributes,
1389 psa_key_slot_get_slot_number(slot));
1390 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001391#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001394#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001395 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001396 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001397 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001398 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001399 * is not yet implemented.
1400 * https://github.com/ARMmbed/mbed-crypto/issues/216
1401 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001403 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001404
Ronald Cron90857082020-11-25 14:58:33 +01001405 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001406 slot->attr.type,
1407 slot->key.data,
1408 slot->key.bytes,
1409 &rsa);
1410 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001411 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 status = psa_get_rsa_public_exponent(rsa,
1415 attributes);
1416 mbedtls_rsa_free(rsa);
1417 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001418 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001419 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001420#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001421 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001422 default:
1423 /* Nothing else to do. */
1424 break;
1425 }
1426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 if (status != PSA_SUCCESS) {
1428 psa_reset_key_attributes(attributes);
1429 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434}
1435
Gilles Peskinec8000c02019-08-02 20:15:51 +02001436#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1437psa_status_t psa_get_key_slot_number(
1438 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001440{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001442 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 return PSA_SUCCESS;
1444 } else {
1445 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001446 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001447}
1448#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1451 size_t key_buffer_size,
1452 uint8_t *data,
1453 size_t data_size,
1454 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001455{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 if (key_buffer_size > data_size) {
1457 return PSA_ERROR_BUFFER_TOO_SMALL;
1458 }
1459 memcpy(data, key_buffer, key_buffer_size);
1460 memset(data + key_buffer_size, 0,
1461 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001462 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001464}
1465
Ronald Cron7285cda2020-11-26 14:46:37 +01001466psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001467 const psa_key_attributes_t *attributes,
1468 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470{
Ronald Crond18b5f82020-11-25 16:46:05 +01001471 psa_key_type_t type = attributes->core.type;
1472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 if (key_type_is_raw_bytes(type) ||
1474 PSA_KEY_TYPE_IS_RSA(type) ||
1475 PSA_KEY_TYPE_IS_ECC(type)) {
1476 return psa_export_key_buffer_internal(
1477 key_buffer, key_buffer_size,
1478 data, data_size, data_length);
1479 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480 /* This shouldn't happen in the reference implementation, but
1481 it is valid for a special-purpose implementation to omit
1482 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001484 }
1485}
1486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everette3e760c2024-01-19 16:03:50 +00001488 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 size_t data_size,
1490 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001491{
1492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1493 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1494 psa_key_slot_t *slot;
Ryan Everette3e760c2024-01-19 16:03:50 +00001495 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Crone907e552021-01-18 13:22:38 +01001496 /* Reject a zero-length output buffer now, since this can never be a
1497 * valid key representation. This way we know that data must be a valid
1498 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001499 if (data_size == 0) {
1500 return PSA_ERROR_BUFFER_TOO_SMALL;
1501 }
Ronald Crone907e552021-01-18 13:22:38 +01001502
Ronald Cron9486f9d2020-11-26 13:36:23 +01001503 /* Set the key to empty now, so that even when there are errors, we always
1504 * set data_length to a value between 0 and data_size. On error, setting
1505 * the key to empty is a good choice because an empty key representation is
1506 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001507 *data_length = 0;
1508
Ronald Cron9486f9d2020-11-26 13:36:23 +01001509 /* Export requires the EXPORT flag. There is an exception for public keys,
1510 * which don't require any flag, but
1511 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1512 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1514 PSA_KEY_USAGE_EXPORT, 0);
1515 if (status != PSA_SUCCESS) {
1516 return status;
1517 }
mohammad160306e79202018-03-28 13:17:44 +03001518
Ryan Everette3e760c2024-01-19 16:03:50 +00001519 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1520
Ronald Crond18b5f82020-11-25 16:46:05 +01001521 psa_key_attributes_t attributes = {
1522 .core = slot->attr
1523 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524 status = psa_driver_wrapper_export_key(&attributes,
1525 slot->key.data, slot->key.bytes,
1526 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527
Ryan Everett3a4153a2024-01-25 12:04:55 +00001528#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everette3e760c2024-01-19 16:03:50 +00001529exit:
Ryan Everett3a4153a2024-01-25 12:04:55 +00001530#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001532
Ryan Everette3e760c2024-01-19 16:03:50 +00001533 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001535}
1536
Ronald Cron7285cda2020-11-26 14:46:37 +01001537psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001538 const psa_key_attributes_t *attributes,
1539 const uint8_t *key_buffer,
1540 size_t key_buffer_size,
1541 uint8_t *data,
1542 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001544{
Ronald Crond18b5f82020-11-25 16:46:05 +01001545 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1548 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001549 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 return psa_export_key_buffer_internal(
1551 key_buffer, key_buffer_size,
1552 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001553 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001556#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1558 return mbedtls_psa_rsa_export_public_key(attributes,
1559 key_buffer,
1560 key_buffer_size,
1561 data,
1562 data_size,
1563 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001564#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001565 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001567#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1568 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001570#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001571 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1572 return mbedtls_psa_ecp_export_public_key(attributes,
1573 key_buffer,
1574 key_buffer_size,
1575 data,
1576 data_size,
1577 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001578#else
1579 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001581#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1582 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001583 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 /* This shouldn't happen in the reference implementation, but
1586 it is valid for a special-purpose implementation to omit
1587 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001589 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590}
Ronald Crond18b5f82020-11-25 16:46:05 +01001591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everett30827912024-01-19 16:05:00 +00001593 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001594 size_t data_size,
1595 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001596{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001598 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001599 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001600 psa_key_slot_t *slot;
Ryan Everett30827912024-01-19 16:05:00 +00001601 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001602
Ronald Crone907e552021-01-18 13:22:38 +01001603 /* Reject a zero-length output buffer now, since this can never be a
1604 * valid key representation. This way we know that data must be a valid
1605 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 if (data_size == 0) {
1607 return PSA_ERROR_BUFFER_TOO_SMALL;
1608 }
Ronald Crone907e552021-01-18 13:22:38 +01001609
Darryl Greendd8fb772018-11-07 16:00:44 +00001610 /* Set the key to empty now, so that even when there are errors, we always
1611 * set data_length to a value between 0 and data_size. On error, setting
1612 * the key to empty is a good choice because an empty key representation is
1613 * unlikely to be accepted anywhere. */
1614 *data_length = 0;
1615
1616 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1618 if (status != PSA_SUCCESS) {
1619 return status;
1620 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001621
Ryan Everett30827912024-01-19 16:05:00 +00001622 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1625 status = PSA_ERROR_INVALID_ARGUMENT;
1626 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001627 }
1628
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001629 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001630 .core = slot->attr
1631 };
Ronald Cron67227982020-11-26 15:16:05 +01001632 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001633 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001634 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001635
1636exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001638
Ryan Everett30827912024-01-19 16:05:00 +00001639 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001641}
1642
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001643MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1644 "One or more key attribute flag is listed as both external-only and dual-use")
1645MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1646 "One or more key attribute flag is listed as both internal-only and dual-use")
1647MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1648 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001649
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001650/** Validate that a key policy is internally well-formed.
1651 *
1652 * This function only rejects invalid policies. It does not validate the
1653 * consistency of the policy with respect to other attributes of the key
1654 * such as the key type.
1655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001657{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001658 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1659 PSA_KEY_USAGE_COPY |
1660 PSA_KEY_USAGE_ENCRYPT |
1661 PSA_KEY_USAGE_DECRYPT |
1662 PSA_KEY_USAGE_SIGN_MESSAGE |
1663 PSA_KEY_USAGE_VERIFY_MESSAGE |
1664 PSA_KEY_USAGE_SIGN_HASH |
1665 PSA_KEY_USAGE_VERIFY_HASH |
1666 PSA_KEY_USAGE_DERIVE)) != 0) {
1667 return PSA_ERROR_INVALID_ARGUMENT;
1668 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001671}
1672
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001673/** Validate the internal consistency of key attributes.
1674 *
1675 * This function only rejects invalid attribute values. If does not
1676 * validate the consistency of the attributes with any key data that may
1677 * be involved in the creation of the key.
1678 *
1679 * Call this function early in the key creation process.
1680 *
1681 * \param[in] attributes Key attributes for the new key.
1682 * \param[out] p_drv On any return, the driver for the key, if any.
1683 * NULL for a transparent key.
1684 *
1685 */
1686static psa_status_t psa_validate_key_attributes(
1687 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001689{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001690 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1692 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 status = psa_validate_key_location(lifetime, p_drv);
1695 if (status != PSA_SUCCESS) {
1696 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001697 }
1698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001699 status = psa_validate_key_persistence(lifetime);
1700 if (status != PSA_SUCCESS) {
1701 return status;
1702 }
1703
1704 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1705 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1706 return PSA_ERROR_INVALID_ARGUMENT;
1707 }
1708 } else {
1709 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1710 return PSA_ERROR_INVALID_ARGUMENT;
1711 }
1712 }
1713
1714 status = psa_validate_key_policy(&attributes->core.policy);
1715 if (status != PSA_SUCCESS) {
1716 return status;
1717 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001718
1719 /* Refuse to create overly large keys.
1720 * Note that this doesn't trigger on import if the attributes don't
1721 * explicitly specify a size (so psa_get_key_bits returns 0), so
1722 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1724 return PSA_ERROR_NOT_SUPPORTED;
1725 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001726
Gilles Peskine91e8c332019-08-02 19:19:39 +02001727 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1729 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1730 return PSA_ERROR_INVALID_ARGUMENT;
1731 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001733 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001734}
1735
Gilles Peskine4747d192019-04-17 15:05:45 +02001736/** Prepare a key slot to receive key material.
1737 *
1738 * This function allocates a key slot and sets its metadata.
1739 *
1740 * If this function fails, call psa_fail_key_creation().
1741 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * This function is intended to be used as follows:
1743 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001744 * it with the specified attributes, and in case of a volatile key assign it
1745 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 * -# Populate the slot with the key material.
1747 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1748 * In case of failure at any step, stop the sequence and call
1749 * psa_fail_key_creation().
1750 *
Ronald Cron5c522922020-11-14 16:35:34 +01001751 * On success, the key slot is locked. It is the responsibility of the caller
1752 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001753 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001754 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 * \param[out] p_slot On success, a pointer to the prepared slot.
1757 * \param[out] p_drv On any return, the driver for the key, if any.
1758 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001759 *
1760 * \retval #PSA_SUCCESS
1761 * The key slot is ready to receive key material.
1762 * \return If this function fails, the key slot is an invalid state.
1763 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 */
1765static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001766 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001770{
1771 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001772 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001773 psa_key_slot_t *slot;
1774
Gilles Peskinedf179142019-07-15 22:02:14 +02001775 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 *p_drv = NULL;
1777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 status = psa_validate_key_attributes(attributes, p_drv);
1779 if (status != PSA_SUCCESS) {
1780 return status;
1781 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1784 if (status != PSA_SUCCESS) {
1785 return status;
1786 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 slot = *p_slot;
1788
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001789 /* We're storing the declared bit-size of the key. It's up to each
1790 * creation mechanism to verify that this information is correct.
1791 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001792 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001793 * is optional (import, copy). In case of a volatile key, assign it the
1794 * volatile key identifier associated to the slot returned to contain its
1795 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001796
1797 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001798 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001799#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1800 slot->attr.id = volatile_key_id;
1801#else
1802 slot->attr.id.key_id = volatile_key_id;
1803#endif
1804 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001805
Gilles Peskine91e8c332019-08-02 19:19:39 +02001806 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001807 * external-only flags, query `attributes`. Thanks to the check
1808 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001809 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001810 * may have set. */
1811 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001812
Gilles Peskinecbaff462019-07-12 23:46:04 +02001813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001814 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001815 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001816 * create the key file in internal storage, create the
1817 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001818 * persistent data. This is done by starting a transaction that will
1819 * encompass these three actions.
1820 * For registering a volatile key, we just need to find an appropriate
1821 * slot number inside the SE. Since the key is designated volatile, creating
1822 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001823 /* The first thing to do is to find a slot number for the new key.
1824 * We save the slot number in persistent storage as part of the
1825 * transaction data. It will be needed to recover if the power
1826 * fails during the key creation process, to clean up on the secure
1827 * element side after restarting. Obtaining a slot number from the
1828 * secure element driver updates its persistent state, but we do not yet
1829 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001830 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001832 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1834 &slot_number);
1835 if (status != PSA_SUCCESS) {
1836 return status;
1837 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1840 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001841 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001842 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001844 status = psa_crypto_save_transaction();
1845 if (status != PSA_SUCCESS) {
1846 (void) psa_crypto_stop_transaction();
1847 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001849 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001850
1851 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001853 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001856 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001858 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001862}
Gilles Peskine4747d192019-04-17 15:05:45 +02001863
1864/** Finalize the creation of a key once its key material has been set.
1865 *
1866 * This entails writing the key to persistent storage.
1867 *
1868 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001869 * See the documentation of psa_start_key_creation() for the intended use
1870 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 *
Ronald Cron5c522922020-11-14 16:35:34 +01001872 * If the finalization succeeds, the function unlocks the key slot (it was
1873 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1874 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001875 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001876 * \param[in,out] slot Pointer to the slot with key material.
1877 * \param[in] driver The secure element driver for the key,
1878 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001879 * \param[out] key On success, identifier of the key. Note that the
1880 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001881 *
1882 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001883 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1885 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1886 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1887 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1888 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1889 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001890 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001891 * \return If this function fails, the key slot is an invalid state.
1892 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001894static psa_status_t psa_finish_key_creation(
1895 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001896 psa_se_drv_table_entry_t *driver,
1897 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001898{
1899 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001900 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001901 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001902
1903#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001904 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001905#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001907 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001908 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001910
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001911 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1912 sizeof(data.slot_number),
1913 "Slot number size does not match psa_se_key_data_storage_t");
1914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001915 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1916 status = psa_save_persistent_key(&slot->attr,
1917 (uint8_t *) &data,
1918 sizeof(data));
1919 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1921 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001922 /* Key material is saved in export representation in the slot, so
1923 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 status = psa_save_persistent_key(&slot->attr,
1925 slot->key.data,
1926 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001927 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001928 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001929#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1930
Gilles Peskinecbaff462019-07-12 23:46:04 +02001931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001932 /* Finish the transaction for a key creation. This does not
1933 * happen when registering an existing key. Detect this case
1934 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001935 * creation of a persistent key in a secure element requires a transaction,
1936 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001937 if (driver != NULL &&
1938 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1939 status = psa_save_se_persistent_data(driver);
1940 if (status != PSA_SUCCESS) {
1941 psa_destroy_persistent_key(slot->attr.id);
1942 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001945 }
1946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001949 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950 status = psa_unlock_key_slot(slot);
1951 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001952 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001953 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001954 }
Ronald Cron50972942020-11-14 11:28:25 +01001955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001957}
1958
1959/** Abort the creation of a key.
1960 *
1961 * You may call this function after calling psa_start_key_creation(),
1962 * or after psa_finish_key_creation() fails. In other circumstances, this
1963 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001964 * See the documentation of psa_start_key_creation() for the intended use
1965 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 * \param[in,out] slot Pointer to the slot with key material.
1968 * \param[in] driver The secure element driver for the key,
1969 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971static void psa_fail_key_creation(psa_key_slot_t *slot,
1972 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001973{
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 (void) driver;
1975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001976 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001979
1980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001981 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001982 * element, and the failure happened later (when saving metadata
1983 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001984 * element.
1985 * https://github.com/ARMmbed/mbed-crypto/issues/217
1986 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001987
Gilles Peskined7729582019-08-05 15:55:54 +02001988 /* Abort the ongoing transaction if any (there may not be one if
1989 * the creation process failed before starting one, or if the
1990 * key creation is a registration of a key in a secure element).
1991 * Earlier functions must already have done what it takes to undo any
1992 * partial creation. All that's left is to update the transaction data
1993 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001995#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001998}
1999
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002000/** Validate optional attributes during key creation.
2001 *
2002 * Some key attributes are optional during key creation. If they are
2003 * specified in the attributes structure, check that they are consistent
2004 * with the data in the slot.
2005 *
2006 * This function should be called near the end of key creation, after
2007 * the slot in memory is fully populated but before saving persistent data.
2008 */
2009static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002013 if (attributes->core.type != 0) {
2014 if (attributes->core.type != slot->attr.type) {
2015 return PSA_ERROR_INVALID_ARGUMENT;
2016 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 }
2018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002020#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2022 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002023 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002024 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002026
Ronald Cron90857082020-11-25 14:58:33 +01002027 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 slot->attr.type,
2029 slot->key.data,
2030 slot->key.bytes,
2031 &rsa);
2032 if (status != PSA_SUCCESS) {
2033 return status;
2034 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 mbedtls_mpi_init(&actual);
2037 mbedtls_mpi_init(&required);
2038 ret = mbedtls_rsa_export(rsa,
2039 NULL, NULL, NULL, NULL, &actual);
2040 mbedtls_rsa_free(rsa);
2041 mbedtls_free(rsa);
2042 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 }
2045 ret = mbedtls_mpi_read_binary(&required,
2046 attributes->domain_parameters,
2047 attributes->domain_parameters_size);
2048 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 }
2051 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 }
2054rsa_exit:
2055 mbedtls_mpi_free(&actual);
2056 mbedtls_mpi_free(&required);
2057 if (ret != 0) {
2058 return mbedtls_to_psa_error(ret);
2059 }
2060 } else
John Durkop9814fa22020-11-04 12:28:15 -08002061#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2062 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002064 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065 }
2066 }
2067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068 if (attributes->core.bits != 0) {
2069 if (attributes->core.bits != slot->attr.bits) {
2070 return PSA_ERROR_INVALID_ARGUMENT;
2071 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 }
2073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075}
2076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002078 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 size_t data_length,
2080 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002081{
2082 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002083 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002084 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002085 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002086 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087
Ronald Cron81709fc2020-11-14 12:10:32 +01002088 *key = MBEDTLS_SVC_KEY_ID_INIT;
2089
Gilles Peskine0f84d622019-09-12 19:03:13 +02002090 /* Reject zero-length symmetric keys (including raw data key objects).
2091 * This also rejects any key which might be encoded as an empty string,
2092 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 if (data_length == 0) {
2094 return PSA_ERROR_INVALID_ARGUMENT;
2095 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002096
Ryan Everett6b970252024-01-19 16:02:59 +00002097 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2100 &slot, &driver);
2101 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002102 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002104
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002105 /* In the case of a transparent key or an opaque key stored in local
2106 * storage (thus not in the case of generating a key in a secure element
2107 * or cryptoprocessor with storage), we have to allocate a buffer to
2108 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 if (slot->key.data == NULL) {
2110 status = psa_allocate_buffer_to_slot(slot, data_length);
2111 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002112 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002114 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002115
2116 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 status = psa_driver_wrapper_import_key(attributes,
2118 data, data_length,
2119 slot->key.data,
2120 slot->key.bytes,
2121 &slot->key.bytes, &bits);
2122 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002123 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002127 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002129 status = PSA_ERROR_INVALID_ARGUMENT;
2130 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002131 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 status = psa_validate_optional_attributes(slot, attributes);
2134 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002135 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002138 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002140 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 if (status != PSA_SUCCESS) {
2142 psa_fail_key_creation(slot, driver);
2143 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002146}
2147
Gilles Peskined7729582019-08-05 15:55:54 +02002148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2149psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002151{
2152 psa_status_t status;
2153 psa_key_slot_t *slot = NULL;
2154 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002156
2157 /* Leaving attributes unspecified is not currently supported.
2158 * It could make sense to query the key type and size from the
2159 * secure element, but not all secure elements support this
2160 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2162 return PSA_ERROR_NOT_SUPPORTED;
2163 }
2164 if (psa_get_key_bits(attributes) == 0) {
2165 return PSA_ERROR_NOT_SUPPORTED;
2166 }
Gilles Peskined7729582019-08-05 15:55:54 +02002167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2169 &slot, &driver);
2170 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002171 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 }
Gilles Peskined7729582019-08-05 15:55:54 +02002173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002175
2176exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002177 if (status != PSA_SUCCESS) {
2178 psa_fail_key_creation(slot, driver);
2179 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002180
Gilles Peskined7729582019-08-05 15:55:54 +02002181 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 psa_close_key(key);
2183 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002184}
2185#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2188 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002189{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 psa_status_t status = psa_copy_key_material_into_slot(target,
2191 source->key.data,
2192 source->key.bytes);
2193 if (status != PSA_SUCCESS) {
2194 return status;
2195 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002196
Steven Cooreman398aee52020-10-13 14:35:45 +02002197 target->attr.type = source->attr.type;
2198 target->attr.bits = source->attr.bits;
2199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002200 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002201}
2202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2204 const psa_key_attributes_t *specified_attributes,
2205 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002206{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002208 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209 psa_key_slot_t *source_slot = NULL;
2210 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002212 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002213
Ronald Cron81709fc2020-11-14 12:10:32 +01002214 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2215
Ronald Cron5c522922020-11-14 16:35:34 +01002216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2218 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002219 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222 status = psa_validate_optional_attributes(source_slot,
2223 specified_attributes);
2224 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002225 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 status = psa_restrict_key_policy(source_slot->attr.type,
2229 &actual_attributes.core.policy,
2230 &source_slot->attr.policy);
2231 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2236 &target_slot, &driver);
2237 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002239 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002243 /* Copying to a secure element is not implemented yet. */
2244 status = PSA_ERROR_NOT_SUPPORTED;
2245 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002246 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002247#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002250 /*
2251 * Copying through an opaque driver is not implemented yet, consider
2252 * a lifetime with an external location as an invalid parameter for
2253 * now.
2254 */
2255 status = PSA_ERROR_INVALID_ARGUMENT;
2256 goto exit;
2257 }
2258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259 status = psa_copy_key_material(source_slot, target_slot);
2260 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002261 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002265exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 if (status != PSA_SUCCESS) {
2267 psa_fail_key_creation(target_slot, driver);
2268 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002273}
2274
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002275
2276
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002277/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002278/* Message digests */
2279/****************************************************************/
2280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 if (operation->id == 0) {
2285 return PSA_SUCCESS;
2286 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002289 operation->id = 0;
2290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292}
2293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2295 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002298
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002299 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002301 status = PSA_ERROR_BAD_STATE;
2302 goto exit;
2303 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002306 status = PSA_ERROR_INVALID_ARGUMENT;
2307 goto exit;
2308 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002309
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002310 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2311 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002314 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002315
2316exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002317 if (status != PSA_SUCCESS) {
2318 psa_hash_abort(operation);
2319 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002320
2321 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322}
2323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002325 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002329 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002331 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002332 status = PSA_ERROR_BAD_STATE;
2333 goto exit;
2334 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002335
Steven Cooremanc8288352021-03-04 14:02:19 +01002336 /* Don't require hash implementations to behave correctly on a
2337 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 if (input_length == 0) {
2339 return PSA_SUCCESS;
2340 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002341
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002342 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002344
2345exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 if (status != PSA_SUCCESS) {
2347 psa_hash_abort(operation);
2348 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002349
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002350 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352}
2353
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002354static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002355 uint8_t *hash,
2356 size_t hash_size,
2357 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2360
Steven Cooremanfbe09282021-03-08 18:41:12 +01002361 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 if (operation->id == 0) {
2363 return PSA_ERROR_BAD_STATE;
2364 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002366 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 operation, hash, hash_size, hash_length);
2368 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002369
2370 return status;
2371}
2372
2373psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2374 uint8_t *hash_external,
2375 size_t hash_size,
2376 size_t *hash_length)
2377{
2378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2379 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2380
2381 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2382 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2383
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002384#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002385exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002386#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002387 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389}
2390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002392 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002395 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2398 LOCAL_INPUT_DECLARE(hash_external, hash);
2399
2400 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002401 operation,
2402 actual_hash, sizeof(actual_hash),
2403 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002406 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002410 status = PSA_ERROR_INVALID_SIGNATURE;
2411 goto exit;
2412 }
2413
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002414 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002416 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002418
2419exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2421 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002422 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002424 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002425 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426}
2427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002429 const uint8_t *input_external, size_t input_length,
2430 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002431 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002432{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2434 LOCAL_INPUT_DECLARE(input_external, input);
2435 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2436
Steven Cooremanfbe09282021-03-08 18:41:12 +01002437 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002438 if (!PSA_ALG_IS_HASH(alg)) {
2439 return PSA_ERROR_INVALID_ARGUMENT;
2440 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002441
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002442 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2443 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2444 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002445 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002446
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002447#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002448exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002449#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002450 LOCAL_INPUT_FREE(input_external, input);
2451 LOCAL_OUTPUT_FREE(hash_external, hash);
2452 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002453}
2454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002456 const uint8_t *input_external, size_t input_length,
2457 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002458{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002459 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002460 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2462
2463 LOCAL_INPUT_DECLARE(input_external, input);
2464 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002467 status = PSA_ERROR_INVALID_ARGUMENT;
2468 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002470
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002471 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2472 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473 alg, input, input_length,
2474 actual_hash, sizeof(actual_hash),
2475 &actual_hash_length);
2476 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002477 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478 }
2479 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002480 status = PSA_ERROR_INVALID_SIGNATURE;
2481 goto exit;
2482 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002483
2484 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002486 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002488
2489exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002491
2492 LOCAL_INPUT_FREE(input_external, input);
2493 LOCAL_INPUT_FREE(hash_external, hash);
2494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002495 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002496}
2497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2499 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002500{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 if (source_operation->id == 0 ||
2502 target_operation->id != 0) {
2503 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002504 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2507 target_operation);
2508 if (status != PSA_SUCCESS) {
2509 psa_hash_abort(target_operation);
2510 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002513}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514
2515
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517/* MAC */
2518/****************************************************************/
2519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002521{
Steven Cooreman07897832021-03-19 18:28:56 +01002522 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 if (operation->id == 0) {
2524 return PSA_SUCCESS;
2525 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002528 operation->mac_size = 0;
2529 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002530 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002532 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002533}
2534
Ronald Cron1c650a12021-06-17 16:33:22 +02002535static psa_status_t psa_mac_finalize_alg_and_key_validation(
2536 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002537 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002538 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002541 psa_key_type_t key_type = psa_get_key_type(attributes);
2542 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 if (!PSA_ALG_IS_MAC(alg)) {
2545 return PSA_ERROR_INVALID_ARGUMENT;
2546 }
Steven Cooreman07897832021-03-19 18:28:56 +01002547
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002548 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 status = psa_mac_key_can_do(alg, key_type);
2550 if (status != PSA_SUCCESS) {
2551 return status;
2552 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002553
Steven Cooremana6474de2021-05-10 11:13:41 +02002554 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002558 /* A very short MAC is too short for security since it can be
2559 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2560 * so we make this our minimum, even though 32 bits is still
2561 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002562 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002563 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2566 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002567 /* It's impossible to "truncate" to a larger length than the full length
2568 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002570 }
2571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002573 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2574 * that is disabled in the compile-time configuration. The result can
2575 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2576 * configuration into account. In this case, force a return of
2577 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2578 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2579 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2580 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2581 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002582 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002583 }
2584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002586}
2587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002588static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2589 mbedtls_svc_key_id_t key,
2590 psa_algorithm_t alg,
2591 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002592{
2593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2594 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002595 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002596 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002597
2598 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002599 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002600 status = PSA_ERROR_BAD_STATE;
2601 goto exit;
2602 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002603
2604 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 key,
2606 &slot,
2607 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2608 alg);
2609 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002610 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002612
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002613 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002614 .core = slot->attr
2615 };
2616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2618 &operation->mac_size);
2619 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002620 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002622
2623 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002624 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 if (is_sign) {
2626 status = psa_driver_wrapper_mac_sign_setup(operation,
2627 &attributes,
2628 slot->key.data,
2629 slot->key.bytes,
2630 alg);
2631 } else {
2632 status = psa_driver_wrapper_mac_verify_setup(operation,
2633 &attributes,
2634 slot->key.data,
2635 slot->key.bytes,
2636 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002638
Gilles Peskinefbfac682018-07-08 20:51:54 +02002639exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640 if (status != PSA_SUCCESS) {
2641 psa_mac_abort(operation);
2642 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647}
2648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2650 mbedtls_svc_key_id_t key,
2651 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002652{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002654}
2655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002656psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2657 mbedtls_svc_key_id_t key,
2658 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002659{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002660 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002661}
2662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002664 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002665 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2668 LOCAL_INPUT_DECLARE(input_external, input);
2669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002671 status = PSA_ERROR_BAD_STATE;
2672 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674
Steven Cooreman11743f92021-03-19 18:38:46 +01002675 /* Don't require hash implementations to behave correctly on a
2676 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002678 status = PSA_SUCCESS;
2679 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002681
Thomas Daubney324f7de2024-01-18 13:18:58 +00002682 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2683 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685 if (status != PSA_SUCCESS) {
2686 psa_mac_abort(operation);
2687 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002688
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002689#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002690exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002691#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002692 LOCAL_INPUT_FREE(input_external, input);
2693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002694 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695}
2696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002698 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 size_t mac_size,
2700 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002701{
Steven Cooreman87885df2021-03-19 19:04:39 +01002702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2703 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002704 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002705 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002708 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002709 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002710 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002713 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002714 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002715 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002716
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002717 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2718 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002720 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002721 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002722 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002725 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002726 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002727 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002729 status = psa_driver_wrapper_mac_sign_finish(operation,
2730 mac, operation->mac_size,
2731 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002732
Dave Rodgman478ab542021-06-25 09:09:02 +01002733exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002734 /* In case of success, set the potential excess room in the output buffer
2735 * to an invalid value, to avoid potentially leaking a longer MAC.
2736 * In case of error, set the output length and content to a safe default,
2737 * such that in case the caller misses an error check, the output would be
2738 * an unachievable MAC.
2739 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002741 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002742 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002743 }
mohammad16036df908f2018-04-02 08:34:15 -07002744
Thomas Daubney480347d2024-02-01 19:00:26 +00002745 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 memset(&mac[operation->mac_size], '!',
2747 mac_size - operation->mac_size);
2748 }
Ronald Cron882eb782021-06-18 13:05:48 +02002749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002751 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002754}
2755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002757 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759{
Steven Cooreman87885df2021-03-19 19:04:39 +01002760 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2761 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002762 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002765 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002766 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002767 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002770 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002771 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002772 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002775 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002776 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002777 }
2778
Thomas Daubney324f7de2024-01-18 13:18:58 +00002779 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780 status = psa_driver_wrapper_mac_verify_finish(operation,
2781 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002782
Dave Rodgman478ab542021-06-25 09:09:02 +01002783exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002784 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002785 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788}
2789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2791 psa_algorithm_t alg,
2792 const uint8_t *input,
2793 size_t input_length,
2794 uint8_t *mac,
2795 size_t mac_size,
2796 size_t *mac_length,
2797 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002798{
2799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002801 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002802 psa_key_slot_t *slot;
2803 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804
Ronald Crondbb86462021-06-17 17:17:20 +02002805 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002806 key,
2807 &slot,
2808 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2809 alg);
2810 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002811 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002813
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002814 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002815 .core = slot->attr
2816 };
2817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2819 &operation_mac_size);
2820 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002821 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002825 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002826 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002827 }
2828
2829 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 &attributes,
2831 slot->key.data, slot->key.bytes,
2832 alg,
2833 input, input_length,
2834 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002835
2836exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002837 /* In case of success, set the potential excess room in the output buffer
2838 * to an invalid value, to avoid potentially leaking a longer MAC.
2839 * In case of error, set the output length and content to a safe default,
2840 * such that in case the caller misses an error check, the output would be
2841 * an unachievable MAC.
2842 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002844 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002845 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002846 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 if (mac_size > operation_mac_size) {
2848 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2849 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002854}
2855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002857 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002858 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002859 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002860 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 size_t mac_size,
2862 size_t *mac_length)
2863{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2865 LOCAL_INPUT_DECLARE(input_external, input);
2866 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2867
2868 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2869 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2870 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002871 input, input_length,
2872 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002873
2874#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002875exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002876#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002877 LOCAL_INPUT_FREE(input_external, input);
2878 LOCAL_OUTPUT_FREE(mac_external, mac);
2879
2880 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881}
2882
2883psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2884 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002885 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002887 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002889{
2890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002891 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2892 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002893 LOCAL_INPUT_DECLARE(input_external, input);
2894 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002895
Thomas Daubney324f7de2024-01-18 13:18:58 +00002896 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 status = psa_mac_compute_internal(key, alg,
2898 input, input_length,
2899 actual_mac, sizeof(actual_mac),
2900 &actual_mac_length, 0);
2901 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002902 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002906 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002907 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002908 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002909
2910 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002912 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002913 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002914 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002915
2916exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002918 LOCAL_INPUT_FREE(input_external, input);
2919 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002922}
Gilles Peskine20035e32018-02-03 22:44:14 +01002923
Gilles Peskine20035e32018-02-03 22:44:14 +01002924/****************************************************************/
2925/* Asymmetric cryptography */
2926/****************************************************************/
2927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2929 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002930{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 if (input_is_message) {
2932 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2933 return PSA_ERROR_INVALID_ARGUMENT;
2934 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2937 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2938 return PSA_ERROR_INVALID_ARGUMENT;
2939 }
2940 }
2941 } else {
2942 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2943 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002944 }
2945 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002948}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2951 int input_is_message,
2952 psa_algorithm_t alg,
2953 const uint8_t *input,
2954 size_t input_length,
2955 uint8_t *signature,
2956 size_t signature_size,
2957 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002958{
2959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002961 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002962 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002963
2964 *signature_length = 0;
2965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002966 status = psa_sign_verify_check_alg(input_is_message, alg);
2967 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002968 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002970
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002971 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002972 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002973 * buffer can in principle be empty since it doesn't actually have
2974 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 if (signature_size == 0) {
2976 return PSA_ERROR_BUFFER_TOO_SMALL;
2977 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002978
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002979 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 key, &slot,
2981 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2982 PSA_KEY_USAGE_SIGN_HASH,
2983 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002985 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002986 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002990 status = PSA_ERROR_INVALID_ARGUMENT;
2991 goto exit;
2992 }
2993
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002994 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002996 };
2997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002998 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002999 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003000 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003001 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003002 signature, signature_size, signature_length);
3003 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003004
3005 status = psa_driver_wrapper_sign_hash(
3006 &attributes, slot->key.data, slot->key.bytes,
3007 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003009 }
3010
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003011
3012exit:
3013 /* Fill the unused part of the output buffer (the whole buffer on error,
3014 * the trailing part on success) with something that isn't a valid signature
3015 * (barring an attack on the signature and deliberately-crafted input),
3016 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 if (status == PSA_SUCCESS) {
3018 memset(signature + *signature_length, '!',
3019 signature_size - *signature_length);
3020 } else {
3021 memset(signature, '!', signature_size);
3022 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003023 /* If signature_size is 0 then we have nothing to do. We must not call
3024 * memset because signature may be NULL in this case. */
3025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003028 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003029}
3030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3032 int input_is_message,
3033 psa_algorithm_t alg,
3034 const uint8_t *input,
3035 size_t input_length,
3036 const uint8_t *signature,
3037 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003038{
3039 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3040 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3041 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 status = psa_sign_verify_check_alg(input_is_message, alg);
3044 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003045 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003046 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003047
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003048 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 key, &slot,
3050 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3051 PSA_KEY_USAGE_VERIFY_HASH,
3052 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 if (status != PSA_SUCCESS) {
3055 return status;
3056 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003057
3058 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003060 };
3061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003063 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003064 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003065 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 signature, signature_length);
3067 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003068 status = psa_driver_wrapper_verify_hash(
3069 &attributes, slot->key.data, slot->key.bytes,
3070 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003071 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003072 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003077
3078}
3079
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003080psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003081 const psa_key_attributes_t *attributes,
3082 const uint8_t *key_buffer,
3083 size_t key_buffer_size,
3084 psa_algorithm_t alg,
3085 const uint8_t *input,
3086 size_t input_length,
3087 uint8_t *signature,
3088 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003090{
3091 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003094 size_t hash_length;
3095 uint8_t hash[PSA_HASH_MAX_SIZE];
3096
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003097 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003098 PSA_ALG_SIGN_GET_HASH(alg),
3099 input, input_length,
3100 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003103 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003105
3106 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 attributes, key_buffer, key_buffer_size,
3108 alg, hash, hash_length,
3109 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003110 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003112 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003113}
3114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3116 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003117 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003119 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 size_t signature_size,
3121 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003122{
Thomas Daubney290aac42024-01-18 17:23:02 +00003123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3124 LOCAL_INPUT_DECLARE(input_external, input);
3125 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3126
3127 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3128 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3129 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3130 signature_size, signature_length);
3131
Thomas Daubneyf446b892024-01-30 13:36:01 +00003132#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003133exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003134#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003135 LOCAL_INPUT_FREE(input_external, input);
3136 LOCAL_OUTPUT_FREE(signature_external, signature);
3137 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003138}
3139
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003140psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003141 const psa_key_attributes_t *attributes,
3142 const uint8_t *key_buffer,
3143 size_t key_buffer_size,
3144 psa_algorithm_t alg,
3145 const uint8_t *input,
3146 size_t input_length,
3147 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003148 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003149{
3150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003153 size_t hash_length;
3154 uint8_t hash[PSA_HASH_MAX_SIZE];
3155
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003156 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 PSA_ALG_SIGN_GET_HASH(alg),
3158 input, input_length,
3159 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003162 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003164
3165 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 attributes, key_buffer, key_buffer_size,
3167 alg, hash, hash_length,
3168 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003169 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003172}
3173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003174psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3175 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003176 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003178 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003180{
Thomas Daubney290aac42024-01-18 17:23:02 +00003181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3182 LOCAL_INPUT_DECLARE(input_external, input);
3183 LOCAL_INPUT_DECLARE(signature_external, signature);
3184
3185 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3186 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3187 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3188 signature_length);
3189
Thomas Daubneyf446b892024-01-30 13:36:01 +00003190#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003191exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003192#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003193 LOCAL_INPUT_FREE(input_external, input);
3194 LOCAL_INPUT_FREE(signature_external, signature);
3195
3196 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003197}
3198
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003199psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003200 const psa_key_attributes_t *attributes,
3201 const uint8_t *key_buffer, size_t key_buffer_size,
3202 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003204{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3206 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3207 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003208#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003209 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3210 return mbedtls_psa_rsa_sign_hash(
3211 attributes,
3212 key_buffer, key_buffer_size,
3213 alg, hash, hash_length,
3214 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003215#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3216 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 } else {
3218 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003219 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3221 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003222#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3224 return mbedtls_psa_ecdsa_sign_hash(
3225 attributes,
3226 key_buffer, key_buffer_size,
3227 alg, hash, hash_length,
3228 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003229#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3230 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 } else {
3232 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003233 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003234 }
Ronald Cron4501c982021-03-19 20:09:32 +01003235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 (void) key_buffer;
3237 (void) key_buffer_size;
3238 (void) hash;
3239 (void) hash_length;
3240 (void) signature;
3241 (void) signature_size;
3242 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003245}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3248 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003249 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003251 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 size_t signature_size,
3253 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003254{
Thomas Daubney290aac42024-01-18 17:23:02 +00003255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3256 LOCAL_INPUT_DECLARE(hash_external, hash);
3257 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3258
3259 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3260 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3261 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3262 signature_size, signature_length);
3263
Thomas Daubneyf446b892024-01-30 13:36:01 +00003264#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003265exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003266#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003267 LOCAL_INPUT_FREE(hash_external, hash);
3268 LOCAL_OUTPUT_FREE(signature_external, signature);
3269
3270 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003271}
3272
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003273psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003274 const psa_key_attributes_t *attributes,
3275 const uint8_t *key_buffer, size_t key_buffer_size,
3276 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003278{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3280 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3281 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003282#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3284 return mbedtls_psa_rsa_verify_hash(
3285 attributes,
3286 key_buffer, key_buffer_size,
3287 alg, hash, hash_length,
3288 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003289#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3290 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003291 } else {
3292 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003293 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3295 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003296#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003297 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3298 return mbedtls_psa_ecdsa_verify_hash(
3299 attributes,
3300 key_buffer, key_buffer_size,
3301 alg, hash, hash_length,
3302 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003303#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3304 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 } else {
3306 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003307 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003308 }
Ronald Cron4501c982021-03-19 20:09:32 +01003309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003310 (void) key_buffer;
3311 (void) key_buffer_size;
3312 (void) hash;
3313 (void) hash_length;
3314 (void) signature;
3315 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003318}
3319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3321 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003322 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003323 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003324 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003326{
Thomas Daubney290aac42024-01-18 17:23:02 +00003327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3328 LOCAL_INPUT_DECLARE(hash_external, hash);
3329 LOCAL_INPUT_DECLARE(signature_external, signature);
3330
3331 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3332 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3333 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3334 signature_length);
3335
Thomas Daubneyf446b892024-01-30 13:36:01 +00003336#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003337exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003338#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003339 LOCAL_INPUT_FREE(hash_external, hash);
3340 LOCAL_INPUT_FREE(signature_external, signature);
3341
3342 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003343}
3344
John Durkop0e005192020-10-31 22:06:54 -07003345#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3347 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003348{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3350 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3351 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3352 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003353}
John Durkop0e005192020-10-31 22:06:54 -07003354#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3357 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003358 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003360 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003362 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 size_t output_size,
3364 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003368 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003369 LOCAL_INPUT_DECLARE(input_external, input);
3370 LOCAL_INPUT_DECLARE(salt_external, salt);
3371 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003372
Darryl Green5cc689a2018-07-24 15:34:10 +01003373 (void) input;
3374 (void) input_length;
3375 (void) salt;
3376 (void) output;
3377 (void) output_size;
3378
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003379 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3382 return PSA_ERROR_INVALID_ARGUMENT;
3383 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003384
Ronald Cron5c522922020-11-14 16:35:34 +01003385 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3387 if (status != PSA_SUCCESS) {
3388 return status;
3389 }
3390 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3391 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003392 status = PSA_ERROR_INVALID_ARGUMENT;
3393 goto exit;
3394 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003397#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003399 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3401 slot->key.data,
3402 slot->key.bytes,
3403 &rsa);
3404 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003405 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003409 status = PSA_ERROR_BUFFER_TOO_SMALL;
3410 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003411 }
Ronald Cron7207d572021-09-08 14:28:35 +02003412#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3413 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003414 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3415 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3416 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003418#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003419 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420 mbedtls_rsa_pkcs1_encrypt(rsa,
3421 mbedtls_psa_get_random,
3422 MBEDTLS_PSA_RANDOM_STATE,
3423 MBEDTLS_RSA_PUBLIC,
3424 input_length,
3425 input,
3426 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003427#else
3428 status = PSA_ERROR_NOT_SUPPORTED;
3429#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003430 } else
3431 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003432#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003434 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3436 mbedtls_psa_get_random,
3437 MBEDTLS_PSA_RANDOM_STATE,
3438 MBEDTLS_RSA_PUBLIC,
3439 salt, salt_length,
3440 input_length,
3441 input,
3442 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003443#else
3444 status = PSA_ERROR_NOT_SUPPORTED;
3445#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003447 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003448 }
Ronald Cron7207d572021-09-08 14:28:35 +02003449#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003451rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 if (status == PSA_SUCCESS) {
3453 *output_length = mbedtls_rsa_get_len(rsa);
3454 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 mbedtls_rsa_free(rsa);
3457 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003458#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003459 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003461 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003462 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003463
3464exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003466
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003467 LOCAL_INPUT_FREE(input_external, input);
3468 LOCAL_INPUT_FREE(salt_external, salt);
3469 LOCAL_OUTPUT_FREE(output_external, output);
3470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003472}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3475 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003476 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003478 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003480 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003481 size_t output_size,
3482 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003483{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003485 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003486 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003487 LOCAL_INPUT_DECLARE(input_external, input);
3488 LOCAL_INPUT_DECLARE(salt_external, salt);
3489 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003490
Darryl Green5cc689a2018-07-24 15:34:10 +01003491 (void) input;
3492 (void) input_length;
3493 (void) salt;
3494 (void) output;
3495 (void) output_size;
3496
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003497 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3500 return PSA_ERROR_INVALID_ARGUMENT;
3501 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003502
Ronald Cron5c522922020-11-14 16:35:34 +01003503 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3505 if (status != PSA_SUCCESS) {
3506 return status;
3507 }
3508 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003509 status = PSA_ERROR_INVALID_ARGUMENT;
3510 goto exit;
3511 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003514#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003516 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003517 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3518 slot->key.data,
3519 slot->key.bytes,
3520 &rsa);
3521 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003522 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003525 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003526 status = PSA_ERROR_INVALID_ARGUMENT;
3527 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003528 }
Ronald Cron7207d572021-09-08 14:28:35 +02003529#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3530 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003531 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3532 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3533 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003535#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003536 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 mbedtls_rsa_pkcs1_decrypt(rsa,
3538 mbedtls_psa_get_random,
3539 MBEDTLS_PSA_RANDOM_STATE,
3540 MBEDTLS_RSA_PRIVATE,
3541 output_length,
3542 input,
3543 output,
3544 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003545#else
3546 status = PSA_ERROR_NOT_SUPPORTED;
3547#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003548 } else
3549 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003550#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003552 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3554 mbedtls_psa_get_random,
3555 MBEDTLS_PSA_RANDOM_STATE,
3556 MBEDTLS_RSA_PRIVATE,
3557 salt, salt_length,
3558 output_length,
3559 input,
3560 output,
3561 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003562#else
3563 status = PSA_ERROR_NOT_SUPPORTED;
3564#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003566 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003568
Ronald Cron7207d572021-09-08 14:28:35 +02003569#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003571rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003572 mbedtls_rsa_free(rsa);
3573 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003574#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3575 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003577 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003578 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003579
3580exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003582
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003583 LOCAL_INPUT_FREE(input_external, input);
3584 LOCAL_INPUT_FREE(salt_external, salt);
3585 LOCAL_OUTPUT_FREE(output_external, output);
3586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003588}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003589
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003590
3591
mohammad1603503973b2018-03-12 15:59:30 +02003592/****************************************************************/
3593/* Symmetric cryptography */
3594/****************************************************************/
3595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3597 mbedtls_svc_key_id_t key,
3598 psa_algorithm_t alg,
3599 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003600{
3601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3602 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003603 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003604 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3606 PSA_KEY_USAGE_ENCRYPT :
3607 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003608
3609 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003611 status = PSA_ERROR_BAD_STATE;
3612 goto exit;
3613 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003616 status = PSA_ERROR_INVALID_ARGUMENT;
3617 goto exit;
3618 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3621 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003622 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003624
Ronald Cron49fafa92021-03-10 08:34:23 +01003625 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003626 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003627 * so we only set it (in the driver wrapper) after resources have been
3628 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003629 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003631 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003633 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003635 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003636 }
3637 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003638
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003639 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003641 };
3642
Ronald Cronab99ac22020-10-01 16:38:28 +02003643 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 if (cipher_operation == MBEDTLS_ENCRYPT) {
3645 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3646 &attributes,
3647 slot->key.data,
3648 slot->key.bytes,
3649 alg);
3650 } else {
3651 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3652 &attributes,
3653 slot->key.data,
3654 slot->key.bytes,
3655 alg);
3656 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003657
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003658exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003659 if (status != PSA_SUCCESS) {
3660 psa_cipher_abort(operation);
3661 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003666}
3667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3669 mbedtls_svc_key_id_t key,
3670 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003671{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003673}
3674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003675psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3676 mbedtls_svc_key_id_t key,
3677 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003678{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003680}
3681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3683 uint8_t *iv,
3684 size_t iv_size,
3685 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003686{
Ronald Cron6d051732020-10-01 14:10:20 +02003687 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003688 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003689 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003692 status = PSA_ERROR_BAD_STATE;
3693 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003694 }
3695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003696 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003697 status = PSA_ERROR_BAD_STATE;
3698 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003699 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003700
Ronald Cronc423acb2021-07-05 12:31:44 +02003701 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003702 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003703 status = PSA_ERROR_BUFFER_TOO_SMALL;
3704 goto exit;
3705 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003708 status = PSA_ERROR_GENERIC_ERROR;
3709 goto exit;
3710 }
3711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003712 status = psa_generate_random(local_iv, default_iv_length);
3713 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003714 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 }
Ronald Cron5618a392021-03-26 09:52:26 +01003716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 status = psa_driver_wrapper_cipher_set_iv(operation,
3718 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003719
3720exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 if (status == PSA_SUCCESS) {
3722 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003723 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003724 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003726 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003728 }
Ronald Cron6d051732020-10-01 14:10:20 +02003729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003731}
3732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3734 const uint8_t *iv,
3735 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003736{
Ronald Cron6d051732020-10-01 14:10:20 +02003737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003740 status = PSA_ERROR_BAD_STATE;
3741 goto exit;
3742 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003745 status = PSA_ERROR_BAD_STATE;
3746 goto exit;
3747 }
Ronald Crona0d68172021-03-26 10:15:08 +01003748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003750 status = PSA_ERROR_INVALID_ARGUMENT;
3751 goto exit;
3752 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 status = psa_driver_wrapper_cipher_set_iv(operation,
3755 iv,
3756 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003757
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003758exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003760 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003761 } else {
3762 psa_cipher_abort(operation);
3763 }
3764 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003765}
3766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003768 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003770 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 size_t output_size,
3772 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003773{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003775
Gabor Mezei13a15c22024-01-24 16:56:00 +01003776 LOCAL_INPUT_DECLARE(input_external, input);
3777 LOCAL_OUTPUT_DECLARE(output_external, output);
3778
3779 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3780 LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
3781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003783 status = PSA_ERROR_BAD_STATE;
3784 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003785 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003788 status = PSA_ERROR_BAD_STATE;
3789 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003790 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 status = psa_driver_wrapper_cipher_update(operation,
3793 input,
3794 input_length,
3795 output,
3796 output_size,
3797 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003798
3799exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003800 if (status != PSA_SUCCESS) {
3801 psa_cipher_abort(operation);
3802 }
Ronald Cron6d051732020-10-01 14:10:20 +02003803
Gabor Mezei13a15c22024-01-24 16:56:00 +01003804 LOCAL_INPUT_FREE(input_external, input);
3805 LOCAL_OUTPUT_FREE(output_external, output);
3806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003808}
3809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003811 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 size_t output_size,
3813 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003814{
David Saadab4ecc272019-02-14 13:48:10 +02003815 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003816
Gabor Mezei13a15c22024-01-24 16:56:00 +01003817 LOCAL_OUTPUT_DECLARE(output_external, output);
3818
3819 LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
3820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003822 status = PSA_ERROR_BAD_STATE;
3823 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003824 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003827 status = PSA_ERROR_BAD_STATE;
3828 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003829 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 status = psa_driver_wrapper_cipher_finish(operation,
3832 output,
3833 output_size,
3834 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003835
3836exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003837 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003838 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003840 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003842 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003843
3844 LOCAL_OUTPUT_FREE(output_external, output);
3845
3846 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003847}
3848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003850{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003852 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003853 * in use. It's ok to call abort on such an object, and there's
3854 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003856 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003858 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003859
Ronald Cron49fafa92021-03-10 08:34:23 +01003860 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003861 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003862 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003864 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003865}
3866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3868 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003869 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003871 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 size_t output_size,
3873 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003874{
3875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003876 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003877 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003878 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003879 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3880 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003881
David Horstmannb80e35a2023-12-14 18:12:47 +00003882 LOCAL_INPUT_DECLARE(input_external, input);
3883 LOCAL_OUTPUT_DECLARE(output_external, output);
3884
David Horstmann926193a2023-12-13 15:55:25 +00003885 LOCAL_INPUT_ALLOC(input_external, input_length, input);
David Horstmann926193a2023-12-13 15:55:25 +00003886 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003889 status = PSA_ERROR_INVALID_ARGUMENT;
3890 goto exit;
3891 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3894 PSA_KEY_USAGE_ENCRYPT,
3895 alg);
3896 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003897 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003899
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003900 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003901 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003902 };
3903
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3905 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003906 status = PSA_ERROR_GENERIC_ERROR;
3907 goto exit;
3908 }
3909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 if (default_iv_length > 0) {
3911 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003912 status = PSA_ERROR_BUFFER_TOO_SMALL;
3913 goto exit;
3914 }
3915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003916 status = psa_generate_random(local_iv, default_iv_length);
3917 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003918 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003920 }
3921
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003922 status = psa_driver_wrapper_cipher_encrypt(
3923 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003924 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 mbedtls_buffer_offset(output, default_iv_length),
3926 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003927
3928exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 unlock_status = psa_unlock_key_slot(slot);
3930 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003931 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003932 }
Ronald Cron16377072021-07-08 19:00:07 +02003933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 if (status == PSA_SUCCESS) {
3935 if (default_iv_length > 0) {
3936 memcpy(output, local_iv, default_iv_length);
3937 }
3938 *output_length += default_iv_length;
3939 } else {
3940 *output_length = 0;
3941 }
3942
David Horstmannb80e35a2023-12-14 18:12:47 +00003943 LOCAL_INPUT_FREE(input_external, input);
3944 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003946 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003947}
3948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3950 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003951 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003953 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 size_t output_size,
3955 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003956{
3957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003959 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003960 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003961
Gabor Mezei13a15c22024-01-24 16:56:00 +01003962 LOCAL_INPUT_DECLARE(input_external, input);
3963 LOCAL_OUTPUT_DECLARE(output_external, output);
3964
3965 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3966 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
3967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003969 status = PSA_ERROR_INVALID_ARGUMENT;
3970 goto exit;
3971 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3974 PSA_KEY_USAGE_DECRYPT,
3975 alg);
3976 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003977 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003978 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003979
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003980 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003982 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003985 status = PSA_ERROR_INVALID_ARGUMENT;
3986 goto exit;
3987 }
3988
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003989 status = psa_driver_wrapper_cipher_decrypt(
3990 &attributes, slot->key.data, slot->key.bytes,
3991 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003992 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003993
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003994exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 unlock_status = psa_unlock_key_slot(slot);
3996 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003997 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003998 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004001 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 }
Ronald Cron16377072021-07-08 19:00:07 +02004003
Gabor Mezei13a15c22024-01-24 16:56:00 +01004004 LOCAL_INPUT_FREE(input_external, input);
4005 LOCAL_OUTPUT_FREE(output_external, output);
4006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004008}
4009
4010
mohammad16035955c982018-04-26 00:53:03 +03004011/****************************************************************/
4012/* AEAD */
4013/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004015psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4016 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004017 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004018 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004019 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004021 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004022 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004023 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024 size_t ciphertext_size,
4025 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004026{
Ronald Cron215633c2021-03-16 17:15:37 +01004027 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4028 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004029
David Horstmann21c1a942023-11-28 19:25:00 +00004030 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4031 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4032 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4033 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4034
mohammad1603f08a5502018-06-03 15:05:47 +03004035 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4038 return PSA_ERROR_NOT_SUPPORTED;
4039 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004040
Ronald Cron9a986162021-03-26 12:40:07 +01004041 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4043 if (status != PSA_SUCCESS) {
4044 return status;
4045 }
mohammad16035955c982018-04-26 00:53:03 +03004046
Ronald Cron215633c2021-03-16 17:15:37 +01004047 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004048 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004049 };
mohammad16035955c982018-04-26 00:53:03 +03004050
David Horstmann21c1a942023-11-28 19:25:00 +00004051 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4052 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4053 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4054 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4055
Ronald Cronde822812021-03-17 16:08:20 +01004056 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004057 &attributes, slot->key.data, slot->key.bytes,
4058 alg,
4059 nonce, nonce_length,
4060 additional_data, additional_data_length,
4061 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4065 memset(ciphertext, 0, ciphertext_size);
4066 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004067
David Horstmann21c1a942023-11-28 19:25:00 +00004068/* Exit label is only used for buffer copying, prevent unused warnings. */
4069#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4070exit:
4071#endif
4072 LOCAL_INPUT_FREE(nonce_external, nonce);
4073 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4074 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4075 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004080}
4081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4083 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004084 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004086 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004088 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004090 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 size_t plaintext_size,
4092 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004093{
Ronald Cron215633c2021-03-16 17:15:37 +01004094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4095 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004096
David Horstmann6baf6e92023-11-28 19:43:53 +00004097 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4098 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4099 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4100 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4101
Gilles Peskineee652a32018-06-01 19:23:52 +02004102 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4105 return PSA_ERROR_NOT_SUPPORTED;
4106 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004107
Ronald Cron9a986162021-03-26 12:40:07 +01004108 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4110 if (status != PSA_SUCCESS) {
4111 return status;
4112 }
mohammad16035955c982018-04-26 00:53:03 +03004113
Ronald Cron215633c2021-03-16 17:15:37 +01004114 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004116 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004117
David Horstmann6baf6e92023-11-28 19:43:53 +00004118 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4119 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4120 additional_data);
4121 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4122 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4123
Ronald Cronde822812021-03-17 16:08:20 +01004124 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004125 &attributes, slot->key.data, slot->key.bytes,
4126 alg,
4127 nonce, nonce_length,
4128 additional_data, additional_data_length,
4129 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 if (status != PSA_SUCCESS && plaintext_size != 0) {
4133 memset(plaintext, 0, plaintext_size);
4134 }
mohammad160360a64d02018-06-03 17:20:42 +03004135
David Horstmann6baf6e92023-11-28 19:43:53 +00004136/* Exit label is only used for buffer copying, prevent unused warnings. */
4137#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4138exit:
4139#endif
4140 LOCAL_INPUT_FREE(nonce_external, nonce);
4141 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4142 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4143 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 return status;
mohammad16035955c982018-04-26 00:53:03 +03004148}
4149
Gilles Peskinee59236f2018-01-27 23:32:46 +01004150/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004151/* Generators */
4152/****************************************************************/
4153
John Durkop07cc04a2020-11-16 22:08:34 -08004154#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4155 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4156 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4157#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004158#endif /* At least one builtin KDF */
4159
4160#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4163static psa_status_t psa_key_derivation_start_hmac(
4164 psa_mac_operation_t *operation,
4165 psa_algorithm_t hash_alg,
4166 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004168{
4169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4172 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4173 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004174
Steven Cooreman15f0d922021-05-07 14:14:37 +02004175 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004178 status = psa_driver_wrapper_mac_sign_setup(operation,
4179 &attributes,
4180 hmac_key, hmac_key_length,
4181 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 psa_reset_key_attributes(&attributes);
4184 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004185}
4186#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004187
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004188#define HKDF_STATE_INIT 0 /* no input yet */
4189#define HKDF_STATE_STARTED 1 /* got salt */
4190#define HKDF_STATE_KEYED 2 /* got key */
4191#define HKDF_STATE_OUTPUT 3 /* output started */
4192
Gilles Peskinecbe66502019-05-16 16:59:18 +02004193static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004195{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4197 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4198 } else {
4199 return operation->alg;
4200 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004201}
4202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004204{
4205 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4207 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004208 /* The object has (apparently) been initialized but it is not
4209 * in use. It's ok to call abort on such an object, and there's
4210 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 } else
John Durkopd0321952020-10-29 21:37:36 -07004212#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4214 mbedtls_free(operation->ctx.hkdf.info);
4215 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4216 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004217#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4218#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4219 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4221 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4222 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4223 if (operation->ctx.tls12_prf.secret != NULL) {
4224 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4225 operation->ctx.tls12_prf.secret_length);
4226 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004227 }
4228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004229 if (operation->ctx.tls12_prf.seed != NULL) {
4230 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4231 operation->ctx.tls12_prf.seed_length);
4232 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004233 }
4234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 if (operation->ctx.tls12_prf.label != NULL) {
4236 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4237 operation->ctx.tls12_prf.label_length);
4238 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004239 }
4240
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004241 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004242
4243 /* We leave the fields Ai and output_block to be erased safely by the
4244 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004245 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004246#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4247 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004248 {
4249 status = PSA_ERROR_BAD_STATE;
4250 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251 mbedtls_platform_zeroize(operation, sizeof(*operation));
4252 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253}
4254
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004257{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004259 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004261 }
4262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004265}
4266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4268 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004269{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 if (operation->alg == 0) {
4271 return PSA_ERROR_BAD_STATE;
4272 }
4273 if (capacity > operation->capacity) {
4274 return PSA_ERROR_INVALID_ARGUMENT;
4275 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004276 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004277 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004278}
4279
John Durkopd0321952020-10-29 21:37:36 -07004280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004282 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4284 psa_algorithm_t hash_alg,
4285 uint8_t *output,
4286 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004287{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004289 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004290 psa_status_t status;
4291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4293 return PSA_ERROR_BAD_STATE;
4294 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004295 hkdf->state = HKDF_STATE_OUTPUT;
4296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004298 /* Copy what remains of the current block */
4299 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004301 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302 }
4303 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004304 output += n;
4305 output_length -= n;
4306 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004308 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004310 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004311 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004313 * object was corrupted or if this function is called directly
4314 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315 if (hkdf->block_number == 0xff) {
4316 return PSA_ERROR_BAD_STATE;
4317 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004318
4319 /* We need a new block */
4320 ++hkdf->block_number;
4321 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4324 hash_alg,
4325 hkdf->prk,
4326 hash_length);
4327 if (status != PSA_SUCCESS) {
4328 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330
4331 if (hkdf->block_number != 1) {
4332 status = psa_mac_update(&hkdf->hmac,
4333 hkdf->output_block,
4334 hash_length);
4335 if (status != PSA_SUCCESS) {
4336 return status;
4337 }
4338 }
4339 status = psa_mac_update(&hkdf->hmac,
4340 hkdf->info,
4341 hkdf->info_length);
4342 if (status != PSA_SUCCESS) {
4343 return status;
4344 }
4345 status = psa_mac_update(&hkdf->hmac,
4346 &hkdf->block_number, 1);
4347 if (status != PSA_SUCCESS) {
4348 return status;
4349 }
4350 status = psa_mac_sign_finish(&hkdf->hmac,
4351 hkdf->output_block,
4352 sizeof(hkdf->output_block),
4353 &hmac_output_length);
4354 if (status != PSA_SUCCESS) {
4355 return status;
4356 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004357 }
4358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360}
John Durkop07cc04a2020-11-16 22:08:34 -08004361#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004362
John Durkop07cc04a2020-11-16 22:08:34 -08004363#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4364 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004365static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4366 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004368{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004369 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4370 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004371 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4372 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004373 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004374
Janos Follath7742fee2019-06-17 12:58:10 +01004375 /* We can't be wanting more output after block 0xff, otherwise
4376 * the capacity check in psa_key_derivation_output_bytes() would have
4377 * prevented this call. It could happen only if the operation
4378 * object was corrupted or if this function is called directly
4379 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004380 if (tls12_prf->block_number == 0xff) {
4381 return PSA_ERROR_CORRUPTION_DETECTED;
4382 }
Janos Follath7742fee2019-06-17 12:58:10 +01004383
4384 /* We need a new block */
4385 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004386 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004387
4388 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4389 *
4390 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4391 *
4392 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4393 * HMAC_hash(secret, A(2) + seed) +
4394 * HMAC_hash(secret, A(3) + seed) + ...
4395 *
4396 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004397 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004398 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004399 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004400 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004401 * is currently extracted as `output_block` and where i is
4402 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004403 */
4404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 status = psa_key_derivation_start_hmac(&hmac,
4406 hash_alg,
4407 tls12_prf->secret,
4408 tls12_prf->secret_length);
4409 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004410 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004412
4413 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004415 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4416 * the variable seed and in this instance means it in the context of the
4417 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 status = psa_mac_update(&hmac,
4419 tls12_prf->label,
4420 tls12_prf->label_length);
4421 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004422 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 }
4424 status = psa_mac_update(&hmac,
4425 tls12_prf->seed,
4426 tls12_prf->seed_length);
4427 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004428 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 }
4430 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004431 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4433 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004434 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004435 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004436 }
4437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 status = psa_mac_sign_finish(&hmac,
4439 tls12_prf->Ai, hash_length,
4440 &hmac_output_length);
4441 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004442 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004443 }
4444 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004445 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004447
4448 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449 status = psa_key_derivation_start_hmac(&hmac,
4450 hash_alg,
4451 tls12_prf->secret,
4452 tls12_prf->secret_length);
4453 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004454 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004455 }
4456 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4457 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004458 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 }
4460 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4461 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004462 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004463 }
4464 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004466 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 }
4468 status = psa_mac_sign_finish(&hmac,
4469 tls12_prf->output_block, hash_length,
4470 &hmac_output_length);
4471 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004472 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004474
Janos Follath7742fee2019-06-17 12:58:10 +01004475
4476cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 cleanup_status = psa_mac_abort(&hmac);
4478 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004479 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004483}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004484
Janos Follath844eb0e2019-06-19 12:10:49 +01004485static psa_status_t psa_key_derivation_tls12_prf_read(
4486 psa_tls12_prf_key_derivation_t *tls12_prf,
4487 psa_algorithm_t alg,
4488 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004490{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4492 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004493 psa_status_t status;
4494 uint8_t offset, length;
4495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004497 case PSA_TLS12_PRF_STATE_LABEL_SET:
4498 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4499 break;
4500 case PSA_TLS12_PRF_STATE_OUTPUT:
4501 break;
4502 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004503 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004504 }
4505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004507 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004508 if (tls12_prf->left_in_block == 0) {
4509 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4510 alg);
4511 if (status != PSA_SUCCESS) {
4512 return status;
4513 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004514
4515 continue;
4516 }
4517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004519 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004521 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004523
4524 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004526 output += length;
4527 output_length -= length;
4528 tls12_prf->left_in_block -= length;
4529 }
4530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004532}
John Durkop07cc04a2020-11-16 22:08:34 -08004533#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4534 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004535
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004536psa_status_t psa_key_derivation_output_bytes(
4537 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004538 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004540{
4541 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004542 LOCAL_OUTPUT_DECLARE(output_external, output);
4543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004549 }
4550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004552 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004553 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004554 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4555 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004556 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004557 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004559 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004560
4561 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4562 if (output_length > operation->capacity) {
4563 operation->capacity = 0;
4564 /* Go through the error path to wipe all confidential data now
4565 * that the operation object is useless. */
4566 status = PSA_ERROR_INSUFFICIENT_DATA;
4567 goto exit;
4568 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004570
John Durkopd0321952020-10-29 21:37:36 -07004571#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4573 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4574 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4575 output, output_length);
4576 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004577#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4578#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4579 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4581 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4582 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4583 kdf_alg, output,
4584 output_length);
4585 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004586#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4587 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004588 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004589 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004590 status = PSA_ERROR_BAD_STATE;
4591 LOCAL_OUTPUT_FREE(output_external, output);
4592
4593 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004594 }
4595
4596exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004598 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 * This allows us to differentiate between exhausted operations and
4600 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4601 * operations. */
4602 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004605 if (output != NULL) {
4606 memset(output, '!', output_length);
4607 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004608 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004609
4610 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004612}
4613
David Brown7807bf72021-02-09 16:28:23 -07004614#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004616{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 if (data_size >= 8) {
4618 mbedtls_des_key_set_parity(data);
4619 }
4620 if (data_size >= 16) {
4621 mbedtls_des_key_set_parity(data + 8);
4622 }
4623 if (data_size >= 24) {
4624 mbedtls_des_key_set_parity(data + 16);
4625 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004626}
David Brown7807bf72021-02-09 16:28:23 -07004627#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004628
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004629static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004630 psa_key_slot_t *slot,
4631 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004632 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004633{
4634 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004636 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004637 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 if (!key_type_is_raw_bytes(slot->attr.type)) {
4640 return PSA_ERROR_INVALID_ARGUMENT;
4641 }
4642 if (bits % 8 != 0) {
4643 return PSA_ERROR_INVALID_ARGUMENT;
4644 }
4645 data = mbedtls_calloc(1, bytes);
4646 if (data == NULL) {
4647 return PSA_ERROR_INSUFFICIENT_MEMORY;
4648 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 status = psa_key_derivation_output_bytes(operation, data, bytes);
4651 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004652 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004653 }
David Brown12ca5032021-02-11 11:02:00 -07004654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004655 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4656 psa_des_set_key_parity(data, bytes);
4657 }
David Brown8107e312021-02-12 08:08:46 -07004658#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 status = psa_allocate_buffer_to_slot(slot, bytes);
4661 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004662 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 }
Ronald Crondd04d422020-11-28 15:14:42 +01004664
Ronald Cronbf33c932020-11-28 18:06:53 +01004665 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004666 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004668 };
4669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 status = psa_driver_wrapper_import_key(&attributes,
4671 data, bytes,
4672 slot->key.data,
4673 slot->key.bytes,
4674 &slot->key.bytes, &bits);
4675 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004676 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004678
4679exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680 mbedtls_free(data);
4681 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682}
4683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4685 psa_key_derivation_operation_t *operation,
4686 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687{
4688 psa_status_t status;
4689 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004690 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004691
Ronald Cron81709fc2020-11-14 12:10:32 +01004692 *key = MBEDTLS_SVC_KEY_ID_INIT;
4693
Gilles Peskine0f84d622019-09-12 19:03:13 +02004694 /* Reject any attempt to create a zero-length key so that we don't
4695 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004696 if (psa_get_key_bits(attributes) == 0) {
4697 return PSA_ERROR_INVALID_ARGUMENT;
4698 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004700 if (operation->alg == PSA_ALG_NONE) {
4701 return PSA_ERROR_BAD_STATE;
4702 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 if (!operation->can_output_key) {
4705 return PSA_ERROR_NOT_PERMITTED;
4706 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004708 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4709 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004710#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004712 /* Deriving a key in a secure element is not implemented yet. */
4713 status = PSA_ERROR_NOT_SUPPORTED;
4714 }
4715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 if (status == PSA_SUCCESS) {
4717 status = psa_generate_derived_key_internal(slot,
4718 attributes->core.bits,
4719 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004720 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if (status == PSA_SUCCESS) {
4722 status = psa_finish_key_creation(slot, driver, key);
4723 }
4724 if (status != PSA_SUCCESS) {
4725 psa_fail_key_creation(slot, driver);
4726 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729}
4730
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731
4732
4733/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004734/* Key derivation */
4735/****************************************************************/
4736
Steven Cooreman932ffb72021-02-15 12:14:32 +01004737#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004739{
4740#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4742 return 1;
4743 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004744#endif
4745#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4747 return 1;
4748 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004749#endif
4750#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004751 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4752 return 1;
4753 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004754#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004756}
4757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004759{
4760 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 psa_status_t status = psa_hash_setup(&operation, alg);
4762 psa_hash_abort(&operation);
4763 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004764}
4765
Gilles Peskine969c5d62019-01-16 15:53:06 +01004766static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769{
Janos Follath5fe19732019-06-20 15:09:30 +01004770 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4771 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004773
Gilles Peskine969c5d62019-01-16 15:53:06 +01004774 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 if (!is_kdf_alg_supported(kdf_alg)) {
4776 return PSA_ERROR_NOT_SUPPORTED;
4777 }
John Durkop07cc04a2020-11-16 22:08:34 -08004778
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004779 /* All currently supported key derivation algorithms are based on a
4780 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4782 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4783 if (hash_size == 0) {
4784 return PSA_ERROR_NOT_SUPPORTED;
4785 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004786
4787 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4788 * we might fail later, which is somewhat unfriendly and potentially
4789 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004790 psa_status_t status = psa_hash_try_support(hash_alg);
4791 if (status != PSA_SUCCESS) {
4792 return status;
4793 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4796 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4797 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4798 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004799 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004800
Gilles Peskinecdacf042021-04-29 21:10:00 +02004801 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004806{
4807#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004808 if (alg == PSA_ALG_ECDH) {
4809 return PSA_SUCCESS;
4810 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004811#endif
4812 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004814}
John Durkop07cc04a2020-11-16 22:08:34 -08004815#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004817psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4818 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819{
4820 psa_status_t status;
4821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 if (operation->alg != 0) {
4823 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004824 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4827 return PSA_ERROR_INVALID_ARGUMENT;
4828 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4829#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4830 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4831 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4832 status = psa_key_agreement_try_support(ka_alg);
4833 if (status != PSA_SUCCESS) {
4834 return status;
4835 }
4836 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4837#else
4838 return PSA_ERROR_NOT_SUPPORTED;
4839#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4840 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4841#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4842 status = psa_key_derivation_setup_kdf(operation, alg);
4843#else
4844 return PSA_ERROR_NOT_SUPPORTED;
4845#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4846 } else {
4847 return PSA_ERROR_INVALID_ARGUMENT;
4848 }
4849
4850 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 }
4853 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004854}
4855
John Durkopd0321952020-10-29 21:37:36 -07004856#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4858 psa_algorithm_t hash_alg,
4859 psa_key_derivation_step_t step,
4860 const uint8_t *data,
4861 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862{
4863 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004865 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 if (hkdf->state != HKDF_STATE_INIT) {
4867 return PSA_ERROR_BAD_STATE;
4868 } else {
4869 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4870 hash_alg,
4871 data, data_length);
4872 if (status != PSA_SUCCESS) {
4873 return status;
4874 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004875 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004877 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004878 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 if (hkdf->state == HKDF_STATE_INIT) {
4881 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4882 hash_alg,
4883 NULL, 0);
4884 if (status != PSA_SUCCESS) {
4885 return status;
4886 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 hkdf->state = HKDF_STATE_STARTED;
4888 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004889 if (hkdf->state != HKDF_STATE_STARTED) {
4890 return PSA_ERROR_BAD_STATE;
4891 }
4892 status = psa_mac_update(&hkdf->hmac,
4893 data, data_length);
4894 if (status != PSA_SUCCESS) {
4895 return status;
4896 }
4897 status = psa_mac_sign_finish(&hkdf->hmac,
4898 hkdf->prk,
4899 sizeof(hkdf->prk),
4900 &data_length);
4901 if (status != PSA_SUCCESS) {
4902 return status;
4903 }
4904 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004905 hkdf->block_number = 0;
4906 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004908 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909 if (hkdf->state == HKDF_STATE_OUTPUT) {
4910 return PSA_ERROR_BAD_STATE;
4911 }
4912 if (hkdf->info_set) {
4913 return PSA_ERROR_BAD_STATE;
4914 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004915 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916 if (data_length != 0) {
4917 hkdf->info = mbedtls_calloc(1, data_length);
4918 if (hkdf->info == NULL) {
4919 return PSA_ERROR_INSUFFICIENT_MEMORY;
4920 }
4921 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004922 }
4923 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927 }
4928}
John Durkop07cc04a2020-11-16 22:08:34 -08004929#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004930
John Durkop07cc04a2020-11-16 22:08:34 -08004931#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4932 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004933static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4934 const uint8_t *data,
4935 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004936{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4938 return PSA_ERROR_BAD_STATE;
4939 }
Janos Follathf08e2652019-06-13 09:05:41 +01004940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004941 if (data_length != 0) {
4942 prf->seed = mbedtls_calloc(1, data_length);
4943 if (prf->seed == NULL) {
4944 return PSA_ERROR_INSUFFICIENT_MEMORY;
4945 }
Janos Follathf08e2652019-06-13 09:05:41 +01004946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004947 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004948 prf->seed_length = data_length;
4949 }
Janos Follathf08e2652019-06-13 09:05:41 +01004950
Gilles Peskine43f958b2020-12-13 14:55:14 +01004951 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004953 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004954}
4955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4957 const uint8_t *data,
4958 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004959{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4961 return PSA_ERROR_BAD_STATE;
4962 }
Janos Follath81550542019-06-13 14:26:34 +01004963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 if (data_length != 0) {
4965 prf->secret = mbedtls_calloc(1, data_length);
4966 if (prf->secret == NULL) {
4967 return PSA_ERROR_INSUFFICIENT_MEMORY;
4968 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004971 prf->secret_length = data_length;
4972 }
Janos Follath81550542019-06-13 14:26:34 +01004973
Gilles Peskine43f958b2020-12-13 14:55:14 +01004974 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004977}
4978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004979static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4980 const uint8_t *data,
4981 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004982{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4984 return PSA_ERROR_BAD_STATE;
4985 }
Janos Follath63028dd2019-06-13 09:15:47 +01004986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004987 if (data_length != 0) {
4988 prf->label = mbedtls_calloc(1, data_length);
4989 if (prf->label == NULL) {
4990 return PSA_ERROR_INSUFFICIENT_MEMORY;
4991 }
Janos Follath63028dd2019-06-13 09:15:47 +01004992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004993 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004994 prf->label_length = data_length;
4995 }
Janos Follath63028dd2019-06-13 09:15:47 +01004996
Gilles Peskine43f958b2020-12-13 14:55:14 +01004997 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005000}
5001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5003 psa_key_derivation_step_t step,
5004 const uint8_t *data,
5005 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005006{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005008 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005010 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005012 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005013 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005014 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005016 }
5017}
John Durkop07cc04a2020-11-16 22:08:34 -08005018#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5019 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5020
5021#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5022static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5023 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005024 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005025 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005026{
5027 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005029 uint8_t *cur = pms;
5030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5032 return PSA_ERROR_INVALID_ARGUMENT;
5033 }
John Durkop07cc04a2020-11-16 22:08:34 -08005034
5035 /* Quoting RFC 4279, Section 2:
5036 *
5037 * The premaster secret is formed as follows: if the PSK is N octets
5038 * long, concatenate a uint16 with the value N, N zero octets, a second
5039 * uint16 with the value N, and the PSK itself.
5040 */
5041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 *cur++ = MBEDTLS_BYTE_1(data_length);
5043 *cur++ = MBEDTLS_BYTE_0(data_length);
5044 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005045 cur += data_length;
5046 *cur++ = pms[0];
5047 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005049 cur += data_length;
5050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 mbedtls_platform_zeroize(pms, sizeof(pms));
5054 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005055}
Janos Follath6660f0e2019-06-17 08:44:03 +01005056
5057static psa_status_t psa_tls12_prf_psk_to_ms_input(
5058 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005059 psa_key_derivation_step_t step,
5060 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005062{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005063 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5064 return psa_tls12_prf_psk_to_ms_set_key(prf,
5065 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005066 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005069}
John Durkop07cc04a2020-11-16 22:08:34 -08005070#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005071
Gilles Peskineb8965192019-09-24 16:21:10 +02005072/** Check whether the given key type is acceptable for the given
5073 * input step of a key derivation.
5074 *
5075 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5076 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5077 * Both secret and non-secret inputs can alternatively have the type
5078 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5079 * that the input was passed as a buffer rather than via a key object.
5080 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005081static int psa_key_derivation_check_input_type(
5082 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005086 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087 if (key_type == PSA_KEY_TYPE_DERIVE) {
5088 return PSA_SUCCESS;
5089 }
5090 if (key_type == PSA_KEY_TYPE_NONE) {
5091 return PSA_SUCCESS;
5092 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005093 break;
5094 case PSA_KEY_DERIVATION_INPUT_LABEL:
5095 case PSA_KEY_DERIVATION_INPUT_SALT:
5096 case PSA_KEY_DERIVATION_INPUT_INFO:
5097 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5099 return PSA_SUCCESS;
5100 }
5101 if (key_type == PSA_KEY_TYPE_NONE) {
5102 return PSA_SUCCESS;
5103 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005104 break;
5105 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005106 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005107}
5108
Janos Follathb80a94e2019-06-12 15:54:46 +01005109static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005111 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005112 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005113 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005116 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119 status = psa_key_derivation_check_input_type(step, key_type);
5120 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005121 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005123
John Durkopd0321952020-10-29 21:37:36 -07005124#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5126 status = psa_hkdf_input(&operation->ctx.hkdf,
5127 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5128 step, data, data_length);
5129 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005130#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5131#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5133 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5134 step, data, data_length);
5135 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005136#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5137#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005138 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5139 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5140 step, data, data_length);
5141 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005142#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005143 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005145 (void) data;
5146 (void) data_length;
5147 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005149 }
5150
Gilles Peskine224b0d62019-09-23 18:13:17 +02005151exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152 if (status != PSA_SUCCESS) {
5153 psa_key_derivation_abort(operation);
5154 }
5155 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156}
5157
Janos Follath51f4a0f2019-06-14 11:35:55 +01005158psa_status_t psa_key_derivation_input_bytes(
5159 psa_key_derivation_operation_t *operation,
5160 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005161 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005163{
Ryan Everett6f682062024-02-09 16:18:39 +00005164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5165 LOCAL_INPUT_DECLARE(data_external, data);
5166
5167 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5168
5169 status = psa_key_derivation_input_internal(operation, step,
5170 PSA_KEY_TYPE_NONE,
5171 data, data_length);
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005172#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005173exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005174#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005175 LOCAL_INPUT_FREE(data_external, data);
5176 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005177}
5178
Janos Follath51f4a0f2019-06-14 11:35:55 +01005179psa_status_t psa_key_derivation_input_key(
5180 psa_key_derivation_operation_t *operation,
5181 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005185 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005187
Ronald Cron5c522922020-11-14 16:35:34 +01005188 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5190 if (status != PSA_SUCCESS) {
5191 psa_key_derivation_abort(operation);
5192 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005193 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005194
5195 /* Passing a key object as a SECRET input unlocks the permission
5196 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005198 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 status = psa_key_derivation_input_internal(operation,
5202 step, slot->attr.type,
5203 slot->key.data,
5204 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005208 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005210
5211
5212
5213/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005214/* Key agreement */
5215/****************************************************************/
5216
John Durkop6ba40d12020-11-10 08:50:04 -08005217#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005218static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5219 size_t peer_key_length,
5220 const mbedtls_ecp_keypair *our_key,
5221 uint8_t *shared_secret,
5222 size_t shared_secret_size,
5223 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224{
Steven Cooremand4867872020-08-05 16:31:39 +02005225 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005226 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005227 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005228 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005229 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5230 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005231
Ronald Cron90857082020-11-25 14:58:33 +01005232 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5234 bits,
5235 peer_key,
5236 peer_key_length,
5237 &their_key);
5238 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005239 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005240 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005241
Jaeden Amero97271b32019-01-10 19:38:51 +00005242 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005243 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5244 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005245 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005247 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5249 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005250 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005252
Jaeden Amero97271b32019-01-10 19:38:51 +00005253 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005254 mbedtls_ecdh_calc_secret(&ecdh,
5255 shared_secret_length,
5256 shared_secret, shared_secret_size,
5257 mbedtls_psa_get_random,
5258 MBEDTLS_PSA_RANDOM_STATE));
5259 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005260 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261 }
5262 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005263 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005264 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005265
5266exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 if (status != PSA_SUCCESS) {
5268 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5269 }
5270 mbedtls_ecdh_free(&ecdh);
5271 mbedtls_ecp_keypair_free(their_key);
5272 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005275}
John Durkop6ba40d12020-11-10 08:50:04 -08005276#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005277
Gilles Peskine01d718c2018-09-18 12:01:02 +02005278#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005280static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5281 psa_key_slot_t *private_key,
5282 const uint8_t *peer_key,
5283 size_t peer_key_length,
5284 uint8_t *shared_secret,
5285 size_t shared_secret_size,
5286 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005287{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005288 mbedtls_ecp_keypair *ecp = NULL;
5289 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005291 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005292#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005293 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005294 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5295 return PSA_ERROR_INVALID_ARGUMENT;
5296 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005297 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005298 private_key->attr.type,
5299 private_key->attr.bits,
5300 private_key->key.data,
5301 private_key->key.bytes,
5302 &ecp);
5303 if (status != PSA_SUCCESS) {
5304 return status;
5305 }
5306 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5307 ecp,
5308 shared_secret, shared_secret_size,
5309 shared_secret_length);
5310 mbedtls_ecp_keypair_free(ecp);
5311 mbedtls_free(ecp);
5312 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005313#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005314 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005315 (void) ecp;
5316 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005317 (void) private_key;
5318 (void) peer_key;
5319 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005320 (void) shared_secret;
5321 (void) shared_secret_size;
5322 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005323 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005324 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005325}
5326
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005327/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005328 * to potentially free embedded data structures and wipe confidential data.
5329 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005330static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5331 psa_key_derivation_step_t step,
5332 psa_key_slot_t *private_key,
5333 const uint8_t *peer_key,
5334 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005335{
5336 psa_status_t status;
5337 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5338 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005340
5341 /* Step 1: run the secret agreement algorithm to generate the shared
5342 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 status = psa_key_agreement_raw_internal(ka_alg,
5344 private_key,
5345 peer_key, peer_key_length,
5346 shared_secret,
5347 sizeof(shared_secret),
5348 &shared_secret_length);
5349 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005350 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005351 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005352
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005353 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005354 * the shared secret. A shared secret is permitted wherever a key
5355 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 status = psa_key_derivation_input_internal(operation, step,
5357 PSA_KEY_TYPE_DERIVE,
5358 shared_secret,
5359 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005360exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005361 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5362 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363}
5364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5366 psa_key_derivation_step_t step,
5367 mbedtls_svc_key_id_t private_key,
5368 const uint8_t *peer_key,
5369 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005373 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5376 return PSA_ERROR_INVALID_ARGUMENT;
5377 }
Ronald Cron5c522922020-11-14 16:35:34 +01005378 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5380 if (status != PSA_SUCCESS) {
5381 return status;
5382 }
5383 status = psa_key_agreement_internal(operation, step,
5384 slot,
5385 peer_key, peer_key_length);
5386 if (status != PSA_SUCCESS) {
5387 psa_key_derivation_abort(operation);
5388 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005389 /* If a private key has been added as SECRET, we allow the derived
5390 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005392 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005394 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005399}
5400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005401psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5402 mbedtls_svc_key_id_t private_key,
5403 const uint8_t *peer_key,
5404 size_t peer_key_length,
5405 uint8_t *output,
5406 size_t output_size,
5407 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005408{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005411 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005412 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005415 status = PSA_ERROR_INVALID_ARGUMENT;
5416 goto exit;
5417 }
Ronald Cron5c522922020-11-14 16:35:34 +01005418 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5420 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005421 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005423
Gilles Peskine42313fb2022-04-14 00:17:15 +02005424 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5425 * for the output size. The PSA specification only guarantees that this
5426 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5427 * but it might be nice to allow smaller buffers if the output fits.
5428 * At the time of writing this comment, with only ECDH implemented,
5429 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5430 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5431 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005432 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5434 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005435 status = PSA_ERROR_BUFFER_TOO_SMALL;
5436 goto exit;
5437 }
5438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 status = psa_key_agreement_raw_internal(alg, slot,
5440 peer_key, peer_key_length,
5441 output, output_size,
5442 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005443
5444exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005446 /* If an error happens and is not handled properly, the output
5447 * may be used as a key to protect sensitive data. Arrange for such
5448 * a key to be random, which is likely to result in decryption or
5449 * verification errors. This is better than filling the buffer with
5450 * some constant data such as zeros, which would result in the data
5451 * being protected with a reproducible, easily knowable key.
5452 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005454 *output_length = output_size;
5455 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005459 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005460}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005461
5462
Gilles Peskine30524eb2020-11-13 17:02:26 +01005463
Gilles Peskine86a440b2018-11-12 18:39:40 +01005464/****************************************************************/
5465/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005466/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005467
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005468#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5469#include "mbedtls/entropy_poll.h"
5470#endif
5471
Gilles Peskine30524eb2020-11-13 17:02:26 +01005472/** Initialize the PSA random generator.
5473 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005475{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005476#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005478#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5479
Gilles Peskine30524eb2020-11-13 17:02:26 +01005480 /* Set default configuration if
5481 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005483 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 }
5485 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005486 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005490#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5491 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5492 /* The PSA entropy injection feature depends on using NV seed as an entropy
5493 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 mbedtls_entropy_add_source(&rng->entropy,
5495 mbedtls_nv_seed_poll, NULL,
5496 MBEDTLS_ENTROPY_BLOCK_SIZE,
5497 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005498#endif
5499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005501#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005502}
5503
5504/** Deinitialize the PSA random generator.
5505 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005506static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005507{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005508#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005510#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5512 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005513#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005514}
5515
5516/** Seed the PSA random generator.
5517 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005519{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005520#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5521 /* Do nothing: the external RNG seeds itself. */
5522 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005524#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005525 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5527 drbg_seed, sizeof(drbg_seed) - 1);
5528 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005529#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005530}
5531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532psa_status_t psa_generate_random(uint8_t *output,
5533 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535 GUARD_MODULE_INITIALIZED;
5536
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005537#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5538
5539 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5541 output, output_size,
5542 &output_length);
5543 if (status != PSA_SUCCESS) {
5544 return status;
5545 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005546 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005547 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005548 if (output_length != output_size) {
5549 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5550 }
5551 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005552
5553#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005556 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005557 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5558 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5559 output_size);
5560 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5561 output, request_size);
5562 if (ret != 0) {
5563 return mbedtls_to_psa_error(ret);
5564 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005565 output_size -= request_size;
5566 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005567 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005568 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005569#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005570}
5571
Gilles Peskine8814fc42020-12-14 15:33:44 +01005572/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005573 *
5574 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5575 * `psa_generate_random(...)`. The state parameter is ignored since the
5576 * PSA API doesn't support passing an explicit state.
5577 *
5578 * In the non-external case, psa_generate_random() calls an
5579 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5580 * and semantics as mbedtls_psa_get_random(). As an optimization,
5581 * instead of doing this back-and-forth between the PSA API and the
5582 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5583 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005584 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5586int mbedtls_psa_get_random(void *p_rng,
5587 unsigned char *output,
5588 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005589{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005590 /* This function takes a pointer to the RNG state because that's what
5591 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5592 * its own state internally and doesn't let the caller access that state.
5593 * So we just ignore the state parameter, and in practice we'll pass
5594 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005595 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 psa_status_t status = psa_generate_random(output, output_size);
5597 if (status == PSA_SUCCESS) {
5598 return 0;
5599 } else {
5600 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5601 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005602}
5603#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5604
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005605#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005606psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5607 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005608{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 if (global_data.initialized) {
5610 return PSA_ERROR_NOT_PERMITTED;
5611 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005613 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5614 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5615 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5616 return PSA_ERROR_INVALID_ARGUMENT;
5617 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005619 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005620}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005621#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005622
Ronald Cron3772afe2021-02-08 16:10:05 +01005623/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005624 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005625 * \param type The key type
5626 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005627 *
5628 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005629 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005630 * \retval #PSA_ERROR_INVALID_ARGUMENT
5631 * The size in bits of the key is not valid.
5632 * \retval #PSA_ERROR_NOT_SUPPORTED
5633 * The type and/or the size in bits of the key or the combination of
5634 * the two is not supported.
5635 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005636static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005637 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005638{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005639 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005641 if (key_type_is_raw_bytes(type)) {
5642 status = validate_unstructured_key_bit_size(type, bits);
5643 if (status != PSA_SUCCESS) {
5644 return status;
5645 }
5646 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005647#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005648 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5649 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5650 return PSA_ERROR_NOT_SUPPORTED;
5651 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005652
Ronald Cron01b2aba2020-10-05 09:42:02 +02005653 /* Accept only byte-aligned keys, for the same reasons as
5654 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005655 if (bits % 8 != 0) {
5656 return PSA_ERROR_NOT_SUPPORTED;
5657 }
5658 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005659#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005660
Ronald Cron5c4d3862020-12-07 11:07:24 +01005661#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005662 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005663 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005664 return PSA_SUCCESS;
5665 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005666#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005667 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005668 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005669 }
5670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005671 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005672}
5673
Ronald Cron55ed0592020-10-05 10:30:40 +02005674psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005675 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005677{
5678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005679 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 if ((attributes->domain_parameters == NULL) &&
5682 (attributes->domain_parameters_size != 0)) {
5683 return PSA_ERROR_INVALID_ARGUMENT;
5684 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005686 if (key_type_is_raw_bytes(type)) {
5687 status = psa_generate_random(key_buffer, key_buffer_size);
5688 if (status != PSA_SUCCESS) {
5689 return status;
5690 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005691
David Brown12ca5032021-02-11 11:02:00 -07005692#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005693 if (type == PSA_KEY_TYPE_DES) {
5694 psa_des_set_key_parity(key_buffer, key_buffer_size);
5695 }
David Brown8107e312021-02-12 08:08:46 -07005696#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005697 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005698
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005699#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5700 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005701 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5702 return mbedtls_psa_rsa_generate_key(attributes,
5703 key_buffer,
5704 key_buffer_size,
5705 key_buffer_length);
5706 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005707#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5708 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005709
John Durkop9814fa22020-11-04 12:28:15 -08005710#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005711 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5712 return mbedtls_psa_ecp_generate_key(attributes,
5713 key_buffer,
5714 key_buffer_size,
5715 key_buffer_length);
5716 } else
John Durkop9814fa22020-11-04 12:28:15 -08005717#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005718 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005719 (void) key_buffer_length;
5720 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005721 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005723 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005724}
Darryl Green0c6575a2018-11-07 16:05:30 +00005725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005726psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5727 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005728{
5729 psa_status_t status;
5730 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005731 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005732 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005733
Ronald Cron81709fc2020-11-14 12:10:32 +01005734 *key = MBEDTLS_SVC_KEY_ID_INIT;
5735
Gilles Peskine0f84d622019-09-12 19:03:13 +02005736 /* Reject any attempt to create a zero-length key so that we don't
5737 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005738 if (psa_get_key_bits(attributes) == 0) {
5739 return PSA_ERROR_INVALID_ARGUMENT;
5740 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005741
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005742 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005743 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5744 return PSA_ERROR_INVALID_ARGUMENT;
5745 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005747 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5748 &slot, &driver);
5749 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005750 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005751 }
Gilles Peskine11792082019-08-06 18:36:36 +02005752
Ronald Cron977c2472020-10-13 08:32:21 +02005753 /* In the case of a transparent key or an opaque key stored in local
5754 * storage (thus not in the case of generating a key in a secure element
5755 * or cryptoprocessor with storage), we have to allocate a buffer to
5756 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005757 if (slot->key.data == NULL) {
5758 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5759 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005760 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005761 attributes->core.type, attributes->core.bits);
5762 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005764 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005765
5766 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005767 attributes->core.type,
5768 attributes->core.bits);
5769 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005770 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005771 attributes, &key_buffer_size);
5772 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005773 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774 }
Ronald Cron977c2472020-10-13 08:32:21 +02005775 }
Ronald Cron977c2472020-10-13 08:32:21 +02005776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005777 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5778 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005779 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005780 }
Ronald Cron977c2472020-10-13 08:32:21 +02005781 }
5782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005783 status = psa_driver_wrapper_generate_key(attributes,
5784 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005786 if (status != PSA_SUCCESS) {
5787 psa_remove_key_data_from_memory(slot);
5788 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005789
Gilles Peskine11792082019-08-06 18:36:36 +02005790exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005791 if (status == PSA_SUCCESS) {
5792 status = psa_finish_key_creation(slot, driver, key);
5793 }
5794 if (status != PSA_SUCCESS) {
5795 psa_fail_key_creation(slot, driver);
5796 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005798 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799}
5800
Gilles Peskinee59236f2018-01-27 23:32:46 +01005801/****************************************************************/
5802/* Module setup */
5803/****************************************************************/
5804
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005805#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005806psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005807 void (* entropy_init)(mbedtls_entropy_context *ctx),
5808 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005809{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005810 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5811 return PSA_ERROR_BAD_STATE;
5812 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005813 global_data.rng.entropy_init = entropy_init;
5814 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005815 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005816}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005817#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005819void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005821 psa_wipe_all_key_slots();
5822 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5823 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005824 }
5825 /* Wipe all remaining data, including configuration.
5826 * In particular, this sets all state indicator to the value
5827 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005828 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005829
5830 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005831 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005832}
5833
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005834#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5835/** Recover a transaction that was interrupted by a power failure.
5836 *
5837 * This function is called during initialization, before psa_crypto_init()
5838 * returns. If this function returns a failure status, the initialization
5839 * fails.
5840 */
5841static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005843{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005844 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005845 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5846 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005847 /* TODO - fall through to the failure case until this
5848 * is implemented.
5849 * https://github.com/ARMmbed/mbed-crypto/issues/218
5850 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005851 default:
5852 /* We found an unsupported transaction in the storage.
5853 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005855 }
5856}
5857#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005859psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005860{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005861 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005862
Gilles Peskinec6b69072018-11-20 21:42:52 +01005863 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005864 if (global_data.initialized != 0) {
5865 return PSA_SUCCESS;
5866 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005867
Gilles Peskine30524eb2020-11-13 17:02:26 +01005868 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005869 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005870 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005871 status = mbedtls_psa_random_seed(&global_data.rng);
5872 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005874 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005875 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005877 status = psa_initialize_key_slots();
5878 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005879 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005880 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005881
Ronald Cron088d5d02021-04-10 16:57:30 +02005882 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005883 status = psa_driver_wrapper_init();
5884 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005885 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005886 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005887
Gilles Peskine4b734222019-07-24 15:56:31 +02005888#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 status = psa_crypto_load_transaction();
5890 if (status == PSA_SUCCESS) {
5891 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5892 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005893 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005894 }
5895 status = psa_crypto_stop_transaction();
5896 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005897 /* There's no transaction to complete. It's all good. */
5898 status = PSA_SUCCESS;
5899 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005900#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005901
Gilles Peskinec6b69072018-11-20 21:42:52 +01005902 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005903 global_data.initialized = 1;
5904
5905exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 if (status != PSA_SUCCESS) {
5907 mbedtls_psa_crypto_free();
5908 }
5909 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005910}
5911
David Horstmann114d8242023-12-07 18:39:17 +00005912/* Memory copying test hooks. These are called before input copy, after input
5913 * copy, before output copy and after output copy, respectively.
5914 * They are used by memory-poisoning tests to temporarily unpoison buffers
5915 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005916#if defined(MBEDTLS_TEST_HOOKS)
5917void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5918void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5919void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5920void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5921#endif
5922
David Horstmanndf493552023-11-15 15:18:30 +00005923/** Copy from an input buffer to a local copy.
5924 *
5925 * \param[in] input Pointer to input buffer.
5926 * \param[in] input_len Length of the input buffer.
5927 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5928 * \param[out] input_copy_len Length of the local copy buffer.
5929 * \return #PSA_SUCCESS, if the buffer was successfully
5930 * copied.
5931 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5932 * copy is too small to hold contents of the
5933 * input buffer.
5934 */
5935MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005936psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5937 uint8_t *input_copy, size_t input_copy_len)
5938{
5939 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005940 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005941 }
5942
David Horstmann0760b152023-11-24 16:21:04 +00005943#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005944 if (psa_input_pre_copy_hook != NULL) {
5945 psa_input_pre_copy_hook(input, input_len);
5946 }
David Horstmann0760b152023-11-24 16:21:04 +00005947#endif
5948
David Horstmann660027f2023-11-15 17:33:47 +00005949 if (input_len > 0) {
5950 memcpy(input_copy, input, input_len);
5951 }
David Horstmann957f9802023-10-30 20:29:43 +00005952
David Horstmann0760b152023-11-24 16:21:04 +00005953#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005954 if (psa_input_post_copy_hook != NULL) {
5955 psa_input_post_copy_hook(input, input_len);
5956 }
David Horstmann0760b152023-11-24 16:21:04 +00005957#endif
5958
David Horstmann957f9802023-10-30 20:29:43 +00005959 return PSA_SUCCESS;
5960}
5961
David Horstmanndf493552023-11-15 15:18:30 +00005962/** Copy from a local output buffer into a user-supplied one.
5963 *
5964 * \param[in] output_copy Pointer to a local buffer containing the output.
5965 * \param[in] output_copy_len Length of the local buffer.
5966 * \param[out] output Pointer to user-supplied output buffer.
5967 * \param[out] output_len Length of the user-supplied output buffer.
5968 * \return #PSA_SUCCESS, if the buffer was successfully
5969 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005970 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005971 * user-supplied output buffer is too small to
5972 * hold the contents of the local buffer.
5973 */
5974MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005975psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5976 uint8_t *output, size_t output_len)
5977{
5978 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005979 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005980 }
David Horstmann660027f2023-11-15 17:33:47 +00005981
David Horstmann0760b152023-11-24 16:21:04 +00005982#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005983 if (psa_output_pre_copy_hook != NULL) {
5984 psa_output_pre_copy_hook(output, output_len);
5985 }
David Horstmann0760b152023-11-24 16:21:04 +00005986#endif
5987
David Horstmann660027f2023-11-15 17:33:47 +00005988 if (output_copy_len > 0) {
5989 memcpy(output, output_copy, output_copy_len);
5990 }
5991
David Horstmann0760b152023-11-24 16:21:04 +00005992#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005993 if (psa_output_post_copy_hook != NULL) {
5994 psa_output_post_copy_hook(output, output_len);
5995 }
David Horstmann0760b152023-11-24 16:21:04 +00005996#endif
5997
David Horstmann2bd296e2023-10-30 20:37:12 +00005998 return PSA_SUCCESS;
5999}
6000
David Horstmann81a0d572023-11-20 17:15:32 +00006001psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6002 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006003{
6004 psa_status_t status;
6005
David Horstmann0d52c712023-11-23 15:50:37 +00006006 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006007
David Horstmann726bf052023-11-15 18:11:26 +00006008 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006009 return PSA_SUCCESS;
6010 }
6011
David Horstmann81a0d572023-11-20 17:15:32 +00006012 local_input->buffer = mbedtls_calloc(input_len, 1);
6013 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006014 /* Since we dealt with the zero-length case above, we know that
6015 * a NULL return value means a failure of allocation. */
6016 return PSA_ERROR_INSUFFICIENT_MEMORY;
6017 }
David Horstmann81a0d572023-11-20 17:15:32 +00006018 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006019
David Horstmann81a0d572023-11-20 17:15:32 +00006020 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006021
6022 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006023 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006024 if (status != PSA_SUCCESS) {
6025 goto error;
6026 }
6027
6028 return PSA_SUCCESS;
6029
6030error:
David Horstmann81a0d572023-11-20 17:15:32 +00006031 mbedtls_free(local_input->buffer);
6032 local_input->buffer = NULL;
6033 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006034 return status;
6035}
6036
David Horstmann81a0d572023-11-20 17:15:32 +00006037void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006038{
David Horstmann81a0d572023-11-20 17:15:32 +00006039 mbedtls_free(local_input->buffer);
6040 local_input->buffer = NULL;
6041 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006042}
6043
David Horstmann1a76ab12023-11-20 12:54:09 +00006044psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6045 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006046{
David Horstmann0d52c712023-11-23 15:50:37 +00006047 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006048
David Horstmann726bf052023-11-15 18:11:26 +00006049 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006050 return PSA_SUCCESS;
6051 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006052 local_output->buffer = mbedtls_calloc(output_len, 1);
6053 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006054 /* Since we dealt with the zero-length case above, we know that
6055 * a NULL return value means a failure of allocation. */
6056 return PSA_ERROR_INSUFFICIENT_MEMORY;
6057 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006058 local_output->length = output_len;
6059 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006060
6061 return PSA_SUCCESS;
6062}
6063
Gabor Mezei3b0c3712024-01-24 13:07:17 +01006064psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len,
6065 psa_crypto_local_output_t *local_output)
6066{
6067 psa_status_t status;
6068 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
6069
6070 if (output_len == 0) {
6071 return PSA_SUCCESS;
6072 }
6073 local_output->buffer = mbedtls_calloc(output_len, 1);
6074 if (local_output->buffer == NULL) {
6075 /* Since we dealt with the zero-length case above, we know that
6076 * a NULL return value means a failure of allocation. */
6077 return PSA_ERROR_INSUFFICIENT_MEMORY;
6078 }
6079 local_output->length = output_len;
6080 local_output->original = output;
6081
6082 status = psa_crypto_copy_input(output, output_len,
6083 local_output->buffer, local_output->length);
6084 if (status != PSA_SUCCESS) {
6085 goto error;
6086 }
6087
6088 return PSA_SUCCESS;
6089
6090error:
6091 mbedtls_free(local_output->buffer);
6092 local_output->buffer = NULL;
6093 local_output->length = 0;
6094 return status;
6095}
6096
David Horstmann1a76ab12023-11-20 12:54:09 +00006097psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006098{
David Horstmann761761f2023-11-15 15:57:32 +00006099 psa_status_t status;
6100
David Horstmann1a76ab12023-11-20 12:54:09 +00006101 if (local_output->buffer == NULL) {
6102 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006103 return PSA_SUCCESS;
6104 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006105 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006106 /* We have an internal copy but nothing to copy back to. */
6107 return PSA_ERROR_CORRUPTION_DETECTED;
6108 }
6109
David Horstmann1a76ab12023-11-20 12:54:09 +00006110 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6111 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006112 if (status != PSA_SUCCESS) {
6113 return status;
6114 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006115
David Horstmann1a76ab12023-11-20 12:54:09 +00006116 mbedtls_free(local_output->buffer);
6117 local_output->buffer = NULL;
6118 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006119
6120 return PSA_SUCCESS;
6121}
6122
Gilles Peskinee59236f2018-01-27 23:32:46 +01006123#endif /* MBEDTLS_PSA_CRYPTO_C */