blob: 3e7fc9a1e4f827250ab22eb773cee4923d318696 [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 Everette3e760c2024-01-19 16:03:50 +00001526exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528
Ryan Everette3e760c2024-01-19 16:03:50 +00001529 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001531}
1532
Ronald Cron7285cda2020-11-26 14:46:37 +01001533psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001534 const psa_key_attributes_t *attributes,
1535 const uint8_t *key_buffer,
1536 size_t key_buffer_size,
1537 uint8_t *data,
1538 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001540{
Ronald Crond18b5f82020-11-25 16:46:05 +01001541 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1544 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001545 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 return psa_export_key_buffer_internal(
1547 key_buffer, key_buffer_size,
1548 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001549 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001553 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1554 return mbedtls_psa_rsa_export_public_key(attributes,
1555 key_buffer,
1556 key_buffer_size,
1557 data,
1558 data_size,
1559 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001560#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001563#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1564 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1568 return mbedtls_psa_ecp_export_public_key(attributes,
1569 key_buffer,
1570 key_buffer_size,
1571 data,
1572 data_size,
1573 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001574#else
1575 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001576 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001577#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1578 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001579 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001581 /* This shouldn't happen in the reference implementation, but
1582 it is valid for a special-purpose implementation to omit
1583 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001586}
Ronald Crond18b5f82020-11-25 16:46:05 +01001587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Ryan Everett30827912024-01-19 16:05:00 +00001589 uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 size_t data_size,
1591 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001592{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001594 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001595 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001596 psa_key_slot_t *slot;
Ryan Everett30827912024-01-19 16:05:00 +00001597 LOCAL_OUTPUT_DECLARE(data_external, data);
Darryl Greendd8fb772018-11-07 16:00:44 +00001598
Ronald Crone907e552021-01-18 13:22:38 +01001599 /* Reject a zero-length output buffer now, since this can never be a
1600 * valid key representation. This way we know that data must be a valid
1601 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 if (data_size == 0) {
1603 return PSA_ERROR_BUFFER_TOO_SMALL;
1604 }
Ronald Crone907e552021-01-18 13:22:38 +01001605
Darryl Greendd8fb772018-11-07 16:00:44 +00001606 /* Set the key to empty now, so that even when there are errors, we always
1607 * set data_length to a value between 0 and data_size. On error, setting
1608 * the key to empty is a good choice because an empty key representation is
1609 * unlikely to be accepted anywhere. */
1610 *data_length = 0;
1611
1612 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1614 if (status != PSA_SUCCESS) {
1615 return status;
1616 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001617
Ryan Everett30827912024-01-19 16:05:00 +00001618 LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
1619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1621 status = PSA_ERROR_INVALID_ARGUMENT;
1622 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001623 }
1624
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001625 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001626 .core = slot->attr
1627 };
Ronald Cron67227982020-11-26 15:16:05 +01001628 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001629 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001630 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001631
1632exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001634
Ryan Everett30827912024-01-19 16:05:00 +00001635 LOCAL_OUTPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001637}
1638
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001639MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1640 "One or more key attribute flag is listed as both external-only and dual-use")
1641MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1642 "One or more key attribute flag is listed as both internal-only and dual-use")
1643MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1644 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001645
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646/** Validate that a key policy is internally well-formed.
1647 *
1648 * This function only rejects invalid policies. It does not validate the
1649 * consistency of the policy with respect to other attributes of the key
1650 * such as the key type.
1651 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001652static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001653{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1655 PSA_KEY_USAGE_COPY |
1656 PSA_KEY_USAGE_ENCRYPT |
1657 PSA_KEY_USAGE_DECRYPT |
1658 PSA_KEY_USAGE_SIGN_MESSAGE |
1659 PSA_KEY_USAGE_VERIFY_MESSAGE |
1660 PSA_KEY_USAGE_SIGN_HASH |
1661 PSA_KEY_USAGE_VERIFY_HASH |
1662 PSA_KEY_USAGE_DERIVE)) != 0) {
1663 return PSA_ERROR_INVALID_ARGUMENT;
1664 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001666 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001667}
1668
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001669/** Validate the internal consistency of key attributes.
1670 *
1671 * This function only rejects invalid attribute values. If does not
1672 * validate the consistency of the attributes with any key data that may
1673 * be involved in the creation of the key.
1674 *
1675 * Call this function early in the key creation process.
1676 *
1677 * \param[in] attributes Key attributes for the new key.
1678 * \param[out] p_drv On any return, the driver for the key, if any.
1679 * NULL for a transparent key.
1680 *
1681 */
1682static psa_status_t psa_validate_key_attributes(
1683 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001685{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001686 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001687 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1688 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 status = psa_validate_key_location(lifetime, p_drv);
1691 if (status != PSA_SUCCESS) {
1692 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001693 }
1694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001695 status = psa_validate_key_persistence(lifetime);
1696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
1699
1700 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1701 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1702 return PSA_ERROR_INVALID_ARGUMENT;
1703 }
1704 } else {
1705 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1706 return PSA_ERROR_INVALID_ARGUMENT;
1707 }
1708 }
1709
1710 status = psa_validate_key_policy(&attributes->core.policy);
1711 if (status != PSA_SUCCESS) {
1712 return status;
1713 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001714
1715 /* Refuse to create overly large keys.
1716 * Note that this doesn't trigger on import if the attributes don't
1717 * explicitly specify a size (so psa_get_key_bits returns 0), so
1718 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1720 return PSA_ERROR_NOT_SUPPORTED;
1721 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001722
Gilles Peskine91e8c332019-08-02 19:19:39 +02001723 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1725 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1726 return PSA_ERROR_INVALID_ARGUMENT;
1727 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730}
1731
Gilles Peskine4747d192019-04-17 15:05:45 +02001732/** Prepare a key slot to receive key material.
1733 *
1734 * This function allocates a key slot and sets its metadata.
1735 *
1736 * If this function fails, call psa_fail_key_creation().
1737 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001738 * This function is intended to be used as follows:
1739 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001740 * it with the specified attributes, and in case of a volatile key assign it
1741 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * -# Populate the slot with the key material.
1743 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1744 * In case of failure at any step, stop the sequence and call
1745 * psa_fail_key_creation().
1746 *
Ronald Cron5c522922020-11-14 16:35:34 +01001747 * On success, the key slot is locked. It is the responsibility of the caller
1748 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001749 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001750 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 * \param[out] p_slot On success, a pointer to the prepared slot.
1753 * \param[out] p_drv On any return, the driver for the key, if any.
1754 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001755 *
1756 * \retval #PSA_SUCCESS
1757 * The key slot is ready to receive key material.
1758 * \return If this function fails, the key slot is an invalid state.
1759 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 */
1761static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001762 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001764 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001765 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001766{
1767 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001768 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 psa_key_slot_t *slot;
1770
Gilles Peskinedf179142019-07-15 22:02:14 +02001771 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001772 *p_drv = NULL;
1773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 status = psa_validate_key_attributes(attributes, p_drv);
1775 if (status != PSA_SUCCESS) {
1776 return status;
1777 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1780 if (status != PSA_SUCCESS) {
1781 return status;
1782 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 slot = *p_slot;
1784
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001785 /* We're storing the declared bit-size of the key. It's up to each
1786 * creation mechanism to verify that this information is correct.
1787 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001788 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001789 * is optional (import, copy). In case of a volatile key, assign it the
1790 * volatile key identifier associated to the slot returned to contain its
1791 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001792
1793 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001794 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001795#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1796 slot->attr.id = volatile_key_id;
1797#else
1798 slot->attr.id.key_id = volatile_key_id;
1799#endif
1800 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001801
Gilles Peskine91e8c332019-08-02 19:19:39 +02001802 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001803 * external-only flags, query `attributes`. Thanks to the check
1804 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001805 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001806 * may have set. */
1807 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001808
Gilles Peskinecbaff462019-07-12 23:46:04 +02001809#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001810 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001811 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001812 * create the key file in internal storage, create the
1813 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 * persistent data. This is done by starting a transaction that will
1815 * encompass these three actions.
1816 * For registering a volatile key, we just need to find an appropriate
1817 * slot number inside the SE. Since the key is designated volatile, creating
1818 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001819 /* The first thing to do is to find a slot number for the new key.
1820 * We save the slot number in persistent storage as part of the
1821 * transaction data. It will be needed to recover if the power
1822 * fails during the key creation process, to clean up on the secure
1823 * element side after restarting. Obtaining a slot number from the
1824 * secure element driver updates its persistent state, but we do not yet
1825 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001826 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001828 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1830 &slot_number);
1831 if (status != PSA_SUCCESS) {
1832 return status;
1833 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1836 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001837 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001838 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001839 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001840 status = psa_crypto_save_transaction();
1841 if (status != PSA_SUCCESS) {
1842 (void) psa_crypto_stop_transaction();
1843 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001844 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001845 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001846
1847 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001848 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001849 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001851 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001852 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001854 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001855#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001857 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001858}
Gilles Peskine4747d192019-04-17 15:05:45 +02001859
1860/** Finalize the creation of a key once its key material has been set.
1861 *
1862 * This entails writing the key to persistent storage.
1863 *
1864 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001865 * See the documentation of psa_start_key_creation() for the intended use
1866 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 *
Ronald Cron5c522922020-11-14 16:35:34 +01001868 * If the finalization succeeds, the function unlocks the key slot (it was
1869 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1870 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001871 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 * \param[in,out] slot Pointer to the slot with key material.
1873 * \param[in] driver The secure element driver for the key,
1874 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001875 * \param[out] key On success, identifier of the key. Note that the
1876 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001877 *
1878 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001879 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1881 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1882 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1883 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1884 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1885 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001886 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001887 * \return If this function fails, the key slot is an invalid state.
1888 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001890static psa_status_t psa_finish_key_creation(
1891 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001892 psa_se_drv_table_entry_t *driver,
1893 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
1895 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001896 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001898
1899#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001901#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001903 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001904 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001905 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001906
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001907 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1908 sizeof(data.slot_number),
1909 "Slot number size does not match psa_se_key_data_storage_t");
1910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1912 status = psa_save_persistent_key(&slot->attr,
1913 (uint8_t *) &data,
1914 sizeof(data));
1915 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001916#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1917 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001918 /* Key material is saved in export representation in the slot, so
1919 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920 status = psa_save_persistent_key(&slot->attr,
1921 slot->key.data,
1922 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001923 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001924 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001925#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1926
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001928 /* Finish the transaction for a key creation. This does not
1929 * happen when registering an existing key. Detect this case
1930 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001931 * creation of a persistent key in a secure element requires a transaction,
1932 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 if (driver != NULL &&
1934 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1935 status = psa_save_se_persistent_data(driver);
1936 if (status != PSA_SUCCESS) {
1937 psa_destroy_persistent_key(slot->attr.id);
1938 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001941 }
1942#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001945 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 status = psa_unlock_key_slot(slot);
1947 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001948 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001950 }
Ronald Cron50972942020-11-14 11:28:25 +01001951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001953}
1954
1955/** Abort the creation of a key.
1956 *
1957 * You may call this function after calling psa_start_key_creation(),
1958 * or after psa_finish_key_creation() fails. In other circumstances, this
1959 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001960 * See the documentation of psa_start_key_creation() for the intended use
1961 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001962 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001963 * \param[in,out] slot Pointer to the slot with key material.
1964 * \param[in] driver The secure element driver for the key,
1965 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967static void psa_fail_key_creation(psa_key_slot_t *slot,
1968 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001969{
Gilles Peskine011e4282019-06-26 18:34:38 +02001970 (void) driver;
1971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001975
1976#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001977 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001978 * element, and the failure happened later (when saving metadata
1979 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001980 * element.
1981 * https://github.com/ARMmbed/mbed-crypto/issues/217
1982 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001983
Gilles Peskined7729582019-08-05 15:55:54 +02001984 /* Abort the ongoing transaction if any (there may not be one if
1985 * the creation process failed before starting one, or if the
1986 * key creation is a registration of a key in a secure element).
1987 * Earlier functions must already have done what it takes to undo any
1988 * partial creation. All that's left is to update the transaction data
1989 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001990 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001991#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001993 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001994}
1995
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001996/** Validate optional attributes during key creation.
1997 *
1998 * Some key attributes are optional during key creation. If they are
1999 * specified in the attributes structure, check that they are consistent
2000 * with the data in the slot.
2001 *
2002 * This function should be called near the end of key creation, after
2003 * the slot in memory is fully populated but before saving persistent data.
2004 */
2005static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 if (attributes->core.type != 0) {
2010 if (attributes->core.type != slot->attr.type) {
2011 return PSA_ERROR_INVALID_ARGUMENT;
2012 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 }
2014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07002016#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2018 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002019 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002020 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002022
Ronald Cron90857082020-11-25 14:58:33 +01002023 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 slot->attr.type,
2025 slot->key.data,
2026 slot->key.bytes,
2027 &rsa);
2028 if (status != PSA_SUCCESS) {
2029 return status;
2030 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002032 mbedtls_mpi_init(&actual);
2033 mbedtls_mpi_init(&required);
2034 ret = mbedtls_rsa_export(rsa,
2035 NULL, NULL, NULL, NULL, &actual);
2036 mbedtls_rsa_free(rsa);
2037 mbedtls_free(rsa);
2038 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 }
2041 ret = mbedtls_mpi_read_binary(&required,
2042 attributes->domain_parameters,
2043 attributes->domain_parameters_size);
2044 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 }
2047 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 }
2050rsa_exit:
2051 mbedtls_mpi_free(&actual);
2052 mbedtls_mpi_free(&required);
2053 if (ret != 0) {
2054 return mbedtls_to_psa_error(ret);
2055 }
2056 } else
John Durkop9814fa22020-11-04 12:28:15 -08002057#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2058 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002059 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002061 }
2062 }
2063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002064 if (attributes->core.bits != 0) {
2065 if (attributes->core.bits != slot->attr.bits) {
2066 return PSA_ERROR_INVALID_ARGUMENT;
2067 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068 }
2069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002070 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071}
2072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002073psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Ryan Everett6b970252024-01-19 16:02:59 +00002074 const uint8_t *data_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002075 size_t data_length,
2076 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002077{
2078 psa_status_t status;
Ryan Everett6b970252024-01-19 16:02:59 +00002079 LOCAL_INPUT_DECLARE(data_external, data);
Gilles Peskine4747d192019-04-17 15:05:45 +02002080 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002081 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002082 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083
Ronald Cron81709fc2020-11-14 12:10:32 +01002084 *key = MBEDTLS_SVC_KEY_ID_INIT;
2085
Gilles Peskine0f84d622019-09-12 19:03:13 +02002086 /* Reject zero-length symmetric keys (including raw data key objects).
2087 * This also rejects any key which might be encoded as an empty string,
2088 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 if (data_length == 0) {
2090 return PSA_ERROR_INVALID_ARGUMENT;
2091 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002092
Ryan Everett6b970252024-01-19 16:02:59 +00002093 LOCAL_INPUT_ALLOC(data_external, data_length, data);
2094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2096 &slot, &driver);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002101 /* In the case of a transparent key or an opaque key stored in local
2102 * storage (thus not in the case of generating a key in a secure element
2103 * or cryptoprocessor with storage), we have to allocate a buffer to
2104 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105 if (slot->key.data == NULL) {
2106 status = psa_allocate_buffer_to_slot(slot, data_length);
2107 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002108 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002110 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002111
2112 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 status = psa_driver_wrapper_import_key(attributes,
2114 data, data_length,
2115 slot->key.data,
2116 slot->key.bytes,
2117 &slot->key.bytes, &bits);
2118 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002123 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002125 status = PSA_ERROR_INVALID_ARGUMENT;
2126 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002127 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 status = psa_validate_optional_attributes(slot, attributes);
2130 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002131 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002135exit:
Ryan Everett6b970252024-01-19 16:02:59 +00002136 LOCAL_INPUT_FREE(data_external, data);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 if (status != PSA_SUCCESS) {
2138 psa_fail_key_creation(slot, driver);
2139 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002142}
2143
Gilles Peskined7729582019-08-05 15:55:54 +02002144#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2145psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002147{
2148 psa_status_t status;
2149 psa_key_slot_t *slot = NULL;
2150 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002152
2153 /* Leaving attributes unspecified is not currently supported.
2154 * It could make sense to query the key type and size from the
2155 * secure element, but not all secure elements support this
2156 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002157 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2158 return PSA_ERROR_NOT_SUPPORTED;
2159 }
2160 if (psa_get_key_bits(attributes) == 0) {
2161 return PSA_ERROR_NOT_SUPPORTED;
2162 }
Gilles Peskined7729582019-08-05 15:55:54 +02002163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2165 &slot, &driver);
2166 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002167 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 }
Gilles Peskined7729582019-08-05 15:55:54 +02002169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002170 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002171
2172exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 if (status != PSA_SUCCESS) {
2174 psa_fail_key_creation(slot, driver);
2175 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002176
Gilles Peskined7729582019-08-05 15:55:54 +02002177 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 psa_close_key(key);
2179 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002180}
2181#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2184 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002185{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002186 psa_status_t status = psa_copy_key_material_into_slot(target,
2187 source->key.data,
2188 source->key.bytes);
2189 if (status != PSA_SUCCESS) {
2190 return status;
2191 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002192
Steven Cooreman398aee52020-10-13 14:35:45 +02002193 target->attr.type = source->attr.type;
2194 target->attr.bits = source->attr.bits;
2195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002196 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002197}
2198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2200 const psa_key_attributes_t *specified_attributes,
2201 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002202{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002204 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002205 psa_key_slot_t *source_slot = NULL;
2206 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002208 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209
Ronald Cron81709fc2020-11-14 12:10:32 +01002210 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2211
Ronald Cron5c522922020-11-14 16:35:34 +01002212 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2214 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 status = psa_validate_optional_attributes(source_slot,
2219 specified_attributes);
2220 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002221 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 status = psa_restrict_key_policy(source_slot->attr.type,
2225 &actual_attributes.core.policy,
2226 &source_slot->attr.policy);
2227 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2232 &target_slot, &driver);
2233 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002236
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002239 /* Copying to a secure element is not implemented yet. */
2240 status = PSA_ERROR_NOT_SUPPORTED;
2241 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002242 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002246 /*
2247 * Copying through an opaque driver is not implemented yet, consider
2248 * a lifetime with an external location as an invalid parameter for
2249 * now.
2250 */
2251 status = PSA_ERROR_INVALID_ARGUMENT;
2252 goto exit;
2253 }
2254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 status = psa_copy_key_material(source_slot, target_slot);
2256 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002257 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002261exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 if (status != PSA_SUCCESS) {
2263 psa_fail_key_creation(target_slot, driver);
2264 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002269}
2270
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002271
2272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002273/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002274/* Message digests */
2275/****************************************************************/
2276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002279 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280 if (operation->id == 0) {
2281 return PSA_SUCCESS;
2282 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002285 operation->id = 0;
2286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288}
2289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2291 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002294
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002295 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002297 status = PSA_ERROR_BAD_STATE;
2298 goto exit;
2299 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002301 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002302 status = PSA_ERROR_INVALID_ARGUMENT;
2303 goto exit;
2304 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002305
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002306 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2307 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002310 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002311
2312exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 if (status != PSA_SUCCESS) {
2314 psa_hash_abort(operation);
2315 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002316
2317 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318}
2319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2321 const uint8_t *input,
2322 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002327 status = PSA_ERROR_BAD_STATE;
2328 goto exit;
2329 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002330
Steven Cooremanc8288352021-03-04 14:02:19 +01002331 /* Don't require hash implementations to behave correctly on a
2332 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333 if (input_length == 0) {
2334 return PSA_SUCCESS;
2335 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002338
2339exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 if (status != PSA_SUCCESS) {
2341 psa_hash_abort(operation);
2342 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345}
2346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2348 uint8_t *hash,
2349 size_t hash_size,
2350 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002352 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353 if (operation->id == 0) {
2354 return PSA_ERROR_BAD_STATE;
2355 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356
Steven Cooreman1e582352021-02-18 17:24:37 +01002357 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002358 operation, hash, hash_size, hash_length);
2359 psa_hash_abort(operation);
2360 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361}
2362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2364 const uint8_t *hash,
2365 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002366{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002367 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002369 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002370 operation,
2371 actual_hash, sizeof(actual_hash),
2372 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002375 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002379 status = PSA_ERROR_INVALID_SIGNATURE;
2380 goto exit;
2381 }
2382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002384 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002386
2387exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2389 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002390 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394}
2395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396psa_status_t psa_hash_compute(psa_algorithm_t alg,
2397 const uint8_t *input, size_t input_length,
2398 uint8_t *hash, size_t hash_size,
2399 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002400{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002401 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402 if (!PSA_ALG_IS_HASH(alg)) {
2403 return PSA_ERROR_INVALID_ARGUMENT;
2404 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2407 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002408}
2409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410psa_status_t psa_hash_compare(psa_algorithm_t alg,
2411 const uint8_t *input, size_t input_length,
2412 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002413{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002414 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002415 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 if (!PSA_ALG_IS_HASH(alg)) {
2418 return PSA_ERROR_INVALID_ARGUMENT;
2419 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002420
Steven Cooreman1e582352021-02-18 17:24:37 +01002421 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 alg, input, input_length,
2423 actual_hash, sizeof(actual_hash),
2424 &actual_hash_length);
2425 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002426 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 }
2428 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002429 status = PSA_ERROR_INVALID_SIGNATURE;
2430 goto exit;
2431 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002433 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002435
2436exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2438 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002439}
2440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2442 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002443{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002444 if (source_operation->id == 0 ||
2445 target_operation->id != 0) {
2446 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002447 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002449 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2450 target_operation);
2451 if (status != PSA_SUCCESS) {
2452 psa_hash_abort(target_operation);
2453 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002456}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002457
2458
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460/* MAC */
2461/****************************************************************/
2462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464{
Steven Cooreman07897832021-03-19 18:28:56 +01002465 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (operation->id == 0) {
2467 return PSA_SUCCESS;
2468 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002471 operation->mac_size = 0;
2472 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002473 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476}
2477
Ronald Cron1c650a12021-06-17 16:33:22 +02002478static psa_status_t psa_mac_finalize_alg_and_key_validation(
2479 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002480 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 psa_key_type_t key_type = psa_get_key_type(attributes);
2485 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 if (!PSA_ALG_IS_MAC(alg)) {
2488 return PSA_ERROR_INVALID_ARGUMENT;
2489 }
Steven Cooreman07897832021-03-19 18:28:56 +01002490
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002491 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 status = psa_mac_key_can_do(alg, key_type);
2493 if (status != PSA_SUCCESS) {
2494 return status;
2495 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002496
Steven Cooremana6474de2021-05-10 11:13:41 +02002497 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002501 /* A very short MAC is too short for security since it can be
2502 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2503 * so we make this our minimum, even though 32 bits is still
2504 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002505 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002506 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2509 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002510 /* It's impossible to "truncate" to a larger length than the full length
2511 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002513 }
2514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002516 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2517 * that is disabled in the compile-time configuration. The result can
2518 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2519 * configuration into account. In this case, force a return of
2520 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2521 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2522 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2523 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2524 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002526 }
2527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002529}
2530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2532 mbedtls_svc_key_id_t key,
2533 psa_algorithm_t alg,
2534 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002535{
2536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2537 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002538 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002539 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002540
2541 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002543 status = PSA_ERROR_BAD_STATE;
2544 goto exit;
2545 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002546
2547 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548 key,
2549 &slot,
2550 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2551 alg);
2552 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002553 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002555
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002556 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002557 .core = slot->attr
2558 };
2559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002560 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2561 &operation->mac_size);
2562 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002563 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002565
2566 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002567 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568 if (is_sign) {
2569 status = psa_driver_wrapper_mac_sign_setup(operation,
2570 &attributes,
2571 slot->key.data,
2572 slot->key.bytes,
2573 alg);
2574 } else {
2575 status = psa_driver_wrapper_mac_verify_setup(operation,
2576 &attributes,
2577 slot->key.data,
2578 slot->key.bytes,
2579 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002581
Gilles Peskinefbfac682018-07-08 20:51:54 +02002582exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 if (status != PSA_SUCCESS) {
2584 psa_mac_abort(operation);
2585 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002589 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590}
2591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2593 mbedtls_svc_key_id_t key,
2594 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002595{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002597}
2598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002599psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2600 mbedtls_svc_key_id_t key,
2601 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002602{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002604}
2605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002606psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2607 const uint8_t *input,
2608 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 if (operation->id == 0) {
2611 return PSA_ERROR_BAD_STATE;
2612 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613
Steven Cooreman11743f92021-03-19 18:38:46 +01002614 /* Don't require hash implementations to behave correctly on a
2615 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 if (input_length == 0) {
2617 return PSA_SUCCESS;
2618 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2621 input, input_length);
2622 if (status != PSA_SUCCESS) {
2623 psa_mac_abort(operation);
2624 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002626 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627}
2628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002629psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2630 uint8_t *mac,
2631 size_t mac_size,
2632 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002633{
Steven Cooreman87885df2021-03-19 19:04:39 +01002634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2635 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002638 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002639 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002640 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002643 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002644 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002645 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002646
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002647 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2648 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002650 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002651 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002652 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002655 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002656 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002657 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002659 status = psa_driver_wrapper_mac_sign_finish(operation,
2660 mac, operation->mac_size,
2661 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002662
Dave Rodgman478ab542021-06-25 09:09:02 +01002663exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002664 /* In case of success, set the potential excess room in the output buffer
2665 * to an invalid value, to avoid potentially leaking a longer MAC.
2666 * In case of error, set the output length and content to a safe default,
2667 * such that in case the caller misses an error check, the output would be
2668 * an unachievable MAC.
2669 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002671 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002672 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002673 }
mohammad16036df908f2018-04-02 08:34:15 -07002674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 if (mac_size > operation->mac_size) {
2676 memset(&mac[operation->mac_size], '!',
2677 mac_size - operation->mac_size);
2678 }
Ronald Cron882eb782021-06-18 13:05:48 +02002679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002683}
2684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2686 const uint8_t *mac,
2687 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688{
Steven Cooreman87885df2021-03-19 19:04:39 +01002689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2690 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002693 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002694 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002695 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002698 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002699 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002700 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002703 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002704 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002705 }
2706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 status = psa_driver_wrapper_mac_verify_finish(operation,
2708 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002709
Dave Rodgman478ab542021-06-25 09:09:02 +01002710exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714}
2715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2717 psa_algorithm_t alg,
2718 const uint8_t *input,
2719 size_t input_length,
2720 uint8_t *mac,
2721 size_t mac_size,
2722 size_t *mac_length,
2723 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002724{
2725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002726 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002727 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002728 psa_key_slot_t *slot;
2729 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730
Ronald Crondbb86462021-06-17 17:17:20 +02002731 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 key,
2733 &slot,
2734 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2735 alg);
2736 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002737 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002739
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002740 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002741 .core = slot->attr
2742 };
2743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2745 &operation_mac_size);
2746 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002747 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002751 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002752 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002753 }
2754
2755 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 &attributes,
2757 slot->key.data, slot->key.bytes,
2758 alg,
2759 input, input_length,
2760 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002761
2762exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002763 /* In case of success, set the potential excess room in the output buffer
2764 * to an invalid value, to avoid potentially leaking a longer MAC.
2765 * In case of error, set the output length and content to a safe default,
2766 * such that in case the caller misses an error check, the output would be
2767 * an unachievable MAC.
2768 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002770 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002771 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002772 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002773 if (mac_size > operation_mac_size) {
2774 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2775 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002780}
2781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002783 psa_algorithm_t alg,
2784 const uint8_t *input,
2785 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 uint8_t *mac,
2787 size_t mac_size,
2788 size_t *mac_length)
2789{
2790 return psa_mac_compute_internal(key, alg,
2791 input, input_length,
2792 mac, mac_size, mac_length, 1);
2793}
2794
2795psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2796 psa_algorithm_t alg,
2797 const uint8_t *input,
2798 size_t input_length,
2799 const uint8_t *mac,
2800 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002801{
2802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002803 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2804 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002806 status = psa_mac_compute_internal(key, alg,
2807 input, input_length,
2808 actual_mac, sizeof(actual_mac),
2809 &actual_mac_length, 0);
2810 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002811 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002815 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002816 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002817 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002819 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002820 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002821 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002822
2823exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002827}
Gilles Peskine20035e32018-02-03 22:44:14 +01002828
Gilles Peskine20035e32018-02-03 22:44:14 +01002829/****************************************************************/
2830/* Asymmetric cryptography */
2831/****************************************************************/
2832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002833static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2834 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002835{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 if (input_is_message) {
2837 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2838 return PSA_ERROR_INVALID_ARGUMENT;
2839 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2842 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2843 return PSA_ERROR_INVALID_ARGUMENT;
2844 }
2845 }
2846 } else {
2847 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2848 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002849 }
2850 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002853}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002855static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2856 int input_is_message,
2857 psa_algorithm_t alg,
2858 const uint8_t *input,
2859 size_t input_length,
2860 uint8_t *signature,
2861 size_t signature_size,
2862 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002863{
2864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2865 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002866 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002867 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002868
2869 *signature_length = 0;
2870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 status = psa_sign_verify_check_alg(input_is_message, alg);
2872 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002873 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002875
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002876 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002877 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002878 * buffer can in principle be empty since it doesn't actually have
2879 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 if (signature_size == 0) {
2881 return PSA_ERROR_BUFFER_TOO_SMALL;
2882 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002883
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002884 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002885 key, &slot,
2886 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2887 PSA_KEY_USAGE_SIGN_HASH,
2888 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002891 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002895 status = PSA_ERROR_INVALID_ARGUMENT;
2896 goto exit;
2897 }
2898
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002899 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002901 };
2902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002904 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002905 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002906 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 signature, signature_size, signature_length);
2908 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002909
2910 status = psa_driver_wrapper_sign_hash(
2911 &attributes, slot->key.data, slot->key.bytes,
2912 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002914 }
2915
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002916
2917exit:
2918 /* Fill the unused part of the output buffer (the whole buffer on error,
2919 * the trailing part on success) with something that isn't a valid signature
2920 * (barring an attack on the signature and deliberately-crafted input),
2921 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 if (status == PSA_SUCCESS) {
2923 memset(signature + *signature_length, '!',
2924 signature_size - *signature_length);
2925 } else {
2926 memset(signature, '!', signature_size);
2927 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002928 /* If signature_size is 0 then we have nothing to do. We must not call
2929 * memset because signature may be NULL in this case. */
2930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002933 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002934}
2935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2937 int input_is_message,
2938 psa_algorithm_t alg,
2939 const uint8_t *input,
2940 size_t input_length,
2941 const uint8_t *signature,
2942 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002943{
2944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2945 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2946 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 status = psa_sign_verify_check_alg(input_is_message, alg);
2949 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002950 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002952
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002953 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002954 key, &slot,
2955 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2956 PSA_KEY_USAGE_VERIFY_HASH,
2957 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 if (status != PSA_SUCCESS) {
2960 return status;
2961 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002962
2963 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002965 };
2966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002968 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002969 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002970 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 signature, signature_length);
2972 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002973 status = psa_driver_wrapper_verify_hash(
2974 &attributes, slot->key.data, slot->key.bytes,
2975 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002977 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002982
2983}
2984
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002985psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002986 const psa_key_attributes_t *attributes,
2987 const uint8_t *key_buffer,
2988 size_t key_buffer_size,
2989 psa_algorithm_t alg,
2990 const uint8_t *input,
2991 size_t input_length,
2992 uint8_t *signature,
2993 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002995{
2996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002998 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002999 size_t hash_length;
3000 uint8_t hash[PSA_HASH_MAX_SIZE];
3001
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003002 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003 PSA_ALG_SIGN_GET_HASH(alg),
3004 input, input_length,
3005 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003008 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003010
3011 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003012 attributes, key_buffer, key_buffer_size,
3013 alg, hash, hash_length,
3014 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003015 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003018}
3019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3021 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003022 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003024 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 size_t signature_size,
3026 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003027{
Thomas Daubney290aac42024-01-18 17:23:02 +00003028 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3029 LOCAL_INPUT_DECLARE(input_external, input);
3030 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3031
3032 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3033 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3034 status = psa_sign_internal(key, 1, alg, input, input_length, signature,
3035 signature_size, signature_length);
3036
Thomas Daubneyf446b892024-01-30 13:36:01 +00003037#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003038exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003039#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003040 LOCAL_INPUT_FREE(input_external, input);
3041 LOCAL_OUTPUT_FREE(signature_external, signature);
3042 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003043}
3044
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003045psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003046 const psa_key_attributes_t *attributes,
3047 const uint8_t *key_buffer,
3048 size_t key_buffer_size,
3049 psa_algorithm_t alg,
3050 const uint8_t *input,
3051 size_t input_length,
3052 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003054{
3055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003057 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003058 size_t hash_length;
3059 uint8_t hash[PSA_HASH_MAX_SIZE];
3060
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003061 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 PSA_ALG_SIGN_GET_HASH(alg),
3063 input, input_length,
3064 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003067 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02003069
3070 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003071 attributes, key_buffer, key_buffer_size,
3072 alg, hash, hash_length,
3073 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02003074 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02003077}
3078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003079psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3080 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003081 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 size_t input_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003083 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003084 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003085{
Thomas Daubney290aac42024-01-18 17:23:02 +00003086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3087 LOCAL_INPUT_DECLARE(input_external, input);
3088 LOCAL_INPUT_DECLARE(signature_external, signature);
3089
3090 LOCAL_INPUT_ALLOC(input_external, input_length, input);
3091 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3092 status = psa_verify_internal(key, 1, alg, input, input_length, signature,
3093 signature_length);
3094
Thomas Daubneyf446b892024-01-30 13:36:01 +00003095#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003096exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003097#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003098 LOCAL_INPUT_FREE(input_external, input);
3099 LOCAL_INPUT_FREE(signature_external, signature);
3100
3101 return status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003102}
3103
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003104psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003105 const psa_key_attributes_t *attributes,
3106 const uint8_t *key_buffer, size_t key_buffer_size,
3107 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01003109{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3111 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3112 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003113#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3115 return mbedtls_psa_rsa_sign_hash(
3116 attributes,
3117 key_buffer, key_buffer_size,
3118 alg, hash, hash_length,
3119 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003120#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3121 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 } else {
3123 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003124 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003125 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3126 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003127#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3129 return mbedtls_psa_ecdsa_sign_hash(
3130 attributes,
3131 key_buffer, key_buffer_size,
3132 alg, hash, hash_length,
3133 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003134#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3135 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003136 } else {
3137 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003138 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003139 }
Ronald Cron4501c982021-03-19 20:09:32 +01003140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141 (void) key_buffer;
3142 (void) key_buffer_size;
3143 (void) hash;
3144 (void) hash_length;
3145 (void) signature;
3146 (void) signature_size;
3147 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003150}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3153 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003154 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003156 uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 size_t signature_size,
3158 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003159{
Thomas Daubney290aac42024-01-18 17:23:02 +00003160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3161 LOCAL_INPUT_DECLARE(hash_external, hash);
3162 LOCAL_OUTPUT_DECLARE(signature_external, signature);
3163
3164 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3165 LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature);
3166 status = psa_sign_internal(key, 0, alg, hash, hash_length, signature,
3167 signature_size, signature_length);
3168
Thomas Daubneyf446b892024-01-30 13:36:01 +00003169#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003170exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003171#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003172 LOCAL_INPUT_FREE(hash_external, hash);
3173 LOCAL_OUTPUT_FREE(signature_external, signature);
3174
3175 return status;
itayzafrir5c753392018-05-08 11:18:38 +03003176}
3177
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003178psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003179 const psa_key_attributes_t *attributes,
3180 const uint8_t *key_buffer, size_t key_buffer_size,
3181 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003182 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003183{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3185 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3186 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003187#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003188 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3189 return mbedtls_psa_rsa_verify_hash(
3190 attributes,
3191 key_buffer, key_buffer_size,
3192 alg, hash, hash_length,
3193 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003194#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3195 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 } else {
3197 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003198 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003199 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3200 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003201#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3203 return mbedtls_psa_ecdsa_verify_hash(
3204 attributes,
3205 key_buffer, key_buffer_size,
3206 alg, hash, hash_length,
3207 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003208#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3209 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003210 } else {
3211 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003212 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003213 }
Ronald Cron4501c982021-03-19 20:09:32 +01003214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003215 (void) key_buffer;
3216 (void) key_buffer_size;
3217 (void) hash;
3218 (void) hash_length;
3219 (void) signature;
3220 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003223}
3224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3226 psa_algorithm_t alg,
Thomas Daubney290aac42024-01-18 17:23:02 +00003227 const uint8_t *hash_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 size_t hash_length,
Thomas Daubney290aac42024-01-18 17:23:02 +00003229 const uint8_t *signature_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003231{
Thomas Daubney290aac42024-01-18 17:23:02 +00003232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3233 LOCAL_INPUT_DECLARE(hash_external, hash);
3234 LOCAL_INPUT_DECLARE(signature_external, signature);
3235
3236 LOCAL_INPUT_ALLOC(hash_external, hash_length, hash);
3237 LOCAL_INPUT_ALLOC(signature_external, signature_length, signature);
3238 status = psa_verify_internal(key, 0, alg, hash, hash_length, signature,
3239 signature_length);
3240
Thomas Daubneyf446b892024-01-30 13:36:01 +00003241#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
Thomas Daubney290aac42024-01-18 17:23:02 +00003242exit:
Thomas Daubneyf446b892024-01-30 13:36:01 +00003243#endif
Thomas Daubney290aac42024-01-18 17:23:02 +00003244 LOCAL_INPUT_FREE(hash_external, hash);
3245 LOCAL_INPUT_FREE(signature_external, signature);
3246
3247 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003248}
3249
John Durkop0e005192020-10-31 22:06:54 -07003250#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003251static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3252 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003253{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3255 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3256 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3257 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003258}
John Durkop0e005192020-10-31 22:06:54 -07003259#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3262 psa_algorithm_t alg,
3263 const uint8_t *input,
3264 size_t input_length,
3265 const uint8_t *salt,
3266 size_t salt_length,
3267 uint8_t *output,
3268 size_t output_size,
3269 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003272 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003273 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003274
Darryl Green5cc689a2018-07-24 15:34:10 +01003275 (void) input;
3276 (void) input_length;
3277 (void) salt;
3278 (void) output;
3279 (void) output_size;
3280
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003281 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3284 return PSA_ERROR_INVALID_ARGUMENT;
3285 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003286
Ronald Cron5c522922020-11-14 16:35:34 +01003287 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3289 if (status != PSA_SUCCESS) {
3290 return status;
3291 }
3292 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3293 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003294 status = PSA_ERROR_INVALID_ARGUMENT;
3295 goto exit;
3296 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003298 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003299#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003301 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3303 slot->key.data,
3304 slot->key.bytes,
3305 &rsa);
3306 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003307 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003308 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003310 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003311 status = PSA_ERROR_BUFFER_TOO_SMALL;
3312 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003313 }
Ronald Cron7207d572021-09-08 14:28:35 +02003314#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3315 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003317#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003318 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 mbedtls_rsa_pkcs1_encrypt(rsa,
3320 mbedtls_psa_get_random,
3321 MBEDTLS_PSA_RANDOM_STATE,
3322 MBEDTLS_RSA_PUBLIC,
3323 input_length,
3324 input,
3325 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003326#else
3327 status = PSA_ERROR_NOT_SUPPORTED;
3328#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 } else
3330 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003331#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003333 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3335 mbedtls_psa_get_random,
3336 MBEDTLS_PSA_RANDOM_STATE,
3337 MBEDTLS_RSA_PUBLIC,
3338 salt, salt_length,
3339 input_length,
3340 input,
3341 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003342#else
3343 status = PSA_ERROR_NOT_SUPPORTED;
3344#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003346 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347 }
Ronald Cron7207d572021-09-08 14:28:35 +02003348#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003350rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 if (status == PSA_SUCCESS) {
3352 *output_length = mbedtls_rsa_get_len(rsa);
3353 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 mbedtls_rsa_free(rsa);
3356 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003357#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003358 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003360 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003361 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362
3363exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003364 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003367}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3370 psa_algorithm_t alg,
3371 const uint8_t *input,
3372 size_t input_length,
3373 const uint8_t *salt,
3374 size_t salt_length,
3375 uint8_t *output,
3376 size_t output_size,
3377 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003380 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003381 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003382
Darryl Green5cc689a2018-07-24 15:34:10 +01003383 (void) input;
3384 (void) input_length;
3385 (void) salt;
3386 (void) output;
3387 (void) output_size;
3388
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003389 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003391 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3392 return PSA_ERROR_INVALID_ARGUMENT;
3393 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003394
Ronald Cron5c522922020-11-14 16:35:34 +01003395 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3397 if (status != PSA_SUCCESS) {
3398 return status;
3399 }
3400 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003401 status = PSA_ERROR_INVALID_ARGUMENT;
3402 goto exit;
3403 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003406#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003408 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3410 slot->key.data,
3411 slot->key.bytes,
3412 &rsa);
3413 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003414 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003418 status = PSA_ERROR_INVALID_ARGUMENT;
3419 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003420 }
Ronald Cron7207d572021-09-08 14:28:35 +02003421#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3422 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003425#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003426 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 mbedtls_rsa_pkcs1_decrypt(rsa,
3428 mbedtls_psa_get_random,
3429 MBEDTLS_PSA_RANDOM_STATE,
3430 MBEDTLS_RSA_PRIVATE,
3431 output_length,
3432 input,
3433 output,
3434 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003435#else
3436 status = PSA_ERROR_NOT_SUPPORTED;
3437#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 } else
3439 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003440#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003442 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3444 mbedtls_psa_get_random,
3445 MBEDTLS_PSA_RANDOM_STATE,
3446 MBEDTLS_RSA_PRIVATE,
3447 salt, salt_length,
3448 output_length,
3449 input,
3450 output,
3451 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003452#else
3453 status = PSA_ERROR_NOT_SUPPORTED;
3454#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003456 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003457 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003458
Ronald Cron7207d572021-09-08 14:28:35 +02003459#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003461rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 mbedtls_rsa_free(rsa);
3463 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003464#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3465 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003467 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003469
3470exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003474}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003475
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003476
3477
mohammad1603503973b2018-03-12 15:59:30 +02003478/****************************************************************/
3479/* Symmetric cryptography */
3480/****************************************************************/
3481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3483 mbedtls_svc_key_id_t key,
3484 psa_algorithm_t alg,
3485 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003486{
3487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3488 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003489 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003490 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3492 PSA_KEY_USAGE_ENCRYPT :
3493 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003494
3495 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003497 status = PSA_ERROR_BAD_STATE;
3498 goto exit;
3499 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003502 status = PSA_ERROR_INVALID_ARGUMENT;
3503 goto exit;
3504 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3507 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003508 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003510
Ronald Cron49fafa92021-03-10 08:34:23 +01003511 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003512 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003513 * so we only set it (in the driver wrapper) after resources have been
3514 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003515 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003517 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003519 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003521 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003522 }
3523 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003524
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003525 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003527 };
3528
Ronald Cronab99ac22020-10-01 16:38:28 +02003529 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530 if (cipher_operation == MBEDTLS_ENCRYPT) {
3531 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3532 &attributes,
3533 slot->key.data,
3534 slot->key.bytes,
3535 alg);
3536 } else {
3537 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3538 &attributes,
3539 slot->key.data,
3540 slot->key.bytes,
3541 alg);
3542 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003543
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003544exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 if (status != PSA_SUCCESS) {
3546 psa_cipher_abort(operation);
3547 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003552}
3553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003554psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3555 mbedtls_svc_key_id_t key,
3556 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003557{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003559}
3560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3562 mbedtls_svc_key_id_t key,
3563 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003564{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003565 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003566}
3567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3569 uint8_t *iv,
3570 size_t iv_size,
3571 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003572{
Ronald Cron6d051732020-10-01 14:10:20 +02003573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003574 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003575 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003577 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003578 status = PSA_ERROR_BAD_STATE;
3579 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003580 }
3581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003583 status = PSA_ERROR_BAD_STATE;
3584 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003585 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003586
Ronald Cronc423acb2021-07-05 12:31:44 +02003587 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003588 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003589 status = PSA_ERROR_BUFFER_TOO_SMALL;
3590 goto exit;
3591 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003594 status = PSA_ERROR_GENERIC_ERROR;
3595 goto exit;
3596 }
3597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003598 status = psa_generate_random(local_iv, default_iv_length);
3599 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003600 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003601 }
Ronald Cron5618a392021-03-26 09:52:26 +01003602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 status = psa_driver_wrapper_cipher_set_iv(operation,
3604 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003605
3606exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 if (status == PSA_SUCCESS) {
3608 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003609 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003610 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003612 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003614 }
Ronald Cron6d051732020-10-01 14:10:20 +02003615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003617}
3618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3620 const uint8_t *iv,
3621 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003622{
Ronald Cron6d051732020-10-01 14:10:20 +02003623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003626 status = PSA_ERROR_BAD_STATE;
3627 goto exit;
3628 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003631 status = PSA_ERROR_BAD_STATE;
3632 goto exit;
3633 }
Ronald Crona0d68172021-03-26 10:15:08 +01003634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003636 status = PSA_ERROR_INVALID_ARGUMENT;
3637 goto exit;
3638 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 status = psa_driver_wrapper_cipher_set_iv(operation,
3641 iv,
3642 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003643
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003644exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003646 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 } else {
3648 psa_cipher_abort(operation);
3649 }
3650 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003651}
3652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3654 const uint8_t *input,
3655 size_t input_length,
3656 uint8_t *output,
3657 size_t output_size,
3658 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003659{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003663 status = PSA_ERROR_BAD_STATE;
3664 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003665 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003667 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003668 status = PSA_ERROR_BAD_STATE;
3669 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003670 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 status = psa_driver_wrapper_cipher_update(operation,
3673 input,
3674 input_length,
3675 output,
3676 output_size,
3677 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003678
3679exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680 if (status != PSA_SUCCESS) {
3681 psa_cipher_abort(operation);
3682 }
Ronald Cron6d051732020-10-01 14:10:20 +02003683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003684 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003685}
3686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3688 uint8_t *output,
3689 size_t output_size,
3690 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003691{
David Saadab4ecc272019-02-14 13:48:10 +02003692 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003695 status = PSA_ERROR_BAD_STATE;
3696 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003697 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003700 status = PSA_ERROR_BAD_STATE;
3701 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003702 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 status = psa_driver_wrapper_cipher_finish(operation,
3705 output,
3706 output_size,
3707 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003708
3709exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 if (status == PSA_SUCCESS) {
3711 return psa_cipher_abort(operation);
3712 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003713 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003717 }
mohammad1603503973b2018-03-12 15:59:30 +02003718}
3719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003721{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003722 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003723 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003724 * in use. It's ok to call abort on such an object, and there's
3725 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003727 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003730
Ronald Cron49fafa92021-03-10 08:34:23 +01003731 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003732 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003733 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003736}
3737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3739 psa_algorithm_t alg,
David Horstmann926193a2023-12-13 15:55:25 +00003740 const uint8_t *input_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 size_t input_length,
David Horstmann926193a2023-12-13 15:55:25 +00003742 uint8_t *output_external,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 size_t output_size,
3744 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003745{
3746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003747 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003748 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003749 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003750 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3751 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003752
David Horstmannb80e35a2023-12-14 18:12:47 +00003753 LOCAL_INPUT_DECLARE(input_external, input);
3754 LOCAL_OUTPUT_DECLARE(output_external, output);
3755
David Horstmann926193a2023-12-13 15:55:25 +00003756 LOCAL_INPUT_ALLOC(input_external, input_length, input);
David Horstmann926193a2023-12-13 15:55:25 +00003757 LOCAL_OUTPUT_ALLOC(output_external, output_size, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003760 status = PSA_ERROR_INVALID_ARGUMENT;
3761 goto exit;
3762 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3765 PSA_KEY_USAGE_ENCRYPT,
3766 alg);
3767 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003768 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003770
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003771 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003772 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003773 };
3774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3776 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003777 status = PSA_ERROR_GENERIC_ERROR;
3778 goto exit;
3779 }
3780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781 if (default_iv_length > 0) {
3782 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003783 status = PSA_ERROR_BUFFER_TOO_SMALL;
3784 goto exit;
3785 }
3786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787 status = psa_generate_random(local_iv, default_iv_length);
3788 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003789 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003790 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003791 }
3792
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003793 status = psa_driver_wrapper_cipher_encrypt(
3794 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003795 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 mbedtls_buffer_offset(output, default_iv_length),
3797 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003798
3799exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003800 unlock_status = psa_unlock_key_slot(slot);
3801 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003802 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003803 }
Ronald Cron16377072021-07-08 19:00:07 +02003804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 if (status == PSA_SUCCESS) {
3806 if (default_iv_length > 0) {
3807 memcpy(output, local_iv, default_iv_length);
3808 }
3809 *output_length += default_iv_length;
3810 } else {
3811 *output_length = 0;
3812 }
3813
David Horstmannb80e35a2023-12-14 18:12:47 +00003814 LOCAL_INPUT_FREE(input_external, input);
3815 LOCAL_OUTPUT_FREE(output_external, output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003817 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003818}
3819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3821 psa_algorithm_t alg,
3822 const uint8_t *input,
3823 size_t input_length,
3824 uint8_t *output,
3825 size_t output_size,
3826 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003827{
3828 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003829 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003830 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003831 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003834 status = PSA_ERROR_INVALID_ARGUMENT;
3835 goto exit;
3836 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3839 PSA_KEY_USAGE_DECRYPT,
3840 alg);
3841 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003842 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003844
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003845 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003847 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003849 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003850 status = PSA_ERROR_INVALID_ARGUMENT;
3851 goto exit;
3852 }
3853
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003854 status = psa_driver_wrapper_cipher_decrypt(
3855 &attributes, slot->key.data, slot->key.bytes,
3856 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003858
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003859exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003860 unlock_status = psa_unlock_key_slot(slot);
3861 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003862 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003865 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003866 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 }
Ronald Cron16377072021-07-08 19:00:07 +02003868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003869 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003870}
3871
3872
mohammad16035955c982018-04-26 00:53:03 +03003873/****************************************************************/
3874/* AEAD */
3875/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3878 psa_algorithm_t alg,
3879 const uint8_t *nonce,
3880 size_t nonce_length,
3881 const uint8_t *additional_data,
3882 size_t additional_data_length,
3883 const uint8_t *plaintext,
3884 size_t plaintext_length,
3885 uint8_t *ciphertext,
3886 size_t ciphertext_size,
3887 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003888{
Ronald Cron215633c2021-03-16 17:15:37 +01003889 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3890 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003891
mohammad1603f08a5502018-06-03 15:05:47 +03003892 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3895 return PSA_ERROR_NOT_SUPPORTED;
3896 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003897
Ronald Cron9a986162021-03-26 12:40:07 +01003898 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3900 if (status != PSA_SUCCESS) {
3901 return status;
3902 }
mohammad16035955c982018-04-26 00:53:03 +03003903
Ronald Cron215633c2021-03-16 17:15:37 +01003904 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003906 };
mohammad16035955c982018-04-26 00:53:03 +03003907
Ronald Cronde822812021-03-17 16:08:20 +01003908 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003909 &attributes, slot->key.data, slot->key.bytes,
3910 alg,
3911 nonce, nonce_length,
3912 additional_data, additional_data_length,
3913 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003916 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3917 memset(ciphertext, 0, ciphertext_size);
3918 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003922 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003923}
3924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3926 psa_algorithm_t alg,
3927 const uint8_t *nonce,
3928 size_t nonce_length,
3929 const uint8_t *additional_data,
3930 size_t additional_data_length,
3931 const uint8_t *ciphertext,
3932 size_t ciphertext_length,
3933 uint8_t *plaintext,
3934 size_t plaintext_size,
3935 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003936{
Ronald Cron215633c2021-03-16 17:15:37 +01003937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3938 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003939
Gilles Peskineee652a32018-06-01 19:23:52 +02003940 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3943 return PSA_ERROR_NOT_SUPPORTED;
3944 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003945
Ronald Cron9a986162021-03-26 12:40:07 +01003946 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003947 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3948 if (status != PSA_SUCCESS) {
3949 return status;
3950 }
mohammad16035955c982018-04-26 00:53:03 +03003951
Ronald Cron215633c2021-03-16 17:15:37 +01003952 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003954 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003955
Ronald Cronde822812021-03-17 16:08:20 +01003956 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003957 &attributes, slot->key.data, slot->key.bytes,
3958 alg,
3959 nonce, nonce_length,
3960 additional_data, additional_data_length,
3961 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003962 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964 if (status != PSA_SUCCESS && plaintext_size != 0) {
3965 memset(plaintext, 0, plaintext_size);
3966 }
mohammad160360a64d02018-06-03 17:20:42 +03003967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003970 return status;
mohammad16035955c982018-04-26 00:53:03 +03003971}
3972
Gilles Peskinee59236f2018-01-27 23:32:46 +01003973/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003974/* Generators */
3975/****************************************************************/
3976
John Durkop07cc04a2020-11-16 22:08:34 -08003977#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3978 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3979 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3980#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003981#endif /* At least one builtin KDF */
3982
3983#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3984 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3985 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3986static psa_status_t psa_key_derivation_start_hmac(
3987 psa_mac_operation_t *operation,
3988 psa_algorithm_t hash_alg,
3989 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003991{
3992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003994 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3995 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3996 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003997
Steven Cooreman15f0d922021-05-07 14:14:37 +02003998 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02004000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004001 status = psa_driver_wrapper_mac_sign_setup(operation,
4002 &attributes,
4003 hmac_key, hmac_key_length,
4004 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02004005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004006 psa_reset_key_attributes(&attributes);
4007 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02004008}
4009#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004010
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004011#define HKDF_STATE_INIT 0 /* no input yet */
4012#define HKDF_STATE_STARTED 1 /* got salt */
4013#define HKDF_STATE_KEYED 2 /* got key */
4014#define HKDF_STATE_OUTPUT 3 /* output started */
4015
Gilles Peskinecbe66502019-05-16 16:59:18 +02004016static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004017 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004018{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004019 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4020 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4021 } else {
4022 return operation->alg;
4023 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004024}
4025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004027{
4028 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004029 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4030 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004031 /* The object has (apparently) been initialized but it is not
4032 * in use. It's ok to call abort on such an object, and there's
4033 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 } else
John Durkopd0321952020-10-29 21:37:36 -07004035#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004036 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4037 mbedtls_free(operation->ctx.hkdf.info);
4038 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4039 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004040#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4041#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4042 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4044 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4045 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4046 if (operation->ctx.tls12_prf.secret != NULL) {
4047 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
4048 operation->ctx.tls12_prf.secret_length);
4049 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004050 }
4051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004052 if (operation->ctx.tls12_prf.seed != NULL) {
4053 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
4054 operation->ctx.tls12_prf.seed_length);
4055 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01004056 }
4057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 if (operation->ctx.tls12_prf.label != NULL) {
4059 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
4060 operation->ctx.tls12_prf.label_length);
4061 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01004062 }
4063
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004064 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004065
4066 /* We leave the fields Ai and output_block to be erased safely by the
4067 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004068 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004069#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4070 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004071 {
4072 status = PSA_ERROR_BAD_STATE;
4073 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074 mbedtls_platform_zeroize(operation, sizeof(*operation));
4075 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004076}
4077
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004078psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004080{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004082 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004084 }
4085
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004086 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004088}
4089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4091 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004092{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004093 if (operation->alg == 0) {
4094 return PSA_ERROR_BAD_STATE;
4095 }
4096 if (capacity > operation->capacity) {
4097 return PSA_ERROR_INVALID_ARGUMENT;
4098 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004099 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004100 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004101}
4102
John Durkopd0321952020-10-29 21:37:36 -07004103#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004105 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4107 psa_algorithm_t hash_alg,
4108 uint8_t *output,
4109 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004110{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004111 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02004112 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004113 psa_status_t status;
4114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
4116 return PSA_ERROR_BAD_STATE;
4117 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004118 hkdf->state = HKDF_STATE_OUTPUT;
4119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004120 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004121 /* Copy what remains of the current block */
4122 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004124 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125 }
4126 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02004127 output += n;
4128 output_length -= n;
4129 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02004131 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 }
Gilles Peskined54931c2018-07-17 21:06:59 +02004133 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004134 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004135 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004136 * object was corrupted or if this function is called directly
4137 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 if (hkdf->block_number == 0xff) {
4139 return PSA_ERROR_BAD_STATE;
4140 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004141
4142 /* We need a new block */
4143 ++hkdf->block_number;
4144 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4147 hash_alg,
4148 hkdf->prk,
4149 hash_length);
4150 if (status != PSA_SUCCESS) {
4151 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004152 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004153
4154 if (hkdf->block_number != 1) {
4155 status = psa_mac_update(&hkdf->hmac,
4156 hkdf->output_block,
4157 hash_length);
4158 if (status != PSA_SUCCESS) {
4159 return status;
4160 }
4161 }
4162 status = psa_mac_update(&hkdf->hmac,
4163 hkdf->info,
4164 hkdf->info_length);
4165 if (status != PSA_SUCCESS) {
4166 return status;
4167 }
4168 status = psa_mac_update(&hkdf->hmac,
4169 &hkdf->block_number, 1);
4170 if (status != PSA_SUCCESS) {
4171 return status;
4172 }
4173 status = psa_mac_sign_finish(&hkdf->hmac,
4174 hkdf->output_block,
4175 sizeof(hkdf->output_block),
4176 &hmac_output_length);
4177 if (status != PSA_SUCCESS) {
4178 return status;
4179 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004180 }
4181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004183}
John Durkop07cc04a2020-11-16 22:08:34 -08004184#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004185
John Durkop07cc04a2020-11-16 22:08:34 -08004186#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4187 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004188static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4189 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004191{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4193 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004194 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4195 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004196 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004197
Janos Follath7742fee2019-06-17 12:58:10 +01004198 /* We can't be wanting more output after block 0xff, otherwise
4199 * the capacity check in psa_key_derivation_output_bytes() would have
4200 * prevented this call. It could happen only if the operation
4201 * object was corrupted or if this function is called directly
4202 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 if (tls12_prf->block_number == 0xff) {
4204 return PSA_ERROR_CORRUPTION_DETECTED;
4205 }
Janos Follath7742fee2019-06-17 12:58:10 +01004206
4207 /* We need a new block */
4208 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004209 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004210
4211 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4212 *
4213 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4214 *
4215 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4216 * HMAC_hash(secret, A(2) + seed) +
4217 * HMAC_hash(secret, A(3) + seed) + ...
4218 *
4219 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004220 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004221 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004222 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004223 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004224 * is currently extracted as `output_block` and where i is
4225 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004226 */
4227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 status = psa_key_derivation_start_hmac(&hmac,
4229 hash_alg,
4230 tls12_prf->secret,
4231 tls12_prf->secret_length);
4232 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004233 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004235
4236 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004238 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4239 * the variable seed and in this instance means it in the context of the
4240 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241 status = psa_mac_update(&hmac,
4242 tls12_prf->label,
4243 tls12_prf->label_length);
4244 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004245 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 }
4247 status = psa_mac_update(&hmac,
4248 tls12_prf->seed,
4249 tls12_prf->seed_length);
4250 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004251 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252 }
4253 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004254 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4256 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004257 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004258 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004259 }
4260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004261 status = psa_mac_sign_finish(&hmac,
4262 tls12_prf->Ai, hash_length,
4263 &hmac_output_length);
4264 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004265 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 }
4267 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004268 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004270
4271 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 status = psa_key_derivation_start_hmac(&hmac,
4273 hash_alg,
4274 tls12_prf->secret,
4275 tls12_prf->secret_length);
4276 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004277 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 }
4279 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4280 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004281 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 }
4283 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4284 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004285 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 }
4287 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4288 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004289 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 }
4291 status = psa_mac_sign_finish(&hmac,
4292 tls12_prf->output_block, hash_length,
4293 &hmac_output_length);
4294 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004295 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004297
Janos Follath7742fee2019-06-17 12:58:10 +01004298
4299cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 cleanup_status = psa_mac_abort(&hmac);
4301 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004302 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004303 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004306}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004307
Janos Follath844eb0e2019-06-19 12:10:49 +01004308static psa_status_t psa_key_derivation_tls12_prf_read(
4309 psa_tls12_prf_key_derivation_t *tls12_prf,
4310 psa_algorithm_t alg,
4311 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004313{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4315 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004316 psa_status_t status;
4317 uint8_t offset, length;
4318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004320 case PSA_TLS12_PRF_STATE_LABEL_SET:
4321 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4322 break;
4323 case PSA_TLS12_PRF_STATE_OUTPUT:
4324 break;
4325 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004327 }
4328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004330 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 if (tls12_prf->left_in_block == 0) {
4332 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4333 alg);
4334 if (status != PSA_SUCCESS) {
4335 return status;
4336 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004337
4338 continue;
4339 }
4340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004342 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004344 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004346
4347 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004349 output += length;
4350 output_length -= length;
4351 tls12_prf->left_in_block -= length;
4352 }
4353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004355}
John Durkop07cc04a2020-11-16 22:08:34 -08004356#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4357 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004358
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004359psa_status_t psa_key_derivation_output_bytes(
4360 psa_key_derivation_operation_t *operation,
4361 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004363{
4364 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004369 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004370 }
4371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004374 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004376 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004377 goto exit;
4378 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004380 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004381 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004382 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4383 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004385 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004386 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004387 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004389
John Durkopd0321952020-10-29 21:37:36 -07004390#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4392 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4393 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4394 output, output_length);
4395 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004396#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4397#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4398 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4400 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4401 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4402 kdf_alg, output,
4403 output_length);
4404 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004405#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4406 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004407 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004408 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004410 }
4411
4412exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004413 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004414 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 * This allows us to differentiate between exhausted operations and
4416 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4417 * operations. */
4418 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004419 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004422 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004424}
4425
David Brown7807bf72021-02-09 16:28:23 -07004426#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004428{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 if (data_size >= 8) {
4430 mbedtls_des_key_set_parity(data);
4431 }
4432 if (data_size >= 16) {
4433 mbedtls_des_key_set_parity(data + 8);
4434 }
4435 if (data_size >= 24) {
4436 mbedtls_des_key_set_parity(data + 16);
4437 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004438}
David Brown7807bf72021-02-09 16:28:23 -07004439#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004440
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004441static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004442 psa_key_slot_t *slot,
4443 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004445{
4446 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004447 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004448 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004449 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 if (!key_type_is_raw_bytes(slot->attr.type)) {
4452 return PSA_ERROR_INVALID_ARGUMENT;
4453 }
4454 if (bits % 8 != 0) {
4455 return PSA_ERROR_INVALID_ARGUMENT;
4456 }
4457 data = mbedtls_calloc(1, bytes);
4458 if (data == NULL) {
4459 return PSA_ERROR_INSUFFICIENT_MEMORY;
4460 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 status = psa_key_derivation_output_bytes(operation, data, bytes);
4463 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004464 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 }
David Brown12ca5032021-02-11 11:02:00 -07004466#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004467 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4468 psa_des_set_key_parity(data, bytes);
4469 }
David Brown8107e312021-02-12 08:08:46 -07004470#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472 status = psa_allocate_buffer_to_slot(slot, bytes);
4473 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004474 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004475 }
Ronald Crondd04d422020-11-28 15:14:42 +01004476
Ronald Cronbf33c932020-11-28 18:06:53 +01004477 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004478 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004480 };
4481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 status = psa_driver_wrapper_import_key(&attributes,
4483 data, bytes,
4484 slot->key.data,
4485 slot->key.bytes,
4486 &slot->key.bytes, &bits);
4487 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004488 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004490
4491exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 mbedtls_free(data);
4493 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004494}
4495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4497 psa_key_derivation_operation_t *operation,
4498 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004499{
4500 psa_status_t status;
4501 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004502 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004503
Ronald Cron81709fc2020-11-14 12:10:32 +01004504 *key = MBEDTLS_SVC_KEY_ID_INIT;
4505
Gilles Peskine0f84d622019-09-12 19:03:13 +02004506 /* Reject any attempt to create a zero-length key so that we don't
4507 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004508 if (psa_get_key_bits(attributes) == 0) {
4509 return PSA_ERROR_INVALID_ARGUMENT;
4510 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004512 if (operation->alg == PSA_ALG_NONE) {
4513 return PSA_ERROR_BAD_STATE;
4514 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 if (!operation->can_output_key) {
4517 return PSA_ERROR_NOT_PERMITTED;
4518 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4521 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004522#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004524 /* Deriving a key in a secure element is not implemented yet. */
4525 status = PSA_ERROR_NOT_SUPPORTED;
4526 }
4527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 if (status == PSA_SUCCESS) {
4529 status = psa_generate_derived_key_internal(slot,
4530 attributes->core.bits,
4531 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004532 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 if (status == PSA_SUCCESS) {
4534 status = psa_finish_key_creation(slot, driver, key);
4535 }
4536 if (status != PSA_SUCCESS) {
4537 psa_fail_key_creation(slot, driver);
4538 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004541}
4542
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543
4544
4545/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004546/* Key derivation */
4547/****************************************************************/
4548
Steven Cooreman932ffb72021-02-15 12:14:32 +01004549#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004551{
4552#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4554 return 1;
4555 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004556#endif
4557#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4559 return 1;
4560 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004561#endif
4562#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4564 return 1;
4565 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004566#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004568}
4569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004571{
4572 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 psa_status_t status = psa_hash_setup(&operation, alg);
4574 psa_hash_abort(&operation);
4575 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004576}
4577
Gilles Peskine969c5d62019-01-16 15:53:06 +01004578static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004581{
Janos Follath5fe19732019-06-20 15:09:30 +01004582 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4583 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004585
Gilles Peskine969c5d62019-01-16 15:53:06 +01004586 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004587 if (!is_kdf_alg_supported(kdf_alg)) {
4588 return PSA_ERROR_NOT_SUPPORTED;
4589 }
John Durkop07cc04a2020-11-16 22:08:34 -08004590
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004591 /* All currently supported key derivation algorithms are based on a
4592 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4594 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4595 if (hash_size == 0) {
4596 return PSA_ERROR_NOT_SUPPORTED;
4597 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004598
4599 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4600 * we might fail later, which is somewhat unfriendly and potentially
4601 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004602 psa_status_t status = psa_hash_try_support(hash_alg);
4603 if (status != PSA_SUCCESS) {
4604 return status;
4605 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4608 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4609 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4610 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004611 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004612
Gilles Peskinecdacf042021-04-29 21:10:00 +02004613 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004614 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004615}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004618{
4619#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 if (alg == PSA_ALG_ECDH) {
4621 return PSA_SUCCESS;
4622 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004623#endif
4624 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004626}
John Durkop07cc04a2020-11-16 22:08:34 -08004627#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4630 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004631{
4632 psa_status_t status;
4633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004634 if (operation->alg != 0) {
4635 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004636 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004638 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4639 return PSA_ERROR_INVALID_ARGUMENT;
4640 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4641#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4642 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4643 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4644 status = psa_key_agreement_try_support(ka_alg);
4645 if (status != PSA_SUCCESS) {
4646 return status;
4647 }
4648 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4649#else
4650 return PSA_ERROR_NOT_SUPPORTED;
4651#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4652 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4653#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4654 status = psa_key_derivation_setup_kdf(operation, alg);
4655#else
4656 return PSA_ERROR_NOT_SUPPORTED;
4657#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4658 } else {
4659 return PSA_ERROR_INVALID_ARGUMENT;
4660 }
4661
4662 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664 }
4665 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004666}
4667
John Durkopd0321952020-10-29 21:37:36 -07004668#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4670 psa_algorithm_t hash_alg,
4671 psa_key_derivation_step_t step,
4672 const uint8_t *data,
4673 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004674{
4675 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004677 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 if (hkdf->state != HKDF_STATE_INIT) {
4679 return PSA_ERROR_BAD_STATE;
4680 } else {
4681 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4682 hash_alg,
4683 data, data_length);
4684 if (status != PSA_SUCCESS) {
4685 return status;
4686 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004687 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004689 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004690 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004691 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 if (hkdf->state == HKDF_STATE_INIT) {
4693 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4694 hash_alg,
4695 NULL, 0);
4696 if (status != PSA_SUCCESS) {
4697 return status;
4698 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004699 hkdf->state = HKDF_STATE_STARTED;
4700 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 if (hkdf->state != HKDF_STATE_STARTED) {
4702 return PSA_ERROR_BAD_STATE;
4703 }
4704 status = psa_mac_update(&hkdf->hmac,
4705 data, data_length);
4706 if (status != PSA_SUCCESS) {
4707 return status;
4708 }
4709 status = psa_mac_sign_finish(&hkdf->hmac,
4710 hkdf->prk,
4711 sizeof(hkdf->prk),
4712 &data_length);
4713 if (status != PSA_SUCCESS) {
4714 return status;
4715 }
4716 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004717 hkdf->block_number = 0;
4718 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004720 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if (hkdf->state == HKDF_STATE_OUTPUT) {
4722 return PSA_ERROR_BAD_STATE;
4723 }
4724 if (hkdf->info_set) {
4725 return PSA_ERROR_BAD_STATE;
4726 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004727 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 if (data_length != 0) {
4729 hkdf->info = mbedtls_calloc(1, data_length);
4730 if (hkdf->info == NULL) {
4731 return PSA_ERROR_INSUFFICIENT_MEMORY;
4732 }
4733 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734 }
4735 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004737 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004739 }
4740}
John Durkop07cc04a2020-11-16 22:08:34 -08004741#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004742
John Durkop07cc04a2020-11-16 22:08:34 -08004743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4744 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004745static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4746 const uint8_t *data,
4747 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004748{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4750 return PSA_ERROR_BAD_STATE;
4751 }
Janos Follathf08e2652019-06-13 09:05:41 +01004752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004753 if (data_length != 0) {
4754 prf->seed = mbedtls_calloc(1, data_length);
4755 if (prf->seed == NULL) {
4756 return PSA_ERROR_INSUFFICIENT_MEMORY;
4757 }
Janos Follathf08e2652019-06-13 09:05:41 +01004758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004759 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004760 prf->seed_length = data_length;
4761 }
Janos Follathf08e2652019-06-13 09:05:41 +01004762
Gilles Peskine43f958b2020-12-13 14:55:14 +01004763 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004766}
4767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4769 const uint8_t *data,
4770 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004771{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4773 return PSA_ERROR_BAD_STATE;
4774 }
Janos Follath81550542019-06-13 14:26:34 +01004775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004776 if (data_length != 0) {
4777 prf->secret = mbedtls_calloc(1, data_length);
4778 if (prf->secret == NULL) {
4779 return PSA_ERROR_INSUFFICIENT_MEMORY;
4780 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004783 prf->secret_length = data_length;
4784 }
Janos Follath81550542019-06-13 14:26:34 +01004785
Gilles Peskine43f958b2020-12-13 14:55:14 +01004786 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004789}
4790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004791static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4792 const uint8_t *data,
4793 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004794{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4796 return PSA_ERROR_BAD_STATE;
4797 }
Janos Follath63028dd2019-06-13 09:15:47 +01004798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 if (data_length != 0) {
4800 prf->label = mbedtls_calloc(1, data_length);
4801 if (prf->label == NULL) {
4802 return PSA_ERROR_INSUFFICIENT_MEMORY;
4803 }
Janos Follath63028dd2019-06-13 09:15:47 +01004804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004806 prf->label_length = data_length;
4807 }
Janos Follath63028dd2019-06-13 09:15:47 +01004808
Gilles Peskine43f958b2020-12-13 14:55:14 +01004809 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004812}
4813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4815 psa_key_derivation_step_t step,
4816 const uint8_t *data,
4817 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004818{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004820 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004822 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004823 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004824 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004826 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004828 }
4829}
John Durkop07cc04a2020-11-16 22:08:34 -08004830#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4831 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4832
4833#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4834static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4835 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004836 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004837 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004838{
4839 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004841 uint8_t *cur = pms;
4842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4844 return PSA_ERROR_INVALID_ARGUMENT;
4845 }
John Durkop07cc04a2020-11-16 22:08:34 -08004846
4847 /* Quoting RFC 4279, Section 2:
4848 *
4849 * The premaster secret is formed as follows: if the PSK is N octets
4850 * long, concatenate a uint16 with the value N, N zero octets, a second
4851 * uint16 with the value N, and the PSK itself.
4852 */
4853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 *cur++ = MBEDTLS_BYTE_1(data_length);
4855 *cur++ = MBEDTLS_BYTE_0(data_length);
4856 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004857 cur += data_length;
4858 *cur++ = pms[0];
4859 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004861 cur += data_length;
4862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004863 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865 mbedtls_platform_zeroize(pms, sizeof(pms));
4866 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004867}
Janos Follath6660f0e2019-06-17 08:44:03 +01004868
4869static psa_status_t psa_tls12_prf_psk_to_ms_input(
4870 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004871 psa_key_derivation_step_t step,
4872 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004874{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4876 return psa_tls12_prf_psk_to_ms_set_key(prf,
4877 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004878 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004881}
John Durkop07cc04a2020-11-16 22:08:34 -08004882#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883
Gilles Peskineb8965192019-09-24 16:21:10 +02004884/** Check whether the given key type is acceptable for the given
4885 * input step of a key derivation.
4886 *
4887 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4888 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4889 * Both secret and non-secret inputs can alternatively have the type
4890 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4891 * that the input was passed as a buffer rather than via a key object.
4892 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004893static int psa_key_derivation_check_input_type(
4894 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004895 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004896{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004898 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 if (key_type == PSA_KEY_TYPE_DERIVE) {
4900 return PSA_SUCCESS;
4901 }
4902 if (key_type == PSA_KEY_TYPE_NONE) {
4903 return PSA_SUCCESS;
4904 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004905 break;
4906 case PSA_KEY_DERIVATION_INPUT_LABEL:
4907 case PSA_KEY_DERIVATION_INPUT_SALT:
4908 case PSA_KEY_DERIVATION_INPUT_INFO:
4909 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004910 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4911 return PSA_SUCCESS;
4912 }
4913 if (key_type == PSA_KEY_TYPE_NONE) {
4914 return PSA_SUCCESS;
4915 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004916 break;
4917 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004919}
4920
Janos Follathb80a94e2019-06-12 15:54:46 +01004921static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004923 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004924 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004925 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004928 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931 status = psa_key_derivation_check_input_type(step, key_type);
4932 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004933 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004934 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004935
John Durkopd0321952020-10-29 21:37:36 -07004936#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4938 status = psa_hkdf_input(&operation->ctx.hkdf,
4939 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4940 step, data, data_length);
4941 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004942#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4943#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4945 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4946 step, data, data_length);
4947 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004948#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4949#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4951 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4952 step, data, data_length);
4953 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004954#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004957 (void) data;
4958 (void) data_length;
4959 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004960 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961 }
4962
Gilles Peskine224b0d62019-09-23 18:13:17 +02004963exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 if (status != PSA_SUCCESS) {
4965 psa_key_derivation_abort(operation);
4966 }
4967 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004968}
4969
Janos Follath51f4a0f2019-06-14 11:35:55 +01004970psa_status_t psa_key_derivation_input_bytes(
4971 psa_key_derivation_operation_t *operation,
4972 psa_key_derivation_step_t step,
4973 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004974 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004975{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 return psa_key_derivation_input_internal(operation, step,
4977 PSA_KEY_TYPE_NONE,
4978 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004979}
4980
Janos Follath51f4a0f2019-06-14 11:35:55 +01004981psa_status_t psa_key_derivation_input_key(
4982 psa_key_derivation_operation_t *operation,
4983 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004984 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004987 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004989
Ronald Cron5c522922020-11-14 16:35:34 +01004990 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004991 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4992 if (status != PSA_SUCCESS) {
4993 psa_key_derivation_abort(operation);
4994 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004995 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004996
4997 /* Passing a key object as a SECRET input unlocks the permission
4998 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005000 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 status = psa_key_derivation_input_internal(operation,
5004 step, slot->attr.type,
5005 slot->key.data,
5006 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005008 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005011}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005012
5013
5014
5015/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005016/* Key agreement */
5017/****************************************************************/
5018
John Durkop6ba40d12020-11-10 08:50:04 -08005019#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
5021 size_t peer_key_length,
5022 const mbedtls_ecp_keypair *our_key,
5023 uint8_t *shared_secret,
5024 size_t shared_secret_size,
5025 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005026{
Steven Cooremand4867872020-08-05 16:31:39 +02005027 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005028 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005029 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005030 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
5032 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005033
Ronald Cron90857082020-11-25 14:58:33 +01005034 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005035 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5036 bits,
5037 peer_key,
5038 peer_key_length,
5039 &their_key);
5040 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005041 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005043
Jaeden Amero97271b32019-01-10 19:38:51 +00005044 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
5046 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005047 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048 }
Jaeden Amero97271b32019-01-10 19:38:51 +00005049 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
5051 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005052 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005054
Jaeden Amero97271b32019-01-10 19:38:51 +00005055 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005056 mbedtls_ecdh_calc_secret(&ecdh,
5057 shared_secret_length,
5058 shared_secret, shared_secret_size,
5059 mbedtls_psa_get_random,
5060 MBEDTLS_PSA_RANDOM_STATE));
5061 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005062 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005063 }
5064 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005065 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005067
5068exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005069 if (status != PSA_SUCCESS) {
5070 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
5071 }
5072 mbedtls_ecdh_free(&ecdh);
5073 mbedtls_ecp_keypair_free(their_key);
5074 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02005075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005077}
John Durkop6ba40d12020-11-10 08:50:04 -08005078#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005079
Gilles Peskine01d718c2018-09-18 12:01:02 +02005080#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
5083 psa_key_slot_t *private_key,
5084 const uint8_t *peer_key,
5085 size_t peer_key_length,
5086 uint8_t *shared_secret,
5087 size_t shared_secret_size,
5088 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005089{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005090 mbedtls_ecp_keypair *ecp = NULL;
5091 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01005092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08005094#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005095 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005096 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
5097 return PSA_ERROR_INVALID_ARGUMENT;
5098 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005099 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 private_key->attr.type,
5101 private_key->attr.bits,
5102 private_key->key.data,
5103 private_key->key.bytes,
5104 &ecp);
5105 if (status != PSA_SUCCESS) {
5106 return status;
5107 }
5108 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
5109 ecp,
5110 shared_secret, shared_secret_size,
5111 shared_secret_length);
5112 mbedtls_ecp_keypair_free(ecp);
5113 mbedtls_free(ecp);
5114 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08005115#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005116 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005117 (void) ecp;
5118 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01005119 (void) private_key;
5120 (void) peer_key;
5121 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005122 (void) shared_secret;
5123 (void) shared_secret_size;
5124 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005126 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005127}
5128
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005129/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005130 * to potentially free embedded data structures and wipe confidential data.
5131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
5133 psa_key_derivation_step_t step,
5134 psa_key_slot_t *private_key,
5135 const uint8_t *peer_key,
5136 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02005137{
5138 psa_status_t status;
5139 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5140 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005141 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02005142
5143 /* Step 1: run the secret agreement algorithm to generate the shared
5144 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 status = psa_key_agreement_raw_internal(ka_alg,
5146 private_key,
5147 peer_key, peer_key_length,
5148 shared_secret,
5149 sizeof(shared_secret),
5150 &shared_secret_length);
5151 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005152 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005154
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005155 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005156 * the shared secret. A shared secret is permitted wherever a key
5157 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 status = psa_key_derivation_input_internal(operation, step,
5159 PSA_KEY_TYPE_DERIVE,
5160 shared_secret,
5161 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005162exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5164 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005165}
5166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5168 psa_key_derivation_step_t step,
5169 mbedtls_svc_key_id_t private_key,
5170 const uint8_t *peer_key,
5171 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005172{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005174 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005175 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5178 return PSA_ERROR_INVALID_ARGUMENT;
5179 }
Ronald Cron5c522922020-11-14 16:35:34 +01005180 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5182 if (status != PSA_SUCCESS) {
5183 return status;
5184 }
5185 status = psa_key_agreement_internal(operation, step,
5186 slot,
5187 peer_key, peer_key_length);
5188 if (status != PSA_SUCCESS) {
5189 psa_key_derivation_abort(operation);
5190 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005191 /* If a private key has been added as SECRET, we allow the derived
5192 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005194 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005195 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005196 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005200 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005201}
5202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5204 mbedtls_svc_key_id_t private_key,
5205 const uint8_t *peer_key,
5206 size_t peer_key_length,
5207 uint8_t *output,
5208 size_t output_size,
5209 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005210{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005212 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005213 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005214 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005217 status = PSA_ERROR_INVALID_ARGUMENT;
5218 goto exit;
5219 }
Ronald Cron5c522922020-11-14 16:35:34 +01005220 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5222 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005223 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005224 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005225
Gilles Peskine42313fb2022-04-14 00:17:15 +02005226 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5227 * for the output size. The PSA specification only guarantees that this
5228 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5229 * but it might be nice to allow smaller buffers if the output fits.
5230 * At the time of writing this comment, with only ECDH implemented,
5231 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5232 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5233 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005234 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005235 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5236 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005237 status = PSA_ERROR_BUFFER_TOO_SMALL;
5238 goto exit;
5239 }
5240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 status = psa_key_agreement_raw_internal(alg, slot,
5242 peer_key, peer_key_length,
5243 output, output_size,
5244 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005245
5246exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005247 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005248 /* If an error happens and is not handled properly, the output
5249 * may be used as a key to protect sensitive data. Arrange for such
5250 * a key to be random, which is likely to result in decryption or
5251 * verification errors. This is better than filling the buffer with
5252 * some constant data such as zeros, which would result in the data
5253 * being protected with a reproducible, easily knowable key.
5254 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005256 *output_length = output_size;
5257 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005262}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005263
5264
Gilles Peskine30524eb2020-11-13 17:02:26 +01005265
Gilles Peskine86a440b2018-11-12 18:39:40 +01005266/****************************************************************/
5267/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005268/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005269
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005270#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5271#include "mbedtls/entropy_poll.h"
5272#endif
5273
Gilles Peskine30524eb2020-11-13 17:02:26 +01005274/** Initialize the PSA random generator.
5275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005277{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005278#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005279 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005280#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5281
Gilles Peskine30524eb2020-11-13 17:02:26 +01005282 /* Set default configuration if
5283 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005285 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286 }
5287 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005288 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005291 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005292#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5293 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5294 /* The PSA entropy injection feature depends on using NV seed as an entropy
5295 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005296 mbedtls_entropy_add_source(&rng->entropy,
5297 mbedtls_nv_seed_poll, NULL,
5298 MBEDTLS_ENTROPY_BLOCK_SIZE,
5299 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005300#endif
5301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005303#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005304}
5305
5306/** Deinitialize the PSA random generator.
5307 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005308static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005309{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005310#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005311 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005312#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005313 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5314 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005315#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005316}
5317
5318/** Seed the PSA random generator.
5319 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005321{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005322#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5323 /* Do nothing: the external RNG seeds itself. */
5324 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005326#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005327 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005328 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5329 drbg_seed, sizeof(drbg_seed) - 1);
5330 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005331#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005332}
5333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005334psa_status_t psa_generate_random(uint8_t *output,
5335 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005336{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005337 GUARD_MODULE_INITIALIZED;
5338
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005339#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5340
5341 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005342 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5343 output, output_size,
5344 &output_length);
5345 if (status != PSA_SUCCESS) {
5346 return status;
5347 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005348 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005349 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 if (output_length != output_size) {
5351 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5352 }
5353 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005354
5355#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005358 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005359 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5360 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5361 output_size);
5362 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5363 output, request_size);
5364 if (ret != 0) {
5365 return mbedtls_to_psa_error(ret);
5366 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005367 output_size -= request_size;
5368 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005369 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005371#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005372}
5373
Gilles Peskine8814fc42020-12-14 15:33:44 +01005374/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005375 *
5376 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5377 * `psa_generate_random(...)`. The state parameter is ignored since the
5378 * PSA API doesn't support passing an explicit state.
5379 *
5380 * In the non-external case, psa_generate_random() calls an
5381 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5382 * and semantics as mbedtls_psa_get_random(). As an optimization,
5383 * instead of doing this back-and-forth between the PSA API and the
5384 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5385 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005386 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5388int mbedtls_psa_get_random(void *p_rng,
5389 unsigned char *output,
5390 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005391{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005392 /* This function takes a pointer to the RNG state because that's what
5393 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5394 * its own state internally and doesn't let the caller access that state.
5395 * So we just ignore the state parameter, and in practice we'll pass
5396 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005397 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 psa_status_t status = psa_generate_random(output, output_size);
5399 if (status == PSA_SUCCESS) {
5400 return 0;
5401 } else {
5402 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5403 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005404}
5405#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5406
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005407#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005408psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5409 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005410{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005411 if (global_data.initialized) {
5412 return PSA_ERROR_NOT_PERMITTED;
5413 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005415 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5416 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5417 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5418 return PSA_ERROR_INVALID_ARGUMENT;
5419 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005422}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005423#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005424
Ronald Cron3772afe2021-02-08 16:10:05 +01005425/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005426 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005427 * \param type The key type
5428 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005429 *
5430 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005431 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005432 * \retval #PSA_ERROR_INVALID_ARGUMENT
5433 * The size in bits of the key is not valid.
5434 * \retval #PSA_ERROR_NOT_SUPPORTED
5435 * The type and/or the size in bits of the key or the combination of
5436 * the two is not supported.
5437 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005438static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005440{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 if (key_type_is_raw_bytes(type)) {
5444 status = validate_unstructured_key_bit_size(type, bits);
5445 if (status != PSA_SUCCESS) {
5446 return status;
5447 }
5448 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005449#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5451 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5452 return PSA_ERROR_NOT_SUPPORTED;
5453 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005454
Ronald Cron01b2aba2020-10-05 09:42:02 +02005455 /* Accept only byte-aligned keys, for the same reasons as
5456 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 if (bits % 8 != 0) {
5458 return PSA_ERROR_NOT_SUPPORTED;
5459 }
5460 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005461#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005462
Ronald Cron5c4d3862020-12-07 11:07:24 +01005463#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005464 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005465 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005466 return PSA_SUCCESS;
5467 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005468#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005469 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005471 }
5472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005473 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005474}
5475
Ronald Cron55ed0592020-10-05 10:30:40 +02005476psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005477 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005479{
5480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005481 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 if ((attributes->domain_parameters == NULL) &&
5484 (attributes->domain_parameters_size != 0)) {
5485 return PSA_ERROR_INVALID_ARGUMENT;
5486 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 if (key_type_is_raw_bytes(type)) {
5489 status = psa_generate_random(key_buffer, key_buffer_size);
5490 if (status != PSA_SUCCESS) {
5491 return status;
5492 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005493
David Brown12ca5032021-02-11 11:02:00 -07005494#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 if (type == PSA_KEY_TYPE_DES) {
5496 psa_des_set_key_parity(key_buffer, key_buffer_size);
5497 }
David Brown8107e312021-02-12 08:08:46 -07005498#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005501#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5502 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5504 return mbedtls_psa_rsa_generate_key(attributes,
5505 key_buffer,
5506 key_buffer_size,
5507 key_buffer_length);
5508 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005509#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5510 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511
John Durkop9814fa22020-11-04 12:28:15 -08005512#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005513 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5514 return mbedtls_psa_ecp_generate_key(attributes,
5515 key_buffer,
5516 key_buffer_size,
5517 key_buffer_length);
5518 } else
John Durkop9814fa22020-11-04 12:28:15 -08005519#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005520 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 (void) key_buffer_length;
5522 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005523 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005525 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005526}
Darryl Green0c6575a2018-11-07 16:05:30 +00005527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5529 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005530{
5531 psa_status_t status;
5532 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005533 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005534 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005535
Ronald Cron81709fc2020-11-14 12:10:32 +01005536 *key = MBEDTLS_SVC_KEY_ID_INIT;
5537
Gilles Peskine0f84d622019-09-12 19:03:13 +02005538 /* Reject any attempt to create a zero-length key so that we don't
5539 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 if (psa_get_key_bits(attributes) == 0) {
5541 return PSA_ERROR_INVALID_ARGUMENT;
5542 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005543
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005544 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005545 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5546 return PSA_ERROR_INVALID_ARGUMENT;
5547 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005549 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5550 &slot, &driver);
5551 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005552 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 }
Gilles Peskine11792082019-08-06 18:36:36 +02005554
Ronald Cron977c2472020-10-13 08:32:21 +02005555 /* In the case of a transparent key or an opaque key stored in local
5556 * storage (thus not in the case of generating a key in a secure element
5557 * or cryptoprocessor with storage), we have to allocate a buffer to
5558 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 if (slot->key.data == NULL) {
5560 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5561 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005562 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005563 attributes->core.type, attributes->core.bits);
5564 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005565 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005566 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005567
5568 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005569 attributes->core.type,
5570 attributes->core.bits);
5571 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005572 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005573 attributes, &key_buffer_size);
5574 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005575 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576 }
Ronald Cron977c2472020-10-13 08:32:21 +02005577 }
Ronald Cron977c2472020-10-13 08:32:21 +02005578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005579 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5580 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005581 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 }
Ronald Cron977c2472020-10-13 08:32:21 +02005583 }
5584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005585 status = psa_driver_wrapper_generate_key(attributes,
5586 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 if (status != PSA_SUCCESS) {
5589 psa_remove_key_data_from_memory(slot);
5590 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005591
Gilles Peskine11792082019-08-06 18:36:36 +02005592exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005593 if (status == PSA_SUCCESS) {
5594 status = psa_finish_key_creation(slot, driver, key);
5595 }
5596 if (status != PSA_SUCCESS) {
5597 psa_fail_key_creation(slot, driver);
5598 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005600 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005601}
5602
Gilles Peskinee59236f2018-01-27 23:32:46 +01005603/****************************************************************/
5604/* Module setup */
5605/****************************************************************/
5606
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005607#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005608psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 void (* entropy_init)(mbedtls_entropy_context *ctx),
5610 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005611{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5613 return PSA_ERROR_BAD_STATE;
5614 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005615 global_data.rng.entropy_init = entropy_init;
5616 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005617 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005618}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005619#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005621void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005623 psa_wipe_all_key_slots();
5624 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5625 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005626 }
5627 /* Wipe all remaining data, including configuration.
5628 * In particular, this sets all state indicator to the value
5629 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005631
5632 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005633 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634}
5635
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005636#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5637/** Recover a transaction that was interrupted by a power failure.
5638 *
5639 * This function is called during initialization, before psa_crypto_init()
5640 * returns. If this function returns a failure status, the initialization
5641 * fails.
5642 */
5643static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005644 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005645{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005647 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5648 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005649 /* TODO - fall through to the failure case until this
5650 * is implemented.
5651 * https://github.com/ARMmbed/mbed-crypto/issues/218
5652 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005653 default:
5654 /* We found an unsupported transaction in the storage.
5655 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005656 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005657 }
5658}
5659#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005662{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005663 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005664
Gilles Peskinec6b69072018-11-20 21:42:52 +01005665 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005666 if (global_data.initialized != 0) {
5667 return PSA_SUCCESS;
5668 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005669
Gilles Peskine30524eb2020-11-13 17:02:26 +01005670 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005671 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005672 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005673 status = mbedtls_psa_random_seed(&global_data.rng);
5674 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005677 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005679 status = psa_initialize_key_slots();
5680 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005681 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005682 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005683
Ronald Cron088d5d02021-04-10 16:57:30 +02005684 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005685 status = psa_driver_wrapper_init();
5686 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005687 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005688 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005689
Gilles Peskine4b734222019-07-24 15:56:31 +02005690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 status = psa_crypto_load_transaction();
5692 if (status == PSA_SUCCESS) {
5693 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5694 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005695 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005696 }
5697 status = psa_crypto_stop_transaction();
5698 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005699 /* There's no transaction to complete. It's all good. */
5700 status = PSA_SUCCESS;
5701 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005702#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005703
Gilles Peskinec6b69072018-11-20 21:42:52 +01005704 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705 global_data.initialized = 1;
5706
5707exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005708 if (status != PSA_SUCCESS) {
5709 mbedtls_psa_crypto_free();
5710 }
5711 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005712}
5713
David Horstmann114d8242023-12-07 18:39:17 +00005714/* Memory copying test hooks. These are called before input copy, after input
5715 * copy, before output copy and after output copy, respectively.
5716 * They are used by memory-poisoning tests to temporarily unpoison buffers
5717 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005718#if defined(MBEDTLS_TEST_HOOKS)
5719void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5720void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5721void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5722void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5723#endif
5724
David Horstmanndf493552023-11-15 15:18:30 +00005725/** Copy from an input buffer to a local copy.
5726 *
5727 * \param[in] input Pointer to input buffer.
5728 * \param[in] input_len Length of the input buffer.
5729 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5730 * \param[out] input_copy_len Length of the local copy buffer.
5731 * \return #PSA_SUCCESS, if the buffer was successfully
5732 * copied.
5733 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5734 * copy is too small to hold contents of the
5735 * input buffer.
5736 */
5737MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005738psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5739 uint8_t *input_copy, size_t input_copy_len)
5740{
5741 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005742 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005743 }
5744
David Horstmann0760b152023-11-24 16:21:04 +00005745#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005746 if (psa_input_pre_copy_hook != NULL) {
5747 psa_input_pre_copy_hook(input, input_len);
5748 }
David Horstmann0760b152023-11-24 16:21:04 +00005749#endif
5750
David Horstmann660027f2023-11-15 17:33:47 +00005751 if (input_len > 0) {
5752 memcpy(input_copy, input, input_len);
5753 }
David Horstmann957f9802023-10-30 20:29:43 +00005754
David Horstmann0760b152023-11-24 16:21:04 +00005755#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005756 if (psa_input_post_copy_hook != NULL) {
5757 psa_input_post_copy_hook(input, input_len);
5758 }
David Horstmann0760b152023-11-24 16:21:04 +00005759#endif
5760
David Horstmann957f9802023-10-30 20:29:43 +00005761 return PSA_SUCCESS;
5762}
5763
David Horstmanndf493552023-11-15 15:18:30 +00005764/** Copy from a local output buffer into a user-supplied one.
5765 *
5766 * \param[in] output_copy Pointer to a local buffer containing the output.
5767 * \param[in] output_copy_len Length of the local buffer.
5768 * \param[out] output Pointer to user-supplied output buffer.
5769 * \param[out] output_len Length of the user-supplied output buffer.
5770 * \return #PSA_SUCCESS, if the buffer was successfully
5771 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005772 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005773 * user-supplied output buffer is too small to
5774 * hold the contents of the local buffer.
5775 */
5776MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005777psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5778 uint8_t *output, size_t output_len)
5779{
5780 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005781 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005782 }
David Horstmann660027f2023-11-15 17:33:47 +00005783
David Horstmann0760b152023-11-24 16:21:04 +00005784#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005785 if (psa_output_pre_copy_hook != NULL) {
5786 psa_output_pre_copy_hook(output, output_len);
5787 }
David Horstmann0760b152023-11-24 16:21:04 +00005788#endif
5789
David Horstmann660027f2023-11-15 17:33:47 +00005790 if (output_copy_len > 0) {
5791 memcpy(output, output_copy, output_copy_len);
5792 }
5793
David Horstmann0760b152023-11-24 16:21:04 +00005794#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005795 if (psa_output_post_copy_hook != NULL) {
5796 psa_output_post_copy_hook(output, output_len);
5797 }
David Horstmann0760b152023-11-24 16:21:04 +00005798#endif
5799
David Horstmann2bd296e2023-10-30 20:37:12 +00005800 return PSA_SUCCESS;
5801}
5802
David Horstmann81a0d572023-11-20 17:15:32 +00005803psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5804 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005805{
5806 psa_status_t status;
5807
David Horstmann0d52c712023-11-23 15:50:37 +00005808 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005809
David Horstmann726bf052023-11-15 18:11:26 +00005810 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005811 return PSA_SUCCESS;
5812 }
5813
David Horstmann81a0d572023-11-20 17:15:32 +00005814 local_input->buffer = mbedtls_calloc(input_len, 1);
5815 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005816 /* Since we dealt with the zero-length case above, we know that
5817 * a NULL return value means a failure of allocation. */
5818 return PSA_ERROR_INSUFFICIENT_MEMORY;
5819 }
David Horstmann81a0d572023-11-20 17:15:32 +00005820 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005821
David Horstmann81a0d572023-11-20 17:15:32 +00005822 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005823
5824 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005825 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005826 if (status != PSA_SUCCESS) {
5827 goto error;
5828 }
5829
5830 return PSA_SUCCESS;
5831
5832error:
David Horstmann81a0d572023-11-20 17:15:32 +00005833 mbedtls_free(local_input->buffer);
5834 local_input->buffer = NULL;
5835 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005836 return status;
5837}
5838
David Horstmann81a0d572023-11-20 17:15:32 +00005839void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005840{
David Horstmann81a0d572023-11-20 17:15:32 +00005841 mbedtls_free(local_input->buffer);
5842 local_input->buffer = NULL;
5843 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005844}
5845
David Horstmann1a76ab12023-11-20 12:54:09 +00005846psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5847 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005848{
David Horstmann0d52c712023-11-23 15:50:37 +00005849 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005850
David Horstmann726bf052023-11-15 18:11:26 +00005851 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005852 return PSA_SUCCESS;
5853 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005854 local_output->buffer = mbedtls_calloc(output_len, 1);
5855 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005856 /* Since we dealt with the zero-length case above, we know that
5857 * a NULL return value means a failure of allocation. */
5858 return PSA_ERROR_INSUFFICIENT_MEMORY;
5859 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005860 local_output->length = output_len;
5861 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005862
5863 return PSA_SUCCESS;
5864}
5865
Gabor Mezei3b0c3712024-01-24 13:07:17 +01005866psa_status_t psa_crypto_local_output_alloc_with_copy(uint8_t *output, size_t output_len,
5867 psa_crypto_local_output_t *local_output)
5868{
5869 psa_status_t status;
5870 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
5871
5872 if (output_len == 0) {
5873 return PSA_SUCCESS;
5874 }
5875 local_output->buffer = mbedtls_calloc(output_len, 1);
5876 if (local_output->buffer == NULL) {
5877 /* Since we dealt with the zero-length case above, we know that
5878 * a NULL return value means a failure of allocation. */
5879 return PSA_ERROR_INSUFFICIENT_MEMORY;
5880 }
5881 local_output->length = output_len;
5882 local_output->original = output;
5883
5884 status = psa_crypto_copy_input(output, output_len,
5885 local_output->buffer, local_output->length);
5886 if (status != PSA_SUCCESS) {
5887 goto error;
5888 }
5889
5890 return PSA_SUCCESS;
5891
5892error:
5893 mbedtls_free(local_output->buffer);
5894 local_output->buffer = NULL;
5895 local_output->length = 0;
5896 return status;
5897}
5898
David Horstmann1a76ab12023-11-20 12:54:09 +00005899psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005900{
David Horstmann761761f2023-11-15 15:57:32 +00005901 psa_status_t status;
5902
David Horstmann1a76ab12023-11-20 12:54:09 +00005903 if (local_output->buffer == NULL) {
5904 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005905 return PSA_SUCCESS;
5906 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005907 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005908 /* We have an internal copy but nothing to copy back to. */
5909 return PSA_ERROR_CORRUPTION_DETECTED;
5910 }
5911
David Horstmann1a76ab12023-11-20 12:54:09 +00005912 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5913 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005914 if (status != PSA_SUCCESS) {
5915 return status;
5916 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005917
David Horstmann1a76ab12023-11-20 12:54:09 +00005918 mbedtls_free(local_output->buffer);
5919 local_output->buffer = NULL;
5920 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005921
5922 return PSA_SUCCESS;
5923}
5924
Gilles Peskinee59236f2018-01-27 23:32:46 +01005925#endif /* MBEDTLS_PSA_CRYPTO_C */