blob: 909c0ad3b2356d8007bd977f6a9e7691a6b72bb4 [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;
233#define LOCAL_OUTPUT_FREE(output, output_copy) \
234 output_copy = NULL;
David Horstmannc0a2c302023-11-29 17:24:08 +0000235#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100238{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100239 /* Mbed TLS error codes can combine a high-level error code and a
240 * low-level error code. The low-level error usually reflects the
241 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 int low_level_ret = -(-ret & 0x007f);
243 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100244 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
247 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
248 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
249 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200254 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200256
Gilles Peskine9a944802018-06-21 09:35:35 +0200257 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
258 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
259 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
260 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
261 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200263 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200265 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200267
Jaeden Amero93e21112019-02-20 13:57:28 +0000268#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000269 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000270#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
271 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
272#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Jaeden Amero93e21112019-02-20 13:57:28 +0000278#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000279 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000280#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
281 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
282#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287
288 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294
Gilles Peskine26869f22019-05-06 15:25:00 +0200295 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200297
298 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200300 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200302
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319
320 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
324 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100325 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
326 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
330 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100334#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100335
336 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
342 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
343 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345
346 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352
Gilles Peskine82e57d12020-11-13 21:31:17 +0100353#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100355 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
356 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100357 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100359 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
360 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100362 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100364#endif
365
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200366 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
367 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
368 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200370
Gilles Peskinea5905292018-02-07 20:59:33 +0100371 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100373 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200379 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100381
Gilles Peskinef76aa772018-10-29 19:24:33 +0100382 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100384 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100386 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
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_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100390 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100392 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
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_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100398
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
402 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100404 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100406 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
407 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
412 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416 case MBEDTLS_ERR_PK_INVALID_ALG:
417 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
418 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100422 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100424
Gilles Peskineff2d2002019-05-06 15:26:23 +0200425 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200427 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200429
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200430 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200432
Gilles Peskinea5905292018-02-07 20:59:33 +0100433 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100435 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100437 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100439 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100441 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
442 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100444 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100446 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100448 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100450 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100452 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100454
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200455 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
456 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
457 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200459
Gilles Peskinea5905292018-02-07 20:59:33 +0100460 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100462 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100464
itayzafrir5c753392018-05-08 11:18:38 +0300465 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300466 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300468 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300470 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300472 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
473 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300475 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100477 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300479 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100481
Janos Follatha13b9052019-11-22 12:48:59 +0000482 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300484
Gilles Peskinee59236f2018-01-27 23:32:46 +0100485 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100487 }
488}
489
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200490
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200491
492
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493/****************************************************************/
494/* Key management */
495/****************************************************************/
496
John Durkop9814fa22020-11-04 12:28:15 -0800497#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800498 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100499 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
500 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
501 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
503 size_t bits,
504 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200505{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100507 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100509#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100510 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100512#endif
513#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
525#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100526 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 if (bits_is_sloppy) {
530 return MBEDTLS_ECP_DP_SECP521R1;
531 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100532 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100533#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100534 }
535 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100536
Paul Elliott8ff510a2020-06-02 17:19:28 +0100537 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100542#endif
543#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100544 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#endif
547#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100550#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100551 }
552 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100553
Paul Elliott8ff510a2020-06-02 17:19:28 +0100554 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100556#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100557 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100559 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 if (bits_is_sloppy) {
561 return MBEDTLS_ECP_DP_CURVE25519;
562 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100563 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100564#endif
565#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100566 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100569 }
570 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100571
Paul Elliott8ff510a2020-06-02 17:19:28 +0100572 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100574#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100575 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100577#endif
578#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100579 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100581#endif
582#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100583 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100585#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100586 }
587 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200588 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100589
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100590 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200592}
John Durkop9814fa22020-11-04 12:28:15 -0800593#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100594 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
595 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
596 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
597 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
600 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200601{
602 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200604 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200606 case PSA_KEY_TYPE_DERIVE:
607 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100608#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200609 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 if (bits != 128 && bits != 192 && bits != 256) {
611 return PSA_ERROR_INVALID_ARGUMENT;
612 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 break;
614#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200615#if defined(PSA_WANT_KEY_TYPE_ARIA)
616 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 if (bits != 128 && bits != 192 && bits != 256) {
618 return PSA_ERROR_INVALID_ARGUMENT;
619 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200620 break;
621#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100622#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (bits != 128 && bits != 192 && bits != 256) {
625 return PSA_ERROR_INVALID_ARGUMENT;
626 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 break;
628#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100629#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if (bits != 64 && bits != 128 && bits != 192) {
632 return PSA_ERROR_INVALID_ARGUMENT;
633 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634 break;
635#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100636#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 if (bits < 8 || bits > 2048) {
639 return PSA_ERROR_INVALID_ARGUMENT;
640 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641 break;
642#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100643#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200644 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 if (bits != 256) {
646 return PSA_ERROR_INVALID_ARGUMENT;
647 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200648 break;
649#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200650 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 if (bits % 8 != 0) {
654 return PSA_ERROR_INVALID_ARGUMENT;
655 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200658}
659
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100660/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100661 *
Steven Cooremancd640932021-03-08 12:00:27 +0100662 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
663 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664 *
665 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100666 * \param[in] key_type The key type of the key to be used with the
667 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668 *
669 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100670 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100671 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100672 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100675 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100677{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 if (PSA_ALG_IS_HMAC(algorithm)) {
679 if (key_type == PSA_KEY_TYPE_HMAC) {
680 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100681 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100682 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
685 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
686 * key. */
687 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
688 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
689 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
690 * the block length (larger than 1) for block ciphers. */
691 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
692 return PSA_SUCCESS;
693 }
694 }
695 }
696
697 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100698}
699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
701 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200702{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 if (slot->key.data != NULL) {
704 return PSA_ERROR_ALREADY_EXISTS;
705 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 slot->key.data = mbedtls_calloc(1, buffer_length);
708 if (slot->key.data == NULL) {
709 return PSA_ERROR_INSUFFICIENT_MEMORY;
710 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200711
Ronald Cronea0f8a62020-11-25 17:52:23 +0100712 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200714}
715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
717 const uint8_t *data,
718 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200719{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 psa_status_t status = psa_allocate_buffer_to_slot(slot,
721 data_length);
722 if (status != PSA_SUCCESS) {
723 return status;
724 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 memcpy(slot->key.data, data, data_length);
727 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200728}
729
Ronald Crona0fe59f2020-11-29 11:14:53 +0100730psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100731 const psa_key_attributes_t *attributes,
732 const uint8_t *data, size_t data_length,
733 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
737 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 if (data_length == 0) {
741 return PSA_ERROR_NOT_SUPPORTED;
742 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 if (key_type_is_raw_bytes(type)) {
745 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200746
Steven Cooreman75b74362020-07-28 14:30:13 +0200747 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100748 if (data_length > SIZE_MAX / 8) {
749 return PSA_ERROR_NOT_SUPPORTED;
750 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200751
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if ((*bits) > PSA_MAX_KEY_BITS) {
755 return PSA_ERROR_NOT_SUPPORTED;
756 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 status = validate_unstructured_key_bit_size(type, *bits);
759 if (status != PSA_SUCCESS) {
760 return status;
761 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200762
Ronald Crondd04d422020-11-28 15:14:42 +0100763 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100765 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 return PSA_SUCCESS;
769 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800770#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
772 if (PSA_KEY_TYPE_IS_ECC(type)) {
773 return mbedtls_psa_ecp_import_key(attributes,
774 data, data_length,
775 key_buffer, key_buffer_size,
776 key_buffer_length,
777 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100778 }
John Durkop9814fa22020-11-04 12:28:15 -0800779#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
780 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700781#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
783 if (PSA_KEY_TYPE_IS_RSA(type)) {
784 return mbedtls_psa_rsa_import_key(attributes,
785 data, data_length,
786 key_buffer, key_buffer_size,
787 key_buffer_length,
788 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200789 }
John Durkop9814fa22020-11-04 12:28:15 -0800790#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
791 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100792 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100795}
796
Gilles Peskinef603c712019-01-19 13:40:11 +0100797/** Calculate the intersection of two algorithm usage policies.
798 *
799 * Return 0 (which allows no operation) on incompatibility.
800 */
801static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100802 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100803 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100805{
Gilles Peskine549ea862019-05-22 11:45:59 +0200806 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 if (alg1 == alg2) {
808 return alg1;
809 }
Steven Cooreman03488022021-02-18 12:07:20 +0100810 /* If the policies are from the same hash-and-sign family, check
811 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
813 PSA_ALG_IS_SIGN_HASH(alg2) &&
814 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
815 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
816 return alg2;
817 }
818 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
819 return alg1;
820 }
Steven Cooreman03488022021-02-18 12:07:20 +0100821 }
822 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100823 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100824 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
826 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
827 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
828 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
829 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100830 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100831
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100832 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
834 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
835 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
836 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100837 }
838 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
840 (alg1_len <= alg2_len)) {
841 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100842 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
844 (alg2_len <= alg1_len)) {
845 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100846 }
847 }
848 /* If the policies are from the same MAC family, check whether one
849 * of them is a minimum-MAC-length policy. Calculate the most
850 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
852 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
853 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100854 /* Validate the combination of key type and algorithm. Since the base
855 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
857 return 0;
858 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100859
Steven Cooreman31a876d2021-03-03 20:47:40 +0100860 /* Get the (exact or at-least) output lengths for both sides of the
861 * requested intersection. None of the currently supported algorithms
862 * have an output length dependent on the actual key size, so setting it
863 * to a bogus value of 0 is currently OK.
864 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100865 * Note that for at-least-this-length wildcard algorithms, the output
866 * length is set to the shortest allowed length, which allows us to
867 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
869 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100870 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100871
Steven Cooremana1d83222021-02-25 10:20:29 +0100872 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
874 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
875 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100876 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100877
878 /* If only one is an at-least-this-length policy, the intersection would
879 * be the other (fixed-length) policy as long as said fixed length is
880 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100881 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
882 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100883 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
885 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100886 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100887
Steven Cooremancd640932021-03-08 12:00:27 +0100888 /* If none of them are wildcards, check whether they define the same tag
889 * length. This is still possible here when one is default-length and
890 * the other specific-length. Ensure to always return the
891 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 if (alg1_len == alg2_len) {
893 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
894 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100895 }
896 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100898}
899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900static int psa_key_algorithm_permits(psa_key_type_t key_type,
901 psa_algorithm_t policy_alg,
902 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200903{
Gilles Peskine549ea862019-05-22 11:45:59 +0200904 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905 if (requested_alg == policy_alg) {
906 return 1;
907 }
Steven Cooreman03488022021-02-18 12:07:20 +0100908 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
909 * and requested_alg is the same hash-and-sign family with any hash,
910 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100911 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
912 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
913 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
914 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100915 }
916 /* If policy_alg is a wildcard AEAD algorithm of the same base as
917 * the requested algorithm, check the requested tag length to be
918 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 if (PSA_ALG_IS_AEAD(policy_alg) &&
920 PSA_ALG_IS_AEAD(requested_alg) &&
921 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
922 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
923 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
924 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
925 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100926 }
Steven Cooremancd640932021-03-08 12:00:27 +0100927 /* If policy_alg is a MAC algorithm of the same base as the requested
928 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 if (PSA_ALG_IS_MAC(policy_alg) &&
930 PSA_ALG_IS_MAC(requested_alg) &&
931 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
932 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100933 /* Validate the combination of key type and algorithm. Since the policy
934 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
936 return 0;
937 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100938
Steven Cooreman31a876d2021-03-03 20:47:40 +0100939 /* Get both the requested output length for the algorithm which is to be
940 * verified, and the default output length for the base algorithm.
941 * Note that none of the currently supported algorithms have an output
942 * length dependent on actual key size, so setting it to a bogus value
943 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100944 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100946 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 key_type, 0,
948 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100949
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100950 /* If the policy is default-length, only allow an algorithm with
951 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
953 return requested_output_length == default_output_length;
954 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100955
956 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100957 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100958 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
959 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
960 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100961 }
962
Steven Cooremancd640932021-03-08 12:00:27 +0100963 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
964 * check for the requested MAC length to be equal to or longer than the
965 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
967 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
968 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100969 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200970 }
Steven Cooremance48e852020-10-05 16:02:45 +0200971 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200972 * a key derivation with that key agreement should also be allowed. This
973 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
975 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
976 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
977 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200978 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100979 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100980 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981}
982
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100983/** Test whether a policy permits an algorithm.
984 *
985 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100986 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100987 * \note This function requires providing the key type for which the policy is
988 * being validated, since some algorithm policy definitions (e.g. MAC)
989 * have different properties depending on what kind of cipher it is
990 * combined with.
991 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100992 * \retval PSA_SUCCESS When \p alg is a specific algorithm
993 * allowed by the \p policy.
994 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
995 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
996 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100997 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
999 psa_key_type_t key_type,
1000 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001001{
Steven Cooremand788fab2021-02-25 11:29:17 +01001002 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 if (alg == 0) {
1004 return PSA_ERROR_INVALID_ARGUMENT;
1005 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001006
Steven Cooreman7e39f052021-02-23 14:18:32 +01001007 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 if (PSA_ALG_IS_WILDCARD(alg)) {
1009 return PSA_ERROR_INVALID_ARGUMENT;
1010 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1013 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1014 return PSA_SUCCESS;
1015 } else {
1016 return PSA_ERROR_NOT_PERMITTED;
1017 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001018}
1019
Gilles Peskinef603c712019-01-19 13:40:11 +01001020/** Restrict a key policy based on a constraint.
1021 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001022 * \note This function requires providing the key type for which the policy is
1023 * being restricted, since some algorithm policy definitions (e.g. MAC)
1024 * have different properties depending on what kind of cipher it is
1025 * combined with.
1026 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001027 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 * \param[in,out] policy The policy to restrict.
1029 * \param[in] constraint The policy constraint to apply.
1030 *
1031 * \retval #PSA_SUCCESS
1032 * \c *policy contains the intersection of the original value of
1033 * \c *policy and \c *constraint.
1034 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001035 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001036 * \c *policy is unchanged.
1037 */
1038static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001039 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001040 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001042{
1043 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1045 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001046 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1048 constraint->alg2);
1049 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1050 return PSA_ERROR_INVALID_ARGUMENT;
1051 }
1052 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1053 return PSA_ERROR_INVALID_ARGUMENT;
1054 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 policy->usage &= constraint->usage;
1056 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001059}
1060
Ronald Cron5c522922020-11-14 16:35:34 +01001061/** Get the description of a key given its identifier and policy constraints
1062 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 *
Ronald Cron5c522922020-11-14 16:35:34 +01001064 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +01001065 * nonzero, the key must allow operations with this algorithm. If \p alg is
1066 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +01001067 *
Ronald Cron5c522922020-11-14 16:35:34 +01001068 * In case of a persistent key, the function loads the description of the key
1069 * into a key slot if not already done.
1070 *
1071 * On success, the returned key slot is locked. It is the responsibility of
1072 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001073 */
Ronald Cron5c522922020-11-14 16:35:34 +01001074static psa_status_t psa_get_and_lock_key_slot_with_policy(
1075 mbedtls_svc_key_id_t key,
1076 psa_key_slot_t **p_slot,
1077 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1081 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 status = psa_get_and_lock_key_slot(key, p_slot);
1084 if (status != PSA_SUCCESS) {
1085 return status;
1086 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001087 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001088
1089 /* Enforce that usage policy for the key slot contains all the flags
1090 * required by the usage parameter. There is one exception: public
1091 * keys can always be exported, so we treat public key objects as
1092 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001094 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001098 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001099 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001100 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001101
Shaun Case0e7791f2021-12-20 21:14:10 -08001102 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 if (alg != 0) {
1104 status = psa_key_policy_permits(&slot->attr.policy,
1105 slot->attr.type,
1106 alg);
1107 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001108 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001110 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113
1114error:
1115 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001119}
Darryl Green940d72c2018-07-13 13:18:51 +01001120
Ronald Cron5c522922020-11-14 16:35:34 +01001121/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 *
1123 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001124 * available, as opposed to a key in a secure element and/or to be used
1125 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001126 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001127 * This is a temporary function that may be used instead of
1128 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1129 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001130 *
Ronald Cron5c522922020-11-14 16:35:34 +01001131 * On success, the returned key slot is locked. It is the responsibility of the
1132 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001133 */
Ronald Cron5c522922020-11-14 16:35:34 +01001134static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1135 mbedtls_svc_key_id_t key,
1136 psa_key_slot_t **p_slot,
1137 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001139{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1141 usage, alg);
1142 if (status != PSA_SUCCESS) {
1143 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001144 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1147 psa_unlock_key_slot(*p_slot);
1148 *p_slot = NULL;
1149 return PSA_ERROR_NOT_SUPPORTED;
1150 }
1151
1152 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001153}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001156{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001157 /* Data pointer will always be either a valid pointer or NULL in an
1158 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 if (slot->key.data != NULL) {
1160 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1161 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001164 slot->key.data = NULL;
1165 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001168}
1169
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001170/** Completely wipe a slot in memory, including its policy.
1171 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001172psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001173{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001175
1176 /*
1177 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001178 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001179 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1180 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001181 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001182 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001184#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001186#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001187 status = PSA_ERROR_CORRUPTION_DETECTED;
1188 }
1189
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001190 /* Multipart operations may still be using the key. This is safe
1191 * because all multipart operation objects are independent from
1192 * the key slot: if they need to access the key after the setup
1193 * phase, they have a copy of the key. Note that this means that
1194 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001195 /* At this point, key material and other type-specific content has
1196 * been wiped. Clear remaining metadata. We can call memset and not
1197 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 memset(slot, 0, sizeof(*slot));
1199 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001200}
1201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001204 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001205 psa_status_t status; /* status of the last operation */
1206 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1208 psa_se_drv_table_entry_t *driver;
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 if (mbedtls_svc_key_id_is_null(key)) {
1212 return PSA_SUCCESS;
1213 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001214
Ronald Cronf2911112020-10-29 17:51:10 +01001215 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001216 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001217 * key, this will load the key description from persistent memory if not
1218 * done yet. We cannot avoid this loading as without it we don't know if
1219 * the key is operated by an SE or not and this information is needed by
1220 * the current implementation.
1221 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 status = psa_get_and_lock_key_slot(key, &slot);
1223 if (status != PSA_SUCCESS) {
1224 return status;
1225 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001226
Ronald Cronf2911112020-10-29 17:51:10 +01001227 /*
1228 * If the key slot containing the key description is under access by the
1229 * library (apart from the present access), the key cannot be destroyed
1230 * yet. For the time being, just return in error. Eventually (to be
1231 * implemented), the key should be destroyed when all accesses have
1232 * stopped.
1233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 if (slot->lock_count > 1) {
1235 psa_unlock_key_slot(slot);
1236 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001237 }
1238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001240 /* Refuse the destruction of a read-only key (which may or may not work
1241 * if we attempt it, depending on whether the key is merely read-only
1242 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001243 * Just do the best we can, which is to wipe the copy in memory
1244 * (done in this function's cleanup code). */
1245 overall_status = PSA_ERROR_NOT_PERMITTED;
1246 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001247 }
1248
Gilles Peskine354f7672019-07-12 23:46:38 +02001249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1251 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001252 /* For a key in a secure element, we need to do three things:
1253 * remove the key file in internal storage, destroy the
1254 * key inside the secure element, and update the driver's
1255 * persistent data. Start a transaction that will encompass these
1256 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001258 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001260 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 status = psa_crypto_save_transaction();
1262 if (status != PSA_SUCCESS) {
1263 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264 /* We should still try to destroy the key in the secure
1265 * element and the key metadata in storage. This is especially
1266 * important if the error is that the storage is full.
1267 * But how to do it exactly without risking an inconsistent
1268 * state after a reset?
1269 * https://github.com/ARMmbed/mbed-crypto/issues/215
1270 */
1271 overall_status = status;
1272 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 }
1274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 status = psa_destroy_se_key(driver,
1276 psa_key_slot_get_slot_number(slot));
1277 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001278 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001279 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001280 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1282
Darryl Greend49a4992018-06-18 17:27:26 +01001283#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1285 status = psa_destroy_persistent_key(slot->attr.id);
1286 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001290 /* TODO: other slots may have a copy of the same key. We should
1291 * invalidate them.
1292 * https://github.com/ARMmbed/mbed-crypto/issues/214
1293 */
Darryl Greend49a4992018-06-18 17:27:26 +01001294 }
1295#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001296
Gilles Peskinefc762652019-07-22 19:30:34 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 if (driver != NULL) {
1299 status = psa_save_se_persistent_data(driver);
1300 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 }
1303 status = psa_crypto_stop_transaction();
1304 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001305 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001306 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001307 }
1308#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1309
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001310exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001314 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 }
1316 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317}
1318
John Durkop07cc04a2020-11-16 22:08:34 -08001319#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001320 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001321static psa_status_t psa_get_rsa_public_exponent(
1322 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001324{
1325 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 uint8_t *buffer = NULL;
1328 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1332 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001333 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 }
1335 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001336 /* It's the default value, which is reported as an empty string,
1337 * so there's nothing to do. */
1338 goto exit;
1339 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 buflen = mbedtls_mpi_size(&mpi);
1342 buffer = mbedtls_calloc(1, buflen);
1343 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001344 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1345 goto exit;
1346 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1348 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001349 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001351 attributes->domain_parameters = buffer;
1352 attributes->domain_parameters_size = buflen;
1353
1354exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001355 mbedtls_mpi_free(&mpi);
1356 if (ret != 0) {
1357 mbedtls_free(buffer);
1358 }
1359 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001360}
John Durkop07cc04a2020-11-16 22:08:34 -08001361#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001362 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001363
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001364/** Retrieve all the publicly-accessible attributes of a key.
1365 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1367 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001370 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001371 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1376 if (status != PSA_SUCCESS) {
1377 return status;
1378 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001379
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001380 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1382 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001383
1384#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001385 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1386 psa_set_key_slot_number(attributes,
1387 psa_key_slot_get_slot_number(slot));
1388 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001389#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001392#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001393 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001394 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001395 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001396 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001397 * is not yet implemented.
1398 * https://github.com/ARMmbed/mbed-crypto/issues/216
1399 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001401 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001402
Ronald Cron90857082020-11-25 14:58:33 +01001403 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 slot->attr.type,
1405 slot->key.data,
1406 slot->key.bytes,
1407 &rsa);
1408 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001409 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 status = psa_get_rsa_public_exponent(rsa,
1413 attributes);
1414 mbedtls_rsa_free(rsa);
1415 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001416 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001417 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001418#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001420 default:
1421 /* Nothing else to do. */
1422 break;
1423 }
1424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 if (status != PSA_SUCCESS) {
1426 psa_reset_key_attributes(attributes);
1427 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
1433
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1435psa_status_t psa_get_key_slot_number(
1436 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001440 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 return PSA_SUCCESS;
1442 } else {
1443 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001444 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445}
1446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1449 size_t key_buffer_size,
1450 uint8_t *data,
1451 size_t data_size,
1452 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001453{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 if (key_buffer_size > data_size) {
1455 return PSA_ERROR_BUFFER_TOO_SMALL;
1456 }
1457 memcpy(data, key_buffer, key_buffer_size);
1458 memset(data + key_buffer_size, 0,
1459 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001462}
1463
Ronald Cron7285cda2020-11-26 14:46:37 +01001464psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 const psa_key_attributes_t *attributes,
1466 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001467 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468{
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 psa_key_type_t type = attributes->core.type;
1470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001471 if (key_type_is_raw_bytes(type) ||
1472 PSA_KEY_TYPE_IS_RSA(type) ||
1473 PSA_KEY_TYPE_IS_ECC(type)) {
1474 return psa_export_key_buffer_internal(
1475 key_buffer, key_buffer_size,
1476 data, data_size, data_length);
1477 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001478 /* This shouldn't happen in the reference implementation, but
1479 it is valid for a special-purpose implementation to omit
1480 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001482 }
1483}
1484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Ryan Everette3e760c2024-01-19 16:03:50 +00001486 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 size_t data_size,
1488 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001489{
1490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1491 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1492 psa_key_slot_t *slot;
Ryan Everette3e760c2024-01-19 16:03:50 +00001493 LOCAL_OUTPUT_DECLARE(data_external, data);
Ronald Crone907e552021-01-18 13:22:38 +01001494 /* Reject a zero-length output buffer now, since this can never be a
1495 * valid key representation. This way we know that data must be a valid
1496 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 if (data_size == 0) {
1498 return PSA_ERROR_BUFFER_TOO_SMALL;
1499 }
Ronald Crone907e552021-01-18 13:22:38 +01001500
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501 /* Set the key to empty now, so that even when there are errors, we always
1502 * set data_length to a value between 0 and data_size. On error, setting
1503 * the key to empty is a good choice because an empty key representation is
1504 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001505 *data_length = 0;
1506
Ronald Cron9486f9d2020-11-26 13:36:23 +01001507 /* Export requires the EXPORT flag. There is an exception for public keys,
1508 * which don't require any flag, but
1509 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1510 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001511 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1512 PSA_KEY_USAGE_EXPORT, 0);
1513 if (status != PSA_SUCCESS) {
1514 return status;
1515 }
mohammad160306e79202018-03-28 13:17:44 +03001516
Ryan Everette3e760c2024-01-19 16:03:50 +00001517 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1518
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 psa_key_attributes_t attributes = {
1520 .core = slot->attr
1521 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 status = psa_driver_wrapper_export_key(&attributes,
1523 slot->key.data, slot->key.bytes,
1524 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525
Ryan Everett3a4153a2024-01-25 12:04:55 +00001526#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everette3e760c2024-01-19 16:03:50 +00001527exit:
Ryan Everett3a4153a2024-01-25 12:04:55 +00001528#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001530
Ryan Everette3e760c2024-01-19 16:03:50 +00001531 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001533}
1534
Ronald Cron7285cda2020-11-26 14:46:37 +01001535psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001536 const psa_key_attributes_t *attributes,
1537 const uint8_t *key_buffer,
1538 size_t key_buffer_size,
1539 uint8_t *data,
1540 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001542{
Ronald Crond18b5f82020-11-25 16:46:05 +01001543 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001545 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1546 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001547 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 return psa_export_key_buffer_internal(
1549 key_buffer, key_buffer_size,
1550 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001551 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001554#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1556 return mbedtls_psa_rsa_export_public_key(attributes,
1557 key_buffer,
1558 key_buffer_size,
1559 data,
1560 data_size,
1561 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001562#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001563 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001564 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1566 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001568#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1570 return mbedtls_psa_ecp_export_public_key(attributes,
1571 key_buffer,
1572 key_buffer_size,
1573 data,
1574 data_size,
1575 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001576#else
1577 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001579#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1580 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001581 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001582 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001583 /* This shouldn't happen in the reference implementation, but
1584 it is valid for a special-purpose implementation to omit
1585 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588}
Ronald Crond18b5f82020-11-25 16:46:05 +01001589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everett30827912024-01-19 16:05:00 +00001591 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 size_t data_size,
1593 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001594{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001596 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001597 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001598 psa_key_slot_t *slot;
Ryan Everett30827912024-01-19 16:05:00 +00001599 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001600
Ronald Crone907e552021-01-18 13:22:38 +01001601 /* Reject a zero-length output buffer now, since this can never be a
1602 * valid key representation. This way we know that data must be a valid
1603 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 if (data_size == 0) {
1605 return PSA_ERROR_BUFFER_TOO_SMALL;
1606 }
Ronald Crone907e552021-01-18 13:22:38 +01001607
Darryl Greendd8fb772018-11-07 16:00:44 +00001608 /* Set the key to empty now, so that even when there are errors, we always
1609 * set data_length to a value between 0 and data_size. On error, setting
1610 * the key to empty is a good choice because an empty key representation is
1611 * unlikely to be accepted anywhere. */
1612 *data_length = 0;
1613
1614 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1616 if (status != PSA_SUCCESS) {
1617 return status;
1618 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001619
Ryan Everett30827912024-01-19 16:05:00 +00001620 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1623 status = PSA_ERROR_INVALID_ARGUMENT;
1624 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001625 }
1626
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001627 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001628 .core = slot->attr
1629 };
Ronald Cron67227982020-11-26 15:16:05 +01001630 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001631 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001632 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001633
1634exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001635 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001636
Ryan Everett30827912024-01-19 16:05:00 +00001637 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001639}
1640
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001641MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1642 "One or more key attribute flag is listed as both external-only and dual-use")
1643MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1644 "One or more key attribute flag is listed as both internal-only and dual-use")
1645MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1646 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001647
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001648/** Validate that a key policy is internally well-formed.
1649 *
1650 * This function only rejects invalid policies. It does not validate the
1651 * consistency of the policy with respect to other attributes of the key
1652 * such as the key type.
1653 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001655{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1657 PSA_KEY_USAGE_COPY |
1658 PSA_KEY_USAGE_ENCRYPT |
1659 PSA_KEY_USAGE_DECRYPT |
1660 PSA_KEY_USAGE_SIGN_MESSAGE |
1661 PSA_KEY_USAGE_VERIFY_MESSAGE |
1662 PSA_KEY_USAGE_SIGN_HASH |
1663 PSA_KEY_USAGE_VERIFY_HASH |
1664 PSA_KEY_USAGE_DERIVE)) != 0) {
1665 return PSA_ERROR_INVALID_ARGUMENT;
1666 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001669}
1670
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001671/** Validate the internal consistency of key attributes.
1672 *
1673 * This function only rejects invalid attribute values. If does not
1674 * validate the consistency of the attributes with any key data that may
1675 * be involved in the creation of the key.
1676 *
1677 * Call this function early in the key creation process.
1678 *
1679 * \param[in] attributes Key attributes for the new key.
1680 * \param[out] p_drv On any return, the driver for the key, if any.
1681 * NULL for a transparent key.
1682 *
1683 */
1684static psa_status_t psa_validate_key_attributes(
1685 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001686 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001687{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001688 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1690 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 status = psa_validate_key_location(lifetime, p_drv);
1693 if (status != PSA_SUCCESS) {
1694 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001695 }
1696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697 status = psa_validate_key_persistence(lifetime);
1698 if (status != PSA_SUCCESS) {
1699 return status;
1700 }
1701
1702 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1703 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1704 return PSA_ERROR_INVALID_ARGUMENT;
1705 }
1706 } else {
1707 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1708 return PSA_ERROR_INVALID_ARGUMENT;
1709 }
1710 }
1711
1712 status = psa_validate_key_policy(&attributes->core.policy);
1713 if (status != PSA_SUCCESS) {
1714 return status;
1715 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001716
1717 /* Refuse to create overly large keys.
1718 * Note that this doesn't trigger on import if the attributes don't
1719 * explicitly specify a size (so psa_get_key_bits returns 0), so
1720 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001721 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1722 return PSA_ERROR_NOT_SUPPORTED;
1723 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001724
Gilles Peskine91e8c332019-08-02 19:19:39 +02001725 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1727 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1728 return PSA_ERROR_INVALID_ARGUMENT;
1729 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001731 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001732}
1733
Gilles Peskine4747d192019-04-17 15:05:45 +02001734/** Prepare a key slot to receive key material.
1735 *
1736 * This function allocates a key slot and sets its metadata.
1737 *
1738 * If this function fails, call psa_fail_key_creation().
1739 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 * This function is intended to be used as follows:
1741 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001742 * it with the specified attributes, and in case of a volatile key assign it
1743 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 * -# Populate the slot with the key material.
1745 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1746 * In case of failure at any step, stop the sequence and call
1747 * psa_fail_key_creation().
1748 *
Ronald Cron5c522922020-11-14 16:35:34 +01001749 * On success, the key slot is locked. It is the responsibility of the caller
1750 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001751 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001752 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001754 * \param[out] p_slot On success, a pointer to the prepared slot.
1755 * \param[out] p_drv On any return, the driver for the key, if any.
1756 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 *
1758 * \retval #PSA_SUCCESS
1759 * The key slot is ready to receive key material.
1760 * \return If this function fails, the key slot is an invalid state.
1761 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 */
1763static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001764 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001767 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001768{
1769 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001770 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 psa_key_slot_t *slot;
1772
Gilles Peskinedf179142019-07-15 22:02:14 +02001773 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 *p_drv = NULL;
1775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 status = psa_validate_key_attributes(attributes, p_drv);
1777 if (status != PSA_SUCCESS) {
1778 return status;
1779 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1782 if (status != PSA_SUCCESS) {
1783 return status;
1784 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 slot = *p_slot;
1786
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001787 /* We're storing the declared bit-size of the key. It's up to each
1788 * creation mechanism to verify that this information is correct.
1789 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001790 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001791 * is optional (import, copy). In case of a volatile key, assign it the
1792 * volatile key identifier associated to the slot returned to contain its
1793 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001794
1795 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001796 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001797#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1798 slot->attr.id = volatile_key_id;
1799#else
1800 slot->attr.id.key_id = volatile_key_id;
1801#endif
1802 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001803
Gilles Peskine91e8c332019-08-02 19:19:39 +02001804 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001805 * external-only flags, query `attributes`. Thanks to the check
1806 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001807 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001808 * may have set. */
1809 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001810
Gilles Peskinecbaff462019-07-12 23:46:04 +02001811#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001812 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001813 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001814 * create the key file in internal storage, create the
1815 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 * persistent data. This is done by starting a transaction that will
1817 * encompass these three actions.
1818 * For registering a volatile key, we just need to find an appropriate
1819 * slot number inside the SE. Since the key is designated volatile, creating
1820 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001821 /* The first thing to do is to find a slot number for the new key.
1822 * We save the slot number in persistent storage as part of the
1823 * transaction data. It will be needed to recover if the power
1824 * fails during the key creation process, to clean up on the secure
1825 * element side after restarting. Obtaining a slot number from the
1826 * secure element driver updates its persistent state, but we do not yet
1827 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001828 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001830 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1832 &slot_number);
1833 if (status != PSA_SUCCESS) {
1834 return status;
1835 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1838 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001839 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001840 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001841 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 status = psa_crypto_save_transaction();
1843 if (status != PSA_SUCCESS) {
1844 (void) psa_crypto_stop_transaction();
1845 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001847 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001848
1849 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001851 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001854 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001856 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001857#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001860}
Gilles Peskine4747d192019-04-17 15:05:45 +02001861
1862/** Finalize the creation of a key once its key material has been set.
1863 *
1864 * This entails writing the key to persistent storage.
1865 *
1866 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001867 * See the documentation of psa_start_key_creation() for the intended use
1868 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 *
Ronald Cron5c522922020-11-14 16:35:34 +01001870 * If the finalization succeeds, the function unlocks the key slot (it was
1871 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1872 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001873 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 * \param[in,out] slot Pointer to the slot with key material.
1875 * \param[in] driver The secure element driver for the key,
1876 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001877 * \param[out] key On success, identifier of the key. Note that the
1878 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001879 *
1880 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001881 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1883 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1884 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1885 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1886 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1887 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001888 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001889 * \return If this function fails, the key slot is an invalid state.
1890 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001892static psa_status_t psa_finish_key_creation(
1893 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001894 psa_se_drv_table_entry_t *driver,
1895 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001896{
1897 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001898 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001900
1901#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001903#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001904 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001905 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001906 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001908
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001909 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1910 sizeof(data.slot_number),
1911 "Slot number size does not match psa_se_key_data_storage_t");
1912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1914 status = psa_save_persistent_key(&slot->attr,
1915 (uint8_t *) &data,
1916 sizeof(data));
1917 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001918#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1919 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001920 /* Key material is saved in export representation in the slot, so
1921 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 status = psa_save_persistent_key(&slot->attr,
1923 slot->key.data,
1924 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001925 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001926 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001927#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1928
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001930 /* Finish the transaction for a key creation. This does not
1931 * happen when registering an existing key. Detect this case
1932 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001933 * creation of a persistent key in a secure element requires a transaction,
1934 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001935 if (driver != NULL &&
1936 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1937 status = psa_save_se_persistent_data(driver);
1938 if (status != PSA_SUCCESS) {
1939 psa_destroy_persistent_key(slot->attr.id);
1940 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001941 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943 }
1944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001947 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 status = psa_unlock_key_slot(slot);
1949 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001950 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001952 }
Ronald Cron50972942020-11-14 11:28:25 +01001953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001954 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001955}
1956
1957/** Abort the creation of a key.
1958 *
1959 * You may call this function after calling psa_start_key_creation(),
1960 * or after psa_finish_key_creation() fails. In other circumstances, this
1961 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001962 * See the documentation of psa_start_key_creation() for the intended use
1963 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001964 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001965 * \param[in,out] slot Pointer to the slot with key material.
1966 * \param[in] driver The secure element driver for the key,
1967 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001969static void psa_fail_key_creation(psa_key_slot_t *slot,
1970 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001971{
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 (void) driver;
1973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001976 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001977
1978#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001979 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001980 * element, and the failure happened later (when saving metadata
1981 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001982 * element.
1983 * https://github.com/ARMmbed/mbed-crypto/issues/217
1984 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001985
Gilles Peskined7729582019-08-05 15:55:54 +02001986 /* Abort the ongoing transaction if any (there may not be one if
1987 * the creation process failed before starting one, or if the
1988 * key creation is a registration of a key in a secure element).
1989 * Earlier functions must already have done what it takes to undo any
1990 * partial creation. All that's left is to update the transaction data
1991 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001996}
1997
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001998/** Validate optional attributes during key creation.
1999 *
2000 * Some key attributes are optional during key creation. If they are
2001 * specified in the attributes structure, check that they are consistent
2002 * with the data in the slot.
2003 *
2004 * This function should be called near the end of key creation, after
2005 * the slot in memory is fully populated but before saving persistent data.
2006 */
2007static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011 if (attributes->core.type != 0) {
2012 if (attributes->core.type != slot->attr.type) {
2013 return PSA_ERROR_INVALID_ARGUMENT;
2014 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 }
2016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002018#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2020 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002021 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002022 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002024
Ronald Cron90857082020-11-25 14:58:33 +01002025 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002026 slot->attr.type,
2027 slot->key.data,
2028 slot->key.bytes,
2029 &rsa);
2030 if (status != PSA_SUCCESS) {
2031 return status;
2032 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 mbedtls_mpi_init(&actual);
2035 mbedtls_mpi_init(&required);
2036 ret = mbedtls_rsa_export(rsa,
2037 NULL, NULL, NULL, NULL, &actual);
2038 mbedtls_rsa_free(rsa);
2039 mbedtls_free(rsa);
2040 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 }
2043 ret = mbedtls_mpi_read_binary(&required,
2044 attributes->domain_parameters,
2045 attributes->domain_parameters_size);
2046 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 }
2049 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 }
2052rsa_exit:
2053 mbedtls_mpi_free(&actual);
2054 mbedtls_mpi_free(&required);
2055 if (ret != 0) {
2056 return mbedtls_to_psa_error(ret);
2057 }
2058 } else
John Durkop9814fa22020-11-04 12:28:15 -08002059#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2060 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 }
2064 }
2065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 if (attributes->core.bits != 0) {
2067 if (attributes->core.bits != slot->attr.bits) {
2068 return PSA_ERROR_INVALID_ARGUMENT;
2069 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 }
2071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073}
2074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002075psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002076 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 size_t data_length,
2078 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002079{
2080 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002081 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002083 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002084 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085
Ronald Cron81709fc2020-11-14 12:10:32 +01002086 *key = MBEDTLS_SVC_KEY_ID_INIT;
2087
Gilles Peskine0f84d622019-09-12 19:03:13 +02002088 /* Reject zero-length symmetric keys (including raw data key objects).
2089 * This also rejects any key which might be encoded as an empty string,
2090 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 if (data_length == 0) {
2092 return PSA_ERROR_INVALID_ARGUMENT;
2093 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002094
Ryan Everett6b970252024-01-19 16:02:59 +00002095 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2098 &slot, &driver);
2099 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002102
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002103 /* In the case of a transparent key or an opaque key stored in local
2104 * storage (thus not in the case of generating a key in a secure element
2105 * or cryptoprocessor with storage), we have to allocate a buffer to
2106 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 if (slot->key.data == NULL) {
2108 status = psa_allocate_buffer_to_slot(slot, data_length);
2109 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002110 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002112 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002113
2114 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115 status = psa_driver_wrapper_import_key(attributes,
2116 data, data_length,
2117 slot->key.data,
2118 slot->key.bytes,
2119 &slot->key.bytes, &bits);
2120 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002121 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002125 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002127 status = PSA_ERROR_INVALID_ARGUMENT;
2128 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002129 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 status = psa_validate_optional_attributes(slot, attributes);
2132 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002137exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002138 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 if (status != PSA_SUCCESS) {
2140 psa_fail_key_creation(slot, driver);
2141 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002144}
2145
Gilles Peskined7729582019-08-05 15:55:54 +02002146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2147psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002149{
2150 psa_status_t status;
2151 psa_key_slot_t *slot = NULL;
2152 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002154
2155 /* Leaving attributes unspecified is not currently supported.
2156 * It could make sense to query the key type and size from the
2157 * secure element, but not all secure elements support this
2158 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2160 return PSA_ERROR_NOT_SUPPORTED;
2161 }
2162 if (psa_get_key_bits(attributes) == 0) {
2163 return PSA_ERROR_NOT_SUPPORTED;
2164 }
Gilles Peskined7729582019-08-05 15:55:54 +02002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2167 &slot, &driver);
2168 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002169 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 }
Gilles Peskined7729582019-08-05 15:55:54 +02002171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002173
2174exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 if (status != PSA_SUCCESS) {
2176 psa_fail_key_creation(slot, driver);
2177 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002178
Gilles Peskined7729582019-08-05 15:55:54 +02002179 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 psa_close_key(key);
2181 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002182}
2183#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2186 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002187{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 psa_status_t status = psa_copy_key_material_into_slot(target,
2189 source->key.data,
2190 source->key.bytes);
2191 if (status != PSA_SUCCESS) {
2192 return status;
2193 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002194
Steven Cooreman398aee52020-10-13 14:35:45 +02002195 target->attr.type = source->attr.type;
2196 target->attr.bits = source->attr.bits;
2197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199}
2200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2202 const psa_key_attributes_t *specified_attributes,
2203 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002204{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002206 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002207 psa_key_slot_t *source_slot = NULL;
2208 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002210 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002211
Ronald Cron81709fc2020-11-14 12:10:32 +01002212 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2213
Ronald Cron5c522922020-11-14 16:35:34 +01002214 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2216 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 status = psa_validate_optional_attributes(source_slot,
2221 specified_attributes);
2222 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 status = psa_restrict_key_policy(source_slot->attr.type,
2227 &actual_attributes.core.policy,
2228 &source_slot->attr.policy);
2229 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2234 &target_slot, &driver);
2235 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002238
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002239#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002241 /* Copying to a secure element is not implemented yet. */
2242 status = PSA_ERROR_NOT_SUPPORTED;
2243 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002244 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002247 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002248 /*
2249 * Copying through an opaque driver is not implemented yet, consider
2250 * a lifetime with an external location as an invalid parameter for
2251 * now.
2252 */
2253 status = PSA_ERROR_INVALID_ARGUMENT;
2254 goto exit;
2255 }
2256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 status = psa_copy_key_material(source_slot, target_slot);
2258 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002259 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002263exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 if (status != PSA_SUCCESS) {
2265 psa_fail_key_creation(target_slot, driver);
2266 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002271}
2272
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002273
2274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002275/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002276/* Message digests */
2277/****************************************************************/
2278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002281 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282 if (operation->id == 0) {
2283 return PSA_SUCCESS;
2284 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002287 operation->id = 0;
2288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290}
2291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2293 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002296
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002297 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002299 status = PSA_ERROR_BAD_STATE;
2300 goto exit;
2301 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002304 status = PSA_ERROR_INVALID_ARGUMENT;
2305 goto exit;
2306 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002307
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002308 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2309 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002313
2314exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (status != PSA_SUCCESS) {
2316 psa_hash_abort(operation);
2317 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002318
2319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322psa_status_t psa_hash_update(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002323 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002327 LOCAL_INPUT_DECLARE(input_external, input);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002330 status = PSA_ERROR_BAD_STATE;
2331 goto exit;
2332 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002333
Steven Cooremanc8288352021-03-04 14:02:19 +01002334 /* Don't require hash implementations to behave correctly on a
2335 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002336 if (input_length == 0) {
2337 return PSA_SUCCESS;
2338 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002339
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002340 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002342
2343exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 if (status != PSA_SUCCESS) {
2345 psa_hash_abort(operation);
2346 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002347
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002348 LOCAL_INPUT_FREE(input_external, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350}
2351
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002352static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002353 uint8_t *hash,
2354 size_t hash_size,
2355 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2358
Steven Cooremanfbe09282021-03-08 18:41:12 +01002359 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360 if (operation->id == 0) {
2361 return PSA_ERROR_BAD_STATE;
2362 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002364 status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002365 operation, hash, hash_size, hash_length);
2366 psa_hash_abort(operation);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002367
2368 return status;
2369}
2370
2371psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2372 uint8_t *hash_external,
2373 size_t hash_size,
2374 size_t *hash_length)
2375{
2376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2377 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2378
2379 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2380 status = psa_hash_finish_internal(operation, hash, hash_size, hash_length);
2381
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002382#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002383exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002384#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002385 LOCAL_OUTPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387}
2388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002390 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002393 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2396 LOCAL_INPUT_DECLARE(hash_external, hash);
2397
2398 status = psa_hash_finish_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 operation,
2400 actual_hash, sizeof(actual_hash),
2401 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002404 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
2409 goto exit;
2410 }
2411
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002412 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002414 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002416
2417exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2419 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002420 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002422 LOCAL_INPUT_FREE(hash_external, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424}
2425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426psa_status_t psa_hash_compute(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002427 const uint8_t *input_external, size_t input_length,
2428 uint8_t *hash_external, size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002430{
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2432 LOCAL_INPUT_DECLARE(input_external, input);
2433 LOCAL_OUTPUT_DECLARE(hash_external, hash);
2434
Steven Cooremanfbe09282021-03-08 18:41:12 +01002435 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 if (!PSA_ALG_IS_HASH(alg)) {
2437 return PSA_ERROR_INVALID_ARGUMENT;
2438 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002439
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002440 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2441 LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash);
2442 status = psa_driver_wrapper_hash_compute(alg, input, input_length,
Thomas Daubneycbf09212024-01-25 17:14:29 +00002443 hash, hash_size, hash_length);
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002444
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002445#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002446exit:
Thomas Daubney5e6b84a2024-01-25 17:10:42 +00002447#endif
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002448 LOCAL_INPUT_FREE(input_external, input);
2449 LOCAL_OUTPUT_FREE(hash_external, hash);
2450 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002451}
2452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453psa_status_t psa_hash_compare(psa_algorithm_t alg,
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002454 const uint8_t *input_external, size_t input_length,
2455 const uint8_t *hash_external, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002456{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002457 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002458 size_t actual_hash_length;
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2460
2461 LOCAL_INPUT_DECLARE(input_external, input);
2462 LOCAL_INPUT_DECLARE(hash_external, hash);
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 if (!PSA_ALG_IS_HASH(alg)) {
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002465 status = PSA_ERROR_INVALID_ARGUMENT;
2466 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002468
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002469 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2470 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 alg, input, input_length,
2472 actual_hash, sizeof(actual_hash),
2473 &actual_hash_length);
2474 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002475 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002476 }
2477 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002478 status = PSA_ERROR_INVALID_SIGNATURE;
2479 goto exit;
2480 }
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002481
2482 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002484 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002486
2487exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
Thomas Daubney62cb36a2024-01-12 16:50:26 +00002489
2490 LOCAL_INPUT_FREE(input_external, input);
2491 LOCAL_INPUT_FREE(hash_external, hash);
2492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002494}
2495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2497 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002498{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002499 if (source_operation->id == 0 ||
2500 target_operation->id != 0) {
2501 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002502 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2505 target_operation);
2506 if (status != PSA_SUCCESS) {
2507 psa_hash_abort(target_operation);
2508 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002510 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002511}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002512
2513
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515/* MAC */
2516/****************************************************************/
2517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002519{
Steven Cooreman07897832021-03-19 18:28:56 +01002520 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (operation->id == 0) {
2522 return PSA_SUCCESS;
2523 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002526 operation->mac_size = 0;
2527 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002528 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002531}
2532
Ronald Cron1c650a12021-06-17 16:33:22 +02002533static psa_status_t psa_mac_finalize_alg_and_key_validation(
2534 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002535 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 psa_key_type_t key_type = psa_get_key_type(attributes);
2540 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 if (!PSA_ALG_IS_MAC(alg)) {
2543 return PSA_ERROR_INVALID_ARGUMENT;
2544 }
Steven Cooreman07897832021-03-19 18:28:56 +01002545
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002546 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 status = psa_mac_key_can_do(alg, key_type);
2548 if (status != PSA_SUCCESS) {
2549 return status;
2550 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002551
Steven Cooremana6474de2021-05-10 11:13:41 +02002552 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002553 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002556 /* A very short MAC is too short for security since it can be
2557 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2558 * so we make this our minimum, even though 32 bits is still
2559 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002561 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2564 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002565 /* It's impossible to "truncate" to a larger length than the full length
2566 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002568 }
2569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002571 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2572 * that is disabled in the compile-time configuration. The result can
2573 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2574 * configuration into account. In this case, force a return of
2575 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2576 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2577 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2578 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2579 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002581 }
2582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002584}
2585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2587 mbedtls_svc_key_id_t key,
2588 psa_algorithm_t alg,
2589 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002590{
2591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2592 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002593 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002594 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002595
2596 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002598 status = PSA_ERROR_BAD_STATE;
2599 goto exit;
2600 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002601
2602 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 key,
2604 &slot,
2605 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2606 alg);
2607 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002608 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002610
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002611 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002612 .core = slot->attr
2613 };
2614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2616 &operation->mac_size);
2617 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002618 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002620
2621 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002622 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 if (is_sign) {
2624 status = psa_driver_wrapper_mac_sign_setup(operation,
2625 &attributes,
2626 slot->key.data,
2627 slot->key.bytes,
2628 alg);
2629 } else {
2630 status = psa_driver_wrapper_mac_verify_setup(operation,
2631 &attributes,
2632 slot->key.data,
2633 slot->key.bytes,
2634 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002636
Gilles Peskinefbfac682018-07-08 20:51:54 +02002637exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002638 if (status != PSA_SUCCESS) {
2639 psa_mac_abort(operation);
2640 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645}
2646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2648 mbedtls_svc_key_id_t key,
2649 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002652}
2653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2655 mbedtls_svc_key_id_t key,
2656 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002657{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002658 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002659}
2660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661psa_status_t psa_mac_update(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002662 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2666 LOCAL_INPUT_DECLARE(input_external, input);
2667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (operation->id == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002669 status = PSA_ERROR_BAD_STATE;
2670 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002671 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672
Steven Cooreman11743f92021-03-19 18:38:46 +01002673 /* Don't require hash implementations to behave correctly on a
2674 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 if (input_length == 0) {
Thomas Daubney324f7de2024-01-18 13:18:58 +00002676 status = PSA_SUCCESS;
2677 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002679
Thomas Daubney324f7de2024-01-18 13:18:58 +00002680 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2681 status = psa_driver_wrapper_mac_update(operation, input, input_length);
2682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002683 if (status != PSA_SUCCESS) {
2684 psa_mac_abort(operation);
2685 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002686
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002687#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002688exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002689#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002690 LOCAL_INPUT_FREE(input_external, input);
2691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693}
2694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002696 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 size_t mac_size,
2698 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002699{
Steven Cooreman87885df2021-03-19 19:04:39 +01002700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2701 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002702 LOCAL_OUTPUT_DECLARE(mac_external, mac);
Thomas Daubney301491d2024-02-01 14:32:59 +00002703 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002706 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002707 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002708 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002711 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002712 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002713 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002714
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002715 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2716 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002718 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002719 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002720 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002723 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002724 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002725 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727 status = psa_driver_wrapper_mac_sign_finish(operation,
2728 mac, operation->mac_size,
2729 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002730
Dave Rodgman478ab542021-06-25 09:09:02 +01002731exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002732 /* In case of success, set the potential excess room in the output buffer
2733 * to an invalid value, to avoid potentially leaking a longer MAC.
2734 * In case of error, set the output length and content to a safe default,
2735 * such that in case the caller misses an error check, the output would be
2736 * an unachievable MAC.
2737 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002739 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002740 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002741 }
mohammad16036df908f2018-04-02 08:34:15 -07002742
Thomas Daubney480347d2024-02-01 19:00:26 +00002743 if ((mac != NULL) && (mac_size > operation->mac_size)) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 memset(&mac[operation->mac_size], '!',
2745 mac_size - operation->mac_size);
2746 }
Ronald Cron882eb782021-06-18 13:05:48 +02002747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002749 LOCAL_OUTPUT_FREE(mac_external, mac);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002751 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002752}
2753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002754psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002755 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757{
Steven Cooreman87885df2021-03-19 19:04:39 +01002758 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2759 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002760 LOCAL_INPUT_DECLARE(mac_external, mac);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002762 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002763 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002764 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002765 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002768 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002769 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002770 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002773 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002774 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002775 }
2776
Thomas Daubney324f7de2024-01-18 13:18:58 +00002777 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 status = psa_driver_wrapper_mac_verify_finish(operation,
2779 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002780
Dave Rodgman478ab542021-06-25 09:09:02 +01002781exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 abort_status = psa_mac_abort(operation);
Thomas Daubney324f7de2024-01-18 13:18:58 +00002783 LOCAL_INPUT_FREE(mac_external, mac);
mohammad16036df908f2018-04-02 08:34:15 -07002784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786}
2787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2789 psa_algorithm_t alg,
2790 const uint8_t *input,
2791 size_t input_length,
2792 uint8_t *mac,
2793 size_t mac_size,
2794 size_t *mac_length,
2795 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002796{
2797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002798 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002799 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002800 psa_key_slot_t *slot;
2801 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802
Ronald Crondbb86462021-06-17 17:17:20 +02002803 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 key,
2805 &slot,
2806 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2807 alg);
2808 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002809 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002810 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002811
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002812 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002813 .core = slot->attr
2814 };
2815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2817 &operation_mac_size);
2818 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002819 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002823 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002824 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002825 }
2826
2827 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002828 &attributes,
2829 slot->key.data, slot->key.bytes,
2830 alg,
2831 input, input_length,
2832 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002833
2834exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002835 /* In case of success, set the potential excess room in the output buffer
2836 * to an invalid value, to avoid potentially leaking a longer MAC.
2837 * In case of error, set the output length and content to a safe default,
2838 * such that in case the caller misses an error check, the output would be
2839 * an unachievable MAC.
2840 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002842 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002843 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002844 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 if (mac_size > operation_mac_size) {
2846 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2847 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002852}
2853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002854psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002855 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002856 const uint8_t *input_external,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002857 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002858 uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002859 size_t mac_size,
2860 size_t *mac_length)
2861{
Thomas Daubney324f7de2024-01-18 13:18:58 +00002862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2863 LOCAL_INPUT_DECLARE(input_external, input);
2864 LOCAL_OUTPUT_DECLARE(mac_external, mac);
2865
2866 LOCAL_INPUT_ALLOC(input_external, input_length, input);
2867 LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac);
2868 status = psa_mac_compute_internal(key, alg,
Thomas Daubneyf298f652024-01-30 12:15:36 +00002869 input, input_length,
2870 mac, mac_size, mac_length, 1);
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002871
2872#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney324f7de2024-01-18 13:18:58 +00002873exit:
Thomas Daubney2bb3a1f2024-01-30 12:12:56 +00002874#endif
Thomas Daubney324f7de2024-01-18 13:18:58 +00002875 LOCAL_INPUT_FREE(input_external, input);
2876 LOCAL_OUTPUT_FREE(mac_external, mac);
2877
2878 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879}
2880
2881psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2882 psa_algorithm_t alg,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002883 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 size_t input_length,
Thomas Daubney324f7de2024-01-18 13:18:58 +00002885 const uint8_t *mac_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002887{
2888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002889 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2890 size_t actual_mac_length;
Thomas Daubney324f7de2024-01-18 13:18:58 +00002891 LOCAL_INPUT_DECLARE(input_external, input);
2892 LOCAL_INPUT_DECLARE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002893
Thomas Daubney324f7de2024-01-18 13:18:58 +00002894 LOCAL_INPUT_ALLOC(input_external, input_length, input);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 status = psa_mac_compute_internal(key, alg,
2896 input, input_length,
2897 actual_mac, sizeof(actual_mac),
2898 &actual_mac_length, 0);
2899 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002900 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002904 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002905 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002906 }
Thomas Daubney324f7de2024-01-18 13:18:58 +00002907
2908 LOCAL_INPUT_ALLOC(mac_external, mac_length, mac);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002910 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002911 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002912 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002913
2914exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Thomas Daubney324f7de2024-01-18 13:18:58 +00002916 LOCAL_INPUT_FREE(input_external, input);
2917 LOCAL_INPUT_FREE(mac_external, mac);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002920}
Gilles Peskine20035e32018-02-03 22:44:14 +01002921
Gilles Peskine20035e32018-02-03 22:44:14 +01002922/****************************************************************/
2923/* Asymmetric cryptography */
2924/****************************************************************/
2925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2927 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002928{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002929 if (input_is_message) {
2930 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2931 return PSA_ERROR_INVALID_ARGUMENT;
2932 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2935 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2936 return PSA_ERROR_INVALID_ARGUMENT;
2937 }
2938 }
2939 } else {
2940 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2941 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002942 }
2943 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002945 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002946}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2949 int input_is_message,
2950 psa_algorithm_t alg,
2951 const uint8_t *input,
2952 size_t input_length,
2953 uint8_t *signature,
2954 size_t signature_size,
2955 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002956{
2957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002959 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002960 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002961
2962 *signature_length = 0;
2963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 status = psa_sign_verify_check_alg(input_is_message, alg);
2965 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002966 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002968
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002969 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002970 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002971 * buffer can in principle be empty since it doesn't actually have
2972 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002973 if (signature_size == 0) {
2974 return PSA_ERROR_BUFFER_TOO_SMALL;
2975 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002976
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002977 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 key, &slot,
2979 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2980 PSA_KEY_USAGE_SIGN_HASH,
2981 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002984 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002985 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002988 status = PSA_ERROR_INVALID_ARGUMENT;
2989 goto exit;
2990 }
2991
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002992 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002993 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002994 };
2995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002997 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002998 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002999 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 signature, signature_size, signature_length);
3001 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003002
3003 status = psa_driver_wrapper_sign_hash(
3004 &attributes, slot->key.data, slot->key.bytes,
3005 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003006 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003007 }
3008
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003009
3010exit:
3011 /* Fill the unused part of the output buffer (the whole buffer on error,
3012 * the trailing part on success) with something that isn't a valid signature
3013 * (barring an attack on the signature and deliberately-crafted input),
3014 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 if (status == PSA_SUCCESS) {
3016 memset(signature + *signature_length, '!',
3017 signature_size - *signature_length);
3018 } else {
3019 memset(signature, '!', signature_size);
3020 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003021 /* If signature_size is 0 then we have nothing to do. We must not call
3022 * memset because signature may be NULL in this case. */
3023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003024 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003027}
3028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3030 int input_is_message,
3031 psa_algorithm_t alg,
3032 const uint8_t *input,
3033 size_t input_length,
3034 const uint8_t *signature,
3035 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003036{
3037 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3038 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3039 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 status = psa_sign_verify_check_alg(input_is_message, alg);
3042 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02003043 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003045
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003046 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003047 key, &slot,
3048 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3049 PSA_KEY_USAGE_VERIFY_HASH,
3050 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 if (status != PSA_SUCCESS) {
3053 return status;
3054 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003055
3056 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003057 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003058 };
3059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003061 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003062 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003063 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064 signature, signature_length);
3065 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003066 status = psa_driver_wrapper_verify_hash(
3067 &attributes, slot->key.data, slot->key.bytes,
3068 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003070 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003075
3076}
3077
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003078psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003079 const psa_key_attributes_t *attributes,
3080 const uint8_t *key_buffer,
3081 size_t key_buffer_size,
3082 psa_algorithm_t alg,
3083 const uint8_t *input,
3084 size_t input_length,
3085 uint8_t *signature,
3086 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003088{
3089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003091 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003092 size_t hash_length;
3093 uint8_t hash[PSA_HASH_MAX_SIZE];
3094
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003095 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 PSA_ALG_SIGN_GET_HASH(alg),
3097 input, input_length,
3098 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003101 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003103
3104 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 attributes, key_buffer, key_buffer_size,
3106 alg, hash, hash_length,
3107 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003108 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003111}
3112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003113psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3114 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003115 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003117 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 size_t signature_size,
3119 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003120{
Thomas Daubney290aac42024-01-18 17:23:02 +00003121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3122 LOCAL_INPUT_DECLARE(input_external, input);
3123 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3124
3125 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3126 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3127 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3128 signature_size, signature_length);
3129
Thomas Daubneyf446b892024-01-30 13:36:01 +00003130#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003131exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003132#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003133 LOCAL_INPUT_FREE(input_external, input);
3134 LOCAL_OUTPUT_FREE(signature_external, signature);
3135 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003136}
3137
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003138psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003139 const psa_key_attributes_t *attributes,
3140 const uint8_t *key_buffer,
3141 size_t key_buffer_size,
3142 psa_algorithm_t alg,
3143 const uint8_t *input,
3144 size_t input_length,
3145 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003147{
3148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003151 size_t hash_length;
3152 uint8_t hash[PSA_HASH_MAX_SIZE];
3153
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003154 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 PSA_ALG_SIGN_GET_HASH(alg),
3156 input, input_length,
3157 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003159 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003160 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003162
3163 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 attributes, key_buffer, key_buffer_size,
3165 alg, hash, hash_length,
3166 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003167 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003169 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003170}
3171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3173 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003174 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003175 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003176 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003178{
Thomas Daubney290aac42024-01-18 17:23:02 +00003179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3180 LOCAL_INPUT_DECLARE(input_external, input);
3181 LOCAL_INPUT_DECLARE(signature_external, signature);
3182
3183 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3184 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3185 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3186 signature_length);
3187
Thomas Daubneyf446b892024-01-30 13:36:01 +00003188#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003189exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003190#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003191 LOCAL_INPUT_FREE(input_external, input);
3192 LOCAL_INPUT_FREE(signature_external, signature);
3193
3194 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003195}
3196
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003197psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003198 const psa_key_attributes_t *attributes,
3199 const uint8_t *key_buffer, size_t key_buffer_size,
3200 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003202{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3204 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3205 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003206#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3208 return mbedtls_psa_rsa_sign_hash(
3209 attributes,
3210 key_buffer, key_buffer_size,
3211 alg, hash, hash_length,
3212 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003213#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3214 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 } else {
3216 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003217 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3219 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003220#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003221 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3222 return mbedtls_psa_ecdsa_sign_hash(
3223 attributes,
3224 key_buffer, key_buffer_size,
3225 alg, hash, hash_length,
3226 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003227#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3228 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003229 } else {
3230 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003231 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003232 }
Ronald Cron4501c982021-03-19 20:09:32 +01003233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 (void) key_buffer;
3235 (void) key_buffer_size;
3236 (void) hash;
3237 (void) hash_length;
3238 (void) signature;
3239 (void) signature_size;
3240 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003242 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003243}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3246 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003247 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003249 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250 size_t signature_size,
3251 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003252{
Thomas Daubney290aac42024-01-18 17:23:02 +00003253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3254 LOCAL_INPUT_DECLARE(hash_external, hash);
3255 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3256
3257 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3258 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3259 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3260 signature_size, signature_length);
3261
Thomas Daubneyf446b892024-01-30 13:36:01 +00003262#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003263exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003264#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003265 LOCAL_INPUT_FREE(hash_external, hash);
3266 LOCAL_OUTPUT_FREE(signature_external, signature);
3267
3268 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003269}
3270
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003271psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003272 const psa_key_attributes_t *attributes,
3273 const uint8_t *key_buffer, size_t key_buffer_size,
3274 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003276{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3278 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3279 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3282 return mbedtls_psa_rsa_verify_hash(
3283 attributes,
3284 key_buffer, key_buffer_size,
3285 alg, hash, hash_length,
3286 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003287#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3288 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003289 } else {
3290 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003291 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3293 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003294#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3296 return mbedtls_psa_ecdsa_verify_hash(
3297 attributes,
3298 key_buffer, key_buffer_size,
3299 alg, hash, hash_length,
3300 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003301#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3302 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 } else {
3304 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003305 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003306 }
Ronald Cron4501c982021-03-19 20:09:32 +01003307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 (void) key_buffer;
3309 (void) key_buffer_size;
3310 (void) hash;
3311 (void) hash_length;
3312 (void) signature;
3313 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003316}
3317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3319 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003320 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003322 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003323 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003324{
Thomas Daubney290aac42024-01-18 17:23:02 +00003325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3326 LOCAL_INPUT_DECLARE(hash_external, hash);
3327 LOCAL_INPUT_DECLARE(signature_external, signature);
3328
3329 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3330 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3331 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3332 signature_length);
3333
Thomas Daubneyf446b892024-01-30 13:36:01 +00003334#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003335exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003336#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003337 LOCAL_INPUT_FREE(hash_external, hash);
3338 LOCAL_INPUT_FREE(signature_external, signature);
3339
3340 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003341}
3342
John Durkop0e005192020-10-31 22:06:54 -07003343#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3345 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003346{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3348 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3349 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3350 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003351}
John Durkop0e005192020-10-31 22:06:54 -07003352#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3355 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003356 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003357 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003358 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003360 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 size_t output_size,
3362 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003363{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003366 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003367 LOCAL_INPUT_DECLARE(input_external, input);
3368 LOCAL_INPUT_DECLARE(salt_external, salt);
3369 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003370
Darryl Green5cc689a2018-07-24 15:34:10 +01003371 (void) input;
3372 (void) input_length;
3373 (void) salt;
3374 (void) output;
3375 (void) output_size;
3376
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003377 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3380 return PSA_ERROR_INVALID_ARGUMENT;
3381 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003382
Ronald Cron5c522922020-11-14 16:35:34 +01003383 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3385 if (status != PSA_SUCCESS) {
3386 return status;
3387 }
3388 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3389 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003390 status = PSA_ERROR_INVALID_ARGUMENT;
3391 goto exit;
3392 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003394 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003395#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003397 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3399 slot->key.data,
3400 slot->key.bytes,
3401 &rsa);
3402 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003403 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003407 status = PSA_ERROR_BUFFER_TOO_SMALL;
3408 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003409 }
Ronald Cron7207d572021-09-08 14:28:35 +02003410#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3411 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003412 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3413 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3414 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003416#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003417 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003418 mbedtls_rsa_pkcs1_encrypt(rsa,
3419 mbedtls_psa_get_random,
3420 MBEDTLS_PSA_RANDOM_STATE,
3421 MBEDTLS_RSA_PUBLIC,
3422 input_length,
3423 input,
3424 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003425#else
3426 status = PSA_ERROR_NOT_SUPPORTED;
3427#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 } else
3429 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003430#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003432 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3434 mbedtls_psa_get_random,
3435 MBEDTLS_PSA_RANDOM_STATE,
3436 MBEDTLS_RSA_PUBLIC,
3437 salt, salt_length,
3438 input_length,
3439 input,
3440 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003441#else
3442 status = PSA_ERROR_NOT_SUPPORTED;
3443#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003444 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003445 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003446 }
Ronald Cron7207d572021-09-08 14:28:35 +02003447#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003449rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 if (status == PSA_SUCCESS) {
3451 *output_length = mbedtls_rsa_get_len(rsa);
3452 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 mbedtls_rsa_free(rsa);
3455 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003456#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003457 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003459 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003460 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003461
3462exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003464
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003465 LOCAL_INPUT_FREE(input_external, input);
3466 LOCAL_INPUT_FREE(salt_external, salt);
3467 LOCAL_OUTPUT_FREE(output_external, output);
3468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003470}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3473 psa_algorithm_t alg,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003474 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003475 size_t input_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003476 const uint8_t *salt_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477 size_t salt_length,
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003478 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 size_t output_size,
3480 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003481{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003483 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003484 psa_key_slot_t *slot;
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003485 LOCAL_INPUT_DECLARE(input_external, input);
3486 LOCAL_INPUT_DECLARE(salt_external, salt);
3487 LOCAL_OUTPUT_DECLARE(output_external, output);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003488
Darryl Green5cc689a2018-07-24 15:34:10 +01003489 (void) input;
3490 (void) input_length;
3491 (void) salt;
3492 (void) output;
3493 (void) output_size;
3494
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003495 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3498 return PSA_ERROR_INVALID_ARGUMENT;
3499 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003500
Ronald Cron5c522922020-11-14 16:35:34 +01003501 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3503 if (status != PSA_SUCCESS) {
3504 return status;
3505 }
3506 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003507 status = PSA_ERROR_INVALID_ARGUMENT;
3508 goto exit;
3509 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003512#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003513 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003514 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3516 slot->key.data,
3517 slot->key.bytes,
3518 &rsa);
3519 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003520 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003524 status = PSA_ERROR_INVALID_ARGUMENT;
3525 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003526 }
Ronald Cron7207d572021-09-08 14:28:35 +02003527#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3528 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003529 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3530 LOCAL_INPUT_ALLOC(salt_external, salt_length, salt);
3531 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003533#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003534 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 mbedtls_rsa_pkcs1_decrypt(rsa,
3536 mbedtls_psa_get_random,
3537 MBEDTLS_PSA_RANDOM_STATE,
3538 MBEDTLS_RSA_PRIVATE,
3539 output_length,
3540 input,
3541 output,
3542 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003543#else
3544 status = PSA_ERROR_NOT_SUPPORTED;
3545#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 } else
3547 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003548#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003550 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3552 mbedtls_psa_get_random,
3553 MBEDTLS_PSA_RANDOM_STATE,
3554 MBEDTLS_RSA_PRIVATE,
3555 salt, salt_length,
3556 output_length,
3557 input,
3558 output,
3559 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003560#else
3561 status = PSA_ERROR_NOT_SUPPORTED;
3562#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003564 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003566
Ronald Cron7207d572021-09-08 14:28:35 +02003567#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003569rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 mbedtls_rsa_free(rsa);
3571 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003572#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3573 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003575 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003577
3578exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003580
Thomas Daubney1a6137b2024-01-19 10:26:26 +00003581 LOCAL_INPUT_FREE(input_external, input);
3582 LOCAL_INPUT_FREE(salt_external, salt);
3583 LOCAL_OUTPUT_FREE(output_external, output);
3584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003586}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003587
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003588
3589
mohammad1603503973b2018-03-12 15:59:30 +02003590/****************************************************************/
3591/* Symmetric cryptography */
3592/****************************************************************/
3593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3595 mbedtls_svc_key_id_t key,
3596 psa_algorithm_t alg,
3597 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003598{
3599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3600 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003601 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003602 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3604 PSA_KEY_USAGE_ENCRYPT :
3605 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003606
3607 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003609 status = PSA_ERROR_BAD_STATE;
3610 goto exit;
3611 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003614 status = PSA_ERROR_INVALID_ARGUMENT;
3615 goto exit;
3616 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3619 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003620 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003622
Ronald Cron49fafa92021-03-10 08:34:23 +01003623 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003624 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003625 * so we only set it (in the driver wrapper) after resources have been
3626 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003627 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003628 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003629 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003631 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003633 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 }
3635 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003636
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003637 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003639 };
3640
Ronald Cronab99ac22020-10-01 16:38:28 +02003641 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 if (cipher_operation == MBEDTLS_ENCRYPT) {
3643 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3644 &attributes,
3645 slot->key.data,
3646 slot->key.bytes,
3647 alg);
3648 } else {
3649 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3650 &attributes,
3651 slot->key.data,
3652 slot->key.bytes,
3653 alg);
3654 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003655
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003656exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 if (status != PSA_SUCCESS) {
3658 psa_cipher_abort(operation);
3659 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003664}
3665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3667 mbedtls_svc_key_id_t key,
3668 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003669{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003671}
3672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3674 mbedtls_svc_key_id_t key,
3675 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003676{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003677 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003678}
3679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3681 uint8_t *iv,
3682 size_t iv_size,
3683 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003684{
Ronald Cron6d051732020-10-01 14:10:20 +02003685 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003686 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003687 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003690 status = PSA_ERROR_BAD_STATE;
3691 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003692 }
3693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003695 status = PSA_ERROR_BAD_STATE;
3696 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003697 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003698
Ronald Cronc423acb2021-07-05 12:31:44 +02003699 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003700 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003701 status = PSA_ERROR_BUFFER_TOO_SMALL;
3702 goto exit;
3703 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003706 status = PSA_ERROR_GENERIC_ERROR;
3707 goto exit;
3708 }
3709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 status = psa_generate_random(local_iv, default_iv_length);
3711 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003712 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713 }
Ronald Cron5618a392021-03-26 09:52:26 +01003714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003715 status = psa_driver_wrapper_cipher_set_iv(operation,
3716 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003717
3718exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 if (status == PSA_SUCCESS) {
3720 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003721 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003722 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003724 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003726 }
Ronald Cron6d051732020-10-01 14:10:20 +02003727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003729}
3730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3732 const uint8_t *iv,
3733 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003734{
Ronald Cron6d051732020-10-01 14:10:20 +02003735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003738 status = PSA_ERROR_BAD_STATE;
3739 goto exit;
3740 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003741
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003742 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003743 status = PSA_ERROR_BAD_STATE;
3744 goto exit;
3745 }
Ronald Crona0d68172021-03-26 10:15:08 +01003746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003748 status = PSA_ERROR_INVALID_ARGUMENT;
3749 goto exit;
3750 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 status = psa_driver_wrapper_cipher_set_iv(operation,
3753 iv,
3754 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003755
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003756exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003758 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 } else {
3760 psa_cipher_abort(operation);
3761 }
3762 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003763}
3764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003766 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003768 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 size_t output_size,
3770 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003771{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003772 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003773
Gabor Mezei13a15c22024-01-24 16:56:00 +01003774 LOCAL_INPUT_DECLARE(input_external, input);
3775 LOCAL_OUTPUT_DECLARE(output_external, output);
3776
3777 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3778 LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
3779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003781 status = PSA_ERROR_BAD_STATE;
3782 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003783 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003786 status = PSA_ERROR_BAD_STATE;
3787 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003788 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 status = psa_driver_wrapper_cipher_update(operation,
3791 input,
3792 input_length,
3793 output,
3794 output_size,
3795 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003796
3797exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 if (status != PSA_SUCCESS) {
3799 psa_cipher_abort(operation);
3800 }
Ronald Cron6d051732020-10-01 14:10:20 +02003801
Gabor Mezei13a15c22024-01-24 16:56:00 +01003802 LOCAL_INPUT_FREE(input_external, input);
3803 LOCAL_OUTPUT_FREE(output_external, output);
3804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003806}
3807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003809 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 size_t output_size,
3811 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003812{
David Saadab4ecc272019-02-14 13:48:10 +02003813 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003814
Gabor Mezei13a15c22024-01-24 16:56:00 +01003815 LOCAL_OUTPUT_DECLARE(output_external, output);
3816
3817 LOCAL_OUTPUT_ALLOC_WITH_COPY(output_external, output_size, output);
3818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003820 status = PSA_ERROR_BAD_STATE;
3821 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003822 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003824 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003825 status = PSA_ERROR_BAD_STATE;
3826 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003827 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 status = psa_driver_wrapper_cipher_finish(operation,
3830 output,
3831 output_size,
3832 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003833
3834exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835 if (status == PSA_SUCCESS) {
Gabor Mezei13a15c22024-01-24 16:56:00 +01003836 status = psa_cipher_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003837 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003838 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 (void) psa_cipher_abort(operation);
Steven Cooremanef8575e2020-09-11 11:44:50 +02003840 }
Gabor Mezei13a15c22024-01-24 16:56:00 +01003841
3842 LOCAL_OUTPUT_FREE(output_external, output);
3843
3844 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003845}
3846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003847psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003848{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003850 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003851 * in use. It's ok to call abort on such an object, and there's
3852 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003853 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003854 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003857
Ronald Cron49fafa92021-03-10 08:34:23 +01003858 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003859 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003860 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003863}
3864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003865psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3866 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003867 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003869 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 size_t output_size,
3871 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003872{
3873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003874 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003875 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003876 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003877 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3878 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003879
David Horstmannb80e35a2023-12-14 18:12:47 +00003880 LOCAL_INPUT_DECLARE(input_external, input);
3881 LOCAL_OUTPUT_DECLARE(output_external, output);
3882
David Horstmann926193a2023-12-13 15:55:25 +00003883 LOCAL_INPUT_ALLOC(input_external, input_length, input);
David Horstmann926193a2023-12-13 15:55:25 +00003884 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003887 status = PSA_ERROR_INVALID_ARGUMENT;
3888 goto exit;
3889 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3892 PSA_KEY_USAGE_ENCRYPT,
3893 alg);
3894 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003895 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003897
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003898 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003900 };
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3903 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003904 status = PSA_ERROR_GENERIC_ERROR;
3905 goto exit;
3906 }
3907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003908 if (default_iv_length > 0) {
3909 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003910 status = PSA_ERROR_BUFFER_TOO_SMALL;
3911 goto exit;
3912 }
3913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 status = psa_generate_random(local_iv, default_iv_length);
3915 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003916 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003918 }
3919
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003920 status = psa_driver_wrapper_cipher_encrypt(
3921 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003922 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 mbedtls_buffer_offset(output, default_iv_length),
3924 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003925
3926exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 unlock_status = psa_unlock_key_slot(slot);
3928 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003929 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003930 }
Ronald Cron16377072021-07-08 19:00:07 +02003931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 if (status == PSA_SUCCESS) {
3933 if (default_iv_length > 0) {
3934 memcpy(output, local_iv, default_iv_length);
3935 }
3936 *output_length += default_iv_length;
3937 } else {
3938 *output_length = 0;
3939 }
3940
David Horstmannb80e35a2023-12-14 18:12:47 +00003941 LOCAL_INPUT_FREE(input_external, input);
3942 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003945}
3946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003947psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3948 psa_algorithm_t alg,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003949 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 size_t input_length,
Gabor Mezei13a15c22024-01-24 16:56:00 +01003951 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 size_t output_size,
3953 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003954{
3955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003956 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003957 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003958 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003959
Gabor Mezei13a15c22024-01-24 16:56:00 +01003960 LOCAL_INPUT_DECLARE(input_external, input);
3961 LOCAL_OUTPUT_DECLARE(output_external, output);
3962
3963 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3964 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003967 status = PSA_ERROR_INVALID_ARGUMENT;
3968 goto exit;
3969 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3972 PSA_KEY_USAGE_DECRYPT,
3973 alg);
3974 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003975 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003977
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003978 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003980 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto exit;
3985 }
3986
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003987 status = psa_driver_wrapper_cipher_decrypt(
3988 &attributes, slot->key.data, slot->key.bytes,
3989 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003991
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003992exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003993 unlock_status = psa_unlock_key_slot(slot);
3994 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003995 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003996 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003998 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003999 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000 }
Ronald Cron16377072021-07-08 19:00:07 +02004001
Gabor Mezei13a15c22024-01-24 16:56:00 +01004002 LOCAL_INPUT_FREE(input_external, input);
4003 LOCAL_OUTPUT_FREE(output_external, output);
4004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004005 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01004006}
4007
4008
mohammad16035955c982018-04-26 00:53:03 +03004009/****************************************************************/
4010/* AEAD */
4011/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004013psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4014 psa_algorithm_t alg,
David Horstmann21c1a942023-11-28 19:25:00 +00004015 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004016 size_t nonce_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004017 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004018 size_t additional_data_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004019 const uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 size_t plaintext_length,
David Horstmann21c1a942023-11-28 19:25:00 +00004021 uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004022 size_t ciphertext_size,
4023 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004024{
Ronald Cron215633c2021-03-16 17:15:37 +01004025 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4026 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004027
David Horstmann21c1a942023-11-28 19:25:00 +00004028 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4029 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4030 LOCAL_INPUT_DECLARE(plaintext_external, plaintext);
4031 LOCAL_OUTPUT_DECLARE(ciphertext_external, ciphertext);
4032
mohammad1603f08a5502018-06-03 15:05:47 +03004033 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004035 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4036 return PSA_ERROR_NOT_SUPPORTED;
4037 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004038
Ronald Cron9a986162021-03-26 12:40:07 +01004039 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4041 if (status != PSA_SUCCESS) {
4042 return status;
4043 }
mohammad16035955c982018-04-26 00:53:03 +03004044
Ronald Cron215633c2021-03-16 17:15:37 +01004045 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004047 };
mohammad16035955c982018-04-26 00:53:03 +03004048
David Horstmann21c1a942023-11-28 19:25:00 +00004049 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4050 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length, additional_data);
4051 LOCAL_INPUT_ALLOC(plaintext_external, plaintext_length, plaintext);
4052 LOCAL_OUTPUT_ALLOC(ciphertext_external, ciphertext_size, ciphertext);
4053
Ronald Cronde822812021-03-17 16:08:20 +01004054 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004055 &attributes, slot->key.data, slot->key.bytes,
4056 alg,
4057 nonce, nonce_length,
4058 additional_data, additional_data_length,
4059 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4063 memset(ciphertext, 0, ciphertext_size);
4064 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004065
David Horstmann21c1a942023-11-28 19:25:00 +00004066/* Exit label is only used for buffer copying, prevent unused warnings. */
4067#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4068exit:
4069#endif
4070 LOCAL_INPUT_FREE(nonce_external, nonce);
4071 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4072 LOCAL_INPUT_FREE(plaintext_external, plaintext);
4073 LOCAL_OUTPUT_FREE(ciphertext_external, ciphertext);
4074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004078}
4079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004080psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4081 psa_algorithm_t alg,
David Horstmann6baf6e92023-11-28 19:43:53 +00004082 const uint8_t *nonce_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 size_t nonce_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004084 const uint8_t *additional_data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 size_t additional_data_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004086 const uint8_t *ciphertext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 size_t ciphertext_length,
David Horstmann6baf6e92023-11-28 19:43:53 +00004088 uint8_t *plaintext_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 size_t plaintext_size,
4090 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004091{
Ronald Cron215633c2021-03-16 17:15:37 +01004092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4093 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004094
David Horstmann6baf6e92023-11-28 19:43:53 +00004095 LOCAL_INPUT_DECLARE(nonce_external, nonce);
4096 LOCAL_INPUT_DECLARE(additional_data_external, additional_data);
4097 LOCAL_INPUT_DECLARE(ciphertext_external, ciphertext);
4098 LOCAL_OUTPUT_DECLARE(plaintext_external, plaintext);
4099
Gilles Peskineee652a32018-06-01 19:23:52 +02004100 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4103 return PSA_ERROR_NOT_SUPPORTED;
4104 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004105
Ronald Cron9a986162021-03-26 12:40:07 +01004106 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4108 if (status != PSA_SUCCESS) {
4109 return status;
4110 }
mohammad16035955c982018-04-26 00:53:03 +03004111
Ronald Cron215633c2021-03-16 17:15:37 +01004112 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004114 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004115
David Horstmann6baf6e92023-11-28 19:43:53 +00004116 LOCAL_INPUT_ALLOC(nonce_external, nonce_length, nonce);
4117 LOCAL_INPUT_ALLOC(additional_data_external, additional_data_length,
4118 additional_data);
4119 LOCAL_INPUT_ALLOC(ciphertext_external, ciphertext_length, ciphertext);
4120 LOCAL_OUTPUT_ALLOC(plaintext_external, plaintext_size, plaintext);
4121
Ronald Cronde822812021-03-17 16:08:20 +01004122 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004123 &attributes, slot->key.data, slot->key.bytes,
4124 alg,
4125 nonce, nonce_length,
4126 additional_data, additional_data_length,
4127 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 if (status != PSA_SUCCESS && plaintext_size != 0) {
4131 memset(plaintext, 0, plaintext_size);
4132 }
mohammad160360a64d02018-06-03 17:20:42 +03004133
David Horstmann6baf6e92023-11-28 19:43:53 +00004134/* Exit label is only used for buffer copying, prevent unused warnings. */
4135#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
4136exit:
4137#endif
4138 LOCAL_INPUT_FREE(nonce_external, nonce);
4139 LOCAL_INPUT_FREE(additional_data_external, additional_data);
4140 LOCAL_INPUT_FREE(ciphertext_external, ciphertext);
4141 LOCAL_OUTPUT_FREE(plaintext_external, plaintext);
4142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004143 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145 return status;
mohammad16035955c982018-04-26 00:53:03 +03004146}
4147
Gilles Peskinee59236f2018-01-27 23:32:46 +01004148/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004149/* Generators */
4150/****************************************************************/
4151
John Durkop07cc04a2020-11-16 22:08:34 -08004152#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4153 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4155#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02004156#endif /* At least one builtin KDF */
4157
4158#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4161static psa_status_t psa_key_derivation_start_hmac(
4162 psa_mac_operation_t *operation,
4163 psa_algorithm_t hash_alg,
4164 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02004166{
4167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004169 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4170 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4171 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02004172
Steven Cooreman15f0d922021-05-07 14:14:37 +02004173 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 status = psa_driver_wrapper_mac_sign_setup(operation,
4177 &attributes,
4178 hmac_key, hmac_key_length,
4179 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 psa_reset_key_attributes(&attributes);
4182 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004183}
4184#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004185
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004186#define HKDF_STATE_INIT 0 /* no input yet */
4187#define HKDF_STATE_STARTED 1 /* got salt */
4188#define HKDF_STATE_KEYED 2 /* got key */
4189#define HKDF_STATE_OUTPUT 3 /* output started */
4190
Gilles Peskinecbe66502019-05-16 16:59:18 +02004191static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004193{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4195 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4196 } else {
4197 return operation->alg;
4198 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004199}
4200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004201psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004202{
4203 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4205 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004206 /* The object has (apparently) been initialized but it is not
4207 * in use. It's ok to call abort on such an object, and there's
4208 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 } else
John Durkopd0321952020-10-29 21:37:36 -07004210#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4212 mbedtls_free(operation->ctx.hkdf.info);
4213 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4214 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004215#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4216#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4217 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4219 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4220 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4221 if (operation->ctx.tls12_prf.secret != NULL) {
4222 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4223 operation->ctx.tls12_prf.secret_length);
4224 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004225 }
4226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 if (operation->ctx.tls12_prf.seed != NULL) {
4228 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4229 operation->ctx.tls12_prf.seed_length);
4230 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004231 }
4232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233 if (operation->ctx.tls12_prf.label != NULL) {
4234 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4235 operation->ctx.tls12_prf.label_length);
4236 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004237 }
4238
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004239 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004240
4241 /* We leave the fields Ai and output_block to be erased safely by the
4242 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004244#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4245 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004246 {
4247 status = PSA_ERROR_BAD_STATE;
4248 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 mbedtls_platform_zeroize(operation, sizeof(*operation));
4250 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251}
4252
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004253psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004254 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004255{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004257 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004259 }
4260
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004262 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004263}
4264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4266 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004267{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004268 if (operation->alg == 0) {
4269 return PSA_ERROR_BAD_STATE;
4270 }
4271 if (capacity > operation->capacity) {
4272 return PSA_ERROR_INVALID_ARGUMENT;
4273 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004274 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004275 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004276}
4277
John Durkopd0321952020-10-29 21:37:36 -07004278#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004280 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004281static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4282 psa_algorithm_t hash_alg,
4283 uint8_t *output,
4284 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004285{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004287 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004288 psa_status_t status;
4289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4291 return PSA_ERROR_BAD_STATE;
4292 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004293 hkdf->state = HKDF_STATE_OUTPUT;
4294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004296 /* Copy what remains of the current block */
4297 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004299 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 }
4301 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004302 output += n;
4303 output_length -= n;
4304 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004306 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004308 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004309 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004310 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004311 * object was corrupted or if this function is called directly
4312 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 if (hkdf->block_number == 0xff) {
4314 return PSA_ERROR_BAD_STATE;
4315 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004316
4317 /* We need a new block */
4318 ++hkdf->block_number;
4319 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4322 hash_alg,
4323 hkdf->prk,
4324 hash_length);
4325 if (status != PSA_SUCCESS) {
4326 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004327 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328
4329 if (hkdf->block_number != 1) {
4330 status = psa_mac_update(&hkdf->hmac,
4331 hkdf->output_block,
4332 hash_length);
4333 if (status != PSA_SUCCESS) {
4334 return status;
4335 }
4336 }
4337 status = psa_mac_update(&hkdf->hmac,
4338 hkdf->info,
4339 hkdf->info_length);
4340 if (status != PSA_SUCCESS) {
4341 return status;
4342 }
4343 status = psa_mac_update(&hkdf->hmac,
4344 &hkdf->block_number, 1);
4345 if (status != PSA_SUCCESS) {
4346 return status;
4347 }
4348 status = psa_mac_sign_finish(&hkdf->hmac,
4349 hkdf->output_block,
4350 sizeof(hkdf->output_block),
4351 &hmac_output_length);
4352 if (status != PSA_SUCCESS) {
4353 return status;
4354 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004355 }
4356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004358}
John Durkop07cc04a2020-11-16 22:08:34 -08004359#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004360
John Durkop07cc04a2020-11-16 22:08:34 -08004361#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4362 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004363static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4364 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004366{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4368 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004369 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4370 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004371 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004372
Janos Follath7742fee2019-06-17 12:58:10 +01004373 /* We can't be wanting more output after block 0xff, otherwise
4374 * the capacity check in psa_key_derivation_output_bytes() would have
4375 * prevented this call. It could happen only if the operation
4376 * object was corrupted or if this function is called directly
4377 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 if (tls12_prf->block_number == 0xff) {
4379 return PSA_ERROR_CORRUPTION_DETECTED;
4380 }
Janos Follath7742fee2019-06-17 12:58:10 +01004381
4382 /* We need a new block */
4383 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004384 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004385
4386 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4387 *
4388 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4389 *
4390 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4391 * HMAC_hash(secret, A(2) + seed) +
4392 * HMAC_hash(secret, A(3) + seed) + ...
4393 *
4394 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004395 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004396 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004397 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004398 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004399 * is currently extracted as `output_block` and where i is
4400 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004401 */
4402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 status = psa_key_derivation_start_hmac(&hmac,
4404 hash_alg,
4405 tls12_prf->secret,
4406 tls12_prf->secret_length);
4407 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004408 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004410
4411 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004413 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4414 * the variable seed and in this instance means it in the context of the
4415 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004416 status = psa_mac_update(&hmac,
4417 tls12_prf->label,
4418 tls12_prf->label_length);
4419 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004420 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421 }
4422 status = psa_mac_update(&hmac,
4423 tls12_prf->seed,
4424 tls12_prf->seed_length);
4425 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004426 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427 }
4428 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004429 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4431 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004432 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004434 }
4435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004436 status = psa_mac_sign_finish(&hmac,
4437 tls12_prf->Ai, hash_length,
4438 &hmac_output_length);
4439 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004440 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 }
4442 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004443 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004445
4446 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 status = psa_key_derivation_start_hmac(&hmac,
4448 hash_alg,
4449 tls12_prf->secret,
4450 tls12_prf->secret_length);
4451 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004452 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453 }
4454 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4455 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004456 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004457 }
4458 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4459 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004460 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461 }
4462 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4463 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004464 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 }
4466 status = psa_mac_sign_finish(&hmac,
4467 tls12_prf->output_block, hash_length,
4468 &hmac_output_length);
4469 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004470 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004472
Janos Follath7742fee2019-06-17 12:58:10 +01004473
4474cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004475 cleanup_status = psa_mac_abort(&hmac);
4476 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004477 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004481}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004482
Janos Follath844eb0e2019-06-19 12:10:49 +01004483static psa_status_t psa_key_derivation_tls12_prf_read(
4484 psa_tls12_prf_key_derivation_t *tls12_prf,
4485 psa_algorithm_t alg,
4486 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004487 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004488{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4490 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004491 psa_status_t status;
4492 uint8_t offset, length;
4493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004495 case PSA_TLS12_PRF_STATE_LABEL_SET:
4496 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4497 break;
4498 case PSA_TLS12_PRF_STATE_OUTPUT:
4499 break;
4500 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004501 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004502 }
4503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004505 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 if (tls12_prf->left_in_block == 0) {
4507 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4508 alg);
4509 if (status != PSA_SUCCESS) {
4510 return status;
4511 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004512
4513 continue;
4514 }
4515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004517 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004518 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004519 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004521
4522 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004524 output += length;
4525 output_length -= length;
4526 tls12_prf->left_in_block -= length;
4527 }
4528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004530}
John Durkop07cc04a2020-11-16 22:08:34 -08004531#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4532 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004533
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004534psa_status_t psa_key_derivation_output_bytes(
4535 psa_key_derivation_operation_t *operation,
Ryan Everett08bd2462024-02-09 16:15:32 +00004536 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004537 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004538{
4539 psa_status_t status;
Ryan Everett08bd2462024-02-09 16:15:32 +00004540 LOCAL_OUTPUT_DECLARE(output_external, output);
4541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004547 }
4548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004551 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004552 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4553 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004555 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004556 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004557 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004558
4559 LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
4560 if (output_length > operation->capacity) {
4561 operation->capacity = 0;
4562 /* Go through the error path to wipe all confidential data now
4563 * that the operation object is useless. */
4564 status = PSA_ERROR_INSUFFICIENT_DATA;
4565 goto exit;
4566 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004568
John Durkopd0321952020-10-29 21:37:36 -07004569#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4571 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4572 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4573 output, output_length);
4574 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004575#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4576#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4577 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004578 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4579 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4580 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4581 kdf_alg, output,
4582 output_length);
4583 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004584#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4585 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004586 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004587 (void) kdf_alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004588 status = PSA_ERROR_BAD_STATE;
4589 LOCAL_OUTPUT_FREE(output_external, output);
4590
4591 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004592 }
4593
4594exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004596 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 * This allows us to differentiate between exhausted operations and
4598 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4599 * operations. */
4600 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 operation->alg = alg;
Ryan Everett08bd2462024-02-09 16:15:32 +00004603 if (output != NULL) {
4604 memset(output, '!', output_length);
4605 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004606 }
Ryan Everett08bd2462024-02-09 16:15:32 +00004607
4608 LOCAL_OUTPUT_FREE(output_external, output);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004610}
4611
David Brown7807bf72021-02-09 16:28:23 -07004612#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004614{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 if (data_size >= 8) {
4616 mbedtls_des_key_set_parity(data);
4617 }
4618 if (data_size >= 16) {
4619 mbedtls_des_key_set_parity(data + 8);
4620 }
4621 if (data_size >= 24) {
4622 mbedtls_des_key_set_parity(data + 16);
4623 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004624}
David Brown7807bf72021-02-09 16:28:23 -07004625#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004626
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004627static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004628 psa_key_slot_t *slot,
4629 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004630 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631{
4632 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004634 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004635 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 if (!key_type_is_raw_bytes(slot->attr.type)) {
4638 return PSA_ERROR_INVALID_ARGUMENT;
4639 }
4640 if (bits % 8 != 0) {
4641 return PSA_ERROR_INVALID_ARGUMENT;
4642 }
4643 data = mbedtls_calloc(1, bytes);
4644 if (data == NULL) {
4645 return PSA_ERROR_INSUFFICIENT_MEMORY;
4646 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 status = psa_key_derivation_output_bytes(operation, data, bytes);
4649 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004650 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 }
David Brown12ca5032021-02-11 11:02:00 -07004652#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004653 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4654 psa_des_set_key_parity(data, bytes);
4655 }
David Brown8107e312021-02-12 08:08:46 -07004656#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 status = psa_allocate_buffer_to_slot(slot, bytes);
4659 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004660 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004661 }
Ronald Crondd04d422020-11-28 15:14:42 +01004662
Ronald Cronbf33c932020-11-28 18:06:53 +01004663 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004664 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004666 };
4667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668 status = psa_driver_wrapper_import_key(&attributes,
4669 data, bytes,
4670 slot->key.data,
4671 slot->key.bytes,
4672 &slot->key.bytes, &bits);
4673 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004674 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004676
4677exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 mbedtls_free(data);
4679 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680}
4681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4683 psa_key_derivation_operation_t *operation,
4684 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685{
4686 psa_status_t status;
4687 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004688 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004689
Ronald Cron81709fc2020-11-14 12:10:32 +01004690 *key = MBEDTLS_SVC_KEY_ID_INIT;
4691
Gilles Peskine0f84d622019-09-12 19:03:13 +02004692 /* Reject any attempt to create a zero-length key so that we don't
4693 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004694 if (psa_get_key_bits(attributes) == 0) {
4695 return PSA_ERROR_INVALID_ARGUMENT;
4696 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 if (operation->alg == PSA_ALG_NONE) {
4699 return PSA_ERROR_BAD_STATE;
4700 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004702 if (!operation->can_output_key) {
4703 return PSA_ERROR_NOT_PERMITTED;
4704 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004706 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4707 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004708#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004710 /* Deriving a key in a secure element is not implemented yet. */
4711 status = PSA_ERROR_NOT_SUPPORTED;
4712 }
4713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 if (status == PSA_SUCCESS) {
4715 status = psa_generate_derived_key_internal(slot,
4716 attributes->core.bits,
4717 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004718 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 if (status == PSA_SUCCESS) {
4720 status = psa_finish_key_creation(slot, driver, key);
4721 }
4722 if (status != PSA_SUCCESS) {
4723 psa_fail_key_creation(slot, driver);
4724 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004727}
4728
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729
4730
4731/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004732/* Key derivation */
4733/****************************************************************/
4734
Steven Cooreman932ffb72021-02-15 12:14:32 +01004735#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004737{
4738#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4740 return 1;
4741 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004742#endif
4743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4745 return 1;
4746 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004747#endif
4748#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4750 return 1;
4751 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004752#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004753 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004754}
4755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004757{
4758 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 psa_status_t status = psa_hash_setup(&operation, alg);
4760 psa_hash_abort(&operation);
4761 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004762}
4763
Gilles Peskine969c5d62019-01-16 15:53:06 +01004764static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004766 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767{
Janos Follath5fe19732019-06-20 15:09:30 +01004768 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4769 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004771
Gilles Peskine969c5d62019-01-16 15:53:06 +01004772 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 if (!is_kdf_alg_supported(kdf_alg)) {
4774 return PSA_ERROR_NOT_SUPPORTED;
4775 }
John Durkop07cc04a2020-11-16 22:08:34 -08004776
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004777 /* All currently supported key derivation algorithms are based on a
4778 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004779 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4780 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4781 if (hash_size == 0) {
4782 return PSA_ERROR_NOT_SUPPORTED;
4783 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004784
4785 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4786 * we might fail later, which is somewhat unfriendly and potentially
4787 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 psa_status_t status = psa_hash_try_support(hash_alg);
4789 if (status != PSA_SUCCESS) {
4790 return status;
4791 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004793 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4794 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4795 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4796 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004798
Gilles Peskinecdacf042021-04-29 21:10:00 +02004799 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004804{
4805#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 if (alg == PSA_ALG_ECDH) {
4807 return PSA_SUCCESS;
4808 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004809#endif
4810 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004812}
John Durkop07cc04a2020-11-16 22:08:34 -08004813#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4816 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004817{
4818 psa_status_t status;
4819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 if (operation->alg != 0) {
4821 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004822 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4825 return PSA_ERROR_INVALID_ARGUMENT;
4826 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4827#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4828 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4829 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4830 status = psa_key_agreement_try_support(ka_alg);
4831 if (status != PSA_SUCCESS) {
4832 return status;
4833 }
4834 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4835#else
4836 return PSA_ERROR_NOT_SUPPORTED;
4837#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4838 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4839#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4840 status = psa_key_derivation_setup_kdf(operation, alg);
4841#else
4842 return PSA_ERROR_NOT_SUPPORTED;
4843#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4844 } else {
4845 return PSA_ERROR_INVALID_ARGUMENT;
4846 }
4847
4848 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 }
4851 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004852}
4853
John Durkopd0321952020-10-29 21:37:36 -07004854#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004855static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4856 psa_algorithm_t hash_alg,
4857 psa_key_derivation_step_t step,
4858 const uint8_t *data,
4859 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004860{
4861 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004862 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004863 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 if (hkdf->state != HKDF_STATE_INIT) {
4865 return PSA_ERROR_BAD_STATE;
4866 } else {
4867 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4868 hash_alg,
4869 data, data_length);
4870 if (status != PSA_SUCCESS) {
4871 return status;
4872 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004873 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004875 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004876 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 if (hkdf->state == HKDF_STATE_INIT) {
4879 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4880 hash_alg,
4881 NULL, 0);
4882 if (status != PSA_SUCCESS) {
4883 return status;
4884 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885 hkdf->state = HKDF_STATE_STARTED;
4886 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004887 if (hkdf->state != HKDF_STATE_STARTED) {
4888 return PSA_ERROR_BAD_STATE;
4889 }
4890 status = psa_mac_update(&hkdf->hmac,
4891 data, data_length);
4892 if (status != PSA_SUCCESS) {
4893 return status;
4894 }
4895 status = psa_mac_sign_finish(&hkdf->hmac,
4896 hkdf->prk,
4897 sizeof(hkdf->prk),
4898 &data_length);
4899 if (status != PSA_SUCCESS) {
4900 return status;
4901 }
4902 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004903 hkdf->block_number = 0;
4904 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004906 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 if (hkdf->state == HKDF_STATE_OUTPUT) {
4908 return PSA_ERROR_BAD_STATE;
4909 }
4910 if (hkdf->info_set) {
4911 return PSA_ERROR_BAD_STATE;
4912 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004914 if (data_length != 0) {
4915 hkdf->info = mbedtls_calloc(1, data_length);
4916 if (hkdf->info == NULL) {
4917 return PSA_ERROR_INSUFFICIENT_MEMORY;
4918 }
4919 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920 }
4921 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004922 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004923 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925 }
4926}
John Durkop07cc04a2020-11-16 22:08:34 -08004927#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004928
John Durkop07cc04a2020-11-16 22:08:34 -08004929#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4930 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4932 const uint8_t *data,
4933 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004934{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004935 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4936 return PSA_ERROR_BAD_STATE;
4937 }
Janos Follathf08e2652019-06-13 09:05:41 +01004938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004939 if (data_length != 0) {
4940 prf->seed = mbedtls_calloc(1, data_length);
4941 if (prf->seed == NULL) {
4942 return PSA_ERROR_INSUFFICIENT_MEMORY;
4943 }
Janos Follathf08e2652019-06-13 09:05:41 +01004944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004946 prf->seed_length = data_length;
4947 }
Janos Follathf08e2652019-06-13 09:05:41 +01004948
Gilles Peskine43f958b2020-12-13 14:55:14 +01004949 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004952}
4953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004954static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4955 const uint8_t *data,
4956 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004957{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4959 return PSA_ERROR_BAD_STATE;
4960 }
Janos Follath81550542019-06-13 14:26:34 +01004961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 if (data_length != 0) {
4963 prf->secret = mbedtls_calloc(1, data_length);
4964 if (prf->secret == NULL) {
4965 return PSA_ERROR_INSUFFICIENT_MEMORY;
4966 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004968 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004969 prf->secret_length = data_length;
4970 }
Janos Follath81550542019-06-13 14:26:34 +01004971
Gilles Peskine43f958b2020-12-13 14:55:14 +01004972 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004974 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004975}
4976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4978 const uint8_t *data,
4979 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004980{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4982 return PSA_ERROR_BAD_STATE;
4983 }
Janos Follath63028dd2019-06-13 09:15:47 +01004984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004985 if (data_length != 0) {
4986 prf->label = mbedtls_calloc(1, data_length);
4987 if (prf->label == NULL) {
4988 return PSA_ERROR_INSUFFICIENT_MEMORY;
4989 }
Janos Follath63028dd2019-06-13 09:15:47 +01004990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004991 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004992 prf->label_length = data_length;
4993 }
Janos Follath63028dd2019-06-13 09:15:47 +01004994
Gilles Peskine43f958b2020-12-13 14:55:14 +01004995 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004998}
4999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
5001 psa_key_derivation_step_t step,
5002 const uint8_t *data,
5003 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01005004{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005005 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01005006 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01005008 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01005010 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01005012 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005013 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01005014 }
5015}
John Durkop07cc04a2020-11-16 22:08:34 -08005016#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5017 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5018
5019#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5020static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5021 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005022 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08005024{
5025 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005026 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08005027 uint8_t *cur = pms;
5028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
5030 return PSA_ERROR_INVALID_ARGUMENT;
5031 }
John Durkop07cc04a2020-11-16 22:08:34 -08005032
5033 /* Quoting RFC 4279, Section 2:
5034 *
5035 * The premaster secret is formed as follows: if the PSK is N octets
5036 * long, concatenate a uint16 with the value N, N zero octets, a second
5037 * uint16 with the value N, and the PSK itself.
5038 */
5039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040 *cur++ = MBEDTLS_BYTE_1(data_length);
5041 *cur++ = MBEDTLS_BYTE_0(data_length);
5042 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005043 cur += data_length;
5044 *cur++ = pms[0];
5045 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08005047 cur += data_length;
5048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005049 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08005050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 mbedtls_platform_zeroize(pms, sizeof(pms));
5052 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08005053}
Janos Follath6660f0e2019-06-17 08:44:03 +01005054
5055static psa_status_t psa_tls12_prf_psk_to_ms_input(
5056 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005057 psa_key_derivation_step_t step,
5058 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01005060{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005061 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
5062 return psa_tls12_prf_psk_to_ms_set_key(prf,
5063 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01005064 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01005067}
John Durkop07cc04a2020-11-16 22:08:34 -08005068#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005069
Gilles Peskineb8965192019-09-24 16:21:10 +02005070/** Check whether the given key type is acceptable for the given
5071 * input step of a key derivation.
5072 *
5073 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5074 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5075 * Both secret and non-secret inputs can alternatively have the type
5076 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5077 * that the input was passed as a buffer rather than via a key object.
5078 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005079static int psa_key_derivation_check_input_type(
5080 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02005082{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005084 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 if (key_type == PSA_KEY_TYPE_DERIVE) {
5086 return PSA_SUCCESS;
5087 }
5088 if (key_type == PSA_KEY_TYPE_NONE) {
5089 return PSA_SUCCESS;
5090 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005091 break;
5092 case PSA_KEY_DERIVATION_INPUT_LABEL:
5093 case PSA_KEY_DERIVATION_INPUT_SALT:
5094 case PSA_KEY_DERIVATION_INPUT_INFO:
5095 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005096 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
5097 return PSA_SUCCESS;
5098 }
5099 if (key_type == PSA_KEY_TYPE_NONE) {
5100 return PSA_SUCCESS;
5101 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005102 break;
5103 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02005105}
5106
Janos Follathb80a94e2019-06-12 15:54:46 +01005107static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005109 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005110 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005111 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005113{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005114 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 status = psa_key_derivation_check_input_type(step, key_type);
5118 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02005119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02005121
John Durkopd0321952020-10-29 21:37:36 -07005122#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5124 status = psa_hkdf_input(&operation->ctx.hkdf,
5125 PSA_ALG_HKDF_GET_HASH(kdf_alg),
5126 step, data, data_length);
5127 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005128#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5129#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5131 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
5132 step, data, data_length);
5133 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005134#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5135#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5137 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
5138 step, data, data_length);
5139 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005140#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005141 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005143 (void) data;
5144 (void) data_length;
5145 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005147 }
5148
Gilles Peskine224b0d62019-09-23 18:13:17 +02005149exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150 if (status != PSA_SUCCESS) {
5151 psa_key_derivation_abort(operation);
5152 }
5153 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154}
5155
Janos Follath51f4a0f2019-06-14 11:35:55 +01005156psa_status_t psa_key_derivation_input_bytes(
5157 psa_key_derivation_operation_t *operation,
5158 psa_key_derivation_step_t step,
Ryan Everett6f682062024-02-09 16:18:39 +00005159 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005161{
Ryan Everett6f682062024-02-09 16:18:39 +00005162 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5163 LOCAL_INPUT_DECLARE(data_external, data);
5164
5165 LOCAL_INPUT_ALLOC(data_external, data_length, data);
5166
5167 status = psa_key_derivation_input_internal(operation, step,
5168 PSA_KEY_TYPE_NONE,
5169 data, data_length);
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005170#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Ryan Everett6f682062024-02-09 16:18:39 +00005171exit:
Ryan Everettd0d12fb2024-02-12 09:19:29 +00005172#endif
Ryan Everett6f682062024-02-09 16:18:39 +00005173 LOCAL_INPUT_FREE(data_external, data);
5174 return status;
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005175}
5176
Janos Follath51f4a0f2019-06-14 11:35:55 +01005177psa_status_t psa_key_derivation_input_key(
5178 psa_key_derivation_operation_t *operation,
5179 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005180 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005183 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005185
Ronald Cron5c522922020-11-14 16:35:34 +01005186 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005187 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5188 if (status != PSA_SUCCESS) {
5189 psa_key_derivation_abort(operation);
5190 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02005191 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005192
5193 /* Passing a key object as a SECRET input unlocks the permission
5194 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005195 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005196 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199 status = psa_key_derivation_input_internal(operation,
5200 step, slot->attr.type,
5201 slot->key.data,
5202 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005204 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005206 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005208
5209
5210
5211/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005212/* Key agreement */
5213/****************************************************************/
5214
John Durkop6ba40d12020-11-10 08:50:04 -08005215#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5217 size_t peer_key_length,
5218 const mbedtls_ecp_keypair *our_key,
5219 uint8_t *shared_secret,
5220 size_t shared_secret_size,
5221 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005222{
Steven Cooremand4867872020-08-05 16:31:39 +02005223 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005224 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005225 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005226 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5228 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229
Ronald Cron90857082020-11-25 14:58:33 +01005230 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5232 bits,
5233 peer_key,
5234 peer_key_length,
5235 &their_key);
5236 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005237 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005239
Jaeden Amero97271b32019-01-10 19:38:51 +00005240 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5242 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005243 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005245 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5247 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005248 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005250
Jaeden Amero97271b32019-01-10 19:38:51 +00005251 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 mbedtls_ecdh_calc_secret(&ecdh,
5253 shared_secret_length,
5254 shared_secret, shared_secret_size,
5255 mbedtls_psa_get_random,
5256 MBEDTLS_PSA_RANDOM_STATE));
5257 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005258 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 }
5260 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005261 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005262 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005263
5264exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 if (status != PSA_SUCCESS) {
5266 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5267 }
5268 mbedtls_ecdh_free(&ecdh);
5269 mbedtls_ecp_keypair_free(their_key);
5270 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005273}
John Durkop6ba40d12020-11-10 08:50:04 -08005274#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005275
Gilles Peskine01d718c2018-09-18 12:01:02 +02005276#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005278static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5279 psa_key_slot_t *private_key,
5280 const uint8_t *peer_key,
5281 size_t peer_key_length,
5282 uint8_t *shared_secret,
5283 size_t shared_secret_size,
5284 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005285{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005286 mbedtls_ecp_keypair *ecp = NULL;
5287 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005291 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5293 return PSA_ERROR_INVALID_ARGUMENT;
5294 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005295 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005296 private_key->attr.type,
5297 private_key->attr.bits,
5298 private_key->key.data,
5299 private_key->key.bytes,
5300 &ecp);
5301 if (status != PSA_SUCCESS) {
5302 return status;
5303 }
5304 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5305 ecp,
5306 shared_secret, shared_secret_size,
5307 shared_secret_length);
5308 mbedtls_ecp_keypair_free(ecp);
5309 mbedtls_free(ecp);
5310 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005311#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005312 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005313 (void) ecp;
5314 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005315 (void) private_key;
5316 (void) peer_key;
5317 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318 (void) shared_secret;
5319 (void) shared_secret_size;
5320 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005321 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005322 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005323}
5324
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005325/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326 * to potentially free embedded data structures and wipe confidential data.
5327 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5329 psa_key_derivation_step_t step,
5330 psa_key_slot_t *private_key,
5331 const uint8_t *peer_key,
5332 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333{
5334 psa_status_t status;
5335 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5336 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005337 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338
5339 /* Step 1: run the secret agreement algorithm to generate the shared
5340 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 status = psa_key_agreement_raw_internal(ka_alg,
5342 private_key,
5343 peer_key, peer_key_length,
5344 shared_secret,
5345 sizeof(shared_secret),
5346 &shared_secret_length);
5347 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005348 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005349 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005350
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005351 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005352 * the shared secret. A shared secret is permitted wherever a key
5353 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 status = psa_key_derivation_input_internal(operation, step,
5355 PSA_KEY_TYPE_DERIVE,
5356 shared_secret,
5357 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005358exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005359 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5360 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005361}
5362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005363psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5364 psa_key_derivation_step_t step,
5365 mbedtls_svc_key_id_t private_key,
5366 const uint8_t *peer_key,
5367 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005370 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005371 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5374 return PSA_ERROR_INVALID_ARGUMENT;
5375 }
Ronald Cron5c522922020-11-14 16:35:34 +01005376 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5378 if (status != PSA_SUCCESS) {
5379 return status;
5380 }
5381 status = psa_key_agreement_internal(operation, step,
5382 slot,
5383 peer_key, peer_key_length);
5384 if (status != PSA_SUCCESS) {
5385 psa_key_derivation_abort(operation);
5386 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005387 /* If a private key has been added as SECRET, we allow the derived
5388 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005390 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005392 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005394 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005397}
5398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5400 mbedtls_svc_key_id_t private_key,
5401 const uint8_t *peer_key,
5402 size_t peer_key_length,
5403 uint8_t *output,
5404 size_t output_size,
5405 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005406{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005409 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005410 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005413 status = PSA_ERROR_INVALID_ARGUMENT;
5414 goto exit;
5415 }
Ronald Cron5c522922020-11-14 16:35:34 +01005416 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5418 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005419 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005421
Gilles Peskine42313fb2022-04-14 00:17:15 +02005422 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5423 * for the output size. The PSA specification only guarantees that this
5424 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5425 * but it might be nice to allow smaller buffers if the output fits.
5426 * At the time of writing this comment, with only ECDH implemented,
5427 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5428 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5429 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005430 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005431 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5432 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005433 status = PSA_ERROR_BUFFER_TOO_SMALL;
5434 goto exit;
5435 }
5436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 status = psa_key_agreement_raw_internal(alg, slot,
5438 peer_key, peer_key_length,
5439 output, output_size,
5440 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005441
5442exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005444 /* If an error happens and is not handled properly, the output
5445 * may be used as a key to protect sensitive data. Arrange for such
5446 * a key to be random, which is likely to result in decryption or
5447 * verification errors. This is better than filling the buffer with
5448 * some constant data such as zeros, which would result in the data
5449 * being protected with a reproducible, easily knowable key.
5450 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005452 *output_length = output_size;
5453 }
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 Peskine0216fe12019-04-11 21:23:21 +02005458}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005459
5460
Gilles Peskine30524eb2020-11-13 17:02:26 +01005461
Gilles Peskine86a440b2018-11-12 18:39:40 +01005462/****************************************************************/
5463/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005464/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005465
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005466#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5467#include "mbedtls/entropy_poll.h"
5468#endif
5469
Gilles Peskine30524eb2020-11-13 17:02:26 +01005470/** Initialize the PSA random generator.
5471 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005473{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005474#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005476#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5477
Gilles Peskine30524eb2020-11-13 17:02:26 +01005478 /* Set default configuration if
5479 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005481 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 }
5483 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005484 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005488#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5489 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5490 /* The PSA entropy injection feature depends on using NV seed as an entropy
5491 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 mbedtls_entropy_add_source(&rng->entropy,
5493 mbedtls_nv_seed_poll, NULL,
5494 MBEDTLS_ENTROPY_BLOCK_SIZE,
5495 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005496#endif
5497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005499#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005500}
5501
5502/** Deinitialize the PSA random generator.
5503 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005504static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005505{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005506#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005508#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5510 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005511#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005512}
5513
5514/** Seed the PSA random generator.
5515 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005516static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005517{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005518#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5519 /* Do nothing: the external RNG seeds itself. */
5520 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005522#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005523 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5525 drbg_seed, sizeof(drbg_seed) - 1);
5526 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005527#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005528}
5529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530psa_status_t psa_generate_random(uint8_t *output,
5531 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533 GUARD_MODULE_INITIALIZED;
5534
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005535#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5536
5537 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5539 output, output_size,
5540 &output_length);
5541 if (status != PSA_SUCCESS) {
5542 return status;
5543 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005544 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005545 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005546 if (output_length != output_size) {
5547 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5548 }
5549 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005550
5551#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005554 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005555 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5556 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5557 output_size);
5558 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5559 output, request_size);
5560 if (ret != 0) {
5561 return mbedtls_to_psa_error(ret);
5562 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005563 output_size -= request_size;
5564 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005565 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005566 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005567#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005568}
5569
Gilles Peskine8814fc42020-12-14 15:33:44 +01005570/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005571 *
5572 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5573 * `psa_generate_random(...)`. The state parameter is ignored since the
5574 * PSA API doesn't support passing an explicit state.
5575 *
5576 * In the non-external case, psa_generate_random() calls an
5577 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5578 * and semantics as mbedtls_psa_get_random(). As an optimization,
5579 * instead of doing this back-and-forth between the PSA API and the
5580 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5581 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005582 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005583#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5584int mbedtls_psa_get_random(void *p_rng,
5585 unsigned char *output,
5586 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005587{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005588 /* This function takes a pointer to the RNG state because that's what
5589 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5590 * its own state internally and doesn't let the caller access that state.
5591 * So we just ignore the state parameter, and in practice we'll pass
5592 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005593 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005594 psa_status_t status = psa_generate_random(output, output_size);
5595 if (status == PSA_SUCCESS) {
5596 return 0;
5597 } else {
5598 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5599 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005600}
5601#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5602
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005603#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5605 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005606{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005607 if (global_data.initialized) {
5608 return PSA_ERROR_NOT_PERMITTED;
5609 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005611 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5612 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5613 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5614 return PSA_ERROR_INVALID_ARGUMENT;
5615 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005618}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005619#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005620
Ronald Cron3772afe2021-02-08 16:10:05 +01005621/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005622 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005623 * \param type The key type
5624 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005625 *
5626 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005627 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005628 * \retval #PSA_ERROR_INVALID_ARGUMENT
5629 * The size in bits of the key is not valid.
5630 * \retval #PSA_ERROR_NOT_SUPPORTED
5631 * The type and/or the size in bits of the key or the combination of
5632 * the two is not supported.
5633 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005634static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005636{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005637 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005639 if (key_type_is_raw_bytes(type)) {
5640 status = validate_unstructured_key_bit_size(type, bits);
5641 if (status != PSA_SUCCESS) {
5642 return status;
5643 }
5644 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005645#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5647 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5648 return PSA_ERROR_NOT_SUPPORTED;
5649 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005650
Ronald Cron01b2aba2020-10-05 09:42:02 +02005651 /* Accept only byte-aligned keys, for the same reasons as
5652 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005653 if (bits % 8 != 0) {
5654 return PSA_ERROR_NOT_SUPPORTED;
5655 }
5656 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005657#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005658
Ronald Cron5c4d3862020-12-07 11:07:24 +01005659#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005660 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005661 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005662 return PSA_SUCCESS;
5663 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005664#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005665 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005667 }
5668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005669 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005670}
5671
Ronald Cron55ed0592020-10-05 10:30:40 +02005672psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005673 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005674 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005675{
5676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005677 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005679 if ((attributes->domain_parameters == NULL) &&
5680 (attributes->domain_parameters_size != 0)) {
5681 return PSA_ERROR_INVALID_ARGUMENT;
5682 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005684 if (key_type_is_raw_bytes(type)) {
5685 status = psa_generate_random(key_buffer, key_buffer_size);
5686 if (status != PSA_SUCCESS) {
5687 return status;
5688 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005689
David Brown12ca5032021-02-11 11:02:00 -07005690#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 if (type == PSA_KEY_TYPE_DES) {
5692 psa_des_set_key_parity(key_buffer, key_buffer_size);
5693 }
David Brown8107e312021-02-12 08:08:46 -07005694#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005695 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005696
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005697#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5698 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005699 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5700 return mbedtls_psa_rsa_generate_key(attributes,
5701 key_buffer,
5702 key_buffer_size,
5703 key_buffer_length);
5704 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005705#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5706 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005707
John Durkop9814fa22020-11-04 12:28:15 -08005708#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005709 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5710 return mbedtls_psa_ecp_generate_key(attributes,
5711 key_buffer,
5712 key_buffer_size,
5713 key_buffer_length);
5714 } else
John Durkop9814fa22020-11-04 12:28:15 -08005715#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005716 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717 (void) key_buffer_length;
5718 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005719 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005721 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005722}
Darryl Green0c6575a2018-11-07 16:05:30 +00005723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005724psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5725 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005726{
5727 psa_status_t status;
5728 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005729 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005730 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005731
Ronald Cron81709fc2020-11-14 12:10:32 +01005732 *key = MBEDTLS_SVC_KEY_ID_INIT;
5733
Gilles Peskine0f84d622019-09-12 19:03:13 +02005734 /* Reject any attempt to create a zero-length key so that we don't
5735 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005736 if (psa_get_key_bits(attributes) == 0) {
5737 return PSA_ERROR_INVALID_ARGUMENT;
5738 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005739
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005740 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005741 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5742 return PSA_ERROR_INVALID_ARGUMENT;
5743 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005745 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5746 &slot, &driver);
5747 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005748 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005749 }
Gilles Peskine11792082019-08-06 18:36:36 +02005750
Ronald Cron977c2472020-10-13 08:32:21 +02005751 /* In the case of a transparent key or an opaque key stored in local
5752 * storage (thus not in the case of generating a key in a secure element
5753 * or cryptoprocessor with storage), we have to allocate a buffer to
5754 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005755 if (slot->key.data == NULL) {
5756 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5757 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005758 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005759 attributes->core.type, attributes->core.bits);
5760 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005761 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005762 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005763
5764 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005765 attributes->core.type,
5766 attributes->core.bits);
5767 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005768 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005769 attributes, &key_buffer_size);
5770 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005771 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005772 }
Ronald Cron977c2472020-10-13 08:32:21 +02005773 }
Ronald Cron977c2472020-10-13 08:32:21 +02005774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005775 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5776 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005777 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005778 }
Ronald Cron977c2472020-10-13 08:32:21 +02005779 }
5780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005781 status = psa_driver_wrapper_generate_key(attributes,
5782 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005784 if (status != PSA_SUCCESS) {
5785 psa_remove_key_data_from_memory(slot);
5786 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005787
Gilles Peskine11792082019-08-06 18:36:36 +02005788exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005789 if (status == PSA_SUCCESS) {
5790 status = psa_finish_key_creation(slot, driver, key);
5791 }
5792 if (status != PSA_SUCCESS) {
5793 psa_fail_key_creation(slot, driver);
5794 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005796 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797}
5798
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799/****************************************************************/
5800/* Module setup */
5801/****************************************************************/
5802
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005803#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005804psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005805 void (* entropy_init)(mbedtls_entropy_context *ctx),
5806 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005807{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005808 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5809 return PSA_ERROR_BAD_STATE;
5810 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005811 global_data.rng.entropy_init = entropy_init;
5812 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005813 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005814}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005815#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005817void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005819 psa_wipe_all_key_slots();
5820 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5821 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005822 }
5823 /* Wipe all remaining data, including configuration.
5824 * In particular, this sets all state indicator to the value
5825 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005827
5828 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005829 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005830}
5831
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005832#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5833/** Recover a transaction that was interrupted by a power failure.
5834 *
5835 * This function is called during initialization, before psa_crypto_init()
5836 * returns. If this function returns a failure status, the initialization
5837 * fails.
5838 */
5839static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005840 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005841{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005843 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5844 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005845 /* TODO - fall through to the failure case until this
5846 * is implemented.
5847 * https://github.com/ARMmbed/mbed-crypto/issues/218
5848 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005849 default:
5850 /* We found an unsupported transaction in the storage.
5851 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005852 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005853 }
5854}
5855#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005857psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005859 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005860
Gilles Peskinec6b69072018-11-20 21:42:52 +01005861 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005862 if (global_data.initialized != 0) {
5863 return PSA_SUCCESS;
5864 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005865
Gilles Peskine30524eb2020-11-13 17:02:26 +01005866 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005867 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005868 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005869 status = mbedtls_psa_random_seed(&global_data.rng);
5870 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005872 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005873 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005875 status = psa_initialize_key_slots();
5876 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005877 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005878 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005879
Ronald Cron088d5d02021-04-10 16:57:30 +02005880 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005881 status = psa_driver_wrapper_init();
5882 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005883 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005884 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005885
Gilles Peskine4b734222019-07-24 15:56:31 +02005886#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005887 status = psa_crypto_load_transaction();
5888 if (status == PSA_SUCCESS) {
5889 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5890 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005891 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005892 }
5893 status = psa_crypto_stop_transaction();
5894 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005895 /* There's no transaction to complete. It's all good. */
5896 status = PSA_SUCCESS;
5897 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005898#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005899
Gilles Peskinec6b69072018-11-20 21:42:52 +01005900 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005901 global_data.initialized = 1;
5902
5903exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005904 if (status != PSA_SUCCESS) {
5905 mbedtls_psa_crypto_free();
5906 }
5907 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005908}
5909
David Horstmann114d8242023-12-07 18:39:17 +00005910/* Memory copying test hooks. These are called before input copy, after input
5911 * copy, before output copy and after output copy, respectively.
5912 * They are used by memory-poisoning tests to temporarily unpoison buffers
5913 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005914#if defined(MBEDTLS_TEST_HOOKS)
5915void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5916void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5917void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5918void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5919#endif
5920
David Horstmanndf493552023-11-15 15:18:30 +00005921/** Copy from an input buffer to a local copy.
5922 *
5923 * \param[in] input Pointer to input buffer.
5924 * \param[in] input_len Length of the input buffer.
5925 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5926 * \param[out] input_copy_len Length of the local copy buffer.
5927 * \return #PSA_SUCCESS, if the buffer was successfully
5928 * copied.
5929 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5930 * copy is too small to hold contents of the
5931 * input buffer.
5932 */
5933MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005934psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5935 uint8_t *input_copy, size_t input_copy_len)
5936{
5937 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005938 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005939 }
5940
David Horstmann0760b152023-11-24 16:21:04 +00005941#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005942 if (psa_input_pre_copy_hook != NULL) {
5943 psa_input_pre_copy_hook(input, input_len);
5944 }
David Horstmann0760b152023-11-24 16:21:04 +00005945#endif
5946
David Horstmann660027f2023-11-15 17:33:47 +00005947 if (input_len > 0) {
5948 memcpy(input_copy, input, input_len);
5949 }
David Horstmann957f9802023-10-30 20:29:43 +00005950
David Horstmann0760b152023-11-24 16:21:04 +00005951#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005952 if (psa_input_post_copy_hook != NULL) {
5953 psa_input_post_copy_hook(input, input_len);
5954 }
David Horstmann0760b152023-11-24 16:21:04 +00005955#endif
5956
David Horstmann957f9802023-10-30 20:29:43 +00005957 return PSA_SUCCESS;
5958}
5959
David Horstmanndf493552023-11-15 15:18:30 +00005960/** Copy from a local output buffer into a user-supplied one.
5961 *
5962 * \param[in] output_copy Pointer to a local buffer containing the output.
5963 * \param[in] output_copy_len Length of the local buffer.
5964 * \param[out] output Pointer to user-supplied output buffer.
5965 * \param[out] output_len Length of the user-supplied output buffer.
5966 * \return #PSA_SUCCESS, if the buffer was successfully
5967 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005968 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005969 * user-supplied output buffer is too small to
5970 * hold the contents of the local buffer.
5971 */
5972MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005973psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5974 uint8_t *output, size_t output_len)
5975{
5976 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005977 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005978 }
David Horstmann660027f2023-11-15 17:33:47 +00005979
David Horstmann0760b152023-11-24 16:21:04 +00005980#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005981 if (psa_output_pre_copy_hook != NULL) {
5982 psa_output_pre_copy_hook(output, output_len);
5983 }
David Horstmann0760b152023-11-24 16:21:04 +00005984#endif
5985
David Horstmann660027f2023-11-15 17:33:47 +00005986 if (output_copy_len > 0) {
5987 memcpy(output, output_copy, output_copy_len);
5988 }
5989
David Horstmann0760b152023-11-24 16:21:04 +00005990#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005991 if (psa_output_post_copy_hook != NULL) {
5992 psa_output_post_copy_hook(output, output_len);
5993 }
David Horstmann0760b152023-11-24 16:21:04 +00005994#endif
5995
David Horstmann2bd296e2023-10-30 20:37:12 +00005996 return PSA_SUCCESS;
5997}
5998
David Horstmann81a0d572023-11-20 17:15:32 +00005999psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
6000 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006001{
6002 psa_status_t status;
6003
David Horstmann0d52c712023-11-23 15:50:37 +00006004 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006005
David Horstmann726bf052023-11-15 18:11:26 +00006006 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006007 return PSA_SUCCESS;
6008 }
6009
David Horstmann81a0d572023-11-20 17:15:32 +00006010 local_input->buffer = mbedtls_calloc(input_len, 1);
6011 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006012 /* Since we dealt with the zero-length case above, we know that
6013 * a NULL return value means a failure of allocation. */
6014 return PSA_ERROR_INSUFFICIENT_MEMORY;
6015 }
David Horstmann81a0d572023-11-20 17:15:32 +00006016 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006017
David Horstmann81a0d572023-11-20 17:15:32 +00006018 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006019
6020 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00006021 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006022 if (status != PSA_SUCCESS) {
6023 goto error;
6024 }
6025
6026 return PSA_SUCCESS;
6027
6028error:
David Horstmann81a0d572023-11-20 17:15:32 +00006029 mbedtls_free(local_input->buffer);
6030 local_input->buffer = NULL;
6031 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00006032 return status;
6033}
6034
David Horstmann81a0d572023-11-20 17:15:32 +00006035void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00006036{
David Horstmann81a0d572023-11-20 17:15:32 +00006037 mbedtls_free(local_input->buffer);
6038 local_input->buffer = NULL;
6039 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00006040}
6041
David Horstmann1a76ab12023-11-20 12:54:09 +00006042psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
6043 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00006044{
David Horstmann0d52c712023-11-23 15:50:37 +00006045 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00006046
David Horstmann726bf052023-11-15 18:11:26 +00006047 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006048 return PSA_SUCCESS;
6049 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006050 local_output->buffer = mbedtls_calloc(output_len, 1);
6051 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00006052 /* Since we dealt with the zero-length case above, we know that
6053 * a NULL return value means a failure of allocation. */
6054 return PSA_ERROR_INSUFFICIENT_MEMORY;
6055 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006056 local_output->length = output_len;
6057 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00006058
6059 return PSA_SUCCESS;
6060}
6061
Gabor Mezei3b0c3712024-01-24 13:07:17 +01006062psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len,
6063 psa_crypto_local_output_t *local_output)
6064{
6065 psa_status_t status;
6066 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
6067
6068 if (output_len == 0) {
6069 return PSA_SUCCESS;
6070 }
6071 local_output->buffer = mbedtls_calloc(output_len, 1);
6072 if (local_output->buffer == NULL) {
6073 /* Since we dealt with the zero-length case above, we know that
6074 * a NULL return value means a failure of allocation. */
6075 return PSA_ERROR_INSUFFICIENT_MEMORY;
6076 }
6077 local_output->length = output_len;
6078 local_output->original = output;
6079
6080 status = psa_crypto_copy_input(output, output_len,
6081 local_output->buffer, local_output->length);
6082 if (status != PSA_SUCCESS) {
6083 goto error;
6084 }
6085
6086 return PSA_SUCCESS;
6087
6088error:
6089 mbedtls_free(local_output->buffer);
6090 local_output->buffer = NULL;
6091 local_output->length = 0;
6092 return status;
6093}
6094
David Horstmann1a76ab12023-11-20 12:54:09 +00006095psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00006096{
David Horstmann761761f2023-11-15 15:57:32 +00006097 psa_status_t status;
6098
David Horstmann1a76ab12023-11-20 12:54:09 +00006099 if (local_output->buffer == NULL) {
6100 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006101 return PSA_SUCCESS;
6102 }
David Horstmann1a76ab12023-11-20 12:54:09 +00006103 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00006104 /* We have an internal copy but nothing to copy back to. */
6105 return PSA_ERROR_CORRUPTION_DETECTED;
6106 }
6107
David Horstmann1a76ab12023-11-20 12:54:09 +00006108 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
6109 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00006110 if (status != PSA_SUCCESS) {
6111 return status;
6112 }
David Horstmannaeeb2742023-11-08 17:44:18 +00006113
David Horstmann1a76ab12023-11-20 12:54:09 +00006114 mbedtls_free(local_output->buffer);
6115 local_output->buffer = NULL;
6116 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00006117
6118 return PSA_SUCCESS;
6119}
6120
Gilles Peskinee59236f2018-01-27 23:32:46 +01006121#endif /* MBEDTLS_PSA_CRYPTO_C */