blob: 514911d7a457d8190ad191cfdf6b205ffab40b2e [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
David Horstmann65bf12c2024-02-06 15:27:49 +00003590static psa_status_t psa_generate_random_internal(uint8_t *output,
3591 size_t output_size)
3592{
3593 GUARD_MODULE_INITIALIZED;
3594
3595 psa_status_t status;
3596
3597#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
3598
3599 size_t output_length = 0;
3600 status = mbedtls_psa_external_get_random(&global_data.rng,
3601 output, output_size,
3602 &output_length);
3603 if (status != PSA_SUCCESS) {
3604 goto exit;
3605 }
3606 /* Breaking up a request into smaller chunks is currently not supported
3607 * for the external RNG interface. */
3608 if (output_length != output_size) {
3609 status = PSA_ERROR_INSUFFICIENT_ENTROPY;
3610 goto exit;
3611 }
3612 status = PSA_SUCCESS;
3613
3614#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3615
3616 while (output_size > 0) {
3617 size_t request_size =
3618 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
3619 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
3620 output_size);
3621 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
3622 output, request_size);
3623 if (ret != 0) {
3624 status = mbedtls_to_psa_error(ret);
3625 goto exit;
3626 }
3627 output_size -= request_size;
3628 output += request_size;
3629 }
3630 status = PSA_SUCCESS;
3631#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3632
3633exit:
3634 return status;
3635}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003636
3637
mohammad1603503973b2018-03-12 15:59:30 +02003638/****************************************************************/
3639/* Symmetric cryptography */
3640/****************************************************************/
3641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3643 mbedtls_svc_key_id_t key,
3644 psa_algorithm_t alg,
3645 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003646{
3647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3648 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003649 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003650 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3652 PSA_KEY_USAGE_ENCRYPT :
3653 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003654
3655 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003657 status = PSA_ERROR_BAD_STATE;
3658 goto exit;
3659 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003662 status = PSA_ERROR_INVALID_ARGUMENT;
3663 goto exit;
3664 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3667 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003668 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003670
Ronald Cron49fafa92021-03-10 08:34:23 +01003671 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003672 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003673 * so we only set it (in the driver wrapper) after resources have been
3674 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003675 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003677 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003679 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003681 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682 }
3683 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003684
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003685 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003686 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003687 };
3688
Ronald Cronab99ac22020-10-01 16:38:28 +02003689 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003690 if (cipher_operation == MBEDTLS_ENCRYPT) {
3691 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3692 &attributes,
3693 slot->key.data,
3694 slot->key.bytes,
3695 alg);
3696 } else {
3697 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3698 &attributes,
3699 slot->key.data,
3700 slot->key.bytes,
3701 alg);
3702 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003703
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003704exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705 if (status != PSA_SUCCESS) {
3706 psa_cipher_abort(operation);
3707 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003712}
3713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3715 mbedtls_svc_key_id_t key,
3716 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003717{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003719}
3720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3722 mbedtls_svc_key_id_t key,
3723 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003724{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003726}
3727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003729 uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 size_t iv_size,
3731 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003732{
Ronald Cron6d051732020-10-01 14:10:20 +02003733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Sergey04eb7c02023-03-06 15:37:23 -07003734 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003735
Gabor Mezei8677edd2024-02-07 18:10:13 +01003736 LOCAL_OUTPUT_DECLARE(iv_external, iv);
3737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003739 status = PSA_ERROR_BAD_STATE;
3740 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003741 }
3742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003744 status = PSA_ERROR_BAD_STATE;
3745 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003746 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003747
Ronald Cronc423acb2021-07-05 12:31:44 +02003748 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003750 status = PSA_ERROR_BUFFER_TOO_SMALL;
3751 goto exit;
3752 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003755 status = PSA_ERROR_GENERIC_ERROR;
3756 goto exit;
3757 }
3758
Gabor Mezei8677edd2024-02-07 18:10:13 +01003759 LOCAL_OUTPUT_ALLOC(iv_external, default_iv_length, iv);
3760
Gabor Mezeif3c35042024-03-04 17:15:08 +01003761 status = psa_generate_random_internal(iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 }
Ronald Cron5618a392021-03-26 09:52:26 +01003765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 status = psa_driver_wrapper_cipher_set_iv(operation,
Gabor Mezei8677edd2024-02-07 18:10:13 +01003767 iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003768
3769exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003770 if (status == PSA_SUCCESS) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003771 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003772 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003774 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 psa_cipher_abort(operation);
Gabor Mezei8677edd2024-02-07 18:10:13 +01003776 if (iv != NULL) {
3777 mbedtls_platform_zeroize(iv, default_iv_length);
3778 }
Ronald Cronc423acb2021-07-05 12:31:44 +02003779 }
Ronald Cron6d051732020-10-01 14:10:20 +02003780
Gabor Mezei8677edd2024-02-07 18:10:13 +01003781 LOCAL_OUTPUT_FREE(iv_external, iv);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003783}
3784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Gabor Mezei282bb532024-02-01 10:39:26 +01003786 const uint8_t *iv_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003788{
Ronald Cron6d051732020-10-01 14:10:20 +02003789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003790
Gabor Mezei282bb532024-02-01 10:39:26 +01003791 LOCAL_INPUT_DECLARE(iv_external, iv);
3792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003794 status = PSA_ERROR_BAD_STATE;
3795 goto exit;
3796 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003799 status = PSA_ERROR_BAD_STATE;
3800 goto exit;
3801 }
Ronald Crona0d68172021-03-26 10:15:08 +01003802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003804 status = PSA_ERROR_INVALID_ARGUMENT;
3805 goto exit;
3806 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003807
Gabor Mezei282bb532024-02-01 10:39:26 +01003808 LOCAL_INPUT_ALLOC(iv_external, iv_length, iv);
3809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 status = psa_driver_wrapper_cipher_set_iv(operation,
3811 iv,
3812 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003813
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003814exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003816 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003817 } else {
3818 psa_cipher_abort(operation);
3819 }
Gabor Mezei282bb532024-02-01 10:39:26 +01003820
3821 LOCAL_INPUT_FREE(iv_external, iv);
3822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003824}
3825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003827 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003829 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830 size_t output_size,
3831 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003832{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003834
Gabor Mezei13a15c22024-01-24 16:56:00 +01003835 LOCAL_INPUT_DECLARE(input_external, input);
3836 LOCAL_OUTPUT_DECLARE(output_external, output);
3837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003839 status = PSA_ERROR_BAD_STATE;
3840 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003841 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003844 status = PSA_ERROR_BAD_STATE;
3845 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003846 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003847
Gabor Mezeied96d682024-01-31 17:45:29 +01003848 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gabor Mezeiff783e02024-02-29 10:08:16 +00003849 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 status = psa_driver_wrapper_cipher_update(operation,
3852 input,
3853 input_length,
3854 output,
3855 output_size,
3856 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003857
3858exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 if (status != PSA_SUCCESS) {
3860 psa_cipher_abort(operation);
3861 }
Ronald Cron6d051732020-10-01 14:10:20 +02003862
Gabor Mezei13a15c22024-01-24 16:56:00 +01003863 LOCAL_INPUT_FREE(input_external, input);
3864 LOCAL_OUTPUT_FREE(output_external, output);
3865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003867}
3868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003869psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003870 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 size_t output_size,
3872 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003873{
David Saadab4ecc272019-02-14 13:48:10 +02003874 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003875
Gabor Mezei13a15c22024-01-24 16:56:00 +01003876 LOCAL_OUTPUT_DECLARE(output_external, output);
3877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003879 status = PSA_ERROR_BAD_STATE;
3880 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003881 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003884 status = PSA_ERROR_BAD_STATE;
3885 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003886 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003887
Gabor Mezeiff783e02024-02-29 10:08:16 +00003888 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gabor Mezeied96d682024-01-31 17:45:29 +01003889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 status = psa_driver_wrapper_cipher_finish(operation,
3891 output,
3892 output_size,
3893 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003894
3895exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003897 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003899 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003901 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003902
3903 LOCAL_OUTPUT_FREE(output_external, output);
3904
3905 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003906}
3907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003909{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003911 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003912 * in use. It's ok to call abort on such an object, and there's
3913 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003915 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003918
Ronald Cron49fafa92021-03-10 08:34:23 +01003919 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003920 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003921 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003924}
3925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003926psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3927 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003928 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003930 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 size_t output_size,
3932 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003933{
3934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003935 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003936 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003937 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003938 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3939 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003940
David Horstmannb80e35a2023-12-14 18:12:47 +00003941 LOCAL_INPUT_DECLARE(input_external, input);
3942 LOCAL_OUTPUT_DECLARE(output_external, output);
3943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003945 status = PSA_ERROR_INVALID_ARGUMENT;
3946 goto exit;
3947 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3950 PSA_KEY_USAGE_ENCRYPT,
3951 alg);
3952 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003953 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003955
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003956 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003958 };
3959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003960 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3961 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003962 status = PSA_ERROR_GENERIC_ERROR;
3963 goto exit;
3964 }
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if (default_iv_length > 0) {
3967 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003968 status = PSA_ERROR_BUFFER_TOO_SMALL;
3969 goto exit;
3970 }
3971
David Horstmann65bf12c2024-02-06 15:27:49 +00003972 status = psa_generate_random_internal(local_iv, default_iv_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003974 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003975 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003976 }
3977
Gabor Mezeied96d682024-01-31 17:45:29 +01003978 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3979 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
3980
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003981 status = psa_driver_wrapper_cipher_encrypt(
3982 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003983 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 mbedtls_buffer_offset(output, default_iv_length),
3985 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003986
3987exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 unlock_status = psa_unlock_key_slot(slot);
3989 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003990 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003991 }
Ronald Cron16377072021-07-08 19:00:07 +02003992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 if (status == PSA_SUCCESS) {
3994 if (default_iv_length > 0) {
3995 memcpy(output, local_iv, default_iv_length);
3996 }
3997 *output_length += default_iv_length;
3998 } else {
3999 *output_length = 0;
4000 }
4001
David Horstmannb80e35a2023-12-14 18:12:47 +00004002 LOCAL_INPUT_FREE(input_external, input);
4003 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00004004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004005 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004006}
4007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4009 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004010 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004011 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01004012 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013 size_t output_size,
4014 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004015{
4016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004017 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004018 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02004019 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02004020
Gabor Mezei13a15c22024-01-24 16:56:00 +01004021 LOCAL_INPUT_DECLARE(input_external, input);
4022 LOCAL_OUTPUT_DECLARE(output_external, output);
4023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004024 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02004025 status = PSA_ERROR_INVALID_ARGUMENT;
4026 goto exit;
4027 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4030 PSA_KEY_USAGE_DECRYPT,
4031 alg);
4032 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004033 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004035
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004036 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004038 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004041 status = PSA_ERROR_INVALID_ARGUMENT;
4042 goto exit;
4043 }
4044
Gabor Mezeied96d682024-01-31 17:45:29 +01004045 LOCAL_INPUT_ALLOC(input_external, input_length, input);
4046 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
4047
gabor-mezei-armfa990b52021-03-25 11:17:10 +01004048 status = psa_driver_wrapper_cipher_decrypt(
4049 &attributes, slot->key.data, slot->key.bytes,
4050 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004052
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02004053exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004054 unlock_status = psa_unlock_key_slot(slot);
4055 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004056 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004059 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02004060 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 }
Ronald Cron16377072021-07-08 19:00:07 +02004062
Gabor Mezei13a15c22024-01-24 16:56:00 +01004063 LOCAL_INPUT_FREE(input_external, input);
4064 LOCAL_OUTPUT_FREE(output_external, output);
4065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004067}
4068
4069
mohammad16035955c982018-04-26 00:53:03 +03004070/****************************************************************/
4071/* AEAD */
4072/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4075 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004076 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004078 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004080 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004082 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 size_t ciphertext_size,
4084 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004085{
Ronald Cron215633c2021-03-16 17:15:37 +01004086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4087 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004088
David Horstmann21c1a942023-11-28 19:25:00 +00004089 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4090 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4091 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4092 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4093
mohammad1603f08a5502018-06-03 15:05:47 +03004094 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4097 return PSA_ERROR_NOT_SUPPORTED;
4098 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004099
Ronald Cron9a986162021-03-26 12:40:07 +01004100 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004101 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4102 if (status != PSA_SUCCESS) {
4103 return status;
4104 }
mohammad16035955c982018-04-26 00:53:03 +03004105
Ronald Cron215633c2021-03-16 17:15:37 +01004106 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004108 };
mohammad16035955c982018-04-26 00:53:03 +03004109
David Horstmann21c1a942023-11-28 19:25:00 +00004110 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4111 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4112 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4113 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4114
Ronald Cronde822812021-03-17 16:08:20 +01004115 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004116 &attributes, slot->key.data, slot->key.bytes,
4117 alg,
4118 nonce, nonce_length,
4119 additional_data, additional_data_length,
4120 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004121 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4124 memset(ciphertext, 0, ciphertext_size);
4125 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004126
David Horstmann21c1a942023-11-28 19:25:00 +00004127/* Exit label is only used for buffer copying, prevent unused warnings. */
4128#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4129exit:
4130#endif
4131 LOCAL_INPUT_FREE(nonce_external, nonce);
4132 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4133 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4134 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004139}
4140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4142 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004143 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004145 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004147 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004149 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150 size_t plaintext_size,
4151 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004152{
Ronald Cron215633c2021-03-16 17:15:37 +01004153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4154 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004155
David Horstmann6baf6e92023-11-28 19:43:53 +00004156 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4157 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4158 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4159 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4160
Gilles Peskineee652a32018-06-01 19:23:52 +02004161 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4164 return PSA_ERROR_NOT_SUPPORTED;
4165 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004166
Ronald Cron9a986162021-03-26 12:40:07 +01004167 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004168 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4169 if (status != PSA_SUCCESS) {
4170 return status;
4171 }
mohammad16035955c982018-04-26 00:53:03 +03004172
Ronald Cron215633c2021-03-16 17:15:37 +01004173 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004175 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004176
David Horstmann6baf6e92023-11-28 19:43:53 +00004177 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4178 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4179 additional_data);
4180 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4181 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4182
Ronald Cronde822812021-03-17 16:08:20 +01004183 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004184 &attributes, slot->key.data, slot->key.bytes,
4185 alg,
4186 nonce, nonce_length,
4187 additional_data, additional_data_length,
4188 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004191 if (status != PSA_SUCCESS && plaintext_size != 0) {
4192 memset(plaintext, 0, plaintext_size);
4193 }
mohammad160360a64d02018-06-03 17:20:42 +03004194
David Horstmann6baf6e92023-11-28 19:43:53 +00004195/* Exit label is only used for buffer copying, prevent unused warnings. */
4196#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4197exit:
4198#endif
4199 LOCAL_INPUT_FREE(nonce_external, nonce);
4200 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4201 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4202 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206 return status;
mohammad16035955c982018-04-26 00:53:03 +03004207}
4208
Gilles Peskinee59236f2018-01-27 23:32:46 +01004209/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210/* Generators */
4211/****************************************************************/
4212
John Durkop07cc04a2020-11-16 22:08:34 -08004213#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4214 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4215 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4216#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004217#endif /* At least one builtin KDF */
4218
4219#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4220 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4221 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4222static psa_status_t psa_key_derivation_start_hmac(
4223 psa_mac_operation_t *operation,
4224 psa_algorithm_t hash_alg,
4225 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004227{
4228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4231 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4232 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004233
Steven Cooreman15f0d922021-05-07 14:14:37 +02004234 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 status = psa_driver_wrapper_mac_sign_setup(operation,
4238 &attributes,
4239 hmac_key, hmac_key_length,
4240 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004242 psa_reset_key_attributes(&attributes);
4243 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004244}
4245#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004246
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004247#define HKDF_STATE_INIT 0 /* no input yet */
4248#define HKDF_STATE_STARTED 1 /* got salt */
4249#define HKDF_STATE_KEYED 2 /* got key */
4250#define HKDF_STATE_OUTPUT 3 /* output started */
4251
Gilles Peskinecbe66502019-05-16 16:59:18 +02004252static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004254{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4256 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4257 } else {
4258 return operation->alg;
4259 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004260}
4261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263{
4264 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4266 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004267 /* The object has (apparently) been initialized but it is not
4268 * in use. It's ok to call abort on such an object, and there's
4269 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 } else
John Durkopd0321952020-10-29 21:37:36 -07004271#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4273 mbedtls_free(operation->ctx.hkdf.info);
4274 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4275 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004276#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4277#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4278 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4280 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4281 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4282 if (operation->ctx.tls12_prf.secret != NULL) {
4283 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4284 operation->ctx.tls12_prf.secret_length);
4285 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004286 }
4287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 if (operation->ctx.tls12_prf.seed != NULL) {
4289 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4290 operation->ctx.tls12_prf.seed_length);
4291 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004292 }
4293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 if (operation->ctx.tls12_prf.label != NULL) {
4295 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4296 operation->ctx.tls12_prf.label_length);
4297 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004298 }
4299
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004300 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004301
4302 /* We leave the fields Ai and output_block to be erased safely by the
4303 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004305#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4306 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004307 {
4308 status = PSA_ERROR_BAD_STATE;
4309 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004310 mbedtls_platform_zeroize(operation, sizeof(*operation));
4311 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004312}
4313
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004315 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004316{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004318 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004320 }
4321
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004324}
4325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4327 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004328{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 if (operation->alg == 0) {
4330 return PSA_ERROR_BAD_STATE;
4331 }
4332 if (capacity > operation->capacity) {
4333 return PSA_ERROR_INVALID_ARGUMENT;
4334 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004335 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004337}
4338
John Durkopd0321952020-10-29 21:37:36 -07004339#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004340/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004341 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004342static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4343 psa_algorithm_t hash_alg,
4344 uint8_t *output,
4345 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004346{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004348 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004349 psa_status_t status;
4350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4352 return PSA_ERROR_BAD_STATE;
4353 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354 hkdf->state = HKDF_STATE_OUTPUT;
4355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004357 /* Copy what remains of the current block */
4358 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361 }
4362 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363 output += n;
4364 output_length -= n;
4365 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004367 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004368 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004369 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004370 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004372 * object was corrupted or if this function is called directly
4373 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 if (hkdf->block_number == 0xff) {
4375 return PSA_ERROR_BAD_STATE;
4376 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004377
4378 /* We need a new block */
4379 ++hkdf->block_number;
4380 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4383 hash_alg,
4384 hkdf->prk,
4385 hash_length);
4386 if (status != PSA_SUCCESS) {
4387 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004388 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004389
4390 if (hkdf->block_number != 1) {
4391 status = psa_mac_update(&hkdf->hmac,
4392 hkdf->output_block,
4393 hash_length);
4394 if (status != PSA_SUCCESS) {
4395 return status;
4396 }
4397 }
4398 status = psa_mac_update(&hkdf->hmac,
4399 hkdf->info,
4400 hkdf->info_length);
4401 if (status != PSA_SUCCESS) {
4402 return status;
4403 }
4404 status = psa_mac_update(&hkdf->hmac,
4405 &hkdf->block_number, 1);
4406 if (status != PSA_SUCCESS) {
4407 return status;
4408 }
4409 status = psa_mac_sign_finish(&hkdf->hmac,
4410 hkdf->output_block,
4411 sizeof(hkdf->output_block),
4412 &hmac_output_length);
4413 if (status != PSA_SUCCESS) {
4414 return status;
4415 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004416 }
4417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004419}
John Durkop07cc04a2020-11-16 22:08:34 -08004420#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004421
John Durkop07cc04a2020-11-16 22:08:34 -08004422#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4423 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004424static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4425 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004427{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4429 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004430 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4431 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004432 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004433
Janos Follath7742fee2019-06-17 12:58:10 +01004434 /* We can't be wanting more output after block 0xff, otherwise
4435 * the capacity check in psa_key_derivation_output_bytes() would have
4436 * prevented this call. It could happen only if the operation
4437 * object was corrupted or if this function is called directly
4438 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004439 if (tls12_prf->block_number == 0xff) {
4440 return PSA_ERROR_CORRUPTION_DETECTED;
4441 }
Janos Follath7742fee2019-06-17 12:58:10 +01004442
4443 /* We need a new block */
4444 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004445 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004446
4447 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4448 *
4449 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4450 *
4451 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4452 * HMAC_hash(secret, A(2) + seed) +
4453 * HMAC_hash(secret, A(3) + seed) + ...
4454 *
4455 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004456 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004457 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004458 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004459 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004460 * is currently extracted as `output_block` and where i is
4461 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004462 */
4463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004464 status = psa_key_derivation_start_hmac(&hmac,
4465 hash_alg,
4466 tls12_prf->secret,
4467 tls12_prf->secret_length);
4468 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004469 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004470 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004471
4472 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004474 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4475 * the variable seed and in this instance means it in the context of the
4476 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 status = psa_mac_update(&hmac,
4478 tls12_prf->label,
4479 tls12_prf->label_length);
4480 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004481 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 }
4483 status = psa_mac_update(&hmac,
4484 tls12_prf->seed,
4485 tls12_prf->seed_length);
4486 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004487 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 }
4489 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004490 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4492 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004493 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004495 }
4496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004497 status = psa_mac_sign_finish(&hmac,
4498 tls12_prf->Ai, hash_length,
4499 &hmac_output_length);
4500 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004501 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004502 }
4503 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004504 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004506
4507 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004508 status = psa_key_derivation_start_hmac(&hmac,
4509 hash_alg,
4510 tls12_prf->secret,
4511 tls12_prf->secret_length);
4512 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004513 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004514 }
4515 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4516 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004517 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 }
4519 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4520 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004521 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 }
4523 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4524 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004525 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 }
4527 status = psa_mac_sign_finish(&hmac,
4528 tls12_prf->output_block, hash_length,
4529 &hmac_output_length);
4530 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004531 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004533
Janos Follath7742fee2019-06-17 12:58:10 +01004534
4535cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 cleanup_status = psa_mac_abort(&hmac);
4537 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004538 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004541 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004542}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004543
Janos Follath844eb0e2019-06-19 12:10:49 +01004544static psa_status_t psa_key_derivation_tls12_prf_read(
4545 psa_tls12_prf_key_derivation_t *tls12_prf,
4546 psa_algorithm_t alg,
4547 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004549{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4551 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004552 psa_status_t status;
4553 uint8_t offset, length;
4554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004556 case PSA_TLS12_PRF_STATE_LABEL_SET:
4557 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4558 break;
4559 case PSA_TLS12_PRF_STATE_OUTPUT:
4560 break;
4561 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004563 }
4564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004566 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 if (tls12_prf->left_in_block == 0) {
4568 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4569 alg);
4570 if (status != PSA_SUCCESS) {
4571 return status;
4572 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004573
4574 continue;
4575 }
4576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004578 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004580 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004581 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004582
4583 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004585 output += length;
4586 output_length -= length;
4587 tls12_prf->left_in_block -= length;
4588 }
4589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004591}
John Durkop07cc04a2020-11-16 22:08:34 -08004592#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4593 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004594
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004595psa_status_t psa_key_derivation_output_bytes(
4596 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004597 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004598 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004599{
4600 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004601 LOCAL_OUTPUT_DECLARE(output_external, output);
4602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004605 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004608 }
4609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004610 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004611 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004612 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004613 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4614 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004615 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004616 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004618 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004619
4620 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4621 if (output_length > operation->capacity) {
4622 operation->capacity = 0;
4623 /* Go through the error path to wipe all confidential data now
4624 * that the operation object is useless. */
4625 status = PSA_ERROR_INSUFFICIENT_DATA;
4626 goto exit;
4627 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004629
John Durkopd0321952020-10-29 21:37:36 -07004630#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4632 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4633 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4634 output, output_length);
4635 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004636#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4637#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4638 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4640 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4641 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4642 kdf_alg, output,
4643 output_length);
4644 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004645#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4646 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004648 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004649 status = PSA_ERROR_BAD_STATE;
4650 LOCAL_OUTPUT_FREE(output_external, output);
4651
4652 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653 }
4654
4655exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004657 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 * This allows us to differentiate between exhausted operations and
4659 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4660 * operations. */
4661 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004664 if (output != NULL) {
4665 memset(output, '!', output_length);
4666 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004668
4669 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004671}
4672
David Brown7807bf72021-02-09 16:28:23 -07004673#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004675{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 if (data_size >= 8) {
4677 mbedtls_des_key_set_parity(data);
4678 }
4679 if (data_size >= 16) {
4680 mbedtls_des_key_set_parity(data + 8);
4681 }
4682 if (data_size >= 24) {
4683 mbedtls_des_key_set_parity(data + 16);
4684 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004685}
David Brown7807bf72021-02-09 16:28:23 -07004686#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004687
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004688static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004689 psa_key_slot_t *slot,
4690 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004691 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004692{
4693 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004694 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004695 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004696 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 if (!key_type_is_raw_bytes(slot->attr.type)) {
4699 return PSA_ERROR_INVALID_ARGUMENT;
4700 }
4701 if (bits % 8 != 0) {
4702 return PSA_ERROR_INVALID_ARGUMENT;
4703 }
4704 data = mbedtls_calloc(1, bytes);
4705 if (data == NULL) {
4706 return PSA_ERROR_INSUFFICIENT_MEMORY;
4707 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 status = psa_key_derivation_output_bytes(operation, data, bytes);
4710 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004712 }
David Brown12ca5032021-02-11 11:02:00 -07004713#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4715 psa_des_set_key_parity(data, bytes);
4716 }
David Brown8107e312021-02-12 08:08:46 -07004717#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 status = psa_allocate_buffer_to_slot(slot, bytes);
4720 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004721 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004722 }
Ronald Crondd04d422020-11-28 15:14:42 +01004723
Ronald Cronbf33c932020-11-28 18:06:53 +01004724 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004725 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004727 };
4728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 status = psa_driver_wrapper_import_key(&attributes,
4730 data, bytes,
4731 slot->key.data,
4732 slot->key.bytes,
4733 &slot->key.bytes, &bits);
4734 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004735 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737
4738exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 mbedtls_free(data);
4740 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741}
4742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4744 psa_key_derivation_operation_t *operation,
4745 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004746{
4747 psa_status_t status;
4748 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004749 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004750
Ronald Cron81709fc2020-11-14 12:10:32 +01004751 *key = MBEDTLS_SVC_KEY_ID_INIT;
4752
Gilles Peskine0f84d622019-09-12 19:03:13 +02004753 /* Reject any attempt to create a zero-length key so that we don't
4754 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 if (psa_get_key_bits(attributes) == 0) {
4756 return PSA_ERROR_INVALID_ARGUMENT;
4757 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 if (operation->alg == PSA_ALG_NONE) {
4760 return PSA_ERROR_BAD_STATE;
4761 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004762
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004763 if (!operation->can_output_key) {
4764 return PSA_ERROR_NOT_PERMITTED;
4765 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4768 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004769#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004771 /* Deriving a key in a secure element is not implemented yet. */
4772 status = PSA_ERROR_NOT_SUPPORTED;
4773 }
4774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 if (status == PSA_SUCCESS) {
4776 status = psa_generate_derived_key_internal(slot,
4777 attributes->core.bits,
4778 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 if (status == PSA_SUCCESS) {
4781 status = psa_finish_key_creation(slot, driver, key);
4782 }
4783 if (status != PSA_SUCCESS) {
4784 psa_fail_key_creation(slot, driver);
4785 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004787 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004788}
4789
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790
4791
4792/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004793/* Key derivation */
4794/****************************************************************/
4795
Steven Cooreman932ffb72021-02-15 12:14:32 +01004796#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004797static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004798{
4799#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4801 return 1;
4802 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004803#endif
4804#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4806 return 1;
4807 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004808#endif
4809#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004810 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4811 return 1;
4812 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004813#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004815}
4816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004817static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004818{
4819 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 psa_status_t status = psa_hash_setup(&operation, alg);
4821 psa_hash_abort(&operation);
4822 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004823}
4824
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004828{
Janos Follath5fe19732019-06-20 15:09:30 +01004829 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4830 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004832
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834 if (!is_kdf_alg_supported(kdf_alg)) {
4835 return PSA_ERROR_NOT_SUPPORTED;
4836 }
John Durkop07cc04a2020-11-16 22:08:34 -08004837
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004838 /* All currently supported key derivation algorithms are based on a
4839 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4841 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4842 if (hash_size == 0) {
4843 return PSA_ERROR_NOT_SUPPORTED;
4844 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004845
4846 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4847 * we might fail later, which is somewhat unfriendly and potentially
4848 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849 psa_status_t status = psa_hash_try_support(hash_alg);
4850 if (status != PSA_SUCCESS) {
4851 return status;
4852 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4855 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4856 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4857 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004859
Gilles Peskinecdacf042021-04-29 21:10:00 +02004860 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004862}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004865{
4866#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 if (alg == PSA_ALG_ECDH) {
4868 return PSA_SUCCESS;
4869 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004870#endif
4871 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004873}
John Durkop07cc04a2020-11-16 22:08:34 -08004874#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4877 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004878{
4879 psa_status_t status;
4880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881 if (operation->alg != 0) {
4882 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4886 return PSA_ERROR_INVALID_ARGUMENT;
4887 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4888#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4889 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4890 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4891 status = psa_key_agreement_try_support(ka_alg);
4892 if (status != PSA_SUCCESS) {
4893 return status;
4894 }
4895 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4896#else
4897 return PSA_ERROR_NOT_SUPPORTED;
4898#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4899 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4900#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4901 status = psa_key_derivation_setup_kdf(operation, alg);
4902#else
4903 return PSA_ERROR_NOT_SUPPORTED;
4904#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4905 } else {
4906 return PSA_ERROR_INVALID_ARGUMENT;
4907 }
4908
4909 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911 }
4912 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913}
4914
John Durkopd0321952020-10-29 21:37:36 -07004915#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004916static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4917 psa_algorithm_t hash_alg,
4918 psa_key_derivation_step_t step,
4919 const uint8_t *data,
4920 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004921{
4922 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004923 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004924 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004925 if (hkdf->state != HKDF_STATE_INIT) {
4926 return PSA_ERROR_BAD_STATE;
4927 } else {
4928 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4929 hash_alg,
4930 data, data_length);
4931 if (status != PSA_SUCCESS) {
4932 return status;
4933 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004934 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004936 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004937 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939 if (hkdf->state == HKDF_STATE_INIT) {
4940 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4941 hash_alg,
4942 NULL, 0);
4943 if (status != PSA_SUCCESS) {
4944 return status;
4945 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 hkdf->state = HKDF_STATE_STARTED;
4947 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004948 if (hkdf->state != HKDF_STATE_STARTED) {
4949 return PSA_ERROR_BAD_STATE;
4950 }
4951 status = psa_mac_update(&hkdf->hmac,
4952 data, data_length);
4953 if (status != PSA_SUCCESS) {
4954 return status;
4955 }
4956 status = psa_mac_sign_finish(&hkdf->hmac,
4957 hkdf->prk,
4958 sizeof(hkdf->prk),
4959 &data_length);
4960 if (status != PSA_SUCCESS) {
4961 return status;
4962 }
4963 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004964 hkdf->block_number = 0;
4965 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004966 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004967 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968 if (hkdf->state == HKDF_STATE_OUTPUT) {
4969 return PSA_ERROR_BAD_STATE;
4970 }
4971 if (hkdf->info_set) {
4972 return PSA_ERROR_BAD_STATE;
4973 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 if (data_length != 0) {
4976 hkdf->info = mbedtls_calloc(1, data_length);
4977 if (hkdf->info == NULL) {
4978 return PSA_ERROR_INSUFFICIENT_MEMORY;
4979 }
4980 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004981 }
4982 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004984 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004985 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004986 }
4987}
John Durkop07cc04a2020-11-16 22:08:34 -08004988#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004989
John Durkop07cc04a2020-11-16 22:08:34 -08004990#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4993 const uint8_t *data,
4994 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004995{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004996 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4997 return PSA_ERROR_BAD_STATE;
4998 }
Janos Follathf08e2652019-06-13 09:05:41 +01004999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000 if (data_length != 0) {
5001 prf->seed = mbedtls_calloc(1, data_length);
5002 if (prf->seed == NULL) {
5003 return PSA_ERROR_INSUFFICIENT_MEMORY;
5004 }
Janos Follathf08e2652019-06-13 09:05:41 +01005005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005006 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005007 prf->seed_length = data_length;
5008 }
Janos Follathf08e2652019-06-13 09:05:41 +01005009
Gilles Peskine43f958b2020-12-13 14:55:14 +01005010 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005012 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01005013}
5014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
5016 const uint8_t *data,
5017 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01005018{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
5020 return PSA_ERROR_BAD_STATE;
5021 }
Janos Follath81550542019-06-13 14:26:34 +01005022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 if (data_length != 0) {
5024 prf->secret = mbedtls_calloc(1, data_length);
5025 if (prf->secret == NULL) {
5026 return PSA_ERROR_INSUFFICIENT_MEMORY;
5027 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02005030 prf->secret_length = data_length;
5031 }
Janos Follath81550542019-06-13 14:26:34 +01005032
Gilles Peskine43f958b2020-12-13 14:55:14 +01005033 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01005036}
5037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
5039 const uint8_t *data,
5040 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01005041{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
5043 return PSA_ERROR_BAD_STATE;
5044 }
Janos Follath63028dd2019-06-13 09:15:47 +01005045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046 if (data_length != 0) {
5047 prf->label = mbedtls_calloc(1, data_length);
5048 if (prf->label == NULL) {
5049 return PSA_ERROR_INSUFFICIENT_MEMORY;
5050 }
Janos Follath63028dd2019-06-13 09:15:47 +01005051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005052 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01005053 prf->label_length = data_length;
5054 }
Janos Follath63028dd2019-06-13 09:15:47 +01005055
Gilles Peskine43f958b2020-12-13 14:55:14 +01005056 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01005059}
5060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5062 psa_key_derivation_step_t step,
5063 const uint8_t *data,
5064 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005065{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005067 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005069 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005071 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005073 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005074 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005075 }
5076}
John Durkop07cc04a2020-11-16 22:08:34 -08005077#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5078 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5079
5080#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5081static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5082 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005083 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005085{
5086 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005088 uint8_t *cur = pms;
5089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005090 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5091 return PSA_ERROR_INVALID_ARGUMENT;
5092 }
John Durkop07cc04a2020-11-16 22:08:34 -08005093
5094 /* Quoting RFC 4279, Section 2:
5095 *
5096 * The premaster secret is formed as follows: if the PSK is N octets
5097 * long, concatenate a uint16 with the value N, N zero octets, a second
5098 * uint16 with the value N, and the PSK itself.
5099 */
5100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101 *cur++ = MBEDTLS_BYTE_1(data_length);
5102 *cur++ = MBEDTLS_BYTE_0(data_length);
5103 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005104 cur += data_length;
5105 *cur++ = pms[0];
5106 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005107 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005108 cur += data_length;
5109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005110 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 mbedtls_platform_zeroize(pms, sizeof(pms));
5113 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005114}
Janos Follath6660f0e2019-06-17 08:44:03 +01005115
5116static psa_status_t psa_tls12_prf_psk_to_ms_input(
5117 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005118 psa_key_derivation_step_t step,
5119 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005121{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005122 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5123 return psa_tls12_prf_psk_to_ms_set_key(prf,
5124 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005125 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005128}
John Durkop07cc04a2020-11-16 22:08:34 -08005129#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005130
Gilles Peskineb8965192019-09-24 16:21:10 +02005131/** Check whether the given key type is acceptable for the given
5132 * input step of a key derivation.
5133 *
5134 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5135 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5136 * Both secret and non-secret inputs can alternatively have the type
5137 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5138 * that the input was passed as a buffer rather than via a key object.
5139 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005140static int psa_key_derivation_check_input_type(
5141 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005143{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005145 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 if (key_type == PSA_KEY_TYPE_DERIVE) {
5147 return PSA_SUCCESS;
5148 }
5149 if (key_type == PSA_KEY_TYPE_NONE) {
5150 return PSA_SUCCESS;
5151 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005152 break;
5153 case PSA_KEY_DERIVATION_INPUT_LABEL:
5154 case PSA_KEY_DERIVATION_INPUT_SALT:
5155 case PSA_KEY_DERIVATION_INPUT_INFO:
5156 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5158 return PSA_SUCCESS;
5159 }
5160 if (key_type == PSA_KEY_TYPE_NONE) {
5161 return PSA_SUCCESS;
5162 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005163 break;
5164 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005166}
5167
Janos Follathb80a94e2019-06-12 15:54:46 +01005168static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005170 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005171 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005172 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005175 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178 status = psa_key_derivation_check_input_type(step, key_type);
5179 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005180 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005182
John Durkopd0321952020-10-29 21:37:36 -07005183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5185 status = psa_hkdf_input(&operation->ctx.hkdf,
5186 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5187 step, data, data_length);
5188 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005189#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5190#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5192 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5193 step, data, data_length);
5194 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005195#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5196#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5198 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5199 step, data, data_length);
5200 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005201#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005204 (void) data;
5205 (void) data_length;
5206 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005207 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 }
5209
Gilles Peskine224b0d62019-09-23 18:13:17 +02005210exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005211 if (status != PSA_SUCCESS) {
5212 psa_key_derivation_abort(operation);
5213 }
5214 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215}
5216
Janos Follath51f4a0f2019-06-14 11:35:55 +01005217psa_status_t psa_key_derivation_input_bytes(
5218 psa_key_derivation_operation_t *operation,
5219 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005220 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005222{
Ryan Everett6f682062024-02-09 16:18:39 +00005223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5224 LOCAL_INPUT_DECLARE(data_external, data);
5225
5226 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5227
5228 status = psa_key_derivation_input_internal(operation, step,
5229 PSA_KEY_TYPE_NONE,
5230 data, data_length);
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005231#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005232exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005233#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005234 LOCAL_INPUT_FREE(data_external, data);
5235 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005236}
5237
Janos Follath51f4a0f2019-06-14 11:35:55 +01005238psa_status_t psa_key_derivation_input_key(
5239 psa_key_derivation_operation_t *operation,
5240 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005242{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005244 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005245 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005246
Ronald Cron5c522922020-11-14 16:35:34 +01005247 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5249 if (status != PSA_SUCCESS) {
5250 psa_key_derivation_abort(operation);
5251 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005252 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005253
5254 /* Passing a key object as a SECRET input unlocks the permission
5255 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005257 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005258 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 status = psa_key_derivation_input_internal(operation,
5261 step, slot->attr.type,
5262 slot->key.data,
5263 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005268}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005269
5270
5271
5272/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273/* Key agreement */
5274/****************************************************************/
5275
John Durkop6ba40d12020-11-10 08:50:04 -08005276#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5278 size_t peer_key_length,
5279 const mbedtls_ecp_keypair *our_key,
5280 uint8_t *shared_secret,
5281 size_t shared_secret_size,
5282 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005283{
Steven Cooremand4867872020-08-05 16:31:39 +02005284 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005286 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005287 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005288 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5289 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290
Ronald Cron90857082020-11-25 14:58:33 +01005291 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5293 bits,
5294 peer_key,
5295 peer_key_length,
5296 &their_key);
5297 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005299 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005300
Jaeden Amero97271b32019-01-10 19:38:51 +00005301 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5303 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005304 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005305 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005306 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005307 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5308 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005309 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005310 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005311
Jaeden Amero97271b32019-01-10 19:38:51 +00005312 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005313 mbedtls_ecdh_calc_secret(&ecdh,
5314 shared_secret_length,
5315 shared_secret, shared_secret_size,
5316 mbedtls_psa_get_random,
5317 MBEDTLS_PSA_RANDOM_STATE));
5318 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005319 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 }
5321 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005322 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005323 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324
5325exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005326 if (status != PSA_SUCCESS) {
5327 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5328 }
5329 mbedtls_ecdh_free(&ecdh);
5330 mbedtls_ecp_keypair_free(their_key);
5331 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005333 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005334}
John Durkop6ba40d12020-11-10 08:50:04 -08005335#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005336
Gilles Peskine01d718c2018-09-18 12:01:02 +02005337#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5340 psa_key_slot_t *private_key,
5341 const uint8_t *peer_key,
5342 size_t peer_key_length,
5343 uint8_t *shared_secret,
5344 size_t shared_secret_size,
5345 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005347 mbedtls_ecp_keypair *ecp = NULL;
5348 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005351#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005352 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5354 return PSA_ERROR_INVALID_ARGUMENT;
5355 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005356 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 private_key->attr.type,
5358 private_key->attr.bits,
5359 private_key->key.data,
5360 private_key->key.bytes,
5361 &ecp);
5362 if (status != PSA_SUCCESS) {
5363 return status;
5364 }
5365 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5366 ecp,
5367 shared_secret, shared_secret_size,
5368 shared_secret_length);
5369 mbedtls_ecp_keypair_free(ecp);
5370 mbedtls_free(ecp);
5371 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005372#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005373 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005374 (void) ecp;
5375 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005376 (void) private_key;
5377 (void) peer_key;
5378 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005379 (void) shared_secret;
5380 (void) shared_secret_size;
5381 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005382 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005383 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005384}
5385
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005386/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005387 * to potentially free embedded data structures and wipe confidential data.
5388 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5390 psa_key_derivation_step_t step,
5391 psa_key_slot_t *private_key,
5392 const uint8_t *peer_key,
5393 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394{
5395 psa_status_t status;
5396 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5397 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005399
5400 /* Step 1: run the secret agreement algorithm to generate the shared
5401 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005402 status = psa_key_agreement_raw_internal(ka_alg,
5403 private_key,
5404 peer_key, peer_key_length,
5405 shared_secret,
5406 sizeof(shared_secret),
5407 &shared_secret_length);
5408 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005409 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005411
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005412 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005413 * the shared secret. A shared secret is permitted wherever a key
5414 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 status = psa_key_derivation_input_internal(operation, step,
5416 PSA_KEY_TYPE_DERIVE,
5417 shared_secret,
5418 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005419exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5421 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005422}
5423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5425 psa_key_derivation_step_t step,
5426 mbedtls_svc_key_id_t private_key,
5427 const uint8_t *peer_key,
5428 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005429{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005431 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005432 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5435 return PSA_ERROR_INVALID_ARGUMENT;
5436 }
Ronald Cron5c522922020-11-14 16:35:34 +01005437 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005438 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5439 if (status != PSA_SUCCESS) {
5440 return status;
5441 }
5442 status = psa_key_agreement_internal(operation, step,
5443 slot,
5444 peer_key, peer_key_length);
5445 if (status != PSA_SUCCESS) {
5446 psa_key_derivation_abort(operation);
5447 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005448 /* If a private key has been added as SECRET, we allow the derived
5449 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005451 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005453 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005458}
5459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5461 mbedtls_svc_key_id_t private_key,
5462 const uint8_t *peer_key,
5463 size_t peer_key_length,
5464 uint8_t *output,
5465 size_t output_size,
5466 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005467{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005470 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005471 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005473 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005474 status = PSA_ERROR_INVALID_ARGUMENT;
5475 goto exit;
5476 }
Ronald Cron5c522922020-11-14 16:35:34 +01005477 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5479 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005480 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005481 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005482
Gilles Peskine42313fb2022-04-14 00:17:15 +02005483 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5484 * for the output size. The PSA specification only guarantees that this
5485 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5486 * but it might be nice to allow smaller buffers if the output fits.
5487 * At the time of writing this comment, with only ECDH implemented,
5488 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5489 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5490 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005491 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5493 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005494 status = PSA_ERROR_BUFFER_TOO_SMALL;
5495 goto exit;
5496 }
5497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 status = psa_key_agreement_raw_internal(alg, slot,
5499 peer_key, peer_key_length,
5500 output, output_size,
5501 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005502
5503exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005505 /* If an error happens and is not handled properly, the output
5506 * may be used as a key to protect sensitive data. Arrange for such
5507 * a key to be random, which is likely to result in decryption or
5508 * verification errors. This is better than filling the buffer with
5509 * some constant data such as zeros, which would result in the data
5510 * being protected with a reproducible, easily knowable key.
5511 */
David Horstmann65bf12c2024-02-06 15:27:49 +00005512 psa_generate_random_internal(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005513 *output_length = output_size;
5514 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005516 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005518 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005519}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005520
5521
5522/****************************************************************/
5523/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005524/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005525
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005526#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5527#include "mbedtls/entropy_poll.h"
5528#endif
5529
Gilles Peskine30524eb2020-11-13 17:02:26 +01005530/** Initialize the PSA random generator.
5531 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005533{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005534#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005536#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5537
Gilles Peskine30524eb2020-11-13 17:02:26 +01005538 /* Set default configuration if
5539 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005541 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 }
5543 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005544 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005548#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5549 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5550 /* The PSA entropy injection feature depends on using NV seed as an entropy
5551 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 mbedtls_entropy_add_source(&rng->entropy,
5553 mbedtls_nv_seed_poll, NULL,
5554 MBEDTLS_ENTROPY_BLOCK_SIZE,
5555 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005556#endif
5557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005559#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005560}
5561
5562/** Deinitialize the PSA random generator.
5563 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005564static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005565{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005566#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005567 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005568#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005569 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5570 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005571#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005572}
5573
5574/** Seed the PSA random generator.
5575 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005577{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005578#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5579 /* Do nothing: the external RNG seeds itself. */
5580 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005581 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005582#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005583 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005584 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5585 drbg_seed, sizeof(drbg_seed) - 1);
5586 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005587#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005588}
5589
David Horstmann65bf12c2024-02-06 15:27:49 +00005590psa_status_t psa_generate_random(uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005591 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592{
David Horstmann65bf12c2024-02-06 15:27:49 +00005593 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005594
David Horstmann65bf12c2024-02-06 15:27:49 +00005595 LOCAL_OUTPUT_DECLARE(output_external, output);
5596 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005597
David Horstmann65bf12c2024-02-06 15:27:49 +00005598 status = psa_generate_random_internal(output, output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005599
David Horstmann10e44f32024-02-28 14:17:10 +00005600#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann65bf12c2024-02-06 15:27:49 +00005601exit:
David Horstmann10e44f32024-02-28 14:17:10 +00005602#endif
David Horstmann65bf12c2024-02-06 15:27:49 +00005603 LOCAL_OUTPUT_FREE(output_external, output);
5604 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005605}
5606
Gilles Peskine8814fc42020-12-14 15:33:44 +01005607/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005608 *
5609 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5610 * `psa_generate_random(...)`. The state parameter is ignored since the
5611 * PSA API doesn't support passing an explicit state.
5612 *
5613 * In the non-external case, psa_generate_random() calls an
5614 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5615 * and semantics as mbedtls_psa_get_random(). As an optimization,
5616 * instead of doing this back-and-forth between the PSA API and the
5617 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5618 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005619 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005620#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5621int mbedtls_psa_get_random(void *p_rng,
5622 unsigned char *output,
5623 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005624{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005625 /* This function takes a pointer to the RNG state because that's what
5626 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5627 * its own state internally and doesn't let the caller access that state.
5628 * So we just ignore the state parameter, and in practice we'll pass
5629 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005630 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005631 psa_status_t status = psa_generate_random(output, output_size);
5632 if (status == PSA_SUCCESS) {
5633 return 0;
5634 } else {
5635 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5636 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005637}
5638#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5639
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005640#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005641psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5642 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005643{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 if (global_data.initialized) {
5645 return PSA_ERROR_NOT_PERMITTED;
5646 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005648 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5649 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5650 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5651 return PSA_ERROR_INVALID_ARGUMENT;
5652 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005654 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005655}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005656#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005657
Ronald Cron3772afe2021-02-08 16:10:05 +01005658/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005659 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005660 * \param type The key type
5661 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005662 *
5663 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005664 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005665 * \retval #PSA_ERROR_INVALID_ARGUMENT
5666 * The size in bits of the key is not valid.
5667 * \retval #PSA_ERROR_NOT_SUPPORTED
5668 * The type and/or the size in bits of the key or the combination of
5669 * the two is not supported.
5670 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005671static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005673{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676 if (key_type_is_raw_bytes(type)) {
5677 status = validate_unstructured_key_bit_size(type, bits);
5678 if (status != PSA_SUCCESS) {
5679 return status;
5680 }
5681 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005682#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005683 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5684 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5685 return PSA_ERROR_NOT_SUPPORTED;
5686 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005687
Ronald Cron01b2aba2020-10-05 09:42:02 +02005688 /* Accept only byte-aligned keys, for the same reasons as
5689 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 if (bits % 8 != 0) {
5691 return PSA_ERROR_NOT_SUPPORTED;
5692 }
5693 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005694#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005695
Ronald Cron5c4d3862020-12-07 11:07:24 +01005696#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005697 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005698 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005699 return PSA_SUCCESS;
5700 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005701#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005702 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005703 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005704 }
5705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005706 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005707}
5708
Ronald Cron55ed0592020-10-05 10:30:40 +02005709psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005710 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005711 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005712{
5713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005714 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005716 if ((attributes->domain_parameters == NULL) &&
5717 (attributes->domain_parameters_size != 0)) {
5718 return PSA_ERROR_INVALID_ARGUMENT;
5719 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005721 if (key_type_is_raw_bytes(type)) {
5722 status = psa_generate_random(key_buffer, key_buffer_size);
5723 if (status != PSA_SUCCESS) {
5724 return status;
5725 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005726
David Brown12ca5032021-02-11 11:02:00 -07005727#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005728 if (type == PSA_KEY_TYPE_DES) {
5729 psa_des_set_key_parity(key_buffer, key_buffer_size);
5730 }
David Brown8107e312021-02-12 08:08:46 -07005731#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005732 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005733
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005734#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5735 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005736 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5737 return mbedtls_psa_rsa_generate_key(attributes,
5738 key_buffer,
5739 key_buffer_size,
5740 key_buffer_length);
5741 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005742#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5743 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744
John Durkop9814fa22020-11-04 12:28:15 -08005745#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005746 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5747 return mbedtls_psa_ecp_generate_key(attributes,
5748 key_buffer,
5749 key_buffer_size,
5750 key_buffer_length);
5751 } else
John Durkop9814fa22020-11-04 12:28:15 -08005752#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005753 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005754 (void) key_buffer_length;
5755 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005756 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005758 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005759}
Darryl Green0c6575a2018-11-07 16:05:30 +00005760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005761psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5762 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005763{
5764 psa_status_t status;
5765 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005766 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005767 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005768
Ronald Cron81709fc2020-11-14 12:10:32 +01005769 *key = MBEDTLS_SVC_KEY_ID_INIT;
5770
Gilles Peskine0f84d622019-09-12 19:03:13 +02005771 /* Reject any attempt to create a zero-length key so that we don't
5772 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005773 if (psa_get_key_bits(attributes) == 0) {
5774 return PSA_ERROR_INVALID_ARGUMENT;
5775 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005776
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005777 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005778 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5779 return PSA_ERROR_INVALID_ARGUMENT;
5780 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005782 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5783 &slot, &driver);
5784 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005785 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005786 }
Gilles Peskine11792082019-08-06 18:36:36 +02005787
Ronald Cron977c2472020-10-13 08:32:21 +02005788 /* In the case of a transparent key or an opaque key stored in local
5789 * storage (thus not in the case of generating a key in a secure element
5790 * or cryptoprocessor with storage), we have to allocate a buffer to
5791 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005792 if (slot->key.data == NULL) {
5793 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5794 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005795 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005796 attributes->core.type, attributes->core.bits);
5797 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005798 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005799 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005800
5801 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005802 attributes->core.type,
5803 attributes->core.bits);
5804 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005805 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005806 attributes, &key_buffer_size);
5807 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005808 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005809 }
Ronald Cron977c2472020-10-13 08:32:21 +02005810 }
Ronald Cron977c2472020-10-13 08:32:21 +02005811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005812 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5813 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005814 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005815 }
Ronald Cron977c2472020-10-13 08:32:21 +02005816 }
5817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005818 status = psa_driver_wrapper_generate_key(attributes,
5819 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005821 if (status != PSA_SUCCESS) {
5822 psa_remove_key_data_from_memory(slot);
5823 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005824
Gilles Peskine11792082019-08-06 18:36:36 +02005825exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826 if (status == PSA_SUCCESS) {
5827 status = psa_finish_key_creation(slot, driver, key);
5828 }
5829 if (status != PSA_SUCCESS) {
5830 psa_fail_key_creation(slot, driver);
5831 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005833 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005834}
5835
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836/****************************************************************/
5837/* Module setup */
5838/****************************************************************/
5839
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005840#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005841psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 void (* entropy_init)(mbedtls_entropy_context *ctx),
5843 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005844{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005845 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5846 return PSA_ERROR_BAD_STATE;
5847 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005848 global_data.rng.entropy_init = entropy_init;
5849 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005850 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005851}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005852#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005855{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005856 psa_wipe_all_key_slots();
5857 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5858 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005859 }
5860 /* Wipe all remaining data, including configuration.
5861 * In particular, this sets all state indicator to the value
5862 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005863 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005864
5865 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005866 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005867}
5868
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005869#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5870/** Recover a transaction that was interrupted by a power failure.
5871 *
5872 * This function is called during initialization, before psa_crypto_init()
5873 * returns. If this function returns a failure status, the initialization
5874 * fails.
5875 */
5876static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005877 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005878{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005879 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005880 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5881 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005882 /* TODO - fall through to the failure case until this
5883 * is implemented.
5884 * https://github.com/ARMmbed/mbed-crypto/issues/218
5885 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005886 default:
5887 /* We found an unsupported transaction in the storage.
5888 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005889 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005890 }
5891}
5892#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005894psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005895{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005896 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005897
Gilles Peskinec6b69072018-11-20 21:42:52 +01005898 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005899 if (global_data.initialized != 0) {
5900 return PSA_SUCCESS;
5901 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005902
Gilles Peskine30524eb2020-11-13 17:02:26 +01005903 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005904 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005905 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005906 status = mbedtls_psa_random_seed(&global_data.rng);
5907 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005908 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005909 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005910 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005912 status = psa_initialize_key_slots();
5913 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005914 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005915 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005916
Ronald Cron088d5d02021-04-10 16:57:30 +02005917 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005918 status = psa_driver_wrapper_init();
5919 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005920 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005921 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005922
Gilles Peskine4b734222019-07-24 15:56:31 +02005923#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005924 status = psa_crypto_load_transaction();
5925 if (status == PSA_SUCCESS) {
5926 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5927 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005928 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005929 }
5930 status = psa_crypto_stop_transaction();
5931 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005932 /* There's no transaction to complete. It's all good. */
5933 status = PSA_SUCCESS;
5934 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005935#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005936
Gilles Peskinec6b69072018-11-20 21:42:52 +01005937 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005938 global_data.initialized = 1;
5939
5940exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005941 if (status != PSA_SUCCESS) {
5942 mbedtls_psa_crypto_free();
5943 }
5944 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005945}
5946
David Horstmann114d8242023-12-07 18:39:17 +00005947/* Memory copying test hooks. These are called before input copy, after input
5948 * copy, before output copy and after output copy, respectively.
5949 * They are used by memory-poisoning tests to temporarily unpoison buffers
5950 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005951#if defined(MBEDTLS_TEST_HOOKS)
5952void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5953void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5954void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5955void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5956#endif
5957
David Horstmanndf493552023-11-15 15:18:30 +00005958/** Copy from an input buffer to a local copy.
5959 *
5960 * \param[in] input Pointer to input buffer.
5961 * \param[in] input_len Length of the input buffer.
5962 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5963 * \param[out] input_copy_len Length of the local copy buffer.
5964 * \return #PSA_SUCCESS, if the buffer was successfully
5965 * copied.
5966 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5967 * copy is too small to hold contents of the
5968 * input buffer.
5969 */
5970MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005971psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5972 uint8_t *input_copy, size_t input_copy_len)
5973{
5974 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005975 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005976 }
5977
David Horstmann0760b152023-11-24 16:21:04 +00005978#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005979 if (psa_input_pre_copy_hook != NULL) {
5980 psa_input_pre_copy_hook(input, input_len);
5981 }
David Horstmann0760b152023-11-24 16:21:04 +00005982#endif
5983
David Horstmann660027f2023-11-15 17:33:47 +00005984 if (input_len > 0) {
5985 memcpy(input_copy, input, input_len);
5986 }
David Horstmann957f9802023-10-30 20:29:43 +00005987
David Horstmann0760b152023-11-24 16:21:04 +00005988#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005989 if (psa_input_post_copy_hook != NULL) {
5990 psa_input_post_copy_hook(input, input_len);
5991 }
David Horstmann0760b152023-11-24 16:21:04 +00005992#endif
5993
David Horstmann957f9802023-10-30 20:29:43 +00005994 return PSA_SUCCESS;
5995}
5996
David Horstmanndf493552023-11-15 15:18:30 +00005997/** Copy from a local output buffer into a user-supplied one.
5998 *
5999 * \param[in] output_copy Pointer to a local buffer containing the output.
6000 * \param[in] output_copy_len Length of the local buffer.
6001 * \param[out] output Pointer to user-supplied output buffer.
6002 * \param[out] output_len Length of the user-supplied output buffer.
6003 * \return #PSA_SUCCESS, if the buffer was successfully
6004 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00006005 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00006006 * user-supplied output buffer is too small to
6007 * hold the contents of the local buffer.
6008 */
6009MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00006010psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
6011 uint8_t *output, size_t output_len)
6012{
6013 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00006014 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00006015 }
David Horstmann660027f2023-11-15 17:33:47 +00006016
David Horstmann0760b152023-11-24 16:21:04 +00006017#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006018 if (psa_output_pre_copy_hook != NULL) {
6019 psa_output_pre_copy_hook(output, output_len);
6020 }
David Horstmann0760b152023-11-24 16:21:04 +00006021#endif
6022
David Horstmann660027f2023-11-15 17:33:47 +00006023 if (output_copy_len > 0) {
6024 memcpy(output, output_copy, output_copy_len);
6025 }
6026
David Horstmann0760b152023-11-24 16:21:04 +00006027#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00006028 if (psa_output_post_copy_hook != NULL) {
6029 psa_output_post_copy_hook(output, output_len);
6030 }
David Horstmann0760b152023-11-24 16:21:04 +00006031#endif
6032
David Horstmann2bd296e2023-10-30 20:37:12 +00006033 return PSA_SUCCESS;
6034}
6035
David Horstmann81a0d572023-11-20 17:15:32 +00006036psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6037 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006038{
6039 psa_status_t status;
6040
David Horstmann0d52c712023-11-23 15:50:37 +00006041 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006042
David Horstmann726bf052023-11-15 18:11:26 +00006043 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006044 return PSA_SUCCESS;
6045 }
6046
David Horstmann81a0d572023-11-20 17:15:32 +00006047 local_input->buffer = mbedtls_calloc(input_len, 1);
6048 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006049 /* Since we dealt with the zero-length case above, we know that
6050 * a NULL return value means a failure of allocation. */
6051 return PSA_ERROR_INSUFFICIENT_MEMORY;
6052 }
David Horstmann81a0d572023-11-20 17:15:32 +00006053 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006054
David Horstmann81a0d572023-11-20 17:15:32 +00006055 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006056
6057 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006058 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006059 if (status != PSA_SUCCESS) {
6060 goto error;
6061 }
6062
6063 return PSA_SUCCESS;
6064
6065error:
David Horstmann81a0d572023-11-20 17:15:32 +00006066 mbedtls_free(local_input->buffer);
6067 local_input->buffer = NULL;
6068 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006069 return status;
6070}
6071
David Horstmann81a0d572023-11-20 17:15:32 +00006072void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006073{
David Horstmann81a0d572023-11-20 17:15:32 +00006074 mbedtls_free(local_input->buffer);
6075 local_input->buffer = NULL;
6076 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006077}
6078
David Horstmann1a76ab12023-11-20 12:54:09 +00006079psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6080 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006081{
David Horstmann0d52c712023-11-23 15:50:37 +00006082 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006083
David Horstmann726bf052023-11-15 18:11:26 +00006084 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006085 return PSA_SUCCESS;
6086 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006087 local_output->buffer = mbedtls_calloc(output_len, 1);
6088 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006089 /* Since we dealt with the zero-length case above, we know that
6090 * a NULL return value means a failure of allocation. */
6091 return PSA_ERROR_INSUFFICIENT_MEMORY;
6092 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006093 local_output->length = output_len;
6094 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006095
6096 return PSA_SUCCESS;
6097}
6098
Gabor Mezei3b0c3712024-01-24 13:07:17 +01006099psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len,
6100 psa_crypto_local_output_t *local_output)
6101{
6102 psa_status_t status;
6103 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
6104
6105 if (output_len == 0) {
6106 return PSA_SUCCESS;
6107 }
6108 local_output->buffer = mbedtls_calloc(output_len, 1);
6109 if (local_output->buffer == NULL) {
6110 /* Since we dealt with the zero-length case above, we know that
6111 * a NULL return value means a failure of allocation. */
6112 return PSA_ERROR_INSUFFICIENT_MEMORY;
6113 }
6114 local_output->length = output_len;
6115 local_output->original = output;
6116
6117 status = psa_crypto_copy_input(output, output_len,
6118 local_output->buffer, local_output->length);
6119 if (status != PSA_SUCCESS) {
6120 goto error;
6121 }
6122
6123 return PSA_SUCCESS;
6124
6125error:
6126 mbedtls_free(local_output->buffer);
6127 local_output->buffer = NULL;
6128 local_output->length = 0;
6129 return status;
6130}
6131
David Horstmann1a76ab12023-11-20 12:54:09 +00006132psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006133{
David Horstmann761761f2023-11-15 15:57:32 +00006134 psa_status_t status;
6135
David Horstmann1a76ab12023-11-20 12:54:09 +00006136 if (local_output->buffer == NULL) {
6137 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006138 return PSA_SUCCESS;
6139 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006140 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006141 /* We have an internal copy but nothing to copy back to. */
6142 return PSA_ERROR_CORRUPTION_DETECTED;
6143 }
6144
David Horstmann1a76ab12023-11-20 12:54:09 +00006145 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6146 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006147 if (status != PSA_SUCCESS) {
6148 return status;
6149 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006150
David Horstmann1a76ab12023-11-20 12:54:09 +00006151 mbedtls_free(local_output->buffer);
6152 local_output->buffer = NULL;
6153 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006154
6155 return PSA_SUCCESS;
6156}
6157
Gilles Peskinee59236f2018-01-27 23:32:46 +01006158#endif /* MBEDTLS_PSA_CRYPTO_C */